blob: 54a36a9ef2966c0e48f211051d307212001fe0b7 [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
3062 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3063 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3064 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3065 device2->reset(ARBITRARY_TIME);
3066
3067 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3068 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3069 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
3070 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3071}
3072
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003073// --- KeyboardInputMapperTest_ExternalDevice ---
3074
3075class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3076protected:
Chris Yea52ade12020-08-27 16:49:20 -07003077 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003078};
3079
3080TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003081 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3082 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003083
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003084 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3085 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3086 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3087 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 KeyboardInputMapper& mapper =
3090 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3091 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003092
3093 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3094 NotifyKeyArgs args;
3095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3096 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3097
3098 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3100 ASSERT_EQ(uint32_t(0), args.policyFlags);
3101
3102 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3104 ASSERT_EQ(uint32_t(0), args.policyFlags);
3105
3106 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3108 ASSERT_EQ(uint32_t(0), args.policyFlags);
3109
3110 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3112 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3113
3114 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3116 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3117}
3118
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003119TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003120 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003121
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003122 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3124 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003125
Powei Fengd041c5d2019-05-03 17:11:33 -07003126 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 KeyboardInputMapper& mapper =
3128 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3129 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003130
3131 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3132 NotifyKeyArgs args;
3133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3134 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3135
3136 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3138 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3139
3140 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3142 ASSERT_EQ(uint32_t(0), args.policyFlags);
3143
3144 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3146 ASSERT_EQ(uint32_t(0), args.policyFlags);
3147
3148 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3150 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3151
3152 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3154 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3155}
3156
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157// --- CursorInputMapperTest ---
3158
3159class CursorInputMapperTest : public InputMapperTest {
3160protected:
3161 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3162
Michael Wright17db18e2020-06-26 20:51:44 +01003163 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164
Chris Yea52ade12020-08-27 16:49:20 -07003165 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166 InputMapperTest::SetUp();
3167
Michael Wright17db18e2020-06-26 20:51:44 +01003168 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003169 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170 }
3171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003172 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3173 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003174
3175 void prepareDisplay(int32_t orientation) {
3176 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003177 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003178 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3179 orientation, uniqueId, NO_PORT, viewportType);
3180 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181};
3182
3183const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003185void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3186 int32_t originalY, int32_t rotatedX,
3187 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188 NotifyMotionArgs args;
3189
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003190 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3191 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3192 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3196 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3197 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3198 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3199}
3200
3201TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003202 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003203 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003205 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206}
3207
3208TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003210 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003212 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213}
3214
3215TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218
3219 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003220 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221
3222 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003223 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3224 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3226 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3227
3228 // When the bounds are set, then there should be a valid motion range.
3229 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3230
3231 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003232 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233
3234 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3235 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3236 1, 800 - 1, 0.0f, 0.0f));
3237 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3238 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3239 2, 480 - 1, 0.0f, 0.0f));
3240 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3241 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3242 0.0f, 1.0f, 0.0f, 0.0f));
3243}
3244
3245TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
3249 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
3252 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3253 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3254 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3255 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3256 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3257 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3258 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3259 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3260 0.0f, 1.0f, 0.0f, 0.0f));
3261}
3262
3263TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266
arthurhungdcef2dc2020-08-11 14:47:50 +08003267 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268
3269 NotifyMotionArgs args;
3270
3271 // Button press.
3272 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003273 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3274 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3276 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3277 ASSERT_EQ(DEVICE_ID, args.deviceId);
3278 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3279 ASSERT_EQ(uint32_t(0), args.policyFlags);
3280 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3281 ASSERT_EQ(0, args.flags);
3282 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3283 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3284 ASSERT_EQ(0, args.edgeFlags);
3285 ASSERT_EQ(uint32_t(1), args.pointerCount);
3286 ASSERT_EQ(0, args.pointerProperties[0].id);
3287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3289 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3290 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3291 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3292 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3293
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3295 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3296 ASSERT_EQ(DEVICE_ID, args.deviceId);
3297 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3298 ASSERT_EQ(uint32_t(0), args.policyFlags);
3299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3300 ASSERT_EQ(0, args.flags);
3301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3302 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3303 ASSERT_EQ(0, args.edgeFlags);
3304 ASSERT_EQ(uint32_t(1), args.pointerCount);
3305 ASSERT_EQ(0, args.pointerProperties[0].id);
3306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3308 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3309 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3310 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3311 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3312
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003314 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3315 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3317 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3318 ASSERT_EQ(DEVICE_ID, args.deviceId);
3319 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3320 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3322 ASSERT_EQ(0, args.flags);
3323 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3324 ASSERT_EQ(0, args.buttonState);
3325 ASSERT_EQ(0, args.edgeFlags);
3326 ASSERT_EQ(uint32_t(1), args.pointerCount);
3327 ASSERT_EQ(0, args.pointerProperties[0].id);
3328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3330 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3331 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3332 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3333 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3334
3335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3336 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3337 ASSERT_EQ(DEVICE_ID, args.deviceId);
3338 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3339 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3341 ASSERT_EQ(0, args.flags);
3342 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3343 ASSERT_EQ(0, args.buttonState);
3344 ASSERT_EQ(0, args.edgeFlags);
3345 ASSERT_EQ(uint32_t(1), args.pointerCount);
3346 ASSERT_EQ(0, args.pointerProperties[0].id);
3347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3349 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3350 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3351 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3352 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3353}
3354
3355TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003357 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358
3359 NotifyMotionArgs args;
3360
3361 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003362 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3363 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3367 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3368
3369 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003370 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3371 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3375 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3376}
3377
3378TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003380 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381
3382 NotifyMotionArgs args;
3383
3384 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003385 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3386 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3388 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3390 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3391
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3395 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3396
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003398 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3399 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3403 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3404
3405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3408 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3409}
3410
3411TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003413 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414
3415 NotifyMotionArgs args;
3416
3417 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003418 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3419 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3420 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3421 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3425 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3426 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3427
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3431 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3432 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3433
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003435 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3436 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3437 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3441 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3442 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3443
3444 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003445 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3446 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3450 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3451
3452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3455 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3456}
3457
3458TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003462 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3464 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3465 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3468 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3469 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3470 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3471}
3472
3473TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474 addConfigurationProperty("cursor.mode", "navigation");
3475 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003476 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003478 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3485 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3486 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3487
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003488 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3490 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3497
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003498 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3507
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003508 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3517}
3518
3519TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522
3523 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3524 mFakePointerController->setPosition(100, 200);
3525 mFakePointerController->setButtonState(0);
3526
3527 NotifyMotionArgs motionArgs;
3528 NotifyKeyArgs keyArgs;
3529
3530 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003531 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3532 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3534 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3535 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3536 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3538 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3539
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3542 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3543 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3545 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3546
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003547 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3548 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 ASSERT_EQ(0, motionArgs.buttonState);
3552 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3554 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3555
3556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003557 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558 ASSERT_EQ(0, motionArgs.buttonState);
3559 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3561 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3562
3563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003565 ASSERT_EQ(0, motionArgs.buttonState);
3566 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3568 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3569
3570 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003571 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3572 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3573 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3575 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3576 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3577 motionArgs.buttonState);
3578 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3579 mFakePointerController->getButtonState());
3580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3581 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3582
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3585 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3586 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3587 mFakePointerController->getButtonState());
3588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3589 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3590
3591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3592 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3593 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3594 motionArgs.buttonState);
3595 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3596 mFakePointerController->getButtonState());
3597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3598 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3599
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003600 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3601 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3605 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3607 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3608
3609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003611 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3612 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3614 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3615
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003616 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3617 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3620 ASSERT_EQ(0, motionArgs.buttonState);
3621 ASSERT_EQ(0, mFakePointerController->getButtonState());
3622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3623 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 -08003624 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3625 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003626
3627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_EQ(0, motionArgs.buttonState);
3629 ASSERT_EQ(0, mFakePointerController->getButtonState());
3630 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3632 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 -08003633
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3635 ASSERT_EQ(0, motionArgs.buttonState);
3636 ASSERT_EQ(0, mFakePointerController->getButtonState());
3637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3639 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3640
3641 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003642 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3643 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3645 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3646 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003647
Michael Wrightd02c5b62014-02-10 15:10:22 -08003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003649 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3651 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3653 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3654
3655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3657 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3658 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3660 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3661
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003662 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3663 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 ASSERT_EQ(0, motionArgs.buttonState);
3667 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3669 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3670
3671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003672 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003673 ASSERT_EQ(0, motionArgs.buttonState);
3674 ASSERT_EQ(0, mFakePointerController->getButtonState());
3675
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3677 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3679 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3680 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3681
3682 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003683 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3684 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3686 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3687 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003688
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003690 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3692 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3694 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3695
3696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3697 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3698 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3699 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3701 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3702
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003703 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3704 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707 ASSERT_EQ(0, motionArgs.buttonState);
3708 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3710 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 -08003711
3712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3714 ASSERT_EQ(0, motionArgs.buttonState);
3715 ASSERT_EQ(0, mFakePointerController->getButtonState());
3716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3717 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3718
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3720 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3721 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3722
3723 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003724 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3725 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3727 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3728 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003729
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3733 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3735 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3736
3737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3739 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3740 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3742 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3743
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003744 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3745 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748 ASSERT_EQ(0, motionArgs.buttonState);
3749 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3751 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 -08003752
3753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3754 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3755 ASSERT_EQ(0, motionArgs.buttonState);
3756 ASSERT_EQ(0, mFakePointerController->getButtonState());
3757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3758 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3759
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3761 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3762 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3763
3764 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003765 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3766 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3768 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3769 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003770
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3774 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3776 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3777
3778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3780 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3781 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3783 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3784
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003785 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3786 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003788 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789 ASSERT_EQ(0, motionArgs.buttonState);
3790 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3792 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 -08003793
3794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3795 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3796 ASSERT_EQ(0, motionArgs.buttonState);
3797 ASSERT_EQ(0, mFakePointerController->getButtonState());
3798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3799 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3800
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3802 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3803 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3804}
3805
3806TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003808 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809
3810 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3811 mFakePointerController->setPosition(100, 200);
3812 mFakePointerController->setButtonState(0);
3813
3814 NotifyMotionArgs args;
3815
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003816 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3817 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3818 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003820 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3821 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3823 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 +01003824 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003825}
3826
3827TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003828 addConfigurationProperty("cursor.mode", "pointer");
3829 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003830 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003831
3832 NotifyDeviceResetArgs resetArgs;
3833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3834 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3835 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3836
3837 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3838 mFakePointerController->setPosition(100, 200);
3839 mFakePointerController->setButtonState(0);
3840
3841 NotifyMotionArgs args;
3842
3843 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003844 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3845 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3846 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3848 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3851 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 +01003852 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003853
3854 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003855 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3856 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3858 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3859 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3861 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3863 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3866 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3867
3868 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003869 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3870 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3872 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3873 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3875 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3877 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3878 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3880 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3881
3882 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003883 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3884 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3885 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3887 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3890 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 +01003891 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003892
3893 // Disable pointer capture and check that the device generation got bumped
3894 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08003895 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003896 mFakePolicy->setPointerCapture(false);
3897 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08003898 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003899
3900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3901 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3902 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3903
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003904 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3905 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3906 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3908 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3911 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 +01003912 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913}
3914
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003915TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003916 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003917
Garfield Tan888a6a42020-01-09 11:39:16 -08003918 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003919 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003920 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3921 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003922 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003923 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3924 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3925
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003926 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3927 mFakePointerController->setPosition(100, 200);
3928 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003929
3930 NotifyMotionArgs args;
3931 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3932 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3933 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3935 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3938 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 +01003939 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003940 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3941}
3942
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943// --- TouchInputMapperTest ---
3944
3945class TouchInputMapperTest : public InputMapperTest {
3946protected:
3947 static const int32_t RAW_X_MIN;
3948 static const int32_t RAW_X_MAX;
3949 static const int32_t RAW_Y_MIN;
3950 static const int32_t RAW_Y_MAX;
3951 static const int32_t RAW_TOUCH_MIN;
3952 static const int32_t RAW_TOUCH_MAX;
3953 static const int32_t RAW_TOOL_MIN;
3954 static const int32_t RAW_TOOL_MAX;
3955 static const int32_t RAW_PRESSURE_MIN;
3956 static const int32_t RAW_PRESSURE_MAX;
3957 static const int32_t RAW_ORIENTATION_MIN;
3958 static const int32_t RAW_ORIENTATION_MAX;
3959 static const int32_t RAW_DISTANCE_MIN;
3960 static const int32_t RAW_DISTANCE_MAX;
3961 static const int32_t RAW_TILT_MIN;
3962 static const int32_t RAW_TILT_MAX;
3963 static const int32_t RAW_ID_MIN;
3964 static const int32_t RAW_ID_MAX;
3965 static const int32_t RAW_SLOT_MIN;
3966 static const int32_t RAW_SLOT_MAX;
3967 static const float X_PRECISION;
3968 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003969 static const float X_PRECISION_VIRTUAL;
3970 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971
3972 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003973 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974
3975 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3976
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003977 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003978 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003979
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980 enum Axes {
3981 POSITION = 1 << 0,
3982 TOUCH = 1 << 1,
3983 TOOL = 1 << 2,
3984 PRESSURE = 1 << 3,
3985 ORIENTATION = 1 << 4,
3986 MINOR = 1 << 5,
3987 ID = 1 << 6,
3988 DISTANCE = 1 << 7,
3989 TILT = 1 << 8,
3990 SLOT = 1 << 9,
3991 TOOL_TYPE = 1 << 10,
3992 };
3993
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003994 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3995 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003996 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003998 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 int32_t toRawX(float displayX);
4000 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004001 float toCookedX(float rawX, float rawY);
4002 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004004 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004006 float toDisplayY(int32_t rawY, int32_t displayHeight);
4007
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008};
4009
4010const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4011const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4012const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4013const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4014const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4015const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4016const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4017const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004018const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4019const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4021const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4022const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4023const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4024const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4025const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4026const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4027const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4028const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4029const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4030const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4031const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004032const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4033 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4034const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4035 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004036const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4037 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038
4039const float TouchInputMapperTest::GEOMETRIC_SCALE =
4040 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4041 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4042
4043const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4044 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4045 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4046};
4047
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004048void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004049 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4050 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004051}
4052
4053void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4054 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4055 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056}
4057
Santos Cordonfa5cf462017-04-05 10:37:00 -07004058void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004059 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4060 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4061 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004062}
4063
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004065 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4066 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4067 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4068 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069}
4070
Jason Gerecke489fda82012-09-07 17:19:40 -07004071void TouchInputMapperTest::prepareLocationCalibration() {
4072 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4073}
4074
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075int32_t TouchInputMapperTest::toRawX(float displayX) {
4076 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4077}
4078
4079int32_t TouchInputMapperTest::toRawY(float displayY) {
4080 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4081}
4082
Jason Gerecke489fda82012-09-07 17:19:40 -07004083float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4084 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4085 return rawX;
4086}
4087
4088float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4089 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4090 return rawY;
4091}
4092
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004094 return toDisplayX(rawX, DISPLAY_WIDTH);
4095}
4096
4097float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4098 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099}
4100
4101float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004102 return toDisplayY(rawY, DISPLAY_HEIGHT);
4103}
4104
4105float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4106 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107}
4108
4109
4110// --- SingleTouchInputMapperTest ---
4111
4112class SingleTouchInputMapperTest : public TouchInputMapperTest {
4113protected:
4114 void prepareButtons();
4115 void prepareAxes(int axes);
4116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004117 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4118 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4119 void processUp(SingleTouchInputMapper& mappery);
4120 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4121 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4122 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4123 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4124 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4125 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004126};
4127
4128void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004129 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130}
4131
4132void SingleTouchInputMapperTest::prepareAxes(int axes) {
4133 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004134 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136 }
4137 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004138 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4139 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 }
4141 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004142 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4143 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 }
4145 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4147 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148 }
4149 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004150 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 }
4153}
4154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004155void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004156 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4157 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4158 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159}
4160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004161void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004162 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4163 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164}
4165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004166void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004167 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168}
4169
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004170void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004171 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172}
4173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004174void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4175 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004176 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177}
4178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004179void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004180 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181}
4182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004183void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4184 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004185 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4186 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187}
4188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004189void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4190 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004191 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192}
4193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004194void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004195 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196}
4197
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 prepareButtons();
4200 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004201 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004203 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204}
4205
4206TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004207 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4208 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 prepareButtons();
4210 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004211 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004213 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214}
4215
4216TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 prepareButtons();
4218 prepareAxes(POSITION);
4219 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004220 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004222 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223}
4224
4225TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 prepareButtons();
4227 prepareAxes(POSITION);
4228 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004229 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232}
4233
4234TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 addConfigurationProperty("touch.deviceType", "touchScreen");
4236 prepareDisplay(DISPLAY_ORIENTATION_0);
4237 prepareButtons();
4238 prepareAxes(POSITION);
4239 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004240 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
4242 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004243 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244
4245 // Virtual key is down.
4246 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4247 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4248 processDown(mapper, x, y);
4249 processSync(mapper);
4250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004252 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253
4254 // Virtual key is up.
4255 processUp(mapper);
4256 processSync(mapper);
4257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4258
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004259 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260}
4261
4262TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 addConfigurationProperty("touch.deviceType", "touchScreen");
4264 prepareDisplay(DISPLAY_ORIENTATION_0);
4265 prepareButtons();
4266 prepareAxes(POSITION);
4267 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004268 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
4270 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004271 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272
4273 // Virtual key is down.
4274 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4275 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4276 processDown(mapper, x, y);
4277 processSync(mapper);
4278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4279
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004280 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281
4282 // Virtual key is up.
4283 processUp(mapper);
4284 processSync(mapper);
4285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4286
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004287 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288}
4289
4290TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 addConfigurationProperty("touch.deviceType", "touchScreen");
4292 prepareDisplay(DISPLAY_ORIENTATION_0);
4293 prepareButtons();
4294 prepareAxes(POSITION);
4295 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004296 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297
4298 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4299 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004300 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_TRUE(flags[0]);
4302 ASSERT_FALSE(flags[1]);
4303}
4304
4305TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 addConfigurationProperty("touch.deviceType", "touchScreen");
4307 prepareDisplay(DISPLAY_ORIENTATION_0);
4308 prepareButtons();
4309 prepareAxes(POSITION);
4310 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004311 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312
arthurhungdcef2dc2020-08-11 14:47:50 +08004313 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314
4315 NotifyKeyArgs args;
4316
4317 // Press virtual key.
4318 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4319 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4320 processDown(mapper, x, y);
4321 processSync(mapper);
4322
4323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4324 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4325 ASSERT_EQ(DEVICE_ID, args.deviceId);
4326 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4327 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4328 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4329 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4330 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4331 ASSERT_EQ(KEY_HOME, args.scanCode);
4332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4333 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4334
4335 // Release virtual key.
4336 processUp(mapper);
4337 processSync(mapper);
4338
4339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4340 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4341 ASSERT_EQ(DEVICE_ID, args.deviceId);
4342 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4343 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4344 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4345 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4346 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4347 ASSERT_EQ(KEY_HOME, args.scanCode);
4348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4349 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4350
4351 // Should not have sent any motions.
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4353}
4354
4355TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 addConfigurationProperty("touch.deviceType", "touchScreen");
4357 prepareDisplay(DISPLAY_ORIENTATION_0);
4358 prepareButtons();
4359 prepareAxes(POSITION);
4360 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004361 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362
arthurhungdcef2dc2020-08-11 14:47:50 +08004363 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364
4365 NotifyKeyArgs keyArgs;
4366
4367 // Press virtual key.
4368 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4369 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4370 processDown(mapper, x, y);
4371 processSync(mapper);
4372
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4374 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4375 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4376 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4377 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4378 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4379 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4380 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4381 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4383 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4384
4385 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4386 // into the display area.
4387 y -= 100;
4388 processMove(mapper, x, y);
4389 processSync(mapper);
4390
4391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4392 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4393 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4394 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4395 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4396 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4397 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4398 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4399 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4400 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4401 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4402 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4403
4404 NotifyMotionArgs motionArgs;
4405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4407 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4409 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4410 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4411 ASSERT_EQ(0, motionArgs.flags);
4412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4413 ASSERT_EQ(0, motionArgs.buttonState);
4414 ASSERT_EQ(0, motionArgs.edgeFlags);
4415 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4419 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4420 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4421 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4423
4424 // Keep moving out of bounds. Should generate a pointer move.
4425 y -= 50;
4426 processMove(mapper, x, y);
4427 processSync(mapper);
4428
4429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4430 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4431 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4432 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4433 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4435 ASSERT_EQ(0, motionArgs.flags);
4436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4437 ASSERT_EQ(0, motionArgs.buttonState);
4438 ASSERT_EQ(0, motionArgs.edgeFlags);
4439 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4443 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4444 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4445 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4447
4448 // Release out of bounds. Should generate a pointer up.
4449 processUp(mapper);
4450 processSync(mapper);
4451
4452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4453 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4454 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4455 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4456 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4457 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4458 ASSERT_EQ(0, motionArgs.flags);
4459 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4460 ASSERT_EQ(0, motionArgs.buttonState);
4461 ASSERT_EQ(0, motionArgs.edgeFlags);
4462 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4463 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4466 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4467 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4468 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4469 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4470
4471 // Should not have sent any more keys or motions.
4472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4474}
4475
4476TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 addConfigurationProperty("touch.deviceType", "touchScreen");
4478 prepareDisplay(DISPLAY_ORIENTATION_0);
4479 prepareButtons();
4480 prepareAxes(POSITION);
4481 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004482 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483
arthurhungdcef2dc2020-08-11 14:47:50 +08004484 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485
4486 NotifyMotionArgs motionArgs;
4487
4488 // Initially go down out of bounds.
4489 int32_t x = -10;
4490 int32_t y = -10;
4491 processDown(mapper, x, y);
4492 processSync(mapper);
4493
4494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4495
4496 // Move into the display area. Should generate a pointer down.
4497 x = 50;
4498 y = 75;
4499 processMove(mapper, x, y);
4500 processSync(mapper);
4501
4502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4503 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4504 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4505 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4506 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4508 ASSERT_EQ(0, motionArgs.flags);
4509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4510 ASSERT_EQ(0, motionArgs.buttonState);
4511 ASSERT_EQ(0, motionArgs.edgeFlags);
4512 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4516 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4517 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4518 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4520
4521 // Release. Should generate a pointer up.
4522 processUp(mapper);
4523 processSync(mapper);
4524
4525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4526 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4527 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4528 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4529 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4530 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4531 ASSERT_EQ(0, motionArgs.flags);
4532 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4533 ASSERT_EQ(0, motionArgs.buttonState);
4534 ASSERT_EQ(0, motionArgs.edgeFlags);
4535 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4536 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4539 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4540 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4541 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4543
4544 // Should not have sent any more keys or motions.
4545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4547}
4548
Santos Cordonfa5cf462017-04-05 10:37:00 -07004549TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004550 addConfigurationProperty("touch.deviceType", "touchScreen");
4551 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4552
4553 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4554 prepareButtons();
4555 prepareAxes(POSITION);
4556 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004557 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004558
arthurhungdcef2dc2020-08-11 14:47:50 +08004559 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004560
4561 NotifyMotionArgs motionArgs;
4562
4563 // Down.
4564 int32_t x = 100;
4565 int32_t y = 125;
4566 processDown(mapper, x, y);
4567 processSync(mapper);
4568
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4571 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4572 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4573 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4574 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4576 ASSERT_EQ(0, motionArgs.flags);
4577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4578 ASSERT_EQ(0, motionArgs.buttonState);
4579 ASSERT_EQ(0, motionArgs.edgeFlags);
4580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4584 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4585 1, 0, 0, 0, 0, 0, 0, 0));
4586 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4587 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4588 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4589
4590 // Move.
4591 x += 50;
4592 y += 75;
4593 processMove(mapper, x, y);
4594 processSync(mapper);
4595
4596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4597 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4598 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4599 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4600 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4601 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4603 ASSERT_EQ(0, motionArgs.flags);
4604 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4605 ASSERT_EQ(0, motionArgs.buttonState);
4606 ASSERT_EQ(0, motionArgs.edgeFlags);
4607 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4611 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4612 1, 0, 0, 0, 0, 0, 0, 0));
4613 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4614 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4615 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4616
4617 // Up.
4618 processUp(mapper);
4619 processSync(mapper);
4620
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4622 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4623 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4624 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4625 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4626 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4627 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4628 ASSERT_EQ(0, motionArgs.flags);
4629 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4630 ASSERT_EQ(0, motionArgs.buttonState);
4631 ASSERT_EQ(0, motionArgs.edgeFlags);
4632 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4636 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4637 1, 0, 0, 0, 0, 0, 0, 0));
4638 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4639 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4640 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4641
4642 // Should not have sent any more keys or motions.
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4645}
4646
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 addConfigurationProperty("touch.deviceType", "touchScreen");
4649 prepareDisplay(DISPLAY_ORIENTATION_0);
4650 prepareButtons();
4651 prepareAxes(POSITION);
4652 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004653 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654
arthurhungdcef2dc2020-08-11 14:47:50 +08004655 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656
4657 NotifyMotionArgs motionArgs;
4658
4659 // Down.
4660 int32_t x = 100;
4661 int32_t y = 125;
4662 processDown(mapper, x, y);
4663 processSync(mapper);
4664
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4666 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4667 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4668 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4669 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4670 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4671 ASSERT_EQ(0, motionArgs.flags);
4672 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4673 ASSERT_EQ(0, motionArgs.buttonState);
4674 ASSERT_EQ(0, motionArgs.edgeFlags);
4675 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4679 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4680 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4681 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4682 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4683
4684 // Move.
4685 x += 50;
4686 y += 75;
4687 processMove(mapper, x, y);
4688 processSync(mapper);
4689
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4691 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4692 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4693 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4694 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4696 ASSERT_EQ(0, motionArgs.flags);
4697 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4698 ASSERT_EQ(0, motionArgs.buttonState);
4699 ASSERT_EQ(0, motionArgs.edgeFlags);
4700 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4701 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4704 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4705 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4706 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4707 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4708
4709 // Up.
4710 processUp(mapper);
4711 processSync(mapper);
4712
4713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4714 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4715 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4716 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4717 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4718 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4719 ASSERT_EQ(0, motionArgs.flags);
4720 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4721 ASSERT_EQ(0, motionArgs.buttonState);
4722 ASSERT_EQ(0, motionArgs.edgeFlags);
4723 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4724 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4727 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4728 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4729 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4730 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4731
4732 // Should not have sent any more keys or motions.
4733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4735}
4736
4737TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 addConfigurationProperty("touch.deviceType", "touchScreen");
4739 prepareButtons();
4740 prepareAxes(POSITION);
4741 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004742 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743
4744 NotifyMotionArgs args;
4745
4746 // Rotation 90.
4747 prepareDisplay(DISPLAY_ORIENTATION_90);
4748 processDown(mapper, toRawX(50), toRawY(75));
4749 processSync(mapper);
4750
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4752 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4753 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4754
4755 processUp(mapper);
4756 processSync(mapper);
4757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4758}
4759
4760TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761 addConfigurationProperty("touch.deviceType", "touchScreen");
4762 prepareButtons();
4763 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004764 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765
4766 NotifyMotionArgs args;
4767
4768 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004769 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 prepareDisplay(DISPLAY_ORIENTATION_0);
4771 processDown(mapper, toRawX(50), toRawY(75));
4772 processSync(mapper);
4773
4774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4775 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4776 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4777
4778 processUp(mapper);
4779 processSync(mapper);
4780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4781
4782 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004783 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004784 prepareDisplay(DISPLAY_ORIENTATION_90);
4785 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4786 processSync(mapper);
4787
4788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4789 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4790 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4791
4792 processUp(mapper);
4793 processSync(mapper);
4794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4795
4796 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004797 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 prepareDisplay(DISPLAY_ORIENTATION_180);
4799 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4800 processSync(mapper);
4801
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4803 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4804 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4805
4806 processUp(mapper);
4807 processSync(mapper);
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4809
4810 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004811 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812 prepareDisplay(DISPLAY_ORIENTATION_270);
4813 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4814 processSync(mapper);
4815
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4817 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4818 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4819
4820 processUp(mapper);
4821 processSync(mapper);
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4823}
4824
4825TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 addConfigurationProperty("touch.deviceType", "touchScreen");
4827 prepareDisplay(DISPLAY_ORIENTATION_0);
4828 prepareButtons();
4829 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004830 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831
4832 // These calculations are based on the input device calibration documentation.
4833 int32_t rawX = 100;
4834 int32_t rawY = 200;
4835 int32_t rawPressure = 10;
4836 int32_t rawToolMajor = 12;
4837 int32_t rawDistance = 2;
4838 int32_t rawTiltX = 30;
4839 int32_t rawTiltY = 110;
4840
4841 float x = toDisplayX(rawX);
4842 float y = toDisplayY(rawY);
4843 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4844 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4845 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4846 float distance = float(rawDistance);
4847
4848 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4849 float tiltScale = M_PI / 180;
4850 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4851 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4852 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4853 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4854
4855 processDown(mapper, rawX, rawY);
4856 processPressure(mapper, rawPressure);
4857 processToolMajor(mapper, rawToolMajor);
4858 processDistance(mapper, rawDistance);
4859 processTilt(mapper, rawTiltX, rawTiltY);
4860 processSync(mapper);
4861
4862 NotifyMotionArgs args;
4863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4865 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4866 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4867}
4868
Jason Gerecke489fda82012-09-07 17:19:40 -07004869TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004870 addConfigurationProperty("touch.deviceType", "touchScreen");
4871 prepareDisplay(DISPLAY_ORIENTATION_0);
4872 prepareLocationCalibration();
4873 prepareButtons();
4874 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004875 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004876
4877 int32_t rawX = 100;
4878 int32_t rawY = 200;
4879
4880 float x = toDisplayX(toCookedX(rawX, rawY));
4881 float y = toDisplayY(toCookedY(rawX, rawY));
4882
4883 processDown(mapper, rawX, rawY);
4884 processSync(mapper);
4885
4886 NotifyMotionArgs args;
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4889 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4890}
4891
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893 addConfigurationProperty("touch.deviceType", "touchScreen");
4894 prepareDisplay(DISPLAY_ORIENTATION_0);
4895 prepareButtons();
4896 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004897 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898
4899 NotifyMotionArgs motionArgs;
4900 NotifyKeyArgs keyArgs;
4901
4902 processDown(mapper, 100, 200);
4903 processSync(mapper);
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4905 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4906 ASSERT_EQ(0, motionArgs.buttonState);
4907
4908 // press BTN_LEFT, release BTN_LEFT
4909 processKey(mapper, BTN_LEFT, 1);
4910 processSync(mapper);
4911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4913 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4914
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4917 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4918
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919 processKey(mapper, BTN_LEFT, 0);
4920 processSync(mapper);
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004924
4925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004927 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928
4929 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4930 processKey(mapper, BTN_RIGHT, 1);
4931 processKey(mapper, BTN_MIDDLE, 1);
4932 processSync(mapper);
4933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4935 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4936 motionArgs.buttonState);
4937
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4940 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4941
4942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4943 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4944 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4945 motionArgs.buttonState);
4946
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947 processKey(mapper, BTN_RIGHT, 0);
4948 processSync(mapper);
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004950 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004952
4953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004955 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956
4957 processKey(mapper, BTN_MIDDLE, 0);
4958 processSync(mapper);
4959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004962
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004965 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966
4967 // press BTN_BACK, release BTN_BACK
4968 processKey(mapper, BTN_BACK, 1);
4969 processSync(mapper);
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4971 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4972 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004973
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004976 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4977
4978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4980 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981
4982 processKey(mapper, BTN_BACK, 0);
4983 processSync(mapper);
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004985 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004987
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004990 ASSERT_EQ(0, motionArgs.buttonState);
4991
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4993 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4994 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4995
4996 // press BTN_SIDE, release BTN_SIDE
4997 processKey(mapper, BTN_SIDE, 1);
4998 processSync(mapper);
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5000 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5001 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005002
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005005 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5006
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5008 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5009 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010
5011 processKey(mapper, BTN_SIDE, 0);
5012 processSync(mapper);
5013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005014 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005016
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005019 ASSERT_EQ(0, motionArgs.buttonState);
5020
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5022 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5023 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5024
5025 // press BTN_FORWARD, release BTN_FORWARD
5026 processKey(mapper, BTN_FORWARD, 1);
5027 processSync(mapper);
5028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5029 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5030 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005031
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005034 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5035
5036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5038 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039
5040 processKey(mapper, BTN_FORWARD, 0);
5041 processSync(mapper);
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005043 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005044 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005045
5046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005048 ASSERT_EQ(0, motionArgs.buttonState);
5049
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5051 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5052 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5053
5054 // press BTN_EXTRA, release BTN_EXTRA
5055 processKey(mapper, BTN_EXTRA, 1);
5056 processSync(mapper);
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5058 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5059 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005060
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005063 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5064
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5067 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068
5069 processKey(mapper, BTN_EXTRA, 0);
5070 processSync(mapper);
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005072 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005074
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005077 ASSERT_EQ(0, motionArgs.buttonState);
5078
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5080 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5081 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5082
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5084
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085 // press BTN_STYLUS, release BTN_STYLUS
5086 processKey(mapper, BTN_STYLUS, 1);
5087 processSync(mapper);
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005090 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5091
5092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5094 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095
5096 processKey(mapper, BTN_STYLUS, 0);
5097 processSync(mapper);
5098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005099 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005101
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005104 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105
5106 // press BTN_STYLUS2, release BTN_STYLUS2
5107 processKey(mapper, BTN_STYLUS2, 1);
5108 processSync(mapper);
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005111 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5112
5113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5115 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116
5117 processKey(mapper, BTN_STYLUS2, 0);
5118 processSync(mapper);
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005120 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005122
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005125 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126
5127 // release touch
5128 processUp(mapper);
5129 processSync(mapper);
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5131 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5132 ASSERT_EQ(0, motionArgs.buttonState);
5133}
5134
5135TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136 addConfigurationProperty("touch.deviceType", "touchScreen");
5137 prepareDisplay(DISPLAY_ORIENTATION_0);
5138 prepareButtons();
5139 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005140 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141
5142 NotifyMotionArgs motionArgs;
5143
5144 // default tool type is finger
5145 processDown(mapper, 100, 200);
5146 processSync(mapper);
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5148 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5150
5151 // eraser
5152 processKey(mapper, BTN_TOOL_RUBBER, 1);
5153 processSync(mapper);
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5157
5158 // stylus
5159 processKey(mapper, BTN_TOOL_RUBBER, 0);
5160 processKey(mapper, BTN_TOOL_PEN, 1);
5161 processSync(mapper);
5162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5165
5166 // brush
5167 processKey(mapper, BTN_TOOL_PEN, 0);
5168 processKey(mapper, BTN_TOOL_BRUSH, 1);
5169 processSync(mapper);
5170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5173
5174 // pencil
5175 processKey(mapper, BTN_TOOL_BRUSH, 0);
5176 processKey(mapper, BTN_TOOL_PENCIL, 1);
5177 processSync(mapper);
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5181
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005182 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183 processKey(mapper, BTN_TOOL_PENCIL, 0);
5184 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5185 processSync(mapper);
5186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5189
5190 // mouse
5191 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5192 processKey(mapper, BTN_TOOL_MOUSE, 1);
5193 processSync(mapper);
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5197
5198 // lens
5199 processKey(mapper, BTN_TOOL_MOUSE, 0);
5200 processKey(mapper, BTN_TOOL_LENS, 1);
5201 processSync(mapper);
5202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5205
5206 // double-tap
5207 processKey(mapper, BTN_TOOL_LENS, 0);
5208 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5209 processSync(mapper);
5210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5213
5214 // triple-tap
5215 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5216 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5217 processSync(mapper);
5218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5221
5222 // quad-tap
5223 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5224 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5225 processSync(mapper);
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5229
5230 // finger
5231 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5232 processKey(mapper, BTN_TOOL_FINGER, 1);
5233 processSync(mapper);
5234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5237
5238 // stylus trumps finger
5239 processKey(mapper, BTN_TOOL_PEN, 1);
5240 processSync(mapper);
5241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5244
5245 // eraser trumps stylus
5246 processKey(mapper, BTN_TOOL_RUBBER, 1);
5247 processSync(mapper);
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5251
5252 // mouse trumps eraser
5253 processKey(mapper, BTN_TOOL_MOUSE, 1);
5254 processSync(mapper);
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5258
5259 // back to default tool type
5260 processKey(mapper, BTN_TOOL_MOUSE, 0);
5261 processKey(mapper, BTN_TOOL_RUBBER, 0);
5262 processKey(mapper, BTN_TOOL_PEN, 0);
5263 processKey(mapper, BTN_TOOL_FINGER, 0);
5264 processSync(mapper);
5265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5268}
5269
5270TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271 addConfigurationProperty("touch.deviceType", "touchScreen");
5272 prepareDisplay(DISPLAY_ORIENTATION_0);
5273 prepareButtons();
5274 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005275 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005276 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277
5278 NotifyMotionArgs motionArgs;
5279
5280 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5281 processKey(mapper, BTN_TOOL_FINGER, 1);
5282 processMove(mapper, 100, 200);
5283 processSync(mapper);
5284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5287 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5288
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5292 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5293
5294 // move a little
5295 processMove(mapper, 150, 250);
5296 processSync(mapper);
5297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5300 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5301
5302 // down when BTN_TOUCH is pressed, pressure defaults to 1
5303 processKey(mapper, BTN_TOUCH, 1);
5304 processSync(mapper);
5305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5308 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5309
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5311 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5313 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5314
5315 // up when BTN_TOUCH is released, hover restored
5316 processKey(mapper, BTN_TOUCH, 0);
5317 processSync(mapper);
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5319 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5321 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5322
5323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5326 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5327
5328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5329 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5331 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5332
5333 // exit hover when pointer goes away
5334 processKey(mapper, BTN_TOOL_FINGER, 0);
5335 processSync(mapper);
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5339 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5340}
5341
5342TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343 addConfigurationProperty("touch.deviceType", "touchScreen");
5344 prepareDisplay(DISPLAY_ORIENTATION_0);
5345 prepareButtons();
5346 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005347 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348
5349 NotifyMotionArgs motionArgs;
5350
5351 // initially hovering because pressure is 0
5352 processDown(mapper, 100, 200);
5353 processPressure(mapper, 0);
5354 processSync(mapper);
5355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5358 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5359
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5363 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5364
5365 // move a little
5366 processMove(mapper, 150, 250);
5367 processSync(mapper);
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5371 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5372
5373 // down when pressure is non-zero
5374 processPressure(mapper, RAW_PRESSURE_MAX);
5375 processSync(mapper);
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5379 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5380
5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5384 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5385
5386 // up when pressure becomes 0, hover restored
5387 processPressure(mapper, 0);
5388 processSync(mapper);
5389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5390 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5392 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5393
5394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5395 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5397 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5398
5399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5402 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5403
5404 // exit hover when pointer goes away
5405 processUp(mapper);
5406 processSync(mapper);
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5408 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5410 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5411}
5412
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413// --- MultiTouchInputMapperTest ---
5414
5415class MultiTouchInputMapperTest : public TouchInputMapperTest {
5416protected:
5417 void prepareAxes(int axes);
5418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005419 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5420 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5421 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5422 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5423 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5424 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5425 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5426 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5427 void processId(MultiTouchInputMapper& mapper, int32_t id);
5428 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5429 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5430 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5431 void processMTSync(MultiTouchInputMapper& mapper);
5432 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433};
5434
5435void MultiTouchInputMapperTest::prepareAxes(int axes) {
5436 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005437 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5438 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439 }
5440 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005441 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5442 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005444 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5445 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446 }
5447 }
5448 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005449 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5450 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005452 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5453 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005454 }
5455 }
5456 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005457 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5458 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459 }
5460 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005461 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5462 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463 }
5464 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005465 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5466 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 }
5468 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005469 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5470 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471 }
5472 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005473 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5474 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475 }
5476 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005477 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478 }
5479}
5480
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005481void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5482 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005483 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5484 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485}
5486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005487void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5488 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005489 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490}
5491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5493 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005494 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495}
5496
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005497void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005498 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499}
5500
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005501void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005502 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503}
5504
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005505void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5506 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005507 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508}
5509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005510void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005511 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512}
5513
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005514void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005515 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516}
5517
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005518void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005519 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520}
5521
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005522void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005523 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524}
5525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005526void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005527 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528}
5529
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005530void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5531 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005532 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005533}
5534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005535void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005536 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537}
5538
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005539void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005540 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005541}
5542
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 addConfigurationProperty("touch.deviceType", "touchScreen");
5545 prepareDisplay(DISPLAY_ORIENTATION_0);
5546 prepareAxes(POSITION);
5547 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005548 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549
arthurhungdcef2dc2020-08-11 14:47:50 +08005550 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551
5552 NotifyMotionArgs motionArgs;
5553
5554 // Two fingers down at once.
5555 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5556 processPosition(mapper, x1, y1);
5557 processMTSync(mapper);
5558 processPosition(mapper, x2, y2);
5559 processMTSync(mapper);
5560 processSync(mapper);
5561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5563 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5564 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5565 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5566 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5567 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5568 ASSERT_EQ(0, motionArgs.flags);
5569 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5570 ASSERT_EQ(0, motionArgs.buttonState);
5571 ASSERT_EQ(0, motionArgs.edgeFlags);
5572 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5576 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5577 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5578 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5579 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5580
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5582 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5583 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5584 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5585 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5586 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5587 motionArgs.action);
5588 ASSERT_EQ(0, motionArgs.flags);
5589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5590 ASSERT_EQ(0, motionArgs.buttonState);
5591 ASSERT_EQ(0, motionArgs.edgeFlags);
5592 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5593 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5595 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5598 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5600 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5601 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5602 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5603 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5604
5605 // Move.
5606 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5607 processPosition(mapper, x1, y1);
5608 processMTSync(mapper);
5609 processPosition(mapper, x2, y2);
5610 processMTSync(mapper);
5611 processSync(mapper);
5612
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5615 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5616 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5617 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5619 ASSERT_EQ(0, motionArgs.flags);
5620 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5621 ASSERT_EQ(0, motionArgs.buttonState);
5622 ASSERT_EQ(0, motionArgs.edgeFlags);
5623 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5624 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5626 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5629 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5631 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5632 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5633 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5634 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5635
5636 // First finger up.
5637 x2 += 15; y2 -= 20;
5638 processPosition(mapper, x2, y2);
5639 processMTSync(mapper);
5640 processSync(mapper);
5641
5642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5643 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5644 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5645 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5646 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5647 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5648 motionArgs.action);
5649 ASSERT_EQ(0, motionArgs.flags);
5650 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5651 ASSERT_EQ(0, motionArgs.buttonState);
5652 ASSERT_EQ(0, motionArgs.edgeFlags);
5653 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5654 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5656 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5659 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5661 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5662 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5663 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5664 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5667 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5668 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5669 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5670 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5672 ASSERT_EQ(0, motionArgs.flags);
5673 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5674 ASSERT_EQ(0, motionArgs.buttonState);
5675 ASSERT_EQ(0, motionArgs.edgeFlags);
5676 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5677 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5680 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5681 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5682 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5684
5685 // Move.
5686 x2 += 20; y2 -= 25;
5687 processPosition(mapper, x2, y2);
5688 processMTSync(mapper);
5689 processSync(mapper);
5690
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5693 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5694 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5695 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5697 ASSERT_EQ(0, motionArgs.flags);
5698 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5699 ASSERT_EQ(0, motionArgs.buttonState);
5700 ASSERT_EQ(0, motionArgs.edgeFlags);
5701 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5702 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5705 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5706 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5707 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5709
5710 // New finger down.
5711 int32_t x3 = 700, y3 = 300;
5712 processPosition(mapper, x2, y2);
5713 processMTSync(mapper);
5714 processPosition(mapper, x3, y3);
5715 processMTSync(mapper);
5716 processSync(mapper);
5717
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5720 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5721 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5722 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5724 motionArgs.action);
5725 ASSERT_EQ(0, motionArgs.flags);
5726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5727 ASSERT_EQ(0, motionArgs.buttonState);
5728 ASSERT_EQ(0, motionArgs.edgeFlags);
5729 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5730 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5732 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5735 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5737 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5738 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5739 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5741
5742 // Second finger up.
5743 x3 += 30; y3 -= 20;
5744 processPosition(mapper, x3, y3);
5745 processMTSync(mapper);
5746 processSync(mapper);
5747
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5749 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5750 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5751 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5752 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5753 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5754 motionArgs.action);
5755 ASSERT_EQ(0, motionArgs.flags);
5756 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5757 ASSERT_EQ(0, motionArgs.buttonState);
5758 ASSERT_EQ(0, motionArgs.edgeFlags);
5759 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5760 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5762 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5765 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5767 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5768 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5769 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5770 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5771
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5773 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5774 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5775 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5776 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5778 ASSERT_EQ(0, motionArgs.flags);
5779 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5780 ASSERT_EQ(0, motionArgs.buttonState);
5781 ASSERT_EQ(0, motionArgs.edgeFlags);
5782 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5783 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5786 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5787 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5788 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5789 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5790
5791 // Last finger up.
5792 processMTSync(mapper);
5793 processSync(mapper);
5794
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5797 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5798 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5799 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5801 ASSERT_EQ(0, motionArgs.flags);
5802 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5803 ASSERT_EQ(0, motionArgs.buttonState);
5804 ASSERT_EQ(0, motionArgs.edgeFlags);
5805 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5806 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5809 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5810 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5811 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5812 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5813
5814 // Should not have sent any more keys or motions.
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5817}
5818
5819TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005820 addConfigurationProperty("touch.deviceType", "touchScreen");
5821 prepareDisplay(DISPLAY_ORIENTATION_0);
5822 prepareAxes(POSITION | ID);
5823 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005824 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825
arthurhungdcef2dc2020-08-11 14:47:50 +08005826 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827
5828 NotifyMotionArgs motionArgs;
5829
5830 // Two fingers down at once.
5831 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5832 processPosition(mapper, x1, y1);
5833 processId(mapper, 1);
5834 processMTSync(mapper);
5835 processPosition(mapper, x2, y2);
5836 processId(mapper, 2);
5837 processMTSync(mapper);
5838 processSync(mapper);
5839
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5841 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5842 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5843 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5846 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5850 motionArgs.action);
5851 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5854 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5857 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5859 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5860
5861 // Move.
5862 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5863 processPosition(mapper, x1, y1);
5864 processId(mapper, 1);
5865 processMTSync(mapper);
5866 processPosition(mapper, x2, y2);
5867 processId(mapper, 2);
5868 processMTSync(mapper);
5869 processSync(mapper);
5870
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5873 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5874 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5876 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5879 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5881 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5882
5883 // First finger up.
5884 x2 += 15; y2 -= 20;
5885 processPosition(mapper, x2, y2);
5886 processId(mapper, 2);
5887 processMTSync(mapper);
5888 processSync(mapper);
5889
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5891 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5892 motionArgs.action);
5893 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5896 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5899 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5901 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5902
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5905 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5906 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5909 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5910
5911 // Move.
5912 x2 += 20; y2 -= 25;
5913 processPosition(mapper, x2, y2);
5914 processId(mapper, 2);
5915 processMTSync(mapper);
5916 processSync(mapper);
5917
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5920 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5921 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5924 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5925
5926 // New finger down.
5927 int32_t x3 = 700, y3 = 300;
5928 processPosition(mapper, x2, y2);
5929 processId(mapper, 2);
5930 processMTSync(mapper);
5931 processPosition(mapper, x3, y3);
5932 processId(mapper, 3);
5933 processMTSync(mapper);
5934 processSync(mapper);
5935
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5937 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5938 motionArgs.action);
5939 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5940 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5942 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5945 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5947 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5948
5949 // Second finger up.
5950 x3 += 30; y3 -= 20;
5951 processPosition(mapper, x3, y3);
5952 processId(mapper, 3);
5953 processMTSync(mapper);
5954 processSync(mapper);
5955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5958 motionArgs.action);
5959 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5960 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5962 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5965 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5967 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5968
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5971 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5972 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5975 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5976
5977 // Last finger up.
5978 processMTSync(mapper);
5979 processSync(mapper);
5980
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5983 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5984 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5985 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5987 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5988
5989 // Should not have sent any more keys or motions.
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5992}
5993
5994TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 addConfigurationProperty("touch.deviceType", "touchScreen");
5996 prepareDisplay(DISPLAY_ORIENTATION_0);
5997 prepareAxes(POSITION | ID | SLOT);
5998 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005999 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000
arthurhungdcef2dc2020-08-11 14:47:50 +08006001 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002
6003 NotifyMotionArgs motionArgs;
6004
6005 // Two fingers down at once.
6006 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6007 processPosition(mapper, x1, y1);
6008 processId(mapper, 1);
6009 processSlot(mapper, 1);
6010 processPosition(mapper, x2, y2);
6011 processId(mapper, 2);
6012 processSync(mapper);
6013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6015 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6016 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6020 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6021
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6023 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6024 motionArgs.action);
6025 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6026 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6028 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6031 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6033 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6034
6035 // Move.
6036 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6037 processSlot(mapper, 0);
6038 processPosition(mapper, x1, y1);
6039 processSlot(mapper, 1);
6040 processPosition(mapper, x2, y2);
6041 processSync(mapper);
6042
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6045 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6046 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6048 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6051 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6053 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6054
6055 // First finger up.
6056 x2 += 15; y2 -= 20;
6057 processSlot(mapper, 0);
6058 processId(mapper, -1);
6059 processSlot(mapper, 1);
6060 processPosition(mapper, x2, y2);
6061 processSync(mapper);
6062
6063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6064 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6065 motionArgs.action);
6066 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6067 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6069 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6072 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6074 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6075
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6078 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6079 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6082 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6083
6084 // Move.
6085 x2 += 20; y2 -= 25;
6086 processPosition(mapper, x2, y2);
6087 processSync(mapper);
6088
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6091 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6092 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6095 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6096
6097 // New finger down.
6098 int32_t x3 = 700, y3 = 300;
6099 processPosition(mapper, x2, y2);
6100 processSlot(mapper, 0);
6101 processId(mapper, 3);
6102 processPosition(mapper, x3, y3);
6103 processSync(mapper);
6104
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6106 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6107 motionArgs.action);
6108 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6109 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6111 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6114 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6116 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6117
6118 // Second finger up.
6119 x3 += 30; y3 -= 20;
6120 processSlot(mapper, 1);
6121 processId(mapper, -1);
6122 processSlot(mapper, 0);
6123 processPosition(mapper, x3, y3);
6124 processSync(mapper);
6125
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6127 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6128 motionArgs.action);
6129 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6130 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6132 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6135 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6137 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6138
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6141 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6142 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6145 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6146
6147 // Last finger up.
6148 processId(mapper, -1);
6149 processSync(mapper);
6150
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6153 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6154 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6157 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6158
6159 // Should not have sent any more keys or motions.
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6162}
6163
6164TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 addConfigurationProperty("touch.deviceType", "touchScreen");
6166 prepareDisplay(DISPLAY_ORIENTATION_0);
6167 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006168 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169
6170 // These calculations are based on the input device calibration documentation.
6171 int32_t rawX = 100;
6172 int32_t rawY = 200;
6173 int32_t rawTouchMajor = 7;
6174 int32_t rawTouchMinor = 6;
6175 int32_t rawToolMajor = 9;
6176 int32_t rawToolMinor = 8;
6177 int32_t rawPressure = 11;
6178 int32_t rawDistance = 0;
6179 int32_t rawOrientation = 3;
6180 int32_t id = 5;
6181
6182 float x = toDisplayX(rawX);
6183 float y = toDisplayY(rawY);
6184 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6185 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6186 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6187 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6188 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6189 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6190 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6191 float distance = float(rawDistance);
6192
6193 processPosition(mapper, rawX, rawY);
6194 processTouchMajor(mapper, rawTouchMajor);
6195 processTouchMinor(mapper, rawTouchMinor);
6196 processToolMajor(mapper, rawToolMajor);
6197 processToolMinor(mapper, rawToolMinor);
6198 processPressure(mapper, rawPressure);
6199 processOrientation(mapper, rawOrientation);
6200 processDistance(mapper, rawDistance);
6201 processId(mapper, id);
6202 processMTSync(mapper);
6203 processSync(mapper);
6204
6205 NotifyMotionArgs args;
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6207 ASSERT_EQ(0, args.pointerProperties[0].id);
6208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6209 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6210 orientation, distance));
6211}
6212
6213TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 addConfigurationProperty("touch.deviceType", "touchScreen");
6215 prepareDisplay(DISPLAY_ORIENTATION_0);
6216 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6217 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006218 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219
6220 // These calculations are based on the input device calibration documentation.
6221 int32_t rawX = 100;
6222 int32_t rawY = 200;
6223 int32_t rawTouchMajor = 140;
6224 int32_t rawTouchMinor = 120;
6225 int32_t rawToolMajor = 180;
6226 int32_t rawToolMinor = 160;
6227
6228 float x = toDisplayX(rawX);
6229 float y = toDisplayY(rawY);
6230 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6231 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6232 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6233 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6234 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6235
6236 processPosition(mapper, rawX, rawY);
6237 processTouchMajor(mapper, rawTouchMajor);
6238 processTouchMinor(mapper, rawTouchMinor);
6239 processToolMajor(mapper, rawToolMajor);
6240 processToolMinor(mapper, rawToolMinor);
6241 processMTSync(mapper);
6242 processSync(mapper);
6243
6244 NotifyMotionArgs args;
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6247 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6248}
6249
6250TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251 addConfigurationProperty("touch.deviceType", "touchScreen");
6252 prepareDisplay(DISPLAY_ORIENTATION_0);
6253 prepareAxes(POSITION | TOUCH | TOOL);
6254 addConfigurationProperty("touch.size.calibration", "diameter");
6255 addConfigurationProperty("touch.size.scale", "10");
6256 addConfigurationProperty("touch.size.bias", "160");
6257 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006258 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259
6260 // These calculations are based on the input device calibration documentation.
6261 // Note: We only provide a single common touch/tool value because the device is assumed
6262 // not to emit separate values for each pointer (isSummed = 1).
6263 int32_t rawX = 100;
6264 int32_t rawY = 200;
6265 int32_t rawX2 = 150;
6266 int32_t rawY2 = 250;
6267 int32_t rawTouchMajor = 5;
6268 int32_t rawToolMajor = 8;
6269
6270 float x = toDisplayX(rawX);
6271 float y = toDisplayY(rawY);
6272 float x2 = toDisplayX(rawX2);
6273 float y2 = toDisplayY(rawY2);
6274 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6275 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6276 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6277
6278 processPosition(mapper, rawX, rawY);
6279 processTouchMajor(mapper, rawTouchMajor);
6280 processToolMajor(mapper, rawToolMajor);
6281 processMTSync(mapper);
6282 processPosition(mapper, rawX2, rawY2);
6283 processTouchMajor(mapper, rawTouchMajor);
6284 processToolMajor(mapper, rawToolMajor);
6285 processMTSync(mapper);
6286 processSync(mapper);
6287
6288 NotifyMotionArgs args;
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6291
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6294 args.action);
6295 ASSERT_EQ(size_t(2), args.pointerCount);
6296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6297 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6299 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6300}
6301
6302TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 addConfigurationProperty("touch.deviceType", "touchScreen");
6304 prepareDisplay(DISPLAY_ORIENTATION_0);
6305 prepareAxes(POSITION | TOUCH | TOOL);
6306 addConfigurationProperty("touch.size.calibration", "area");
6307 addConfigurationProperty("touch.size.scale", "43");
6308 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006309 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310
6311 // These calculations are based on the input device calibration documentation.
6312 int32_t rawX = 100;
6313 int32_t rawY = 200;
6314 int32_t rawTouchMajor = 5;
6315 int32_t rawToolMajor = 8;
6316
6317 float x = toDisplayX(rawX);
6318 float y = toDisplayY(rawY);
6319 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6320 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6321 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6322
6323 processPosition(mapper, rawX, rawY);
6324 processTouchMajor(mapper, rawTouchMajor);
6325 processToolMajor(mapper, rawToolMajor);
6326 processMTSync(mapper);
6327 processSync(mapper);
6328
6329 NotifyMotionArgs args;
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6332 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6333}
6334
6335TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 addConfigurationProperty("touch.deviceType", "touchScreen");
6337 prepareDisplay(DISPLAY_ORIENTATION_0);
6338 prepareAxes(POSITION | PRESSURE);
6339 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6340 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006341 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342
Michael Wrightaa449c92017-12-13 21:21:43 +00006343 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006344 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006345 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6346 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6347 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6348
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 // These calculations are based on the input device calibration documentation.
6350 int32_t rawX = 100;
6351 int32_t rawY = 200;
6352 int32_t rawPressure = 60;
6353
6354 float x = toDisplayX(rawX);
6355 float y = toDisplayY(rawY);
6356 float pressure = float(rawPressure) * 0.01f;
6357
6358 processPosition(mapper, rawX, rawY);
6359 processPressure(mapper, rawPressure);
6360 processMTSync(mapper);
6361 processSync(mapper);
6362
6363 NotifyMotionArgs args;
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6366 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6367}
6368
6369TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 addConfigurationProperty("touch.deviceType", "touchScreen");
6371 prepareDisplay(DISPLAY_ORIENTATION_0);
6372 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006373 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374
6375 NotifyMotionArgs motionArgs;
6376 NotifyKeyArgs keyArgs;
6377
6378 processId(mapper, 1);
6379 processPosition(mapper, 100, 200);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6383 ASSERT_EQ(0, motionArgs.buttonState);
6384
6385 // press BTN_LEFT, release BTN_LEFT
6386 processKey(mapper, BTN_LEFT, 1);
6387 processSync(mapper);
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6390 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6391
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6394 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6395
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 processKey(mapper, BTN_LEFT, 0);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006401
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006404 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405
6406 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6407 processKey(mapper, BTN_RIGHT, 1);
6408 processKey(mapper, BTN_MIDDLE, 1);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6412 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6413 motionArgs.buttonState);
6414
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6417 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6418
6419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6421 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6422 motionArgs.buttonState);
6423
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424 processKey(mapper, BTN_RIGHT, 0);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006432 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433
6434 processKey(mapper, BTN_MIDDLE, 0);
6435 processSync(mapper);
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006442 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443
6444 // press BTN_BACK, release BTN_BACK
6445 processKey(mapper, BTN_BACK, 1);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6448 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6449 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006450
Michael Wrightd02c5b62014-02-10 15:10:22 -08006451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006453 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6454
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6457 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458
6459 processKey(mapper, BTN_BACK, 0);
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006464
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006467 ASSERT_EQ(0, motionArgs.buttonState);
6468
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6471 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6472
6473 // press BTN_SIDE, release BTN_SIDE
6474 processKey(mapper, BTN_SIDE, 1);
6475 processSync(mapper);
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6477 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6478 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6483
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6486 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487
6488 processKey(mapper, BTN_SIDE, 0);
6489 processSync(mapper);
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006493
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006496 ASSERT_EQ(0, motionArgs.buttonState);
6497
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6499 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6500 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6501
6502 // press BTN_FORWARD, release BTN_FORWARD
6503 processKey(mapper, BTN_FORWARD, 1);
6504 processSync(mapper);
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6506 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6507 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006508
Michael Wrightd02c5b62014-02-10 15:10:22 -08006509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006511 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6512
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6515 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516
6517 processKey(mapper, BTN_FORWARD, 0);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006521 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006522
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006525 ASSERT_EQ(0, motionArgs.buttonState);
6526
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6528 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6529 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6530
6531 // press BTN_EXTRA, release BTN_EXTRA
6532 processKey(mapper, BTN_EXTRA, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6535 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6536 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006537
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006540 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6541
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6544 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006545
6546 processKey(mapper, BTN_EXTRA, 0);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006551
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006554 ASSERT_EQ(0, motionArgs.buttonState);
6555
Michael Wrightd02c5b62014-02-10 15:10:22 -08006556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6557 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6558 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6559
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6561
Michael Wrightd02c5b62014-02-10 15:10:22 -08006562 // press BTN_STYLUS, release BTN_STYLUS
6563 processKey(mapper, BTN_STYLUS, 1);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006567 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6568
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6571 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006572
6573 processKey(mapper, BTN_STYLUS, 0);
6574 processSync(mapper);
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006576 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006577 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006578
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006581 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006582
6583 // press BTN_STYLUS2, release BTN_STYLUS2
6584 processKey(mapper, BTN_STYLUS2, 1);
6585 processSync(mapper);
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006588 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6589
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6592 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006593
6594 processKey(mapper, BTN_STYLUS2, 0);
6595 processSync(mapper);
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006597 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006599
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006602 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006603
6604 // release touch
6605 processId(mapper, -1);
6606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6608 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6609 ASSERT_EQ(0, motionArgs.buttonState);
6610}
6611
6612TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006613 addConfigurationProperty("touch.deviceType", "touchScreen");
6614 prepareDisplay(DISPLAY_ORIENTATION_0);
6615 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006616 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006617
6618 NotifyMotionArgs motionArgs;
6619
6620 // default tool type is finger
6621 processId(mapper, 1);
6622 processPosition(mapper, 100, 200);
6623 processSync(mapper);
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6627
6628 // eraser
6629 processKey(mapper, BTN_TOOL_RUBBER, 1);
6630 processSync(mapper);
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6634
6635 // stylus
6636 processKey(mapper, BTN_TOOL_RUBBER, 0);
6637 processKey(mapper, BTN_TOOL_PEN, 1);
6638 processSync(mapper);
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6642
6643 // brush
6644 processKey(mapper, BTN_TOOL_PEN, 0);
6645 processKey(mapper, BTN_TOOL_BRUSH, 1);
6646 processSync(mapper);
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6650
6651 // pencil
6652 processKey(mapper, BTN_TOOL_BRUSH, 0);
6653 processKey(mapper, BTN_TOOL_PENCIL, 1);
6654 processSync(mapper);
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6658
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006659 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006660 processKey(mapper, BTN_TOOL_PENCIL, 0);
6661 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6666
6667 // mouse
6668 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6669 processKey(mapper, BTN_TOOL_MOUSE, 1);
6670 processSync(mapper);
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6674
6675 // lens
6676 processKey(mapper, BTN_TOOL_MOUSE, 0);
6677 processKey(mapper, BTN_TOOL_LENS, 1);
6678 processSync(mapper);
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6682
6683 // double-tap
6684 processKey(mapper, BTN_TOOL_LENS, 0);
6685 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6686 processSync(mapper);
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6690
6691 // triple-tap
6692 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6693 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6694 processSync(mapper);
6695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6698
6699 // quad-tap
6700 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6701 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6702 processSync(mapper);
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6705 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6706
6707 // finger
6708 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6709 processKey(mapper, BTN_TOOL_FINGER, 1);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6714
6715 // stylus trumps finger
6716 processKey(mapper, BTN_TOOL_PEN, 1);
6717 processSync(mapper);
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6721
6722 // eraser trumps stylus
6723 processKey(mapper, BTN_TOOL_RUBBER, 1);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6727 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6728
6729 // mouse trumps eraser
6730 processKey(mapper, BTN_TOOL_MOUSE, 1);
6731 processSync(mapper);
6732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6735
6736 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6737 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6738 processSync(mapper);
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6742
6743 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6744 processToolType(mapper, MT_TOOL_PEN);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6749
6750 // back to default tool type
6751 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6752 processKey(mapper, BTN_TOOL_MOUSE, 0);
6753 processKey(mapper, BTN_TOOL_RUBBER, 0);
6754 processKey(mapper, BTN_TOOL_PEN, 0);
6755 processKey(mapper, BTN_TOOL_FINGER, 0);
6756 processSync(mapper);
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6760}
6761
6762TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006763 addConfigurationProperty("touch.deviceType", "touchScreen");
6764 prepareDisplay(DISPLAY_ORIENTATION_0);
6765 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006766 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006767 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006768
6769 NotifyMotionArgs motionArgs;
6770
6771 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6772 processId(mapper, 1);
6773 processPosition(mapper, 100, 200);
6774 processSync(mapper);
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6778 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6779
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6783 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6784
6785 // move a little
6786 processPosition(mapper, 150, 250);
6787 processSync(mapper);
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6791 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6792
6793 // down when BTN_TOUCH is pressed, pressure defaults to 1
6794 processKey(mapper, BTN_TOUCH, 1);
6795 processSync(mapper);
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6797 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6799 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6800
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6802 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6804 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6805
6806 // up when BTN_TOUCH is released, hover restored
6807 processKey(mapper, BTN_TOUCH, 0);
6808 processSync(mapper);
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6810 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6812 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6813
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6815 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6817 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6818
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6820 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6822 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6823
6824 // exit hover when pointer goes away
6825 processId(mapper, -1);
6826 processSync(mapper);
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6830 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6831}
6832
6833TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006834 addConfigurationProperty("touch.deviceType", "touchScreen");
6835 prepareDisplay(DISPLAY_ORIENTATION_0);
6836 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006837 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006838
6839 NotifyMotionArgs motionArgs;
6840
6841 // initially hovering because pressure is 0
6842 processId(mapper, 1);
6843 processPosition(mapper, 100, 200);
6844 processPressure(mapper, 0);
6845 processSync(mapper);
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6849 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6850
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6852 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6854 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6855
6856 // move a little
6857 processPosition(mapper, 150, 250);
6858 processSync(mapper);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6860 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6862 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6863
6864 // down when pressure becomes non-zero
6865 processPressure(mapper, RAW_PRESSURE_MAX);
6866 processSync(mapper);
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6868 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6870 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6871
6872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6873 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6875 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6876
6877 // up when pressure becomes 0, hover restored
6878 processPressure(mapper, 0);
6879 processSync(mapper);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6881 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6883 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6884
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6886 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6888 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6889
6890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6891 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6893 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6894
6895 // exit hover when pointer goes away
6896 processId(mapper, -1);
6897 processSync(mapper);
6898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6899 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6901 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6902}
6903
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006904/**
6905 * Set the input device port <--> display port associations, and check that the
6906 * events are routed to the display that matches the display port.
6907 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6908 */
6909TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006910 const std::string usb2 = "USB2";
6911 const uint8_t hdmi1 = 0;
6912 const uint8_t hdmi2 = 1;
6913 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006914 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006915
6916 addConfigurationProperty("touch.deviceType", "touchScreen");
6917 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006918 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006919
6920 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6921 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6922
6923 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6924 // for this input device is specified, and the matching viewport is not present,
6925 // the input device should be disabled (at the mapper level).
6926
6927 // Add viewport for display 2 on hdmi2
6928 prepareSecondaryDisplay(type, hdmi2);
6929 // Send a touch event
6930 processPosition(mapper, 100, 100);
6931 processSync(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6933
6934 // Add viewport for display 1 on hdmi1
6935 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6936 // Send a touch event again
6937 processPosition(mapper, 100, 100);
6938 processSync(mapper);
6939
6940 NotifyMotionArgs args;
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6942 ASSERT_EQ(DISPLAY_ID, args.displayId);
6943}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006944
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006945TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006946 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006947 std::shared_ptr<FakePointerController> fakePointerController =
6948 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006949 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006950 fakePointerController->setPosition(100, 200);
6951 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006952 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6953
Garfield Tan888a6a42020-01-09 11:39:16 -08006954 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006955 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006956
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006957 prepareDisplay(DISPLAY_ORIENTATION_0);
6958 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006960
6961 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006962 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006963
6964 NotifyMotionArgs motionArgs;
6965 processPosition(mapper, 100, 100);
6966 processSync(mapper);
6967
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6969 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6970 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6971}
6972
Arthur Hung7c645402019-01-25 17:45:42 +08006973TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6974 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006975 prepareAxes(POSITION | ID | SLOT);
6976 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006977 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006978
6979 // Create the second touch screen device, and enable multi fingers.
6980 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08006981 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006982 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006983 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08006984 std::shared_ptr<InputDevice> device2 =
6985 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
6986 Flags<InputDeviceClass>(0));
6987
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006988 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6989 0 /*flat*/, 0 /*fuzz*/);
6990 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6991 0 /*flat*/, 0 /*fuzz*/);
6992 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6993 0 /*flat*/, 0 /*fuzz*/);
6994 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6995 0 /*flat*/, 0 /*fuzz*/);
6996 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6997 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6998 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006999
7000 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007001 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007002 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7003 device2->reset(ARBITRARY_TIME);
7004
7005 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007006 std::shared_ptr<FakePointerController> fakePointerController =
7007 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007008 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7009 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7010
7011 // Setup policy for associated displays and show touches.
7012 const uint8_t hdmi1 = 0;
7013 const uint8_t hdmi2 = 1;
7014 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7015 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7016 mFakePolicy->setShowTouches(true);
7017
7018 // Create displays.
7019 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007020 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007021
7022 // Default device will reconfigure above, need additional reconfiguration for another device.
7023 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007024 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007025
7026 // Two fingers down at default display.
7027 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7028 processPosition(mapper, x1, y1);
7029 processId(mapper, 1);
7030 processSlot(mapper, 1);
7031 processPosition(mapper, x2, y2);
7032 processId(mapper, 2);
7033 processSync(mapper);
7034
7035 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7036 fakePointerController->getSpots().find(DISPLAY_ID);
7037 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7038 ASSERT_EQ(size_t(2), iter->second.size());
7039
7040 // Two fingers down at second display.
7041 processPosition(mapper2, x1, y1);
7042 processId(mapper2, 1);
7043 processSlot(mapper2, 1);
7044 processPosition(mapper2, x2, y2);
7045 processId(mapper2, 2);
7046 processSync(mapper2);
7047
7048 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7049 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7050 ASSERT_EQ(size_t(2), iter->second.size());
7051}
7052
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007053TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007054 prepareAxes(POSITION);
7055 addConfigurationProperty("touch.deviceType", "touchScreen");
7056 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007057 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007058
7059 NotifyMotionArgs motionArgs;
7060 // Unrotated video frame
7061 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7062 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007063 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007064 processPosition(mapper, 100, 200);
7065 processSync(mapper);
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7067 ASSERT_EQ(frames, motionArgs.videoFrames);
7068
7069 // Subsequent touch events should not have any videoframes
7070 // This is implemented separately in FakeEventHub,
7071 // but that should match the behaviour of TouchVideoDevice.
7072 processPosition(mapper, 200, 200);
7073 processSync(mapper);
7074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7075 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7076}
7077
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007078TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007079 prepareAxes(POSITION);
7080 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007081 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007082 // Unrotated video frame
7083 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7084 NotifyMotionArgs motionArgs;
7085
7086 // Test all 4 orientations
7087 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7088 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7089 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7090 clearViewports();
7091 prepareDisplay(orientation);
7092 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007093 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007094 processPosition(mapper, 100, 200);
7095 processSync(mapper);
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7097 frames[0].rotate(orientation);
7098 ASSERT_EQ(frames, motionArgs.videoFrames);
7099 }
7100}
7101
7102TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007103 prepareAxes(POSITION);
7104 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007105 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007106 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7107 // so mix these.
7108 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7109 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7110 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7111 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7112 NotifyMotionArgs motionArgs;
7113
7114 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007115 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007116 processPosition(mapper, 100, 200);
7117 processSync(mapper);
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7119 std::for_each(frames.begin(), frames.end(),
7120 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7121 ASSERT_EQ(frames, motionArgs.videoFrames);
7122}
7123
Arthur Hung9da14732019-09-02 16:16:58 +08007124/**
7125 * If we had defined port associations, but the viewport is not ready, the touch device would be
7126 * expected to be disabled, and it should be enabled after the viewport has found.
7127 */
7128TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007129 constexpr uint8_t hdmi2 = 1;
7130 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007131 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007132
7133 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7134
7135 addConfigurationProperty("touch.deviceType", "touchScreen");
7136 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007137 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007138
7139 ASSERT_EQ(mDevice->isEnabled(), false);
7140
7141 // Add display on hdmi2, the device should be enabled and can receive touch event.
7142 prepareSecondaryDisplay(type, hdmi2);
7143 ASSERT_EQ(mDevice->isEnabled(), true);
7144
7145 // Send a touch event.
7146 processPosition(mapper, 100, 100);
7147 processSync(mapper);
7148
7149 NotifyMotionArgs args;
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7151 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7152}
7153
Arthur Hung6cd19a42019-08-30 19:04:12 +08007154
Arthur Hung6cd19a42019-08-30 19:04:12 +08007155
Arthur Hung421eb1c2020-01-16 00:09:42 +08007156TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007157 addConfigurationProperty("touch.deviceType", "touchScreen");
7158 prepareDisplay(DISPLAY_ORIENTATION_0);
7159 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007160 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007161
7162 NotifyMotionArgs motionArgs;
7163
7164 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7165 // finger down
7166 processId(mapper, 1);
7167 processPosition(mapper, x1, y1);
7168 processSync(mapper);
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7172
7173 // finger move
7174 processId(mapper, 1);
7175 processPosition(mapper, x2, y2);
7176 processSync(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7180
7181 // finger up.
7182 processId(mapper, -1);
7183 processSync(mapper);
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7187
7188 // new finger down
7189 processId(mapper, 1);
7190 processPosition(mapper, x3, y3);
7191 processSync(mapper);
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7195}
7196
7197/**
arthurhungcc7f9802020-04-30 17:55:40 +08007198 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7199 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007200 */
arthurhungcc7f9802020-04-30 17:55:40 +08007201TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007202 addConfigurationProperty("touch.deviceType", "touchScreen");
7203 prepareDisplay(DISPLAY_ORIENTATION_0);
7204 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007206
7207 NotifyMotionArgs motionArgs;
7208
7209 // default tool type is finger
7210 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007211 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007212 processPosition(mapper, x1, y1);
7213 processSync(mapper);
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7215 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7217
7218 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7219 processToolType(mapper, MT_TOOL_PALM);
7220 processSync(mapper);
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7222 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7223
7224 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007225 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007226 processPosition(mapper, x2, y2);
7227 processSync(mapper);
7228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7229
7230 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007231 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7234
7235 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007236 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007237 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007238 processPosition(mapper, x3, y3);
7239 processSync(mapper);
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7241 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7243}
7244
arthurhungbf89a482020-04-17 17:37:55 +08007245/**
arthurhungcc7f9802020-04-30 17:55:40 +08007246 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7247 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007248 */
arthurhungcc7f9802020-04-30 17:55:40 +08007249TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007250 addConfigurationProperty("touch.deviceType", "touchScreen");
7251 prepareDisplay(DISPLAY_ORIENTATION_0);
7252 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7253 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7254
7255 NotifyMotionArgs motionArgs;
7256
7257 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007258 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7259 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007260 processPosition(mapper, x1, y1);
7261 processSync(mapper);
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7263 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7265
7266 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007267 processSlot(mapper, SECOND_SLOT);
7268 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007269 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7273 motionArgs.action);
7274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7275
7276 // If the tool type of the first finger changes to MT_TOOL_PALM,
7277 // we expect to receive ACTION_POINTER_UP with cancel flag.
7278 processSlot(mapper, FIRST_SLOT);
7279 processId(mapper, FIRST_TRACKING_ID);
7280 processToolType(mapper, MT_TOOL_PALM);
7281 processSync(mapper);
7282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7283 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7284 motionArgs.action);
7285 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7286
7287 // The following MOVE events of second finger should be processed.
7288 processSlot(mapper, SECOND_SLOT);
7289 processId(mapper, SECOND_TRACKING_ID);
7290 processPosition(mapper, x2 + 1, y2 + 1);
7291 processSync(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7294 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7295
7296 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7297 // it. Second finger receive move.
7298 processSlot(mapper, FIRST_SLOT);
7299 processId(mapper, INVALID_TRACKING_ID);
7300 processSync(mapper);
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7303 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7304
7305 // Second finger keeps moving.
7306 processSlot(mapper, SECOND_SLOT);
7307 processId(mapper, SECOND_TRACKING_ID);
7308 processPosition(mapper, x2 + 2, y2 + 2);
7309 processSync(mapper);
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7312 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7313
7314 // Second finger up.
7315 processId(mapper, INVALID_TRACKING_ID);
7316 processSync(mapper);
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7318 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7319 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7320}
7321
7322/**
7323 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7324 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7325 */
7326TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7327 addConfigurationProperty("touch.deviceType", "touchScreen");
7328 prepareDisplay(DISPLAY_ORIENTATION_0);
7329 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7330 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7331
7332 NotifyMotionArgs motionArgs;
7333
7334 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7335 // First finger down.
7336 processId(mapper, FIRST_TRACKING_ID);
7337 processPosition(mapper, x1, y1);
7338 processSync(mapper);
7339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7340 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7342
7343 // Second finger down.
7344 processSlot(mapper, SECOND_SLOT);
7345 processId(mapper, SECOND_TRACKING_ID);
7346 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007347 processSync(mapper);
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7350 motionArgs.action);
7351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7352
arthurhungcc7f9802020-04-30 17:55:40 +08007353 // If the tool type of the first finger changes to MT_TOOL_PALM,
7354 // we expect to receive ACTION_POINTER_UP with cancel flag.
7355 processSlot(mapper, FIRST_SLOT);
7356 processId(mapper, FIRST_TRACKING_ID);
7357 processToolType(mapper, MT_TOOL_PALM);
7358 processSync(mapper);
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7361 motionArgs.action);
7362 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7363
7364 // Second finger keeps moving.
7365 processSlot(mapper, SECOND_SLOT);
7366 processId(mapper, SECOND_TRACKING_ID);
7367 processPosition(mapper, x2 + 1, y2 + 1);
7368 processSync(mapper);
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7371
7372 // second finger becomes palm, receive cancel due to only 1 finger is active.
7373 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007374 processToolType(mapper, MT_TOOL_PALM);
7375 processSync(mapper);
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7377 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7378
arthurhungcc7f9802020-04-30 17:55:40 +08007379 // third finger down.
7380 processSlot(mapper, THIRD_SLOT);
7381 processId(mapper, THIRD_TRACKING_ID);
7382 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007383 processPosition(mapper, x3, y3);
7384 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7386 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007388 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7389
7390 // third finger move
7391 processId(mapper, THIRD_TRACKING_ID);
7392 processPosition(mapper, x3 + 1, y3 + 1);
7393 processSync(mapper);
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7396
7397 // first finger up, third finger receive move.
7398 processSlot(mapper, FIRST_SLOT);
7399 processId(mapper, INVALID_TRACKING_ID);
7400 processSync(mapper);
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7403 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7404
7405 // second finger up, third finger receive move.
7406 processSlot(mapper, SECOND_SLOT);
7407 processId(mapper, INVALID_TRACKING_ID);
7408 processSync(mapper);
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7411 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7412
7413 // third finger up.
7414 processSlot(mapper, THIRD_SLOT);
7415 processId(mapper, INVALID_TRACKING_ID);
7416 processSync(mapper);
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7418 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7419 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7420}
7421
7422/**
7423 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7424 * and the active finger could still be allowed to receive the events
7425 */
7426TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7427 addConfigurationProperty("touch.deviceType", "touchScreen");
7428 prepareDisplay(DISPLAY_ORIENTATION_0);
7429 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7430 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7431
7432 NotifyMotionArgs motionArgs;
7433
7434 // default tool type is finger
7435 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7436 processId(mapper, FIRST_TRACKING_ID);
7437 processPosition(mapper, x1, y1);
7438 processSync(mapper);
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7442
7443 // Second finger down.
7444 processSlot(mapper, SECOND_SLOT);
7445 processId(mapper, SECOND_TRACKING_ID);
7446 processPosition(mapper, x2, y2);
7447 processSync(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7450 motionArgs.action);
7451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7452
7453 // If the tool type of the second finger changes to MT_TOOL_PALM,
7454 // we expect to receive ACTION_POINTER_UP with cancel flag.
7455 processId(mapper, SECOND_TRACKING_ID);
7456 processToolType(mapper, MT_TOOL_PALM);
7457 processSync(mapper);
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7460 motionArgs.action);
7461 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7462
7463 // The following MOVE event should be processed.
7464 processSlot(mapper, FIRST_SLOT);
7465 processId(mapper, FIRST_TRACKING_ID);
7466 processPosition(mapper, x1 + 1, y1 + 1);
7467 processSync(mapper);
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7470 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7471
7472 // second finger up.
7473 processSlot(mapper, SECOND_SLOT);
7474 processId(mapper, INVALID_TRACKING_ID);
7475 processSync(mapper);
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7478
7479 // first finger keep moving
7480 processSlot(mapper, FIRST_SLOT);
7481 processId(mapper, FIRST_TRACKING_ID);
7482 processPosition(mapper, x1 + 2, y1 + 2);
7483 processSync(mapper);
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7486
7487 // first finger up.
7488 processId(mapper, INVALID_TRACKING_ID);
7489 processSync(mapper);
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7491 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7492 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007493}
7494
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007495// --- MultiTouchInputMapperTest_ExternalDevice ---
7496
7497class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7498protected:
Chris Yea52ade12020-08-27 16:49:20 -07007499 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007500};
7501
7502/**
7503 * Expect fallback to internal viewport if device is external and external viewport is not present.
7504 */
7505TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7506 prepareAxes(POSITION);
7507 addConfigurationProperty("touch.deviceType", "touchScreen");
7508 prepareDisplay(DISPLAY_ORIENTATION_0);
7509 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7510
7511 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7512
7513 NotifyMotionArgs motionArgs;
7514
7515 // Expect the event to be sent to the internal viewport,
7516 // because an external viewport is not present.
7517 processPosition(mapper, 100, 100);
7518 processSync(mapper);
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7520 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7521
7522 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007523 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007524 processPosition(mapper, 100, 100);
7525 processSync(mapper);
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7527 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7528}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007529
7530/**
7531 * Test touch should not work if outside of surface.
7532 */
7533class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7534protected:
7535 void halfDisplayToCenterHorizontal(int32_t orientation) {
7536 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007537 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007538
7539 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7540 internalViewport->orientation = orientation;
7541 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7542 internalViewport->logicalLeft = 0;
7543 internalViewport->logicalTop = 0;
7544 internalViewport->logicalRight = DISPLAY_HEIGHT;
7545 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7546
7547 internalViewport->physicalLeft = 0;
7548 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7549 internalViewport->physicalRight = DISPLAY_HEIGHT;
7550 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7551
7552 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7553 internalViewport->deviceHeight = DISPLAY_WIDTH;
7554 } else {
7555 internalViewport->logicalLeft = 0;
7556 internalViewport->logicalTop = 0;
7557 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7558 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7559
7560 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7561 internalViewport->physicalTop = 0;
7562 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7563 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7564
7565 internalViewport->deviceWidth = DISPLAY_WIDTH;
7566 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7567 }
7568
7569 mFakePolicy->updateViewport(internalViewport.value());
7570 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7571 }
7572
7573 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7574 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7575 int32_t yExpected) {
7576 // touch on outside area should not work.
7577 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7578 processSync(mapper);
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7580
7581 // touch on inside area should receive the event.
7582 NotifyMotionArgs args;
7583 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7584 processSync(mapper);
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7586 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7587 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7588
7589 // Reset.
7590 mapper.reset(ARBITRARY_TIME);
7591 }
7592};
7593
7594TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7595 addConfigurationProperty("touch.deviceType", "touchScreen");
7596 prepareDisplay(DISPLAY_ORIENTATION_0);
7597 prepareAxes(POSITION);
7598 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7599
7600 // Touch on center of normal display should work.
7601 const int32_t x = DISPLAY_WIDTH / 4;
7602 const int32_t y = DISPLAY_HEIGHT / 2;
7603 processPosition(mapper, toRawX(x), toRawY(y));
7604 processSync(mapper);
7605 NotifyMotionArgs args;
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7608 0.0f, 0.0f, 0.0f, 0.0f));
7609 // Reset.
7610 mapper.reset(ARBITRARY_TIME);
7611
7612 // Let physical display be different to device, and make surface and physical could be 1:1.
7613 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7614
7615 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7616 const int32_t yExpected = y;
7617 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7618}
7619
7620TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7621 addConfigurationProperty("touch.deviceType", "touchScreen");
7622 prepareDisplay(DISPLAY_ORIENTATION_0);
7623 prepareAxes(POSITION);
7624 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7625
7626 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7627 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7628
7629 const int32_t x = DISPLAY_WIDTH / 4;
7630 const int32_t y = DISPLAY_HEIGHT / 2;
7631
7632 // expect x/y = swap x/y then reverse y.
7633 const int32_t xExpected = y;
7634 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7635 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7636}
7637
7638TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7639 addConfigurationProperty("touch.deviceType", "touchScreen");
7640 prepareDisplay(DISPLAY_ORIENTATION_0);
7641 prepareAxes(POSITION);
7642 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7643
7644 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7645 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7646
7647 const int32_t x = DISPLAY_WIDTH / 4;
7648 const int32_t y = DISPLAY_HEIGHT / 2;
7649
7650 // expect x/y = swap x/y then reverse x.
7651 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7652 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7653 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7654}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007655
7656TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7657 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7658 std::shared_ptr<FakePointerController> fakePointerController =
7659 std::make_shared<FakePointerController>();
7660 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7661 fakePointerController->setPosition(0, 0);
7662 fakePointerController->setButtonState(0);
7663
7664 // prepare device and capture
7665 prepareDisplay(DISPLAY_ORIENTATION_0);
7666 prepareAxes(POSITION | ID | SLOT);
7667 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7668 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7669 mFakePolicy->setPointerCapture(true);
7670 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7671 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7672
7673 // captured touchpad should be a touchpad source
7674 NotifyDeviceResetArgs resetArgs;
7675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7676 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7677
Chris Yef74dc422020-09-02 22:41:50 -07007678 InputDeviceInfo deviceInfo;
7679 mDevice->getDeviceInfo(&deviceInfo);
7680
7681 const InputDeviceInfo::MotionRange* relRangeX =
7682 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
7683 ASSERT_NE(relRangeX, nullptr);
7684 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
7685 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
7686 const InputDeviceInfo::MotionRange* relRangeY =
7687 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
7688 ASSERT_NE(relRangeY, nullptr);
7689 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
7690 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
7691
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007692 // run captured pointer tests - note that this is unscaled, so input listener events should be
7693 // identical to what the hardware sends (accounting for any
7694 // calibration).
7695 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007696 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007697 processId(mapper, 1);
7698 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7699 processKey(mapper, BTN_TOUCH, 1);
7700 processSync(mapper);
7701
7702 // expect coord[0] to contain initial location of touch 0
7703 NotifyMotionArgs args;
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7705 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7706 ASSERT_EQ(1U, args.pointerCount);
7707 ASSERT_EQ(0, args.pointerProperties[0].id);
7708 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7709 ASSERT_NO_FATAL_FAILURE(
7710 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7711
7712 // FINGER 1 DOWN
7713 processSlot(mapper, 1);
7714 processId(mapper, 2);
7715 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7716 processSync(mapper);
7717
7718 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007720 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7721 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007722 ASSERT_EQ(2U, args.pointerCount);
7723 ASSERT_EQ(0, args.pointerProperties[0].id);
7724 ASSERT_EQ(1, args.pointerProperties[1].id);
7725 ASSERT_NO_FATAL_FAILURE(
7726 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7727 ASSERT_NO_FATAL_FAILURE(
7728 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7729
7730 // FINGER 1 MOVE
7731 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7732 processSync(mapper);
7733
7734 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7735 // from move
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7738 ASSERT_NO_FATAL_FAILURE(
7739 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7740 ASSERT_NO_FATAL_FAILURE(
7741 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7742
7743 // FINGER 0 MOVE
7744 processSlot(mapper, 0);
7745 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7746 processSync(mapper);
7747
7748 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7751 ASSERT_NO_FATAL_FAILURE(
7752 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7753 ASSERT_NO_FATAL_FAILURE(
7754 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7755
7756 // BUTTON DOWN
7757 processKey(mapper, BTN_LEFT, 1);
7758 processSync(mapper);
7759
7760 // touchinputmapper design sends a move before button press
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7765
7766 // BUTTON UP
7767 processKey(mapper, BTN_LEFT, 0);
7768 processSync(mapper);
7769
7770 // touchinputmapper design sends a move after button release
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7772 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7775
7776 // FINGER 0 UP
7777 processId(mapper, -1);
7778 processSync(mapper);
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7780 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7781
7782 // FINGER 1 MOVE
7783 processSlot(mapper, 1);
7784 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7785 processSync(mapper);
7786
7787 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7790 ASSERT_EQ(1U, args.pointerCount);
7791 ASSERT_EQ(1, args.pointerProperties[0].id);
7792 ASSERT_NO_FATAL_FAILURE(
7793 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7794
7795 // FINGER 1 UP
7796 processId(mapper, -1);
7797 processKey(mapper, BTN_TOUCH, 0);
7798 processSync(mapper);
7799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7800 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7801
7802 // non captured touchpad should be a mouse source
7803 mFakePolicy->setPointerCapture(false);
7804 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7806 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7807}
7808
7809TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7810 std::shared_ptr<FakePointerController> fakePointerController =
7811 std::make_shared<FakePointerController>();
7812 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7813 fakePointerController->setPosition(0, 0);
7814 fakePointerController->setButtonState(0);
7815
7816 // prepare device and capture
7817 prepareDisplay(DISPLAY_ORIENTATION_0);
7818 prepareAxes(POSITION | ID | SLOT);
7819 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7820 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7821 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7822 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7823 // run uncaptured pointer tests - pushes out generic events
7824 // FINGER 0 DOWN
7825 processId(mapper, 3);
7826 processPosition(mapper, 100, 100);
7827 processKey(mapper, BTN_TOUCH, 1);
7828 processSync(mapper);
7829
7830 // start at (100,100), cursor should be at (0,0) * scale
7831 NotifyMotionArgs args;
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7833 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7834 ASSERT_NO_FATAL_FAILURE(
7835 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7836
7837 // FINGER 0 MOVE
7838 processPosition(mapper, 200, 200);
7839 processSync(mapper);
7840
7841 // compute scaling to help with touch position checking
7842 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7843 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7844 float scale =
7845 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7846
7847 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7849 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7851 0, 0, 0, 0, 0, 0, 0));
7852}
7853
7854TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7855 std::shared_ptr<FakePointerController> fakePointerController =
7856 std::make_shared<FakePointerController>();
7857
7858 prepareDisplay(DISPLAY_ORIENTATION_0);
7859 prepareAxes(POSITION | ID | SLOT);
7860 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7861 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7862 mFakePolicy->setPointerCapture(false);
7863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7864
7865 // uncaptured touchpad should be a pointer device
7866 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7867
7868 // captured touchpad should be a touchpad device
7869 mFakePolicy->setPointerCapture(true);
7870 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7871 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7872}
7873
Michael Wrightd02c5b62014-02-10 15:10:22 -08007874} // namespace android