blob: f56735af604445a7e312fa7399f5e4d9eab85f3b [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
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001357TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001358 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001359 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001360 constexpr int32_t eventHubId = 1;
1361 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001362 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001363 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001364 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001365 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001366
Yi Kong9b14ac62018-07-17 13:48:38 -07001367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001368
1369 NotifyDeviceResetArgs resetArgs;
1370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001371 ASSERT_EQ(deviceId, resetArgs.deviceId);
1372
1373 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001374 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001375 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001376
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001378 ASSERT_EQ(deviceId, resetArgs.deviceId);
1379 ASSERT_EQ(device->isEnabled(), false);
1380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001381 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001382 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001385 ASSERT_EQ(device->isEnabled(), false);
1386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001387 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001388 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001390 ASSERT_EQ(deviceId, resetArgs.deviceId);
1391 ASSERT_EQ(device->isEnabled(), true);
1392}
1393
Michael Wrightd02c5b62014-02-10 15:10:22 -08001394TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001395 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001396 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001397 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001398 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001399 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001400 AINPUT_SOURCE_KEYBOARD, nullptr);
1401 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001402
1403 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1404 AINPUT_SOURCE_ANY, AKEYCODE_A))
1405 << "Should return unknown when the device id is >= 0 but unknown.";
1406
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001407 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1408 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1409 << "Should return unknown when the device id is valid but the sources are not "
1410 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001411
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001412 ASSERT_EQ(AKEY_STATE_DOWN,
1413 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1414 AKEYCODE_A))
1415 << "Should return value provided by mapper when device id is valid and the device "
1416 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001417
1418 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1419 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1420 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1421
1422 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1423 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1424 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1425}
1426
1427TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001428 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001429 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001430 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001431 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001432 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001433 AINPUT_SOURCE_KEYBOARD, nullptr);
1434 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001435
1436 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1437 AINPUT_SOURCE_ANY, KEY_A))
1438 << "Should return unknown when the device id is >= 0 but unknown.";
1439
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001440 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1441 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1442 << "Should return unknown when the device id is valid but the sources are not "
1443 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001444
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001445 ASSERT_EQ(AKEY_STATE_DOWN,
1446 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1447 KEY_A))
1448 << "Should return value provided by mapper when device id is valid and the device "
1449 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001450
1451 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1452 AINPUT_SOURCE_TRACKBALL, KEY_A))
1453 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1454
1455 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1456 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1457 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1458}
1459
1460TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001461 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001462 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001463 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001464 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001465 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001466 AINPUT_SOURCE_KEYBOARD, nullptr);
1467 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001468
1469 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1470 AINPUT_SOURCE_ANY, SW_LID))
1471 << "Should return unknown when the device id is >= 0 but unknown.";
1472
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001473 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1474 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1475 << "Should return unknown when the device id is valid but the sources are not "
1476 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001477
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001478 ASSERT_EQ(AKEY_STATE_DOWN,
1479 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1480 SW_LID))
1481 << "Should return value provided by mapper when device id is valid and the device "
1482 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001483
1484 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1485 AINPUT_SOURCE_TRACKBALL, SW_LID))
1486 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1487
1488 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1489 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1490 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1491}
1492
1493TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001494 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001495 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001496 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001497 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001498 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001499 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001500
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001501 mapper.addSupportedKeyCode(AKEYCODE_A);
1502 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503
1504 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1505 uint8_t flags[4] = { 0, 0, 0, 1 };
1506
1507 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1508 << "Should return false when device id is >= 0 but unknown.";
1509 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1510
1511 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001512 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1513 << "Should return false when device id is valid but the sources are not supported by "
1514 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001515 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1516
1517 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001518 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1519 keyCodes, flags))
1520 << "Should return value provided by mapper when device id is valid and the device "
1521 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001522 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1523
1524 flags[3] = 1;
1525 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1526 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1527 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1528
1529 flags[3] = 1;
1530 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1531 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1532 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1533}
1534
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001535TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001536 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001537 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001538
1539 NotifyConfigurationChangedArgs args;
1540
1541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1542 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1543}
1544
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001545TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001546 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001547 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001548 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001549 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001550 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001551 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001552
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001553 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001554 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001555 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1556
1557 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001558 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001560 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561 ASSERT_EQ(EV_KEY, event.type);
1562 ASSERT_EQ(KEY_A, event.code);
1563 ASSERT_EQ(1, event.value);
1564}
1565
Garfield Tan1c7bc862020-01-28 13:24:04 -08001566TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001567 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001568 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001569 constexpr int32_t eventHubId = 1;
1570 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001571 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001572 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001573 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001574 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001575
1576 NotifyDeviceResetArgs resetArgs;
1577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001578 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001580 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001581 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001583 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001584 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001585
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001586 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001587 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001589 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001590 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001592 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001593 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001595 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001596 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001597}
1598
Garfield Tan1c7bc862020-01-28 13:24:04 -08001599TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1600 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001601 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001602 constexpr int32_t eventHubId = 1;
1603 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1604 // Must add at least one mapper or the device will be ignored!
1605 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001606 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001607 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1608
1609 NotifyDeviceResetArgs resetArgs;
1610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1611 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1612}
1613
Arthur Hungc23540e2018-11-29 20:42:11 +08001614TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001616 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001617 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001618 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001619 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1620 FakeInputMapper& mapper =
1621 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001622 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001623
1624 const uint8_t hdmi1 = 1;
1625
1626 // Associated touch screen with second display.
1627 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1628
1629 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001630 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001631 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001632 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1633 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001634 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001635 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1636 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001637 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001638 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001639
1640 // Add the device, and make sure all of the callbacks are triggered.
1641 // The device is added after the input port associations are processed since
1642 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001646 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001647
Arthur Hung2c9a3342019-07-23 14:18:59 +08001648 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001649 ASSERT_EQ(deviceId, device->getId());
1650 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1651 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001652
1653 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001654 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001655 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001656 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001657}
1658
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001659TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1660 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1661 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1662 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1663 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1664 // Must add at least one mapper or the device will be ignored!
1665 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1666 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1667 mReader->pushNextDevice(device);
1668 mReader->pushNextDevice(device);
1669 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1670 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1671
1672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1673
1674 NotifyDeviceResetArgs resetArgs;
1675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1676 ASSERT_EQ(deviceId, resetArgs.deviceId);
1677 ASSERT_TRUE(device->isEnabled());
1678 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1679 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1680
1681 disableDevice(deviceId);
1682 mReader->loopOnce();
1683
1684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1685 ASSERT_EQ(deviceId, resetArgs.deviceId);
1686 ASSERT_FALSE(device->isEnabled());
1687 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1688 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1689
1690 enableDevice(deviceId);
1691 mReader->loopOnce();
1692
1693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1694 ASSERT_EQ(deviceId, resetArgs.deviceId);
1695 ASSERT_TRUE(device->isEnabled());
1696 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1697 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1698}
1699
1700TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1701 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1702 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1703 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1704 // Add two subdevices to device
1705 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1706 FakeInputMapper& mapperDevice1 =
1707 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1708 FakeInputMapper& mapperDevice2 =
1709 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1710 mReader->pushNextDevice(device);
1711 mReader->pushNextDevice(device);
1712 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1713 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1714
1715 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1716 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1717
1718 ASSERT_EQ(AKEY_STATE_DOWN,
1719 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1720 ASSERT_EQ(AKEY_STATE_DOWN,
1721 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1722 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1723 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1724}
1725
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001726// --- InputReaderIntegrationTest ---
1727
1728// These tests create and interact with the InputReader only through its interface.
1729// The InputReader is started during SetUp(), which starts its processing in its own
1730// thread. The tests use linux uinput to emulate input devices.
1731// NOTE: Interacting with the physical device while these tests are running may cause
1732// the tests to fail.
1733class InputReaderIntegrationTest : public testing::Test {
1734protected:
1735 sp<TestInputListener> mTestListener;
1736 sp<FakeInputReaderPolicy> mFakePolicy;
1737 sp<InputReaderInterface> mReader;
1738
Chris Yea52ade12020-08-27 16:49:20 -07001739 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001740 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001741 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1742 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001743
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001744 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001745 ASSERT_EQ(mReader->start(), OK);
1746
1747 // Since this test is run on a real device, all the input devices connected
1748 // to the test device will show up in mReader. We wait for those input devices to
1749 // show up before beginning the tests.
1750 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1752 }
1753
Chris Yea52ade12020-08-27 16:49:20 -07001754 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001755 ASSERT_EQ(mReader->stop(), OK);
1756 mTestListener.clear();
1757 mFakePolicy.clear();
1758 }
1759};
1760
1761TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1762 // An invalid input device that is only used for this test.
1763 class InvalidUinputDevice : public UinputDevice {
1764 public:
1765 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1766
1767 private:
1768 void configureDevice(int fd, uinput_user_dev* device) override {}
1769 };
1770
1771 const size_t numDevices = mFakePolicy->getInputDevices().size();
1772
1773 // UinputDevice does not set any event or key bits, so InputReader should not
1774 // consider it as a valid device.
1775 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1776 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1777 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1778 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1779
1780 invalidDevice.reset();
1781 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1782 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1783 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1784}
1785
1786TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1787 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1788
1789 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1790 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1791 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1792 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1793
1794 // Find the test device by its name.
1795 std::vector<InputDeviceInfo> inputDevices;
1796 mReader->getInputDevices(inputDevices);
1797 InputDeviceInfo* keyboardInfo = nullptr;
1798 const char* keyboardName = keyboard->getName();
1799 for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1800 if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1801 keyboardInfo = &inputDevices[i];
1802 break;
1803 }
1804 }
1805 ASSERT_NE(keyboardInfo, nullptr);
1806 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1807 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1808 ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1809
1810 keyboard.reset();
1811 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1812 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1813 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1814}
1815
1816TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1817 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1818 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1819
1820 NotifyConfigurationChangedArgs configChangedArgs;
1821 ASSERT_NO_FATAL_FAILURE(
1822 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001823 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001824 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1825
1826 NotifyKeyArgs keyArgs;
1827 keyboard->pressAndReleaseHomeKey();
1828 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1829 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001830 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001831 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001832 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1833 prevTimestamp = keyArgs.eventTime;
1834
1835 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1836 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001837 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001838 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1839}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001840
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001841/**
1842 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1843 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1844 * are passed to the listener.
1845 */
1846static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1847TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1848 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1849 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1850 NotifyKeyArgs keyArgs;
1851
1852 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1853 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1854 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1855 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1856
1857 controller->pressAndReleaseKey(BTN_GEAR_UP);
1858 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1859 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1860 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1861}
1862
Arthur Hungaab25622020-01-16 11:22:11 +08001863// --- TouchProcessTest ---
1864class TouchIntegrationTest : public InputReaderIntegrationTest {
1865protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001866 const std::string UNIQUE_ID = "local:0";
1867
Chris Yea52ade12020-08-27 16:49:20 -07001868 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08001869 InputReaderIntegrationTest::SetUp();
1870 // At least add an internal display.
1871 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1872 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001873 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001874
1875 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1876 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1877 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1878 }
1879
1880 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1881 int32_t orientation, const std::string& uniqueId,
1882 std::optional<uint8_t> physicalPort,
1883 ViewportType viewportType) {
1884 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1885 physicalPort, viewportType);
1886 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1887 }
1888
1889 std::unique_ptr<UinputTouchScreen> mDevice;
1890};
1891
1892TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1893 NotifyMotionArgs args;
1894 const Point centerPoint = mDevice->getCenterPoint();
1895
1896 // ACTION_DOWN
1897 mDevice->sendDown(centerPoint);
1898 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1899 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1900
1901 // ACTION_MOVE
1902 mDevice->sendMove(centerPoint + Point(1, 1));
1903 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1905
1906 // ACTION_UP
1907 mDevice->sendUp();
1908 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1909 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1910}
1911
1912TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1913 NotifyMotionArgs args;
1914 const Point centerPoint = mDevice->getCenterPoint();
1915
1916 // ACTION_DOWN
1917 mDevice->sendDown(centerPoint);
1918 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1919 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1920
1921 // ACTION_POINTER_DOWN (Second slot)
1922 const Point secondPoint = centerPoint + Point(100, 100);
1923 mDevice->sendSlot(SECOND_SLOT);
1924 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1925 mDevice->sendDown(secondPoint + Point(1, 1));
1926 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1927 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1928 args.action);
1929
1930 // ACTION_MOVE (Second slot)
1931 mDevice->sendMove(secondPoint);
1932 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1933 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1934
1935 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001936 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08001937 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08001938 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08001939 args.action);
1940
1941 // ACTION_UP
1942 mDevice->sendSlot(FIRST_SLOT);
1943 mDevice->sendUp();
1944 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1945 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1946}
1947
1948TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1949 NotifyMotionArgs args;
1950 const Point centerPoint = mDevice->getCenterPoint();
1951
1952 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001953 mDevice->sendSlot(FIRST_SLOT);
1954 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001955 mDevice->sendDown(centerPoint);
1956 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1957 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1958
arthurhungcc7f9802020-04-30 17:55:40 +08001959 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001960 const Point secondPoint = centerPoint + Point(100, 100);
1961 mDevice->sendSlot(SECOND_SLOT);
1962 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1963 mDevice->sendDown(secondPoint);
1964 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1965 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1966 args.action);
1967
arthurhungcc7f9802020-04-30 17:55:40 +08001968 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001969 mDevice->sendMove(secondPoint + Point(1, 1));
1970 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1972
arthurhungcc7f9802020-04-30 17:55:40 +08001973 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1974 // a palm event.
1975 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001976 mDevice->sendToolType(MT_TOOL_PALM);
1977 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08001978 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1979 args.action);
1980 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001981
arthurhungcc7f9802020-04-30 17:55:40 +08001982 // Send up to second slot, expect first slot send moving.
1983 mDevice->sendPointerUp();
1984 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001986
arthurhungcc7f9802020-04-30 17:55:40 +08001987 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001988 mDevice->sendSlot(FIRST_SLOT);
1989 mDevice->sendUp();
1990
arthurhungcc7f9802020-04-30 17:55:40 +08001991 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1992 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001993}
1994
Michael Wrightd02c5b62014-02-10 15:10:22 -08001995// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08001996class InputDeviceTest : public testing::Test {
1997protected:
1998 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08001999 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000 static const int32_t DEVICE_ID;
2001 static const int32_t DEVICE_GENERATION;
2002 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002003 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002004 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002005
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002006 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002007 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002008 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002009 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002010 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002011
Chris Yea52ade12020-08-27 16:49:20 -07002012 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002013 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002014 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002015 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002016 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2017 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002018 InputDeviceIdentifier identifier;
2019 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002020 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002021 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002022 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002023 mReader->pushNextDevice(mDevice);
2024 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2025 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002026 }
2027
Chris Yea52ade12020-08-27 16:49:20 -07002028 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002029 mFakeListener.clear();
2030 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002031 }
2032};
2033
2034const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002035const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002036const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002037const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2038const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002039const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2040 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002041const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002042
2043TEST_F(InputDeviceTest, ImmutableProperties) {
2044 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002045 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002046 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002047}
2048
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002049TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2050 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002051}
2052
Michael Wrightd02c5b62014-02-10 15:10:22 -08002053TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2054 // Configuration.
2055 InputReaderConfiguration config;
2056 mDevice->configure(ARBITRARY_TIME, &config, 0);
2057
2058 // Reset.
2059 mDevice->reset(ARBITRARY_TIME);
2060
2061 NotifyDeviceResetArgs resetArgs;
2062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2063 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2064 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2065
2066 // Metadata.
2067 ASSERT_TRUE(mDevice->isIgnored());
2068 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2069
2070 InputDeviceInfo info;
2071 mDevice->getDeviceInfo(&info);
2072 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002073 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002074 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2075 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2076
2077 // State queries.
2078 ASSERT_EQ(0, mDevice->getMetaState());
2079
2080 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2081 << "Ignored device should return unknown key code state.";
2082 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2083 << "Ignored device should return unknown scan code state.";
2084 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2085 << "Ignored device should return unknown switch state.";
2086
2087 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2088 uint8_t flags[2] = { 0, 1 };
2089 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2090 << "Ignored device should never mark any key codes.";
2091 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2092 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2093}
2094
2095TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2096 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002097 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002098
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002099 FakeInputMapper& mapper1 =
2100 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002101 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2102 mapper1.setMetaState(AMETA_ALT_ON);
2103 mapper1.addSupportedKeyCode(AKEYCODE_A);
2104 mapper1.addSupportedKeyCode(AKEYCODE_B);
2105 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2106 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2107 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2108 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2109 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002110
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002111 FakeInputMapper& mapper2 =
2112 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002113 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002114
2115 InputReaderConfiguration config;
2116 mDevice->configure(ARBITRARY_TIME, &config, 0);
2117
2118 String8 propertyValue;
2119 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2120 << "Device should have read configuration during configuration phase.";
2121 ASSERT_STREQ("value", propertyValue.string());
2122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002123 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2124 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002125
2126 // Reset
2127 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002128 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2129 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002130
2131 NotifyDeviceResetArgs resetArgs;
2132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2133 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2134 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2135
2136 // Metadata.
2137 ASSERT_FALSE(mDevice->isIgnored());
2138 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2139
2140 InputDeviceInfo info;
2141 mDevice->getDeviceInfo(&info);
2142 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002143 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002144 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2145 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2146
2147 // State queries.
2148 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2149 << "Should query mappers and combine meta states.";
2150
2151 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2152 << "Should return unknown key code state when source not supported.";
2153 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2154 << "Should return unknown scan code state when source not supported.";
2155 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2156 << "Should return unknown switch state when source not supported.";
2157
2158 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2159 << "Should query mapper when source is supported.";
2160 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2161 << "Should query mapper when source is supported.";
2162 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2163 << "Should query mapper when source is supported.";
2164
2165 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2166 uint8_t flags[4] = { 0, 0, 0, 1 };
2167 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2168 << "Should do nothing when source is unsupported.";
2169 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2170 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2171 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2172 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2173
2174 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2175 << "Should query mapper when source is supported.";
2176 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2177 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2178 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2179 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2180
2181 // Event handling.
2182 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002183 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002184 mDevice->process(&event, 1);
2185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002186 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2187 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002188}
2189
Arthur Hung2c9a3342019-07-23 14:18:59 +08002190// A single input device is associated with a specific display. Check that:
2191// 1. Device is disabled if the viewport corresponding to the associated display is not found
2192// 2. Device is disabled when setEnabled API is called
2193TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002194 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002195
2196 // First Configuration.
2197 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2198
2199 // Device should be enabled by default.
2200 ASSERT_TRUE(mDevice->isEnabled());
2201
2202 // Prepare associated info.
2203 constexpr uint8_t hdmi = 1;
2204 const std::string UNIQUE_ID = "local:1";
2205
2206 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2207 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2208 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2209 // Device should be disabled because it is associated with a specific display via
2210 // input port <-> display port association, but the corresponding display is not found
2211 ASSERT_FALSE(mDevice->isEnabled());
2212
2213 // Prepare displays.
2214 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002215 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002216 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2217 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2218 ASSERT_TRUE(mDevice->isEnabled());
2219
2220 // Device should be disabled after set disable.
2221 mFakePolicy->addDisabledDevice(mDevice->getId());
2222 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2223 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2224 ASSERT_FALSE(mDevice->isEnabled());
2225
2226 // Device should still be disabled even found the associated display.
2227 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2228 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2229 ASSERT_FALSE(mDevice->isEnabled());
2230}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002231
2232// --- InputMapperTest ---
2233
2234class InputMapperTest : public testing::Test {
2235protected:
2236 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002237 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238 static const int32_t DEVICE_ID;
2239 static const int32_t DEVICE_GENERATION;
2240 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002241 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002242 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002243
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002244 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002245 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002246 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002247 std::unique_ptr<InstrumentedInputReader> mReader;
2248 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002249
Chris Ye1b0c7342020-07-28 21:57:03 -07002250 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002251 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002252 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002253 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002254 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2255 mFakeListener);
2256 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257 }
2258
Chris Yea52ade12020-08-27 16:49:20 -07002259 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002260
Chris Yea52ade12020-08-27 16:49:20 -07002261 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262 mFakeListener.clear();
2263 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264 }
2265
2266 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002267 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002268 }
2269
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002270 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002271 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002272 mReader->requestRefreshConfiguration(changes);
2273 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002274 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002275 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2276 }
2277
arthurhungdcef2dc2020-08-11 14:47:50 +08002278 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2279 const std::string& location, int32_t eventHubId,
2280 Flags<InputDeviceClass> classes) {
2281 InputDeviceIdentifier identifier;
2282 identifier.name = name;
2283 identifier.location = location;
2284 std::shared_ptr<InputDevice> device =
2285 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2286 identifier);
2287 mReader->pushNextDevice(device);
2288 mFakeEventHub->addDevice(eventHubId, name, classes);
2289 mReader->loopOnce();
2290 return device;
2291 }
2292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002293 template <class T, typename... Args>
2294 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002295 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002296 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002297 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002298 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002299 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002300 }
2301
2302 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002303 int32_t orientation, const std::string& uniqueId,
2304 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002305 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002306 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002307 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2308 }
2309
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002310 void clearViewports() {
2311 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312 }
2313
arthurhungdcef2dc2020-08-11 14:47:50 +08002314 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002315 RawEvent event;
2316 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002317 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002318 event.type = type;
2319 event.code = code;
2320 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002321 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002322 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323 }
2324
2325 static void assertMotionRange(const InputDeviceInfo& info,
2326 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2327 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002328 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2330 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2331 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2332 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2333 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2334 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2335 }
2336
2337 static void assertPointerCoords(const PointerCoords& coords,
2338 float x, float y, float pressure, float size,
2339 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2340 float orientation, float distance) {
2341 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2342 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2343 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2344 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2345 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2346 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2347 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2348 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2349 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2350 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2351 }
2352
Michael Wright17db18e2020-06-26 20:51:44 +01002353 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002354 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002355 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002356 ASSERT_NEAR(x, actualX, 1);
2357 ASSERT_NEAR(y, actualY, 1);
2358 }
2359};
2360
2361const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002362const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002363const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002364const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2365const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002366const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2367 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002368const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002369
2370// --- SwitchInputMapperTest ---
2371
2372class SwitchInputMapperTest : public InputMapperTest {
2373protected:
2374};
2375
2376TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002377 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002379 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002380}
2381
2382TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002383 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002385 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002386 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002388 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002389 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390}
2391
2392TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002393 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002395 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2396 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2397 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2398 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399
2400 NotifySwitchArgs args;
2401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2402 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002403 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2404 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405 args.switchMask);
2406 ASSERT_EQ(uint32_t(0), args.policyFlags);
2407}
2408
2409
2410// --- KeyboardInputMapperTest ---
2411
2412class KeyboardInputMapperTest : public InputMapperTest {
2413protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002414 const std::string UNIQUE_ID = "local:0";
2415
2416 void prepareDisplay(int32_t orientation);
2417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002418 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002419 int32_t originalKeyCode, int32_t rotatedKeyCode,
2420 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002421};
2422
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002423/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2424 * orientation.
2425 */
2426void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002427 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2428 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002429}
2430
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002431void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002432 int32_t originalScanCode, int32_t originalKeyCode,
2433 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 NotifyKeyArgs args;
2435
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002436 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2438 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2439 ASSERT_EQ(originalScanCode, args.scanCode);
2440 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002441 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002443 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2445 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2446 ASSERT_EQ(originalScanCode, args.scanCode);
2447 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002448 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449}
2450
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002452 KeyboardInputMapper& mapper =
2453 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2454 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002456 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457}
2458
2459TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2460 const int32_t USAGE_A = 0x070004;
2461 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002462 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2463 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002464 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2465 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2466 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002468 KeyboardInputMapper& mapper =
2469 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2470 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002471 // Initial metastate to AMETA_NONE.
2472 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2473 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474
2475 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002476 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002477 NotifyKeyArgs args;
2478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2479 ASSERT_EQ(DEVICE_ID, args.deviceId);
2480 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2481 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2482 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2483 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2484 ASSERT_EQ(KEY_HOME, args.scanCode);
2485 ASSERT_EQ(AMETA_NONE, args.metaState);
2486 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2487 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2488 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2489
2490 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002491 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2493 ASSERT_EQ(DEVICE_ID, args.deviceId);
2494 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2495 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2496 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2497 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2498 ASSERT_EQ(KEY_HOME, args.scanCode);
2499 ASSERT_EQ(AMETA_NONE, args.metaState);
2500 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2501 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2502 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2503
2504 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002505 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2506 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2508 ASSERT_EQ(DEVICE_ID, args.deviceId);
2509 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2510 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2511 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2512 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2513 ASSERT_EQ(0, args.scanCode);
2514 ASSERT_EQ(AMETA_NONE, args.metaState);
2515 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2516 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2517 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2518
2519 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002520 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2521 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2523 ASSERT_EQ(DEVICE_ID, args.deviceId);
2524 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2525 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2526 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2527 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2528 ASSERT_EQ(0, args.scanCode);
2529 ASSERT_EQ(AMETA_NONE, args.metaState);
2530 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2531 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2532 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2533
2534 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002535 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2536 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2538 ASSERT_EQ(DEVICE_ID, args.deviceId);
2539 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2540 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2541 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2542 ASSERT_EQ(0, args.keyCode);
2543 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2544 ASSERT_EQ(AMETA_NONE, args.metaState);
2545 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2546 ASSERT_EQ(0U, args.policyFlags);
2547 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2548
2549 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002550 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2551 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2553 ASSERT_EQ(DEVICE_ID, args.deviceId);
2554 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2555 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2556 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2557 ASSERT_EQ(0, args.keyCode);
2558 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2559 ASSERT_EQ(AMETA_NONE, args.metaState);
2560 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2561 ASSERT_EQ(0U, args.policyFlags);
2562 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2563}
2564
2565TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002566 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2567 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002568 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2569 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2570 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002572 KeyboardInputMapper& mapper =
2573 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2574 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575
arthurhungc903df12020-08-11 15:08:42 +08002576 // Initial metastate to AMETA_NONE.
2577 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2578 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579
2580 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002581 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582 NotifyKeyArgs args;
2583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2584 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002585 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002586 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587
2588 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002589 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2591 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002592 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593
2594 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002595 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002598 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599
2600 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002601 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2603 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002604 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002605 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606}
2607
2608TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002609 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2611 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2612 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002614 KeyboardInputMapper& mapper =
2615 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2616 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002618 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2620 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2622 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2624 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2626 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2627}
2628
2629TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2631 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2632 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002636 KeyboardInputMapper& mapper =
2637 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2638 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002640 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002641 ASSERT_NO_FATAL_FAILURE(
2642 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2644 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2646 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2648 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002650 clearViewports();
2651 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002652 ASSERT_NO_FATAL_FAILURE(
2653 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2654 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2655 AKEYCODE_DPAD_UP, DISPLAY_ID));
2656 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2657 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2659 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002661 clearViewports();
2662 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002663 ASSERT_NO_FATAL_FAILURE(
2664 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2665 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2666 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2667 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2668 AKEYCODE_DPAD_UP, DISPLAY_ID));
2669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2670 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002672 clearViewports();
2673 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002674 ASSERT_NO_FATAL_FAILURE(
2675 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2676 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2677 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2678 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2679 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2680 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2681 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682
2683 // Special case: if orientation changes while key is down, we still emit the same keycode
2684 // in the key up as we did in the key down.
2685 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002686 clearViewports();
2687 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002688 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2690 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2691 ASSERT_EQ(KEY_UP, args.scanCode);
2692 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2693
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002694 clearViewports();
2695 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002696 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2698 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2699 ASSERT_EQ(KEY_UP, args.scanCode);
2700 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2701}
2702
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002703TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2704 // If the keyboard is not orientation aware,
2705 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002706 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002708 KeyboardInputMapper& mapper =
2709 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2710 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002711 NotifyKeyArgs args;
2712
2713 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002714 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002716 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2718 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2719
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002720 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002721 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002723 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2725 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2726}
2727
2728TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2729 // If the keyboard is orientation aware,
2730 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002731 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002732
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002733 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002734 KeyboardInputMapper& mapper =
2735 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2736 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002737 NotifyKeyArgs args;
2738
2739 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2740 // ^--- already checked by the previous test
2741
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002742 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002743 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
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(DISPLAY_ID, args.displayId);
2749
2750 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002751 clearViewports();
2752 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002753 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002754 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002756 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2758 ASSERT_EQ(newDisplayId, args.displayId);
2759}
2760
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002762 KeyboardInputMapper& mapper =
2763 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2764 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002766 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002769 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002770 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771}
2772
2773TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002774 KeyboardInputMapper& mapper =
2775 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2776 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002778 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002779 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002781 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002783}
2784
2785TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002786 KeyboardInputMapper& mapper =
2787 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2788 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002790 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791
2792 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2793 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_TRUE(flags[0]);
2796 ASSERT_FALSE(flags[1]);
2797}
2798
2799TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002800 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2801 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2802 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2803 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2804 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2805 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002807 KeyboardInputMapper& mapper =
2808 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2809 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07002810 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08002811 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2812 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813
2814 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002815 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2817 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
2819 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002820 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2821 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002822 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2823 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2824 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002825 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826
2827 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002828 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2829 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002830 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2831 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2832 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002833 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834
2835 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002836 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2837 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002838 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2839 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2840 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002841 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842
2843 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002844 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2845 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002846 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2847 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2848 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002849 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850
2851 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002852 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2853 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002854 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2856 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002857 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858
2859 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002860 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2861 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002862 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2863 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2864 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002865 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866}
2867
Chris Yea52ade12020-08-27 16:49:20 -07002868TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
2869 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
2870 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
2871 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
2872 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
2873
2874 KeyboardInputMapper& mapper =
2875 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2876 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
2877
2878 // Initial metastate should be AMETA_NONE as no meta keys added.
2879 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2880 // Meta state should be AMETA_NONE after reset
2881 mapper.reset(ARBITRARY_TIME);
2882 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2883 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
2884 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
2885 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2886
2887 NotifyKeyArgs args;
2888 // Press button "A"
2889 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
2890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2891 ASSERT_EQ(AMETA_NONE, args.metaState);
2892 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2893 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2894 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2895
2896 // Button up.
2897 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
2898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2899 ASSERT_EQ(AMETA_NONE, args.metaState);
2900 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2901 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2902 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2903}
2904
Arthur Hung2c9a3342019-07-23 14:18:59 +08002905TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2906 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002907 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2908 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2909 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2910 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002911
2912 // keyboard 2.
2913 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08002914 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002915 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002916 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08002917 std::shared_ptr<InputDevice> device2 =
2918 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
2919 Flags<InputDeviceClass>(0));
2920
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002921 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2922 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2923 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2924 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002926 KeyboardInputMapper& mapper =
2927 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2928 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002929
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002930 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002931 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002932 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002933 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2934 device2->reset(ARBITRARY_TIME);
2935
2936 // Prepared displays and associated info.
2937 constexpr uint8_t hdmi1 = 0;
2938 constexpr uint8_t hdmi2 = 1;
2939 const std::string SECONDARY_UNIQUE_ID = "local:1";
2940
2941 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2942 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2943
2944 // No associated display viewport found, should disable the device.
2945 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2946 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2947 ASSERT_FALSE(device2->isEnabled());
2948
2949 // Prepare second display.
2950 constexpr int32_t newDisplayId = 2;
2951 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002952 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002953 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002954 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002955 // Default device will reconfigure above, need additional reconfiguration for another device.
2956 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2957 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2958
2959 // Device should be enabled after the associated display is found.
2960 ASSERT_TRUE(mDevice->isEnabled());
2961 ASSERT_TRUE(device2->isEnabled());
2962
2963 // Test pad key events
2964 ASSERT_NO_FATAL_FAILURE(
2965 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2966 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2967 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2968 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2969 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2970 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2971 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2972
2973 ASSERT_NO_FATAL_FAILURE(
2974 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2975 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2976 AKEYCODE_DPAD_RIGHT, newDisplayId));
2977 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2978 AKEYCODE_DPAD_DOWN, newDisplayId));
2979 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2980 AKEYCODE_DPAD_LEFT, newDisplayId));
2981}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982
arthurhungc903df12020-08-11 15:08:42 +08002983TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
2984 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2985 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2986 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2987 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2988 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2989 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2990
2991 KeyboardInputMapper& mapper =
2992 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2993 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2994 // Initial metastate to AMETA_NONE.
2995 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2996 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
2997
2998 // Initialization should have turned all of the lights off.
2999 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3000 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3001 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3002
3003 // Toggle caps lock on.
3004 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3005 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3006 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3007 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3008
3009 // Toggle num lock on.
3010 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3011 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3012 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3013 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3014
3015 // Toggle scroll lock on.
3016 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3017 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3018 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3019 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3020
3021 mFakeEventHub->removeDevice(EVENTHUB_ID);
3022 mReader->loopOnce();
3023
3024 // keyboard 2 should default toggle keys.
3025 const std::string USB2 = "USB2";
3026 const std::string DEVICE_NAME2 = "KEYBOARD2";
3027 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3028 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3029 std::shared_ptr<InputDevice> device2 =
3030 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3031 Flags<InputDeviceClass>(0));
3032 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3033 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3034 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3035 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3036 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3037 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3038
3039 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3040 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3041 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3042 device2->reset(ARBITRARY_TIME);
3043
3044 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3045 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3046 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
3047 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3048}
3049
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003050// --- KeyboardInputMapperTest_ExternalDevice ---
3051
3052class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3053protected:
Chris Yea52ade12020-08-27 16:49:20 -07003054 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003055};
3056
3057TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003058 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3059 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003060
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003061 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3062 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3063 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3064 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003066 KeyboardInputMapper& mapper =
3067 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3068 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003069
3070 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3071 NotifyKeyArgs args;
3072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3073 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3074
3075 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3077 ASSERT_EQ(uint32_t(0), args.policyFlags);
3078
3079 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3081 ASSERT_EQ(uint32_t(0), args.policyFlags);
3082
3083 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3085 ASSERT_EQ(uint32_t(0), args.policyFlags);
3086
3087 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3089 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3090
3091 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3093 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3094}
3095
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003096TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003097 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003098
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003099 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3100 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3101 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003102
Powei Fengd041c5d2019-05-03 17:11:33 -07003103 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003104 KeyboardInputMapper& mapper =
3105 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3106 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003107
3108 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3109 NotifyKeyArgs args;
3110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3111 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3112
3113 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3115 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3116
3117 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3119 ASSERT_EQ(uint32_t(0), args.policyFlags);
3120
3121 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3123 ASSERT_EQ(uint32_t(0), args.policyFlags);
3124
3125 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3127 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3128
3129 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3131 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3132}
3133
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134// --- CursorInputMapperTest ---
3135
3136class CursorInputMapperTest : public InputMapperTest {
3137protected:
3138 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3139
Michael Wright17db18e2020-06-26 20:51:44 +01003140 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141
Chris Yea52ade12020-08-27 16:49:20 -07003142 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 InputMapperTest::SetUp();
3144
Michael Wright17db18e2020-06-26 20:51:44 +01003145 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003146 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147 }
3148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003149 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3150 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003151
3152 void prepareDisplay(int32_t orientation) {
3153 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003154 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003155 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3156 orientation, uniqueId, NO_PORT, viewportType);
3157 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158};
3159
3160const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003162void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3163 int32_t originalY, int32_t rotatedX,
3164 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165 NotifyMotionArgs args;
3166
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003167 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3168 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3169 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3173 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3174 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3175 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3176}
3177
3178TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003180 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003182 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183}
3184
3185TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003186 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003187 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003189 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190}
3191
3192TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003194 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195
3196 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003197 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198
3199 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003200 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3201 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003202 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3203 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3204
3205 // When the bounds are set, then there should be a valid motion range.
3206 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3207
3208 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003209 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
3211 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3212 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3213 1, 800 - 1, 0.0f, 0.0f));
3214 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3215 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3216 2, 480 - 1, 0.0f, 0.0f));
3217 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3218 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3219 0.0f, 1.0f, 0.0f, 0.0f));
3220}
3221
3222TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003224 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225
3226 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003227 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228
3229 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3230 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3231 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3232 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3233 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3234 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3235 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3236 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3237 0.0f, 1.0f, 0.0f, 0.0f));
3238}
3239
3240TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003242 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243
arthurhungdcef2dc2020-08-11 14:47:50 +08003244 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245
3246 NotifyMotionArgs args;
3247
3248 // Button press.
3249 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003250 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3251 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3253 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3254 ASSERT_EQ(DEVICE_ID, args.deviceId);
3255 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3256 ASSERT_EQ(uint32_t(0), args.policyFlags);
3257 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3258 ASSERT_EQ(0, args.flags);
3259 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3260 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3261 ASSERT_EQ(0, args.edgeFlags);
3262 ASSERT_EQ(uint32_t(1), args.pointerCount);
3263 ASSERT_EQ(0, args.pointerProperties[0].id);
3264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3266 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3267 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3268 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3269 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3270
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3272 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3273 ASSERT_EQ(DEVICE_ID, args.deviceId);
3274 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3275 ASSERT_EQ(uint32_t(0), args.policyFlags);
3276 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3277 ASSERT_EQ(0, args.flags);
3278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3279 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3280 ASSERT_EQ(0, args.edgeFlags);
3281 ASSERT_EQ(uint32_t(1), args.pointerCount);
3282 ASSERT_EQ(0, args.pointerProperties[0].id);
3283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3285 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3286 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3287 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3288 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3289
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003291 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3292 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3294 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3295 ASSERT_EQ(DEVICE_ID, args.deviceId);
3296 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3297 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3299 ASSERT_EQ(0, args.flags);
3300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3301 ASSERT_EQ(0, args.buttonState);
3302 ASSERT_EQ(0, args.edgeFlags);
3303 ASSERT_EQ(uint32_t(1), args.pointerCount);
3304 ASSERT_EQ(0, args.pointerProperties[0].id);
3305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3307 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3308 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3309 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3310 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3311
3312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3313 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3314 ASSERT_EQ(DEVICE_ID, args.deviceId);
3315 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3316 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3318 ASSERT_EQ(0, args.flags);
3319 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3320 ASSERT_EQ(0, args.buttonState);
3321 ASSERT_EQ(0, args.edgeFlags);
3322 ASSERT_EQ(uint32_t(1), args.pointerCount);
3323 ASSERT_EQ(0, args.pointerProperties[0].id);
3324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3326 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3327 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3328 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3329 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3330}
3331
3332TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003334 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335
3336 NotifyMotionArgs args;
3337
3338 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003339 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3340 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3344 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3345
3346 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003347 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3348 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3352 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3353}
3354
3355TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
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 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003362 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 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_DOWN, args.action);
3366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3367 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3368
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3372 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3373
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003375 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3376 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003378 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3380 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3381
3382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3385 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3386}
3387
3388TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003390 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391
3392 NotifyMotionArgs args;
3393
3394 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003395 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3396 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3397 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3398 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3400 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3402 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3403 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3404
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3408 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3409 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3410
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003412 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3413 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3414 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3418 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3419 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3420
3421 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003422 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3423 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003425 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3427 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3428
3429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3432 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3433}
3434
3435TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003437 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003439 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3442 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3443 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3444 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3445 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3448}
3449
3450TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 addConfigurationProperty("cursor.mode", "navigation");
3452 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003453 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003455 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3457 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3458 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3459 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3460 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3461 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3462 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3463 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3464
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003465 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3468 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3469 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3470 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3471 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3472 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3473 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3474
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003475 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3484
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003485 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3487 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3488 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3489 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3490 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3494}
3495
3496TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003498 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
3500 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3501 mFakePointerController->setPosition(100, 200);
3502 mFakePointerController->setButtonState(0);
3503
3504 NotifyMotionArgs motionArgs;
3505 NotifyKeyArgs keyArgs;
3506
3507 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003508 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3509 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3511 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3512 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3513 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3515 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3516
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3518 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3519 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3520 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3522 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3523
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003524 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3525 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003527 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_EQ(0, motionArgs.buttonState);
3529 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3531 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3532
3533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003534 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 ASSERT_EQ(0, motionArgs.buttonState);
3536 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3538 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3539
3540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003542 ASSERT_EQ(0, motionArgs.buttonState);
3543 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3545 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3546
3547 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003548 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3549 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3550 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3552 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3553 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3554 motionArgs.buttonState);
3555 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3556 mFakePointerController->getButtonState());
3557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3558 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3559
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3561 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3562 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3563 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3564 mFakePointerController->getButtonState());
3565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3566 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3567
3568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3570 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3571 motionArgs.buttonState);
3572 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3573 mFakePointerController->getButtonState());
3574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3575 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3576
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003577 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3578 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3582 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3584 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3585
3586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003588 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3589 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3591 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3592
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003593 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3594 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3597 ASSERT_EQ(0, motionArgs.buttonState);
3598 ASSERT_EQ(0, mFakePointerController->getButtonState());
3599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3600 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 -08003601 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3602 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003603
3604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 ASSERT_EQ(0, motionArgs.buttonState);
3606 ASSERT_EQ(0, mFakePointerController->getButtonState());
3607 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3609 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 -08003610
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3612 ASSERT_EQ(0, motionArgs.buttonState);
3613 ASSERT_EQ(0, mFakePointerController->getButtonState());
3614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3616 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3617
3618 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003619 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3620 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3622 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3623 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003624
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3628 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3630 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3631
3632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3634 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3635 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3637 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3638
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003639 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3640 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003642 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643 ASSERT_EQ(0, motionArgs.buttonState);
3644 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3646 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3647
3648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003650 ASSERT_EQ(0, motionArgs.buttonState);
3651 ASSERT_EQ(0, mFakePointerController->getButtonState());
3652
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3654 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3656 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3657 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3658
3659 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003660 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3661 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3663 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3664 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003665
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3669 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3671 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3672
3673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3675 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3676 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3678 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3679
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003680 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3681 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003683 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684 ASSERT_EQ(0, motionArgs.buttonState);
3685 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3687 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 -08003688
3689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3690 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3691 ASSERT_EQ(0, motionArgs.buttonState);
3692 ASSERT_EQ(0, mFakePointerController->getButtonState());
3693 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3697 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3698 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3699
3700 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003701 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3702 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3704 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3705 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003706
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3710 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3712 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3713
3714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3716 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3717 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3719 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3720
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003721 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3722 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003724 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725 ASSERT_EQ(0, motionArgs.buttonState);
3726 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3728 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 -08003729
3730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3732 ASSERT_EQ(0, motionArgs.buttonState);
3733 ASSERT_EQ(0, mFakePointerController->getButtonState());
3734 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3738 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3739 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3740
3741 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003742 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3743 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3745 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3746 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003747
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003749 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3751 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3753 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3754
3755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3757 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3758 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3760 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3761
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003762 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3763 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003765 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766 ASSERT_EQ(0, motionArgs.buttonState);
3767 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3769 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 -08003770
3771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3773 ASSERT_EQ(0, motionArgs.buttonState);
3774 ASSERT_EQ(0, mFakePointerController->getButtonState());
3775 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3779 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3780 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3781}
3782
3783TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
3787 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3788 mFakePointerController->setPosition(100, 200);
3789 mFakePointerController->setButtonState(0);
3790
3791 NotifyMotionArgs args;
3792
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003793 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3794 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3795 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003797 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3798 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3800 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 +01003801 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003802}
3803
3804TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003805 addConfigurationProperty("cursor.mode", "pointer");
3806 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003808
3809 NotifyDeviceResetArgs resetArgs;
3810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3811 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3812 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3813
3814 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3815 mFakePointerController->setPosition(100, 200);
3816 mFakePointerController->setButtonState(0);
3817
3818 NotifyMotionArgs args;
3819
3820 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003821 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3822 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3823 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3825 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3828 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 +01003829 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003830
3831 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003832 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3833 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3835 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3836 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3838 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3840 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3843 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3844
3845 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003846 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3847 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3849 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3850 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3852 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3854 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3855 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3857 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3858
3859 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003860 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3861 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3862 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3864 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3867 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 +01003868 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003869
3870 // Disable pointer capture and check that the device generation got bumped
3871 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08003872 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003873 mFakePolicy->setPointerCapture(false);
3874 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08003875 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003876
3877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3878 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3879 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3880
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003881 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3882 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3883 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3885 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3888 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 +01003889 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890}
3891
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003892TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003893 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003894
Garfield Tan888a6a42020-01-09 11:39:16 -08003895 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003896 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003897 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3898 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003899 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003900 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3901 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3902
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003903 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3904 mFakePointerController->setPosition(100, 200);
3905 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003906
3907 NotifyMotionArgs args;
3908 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3909 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3910 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3912 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3913 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3915 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 +01003916 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003917 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3918}
3919
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920// --- TouchInputMapperTest ---
3921
3922class TouchInputMapperTest : public InputMapperTest {
3923protected:
3924 static const int32_t RAW_X_MIN;
3925 static const int32_t RAW_X_MAX;
3926 static const int32_t RAW_Y_MIN;
3927 static const int32_t RAW_Y_MAX;
3928 static const int32_t RAW_TOUCH_MIN;
3929 static const int32_t RAW_TOUCH_MAX;
3930 static const int32_t RAW_TOOL_MIN;
3931 static const int32_t RAW_TOOL_MAX;
3932 static const int32_t RAW_PRESSURE_MIN;
3933 static const int32_t RAW_PRESSURE_MAX;
3934 static const int32_t RAW_ORIENTATION_MIN;
3935 static const int32_t RAW_ORIENTATION_MAX;
3936 static const int32_t RAW_DISTANCE_MIN;
3937 static const int32_t RAW_DISTANCE_MAX;
3938 static const int32_t RAW_TILT_MIN;
3939 static const int32_t RAW_TILT_MAX;
3940 static const int32_t RAW_ID_MIN;
3941 static const int32_t RAW_ID_MAX;
3942 static const int32_t RAW_SLOT_MIN;
3943 static const int32_t RAW_SLOT_MAX;
3944 static const float X_PRECISION;
3945 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07003946 static const float X_PRECISION_VIRTUAL;
3947 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948
3949 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07003950 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951
3952 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3953
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003954 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003955 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003956
Michael Wrightd02c5b62014-02-10 15:10:22 -08003957 enum Axes {
3958 POSITION = 1 << 0,
3959 TOUCH = 1 << 1,
3960 TOOL = 1 << 2,
3961 PRESSURE = 1 << 3,
3962 ORIENTATION = 1 << 4,
3963 MINOR = 1 << 5,
3964 ID = 1 << 6,
3965 DISTANCE = 1 << 7,
3966 TILT = 1 << 8,
3967 SLOT = 1 << 9,
3968 TOOL_TYPE = 1 << 10,
3969 };
3970
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003971 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3972 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003973 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07003975 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976 int32_t toRawX(float displayX);
3977 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07003978 float toCookedX(float rawX, float rawY);
3979 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003981 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003983 float toDisplayY(int32_t rawY, int32_t displayHeight);
3984
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985};
3986
3987const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3988const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3989const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3990const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3991const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3992const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3993const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3994const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00003995const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3996const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3998const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3999const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4000const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4001const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4002const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4003const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4004const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4005const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4006const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4007const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4008const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004009const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4010 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4011const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4012 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004013const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4014 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015
4016const float TouchInputMapperTest::GEOMETRIC_SCALE =
4017 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4018 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4019
4020const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4021 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4022 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4023};
4024
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004025void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004026 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4027 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004028}
4029
4030void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4031 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4032 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033}
4034
Santos Cordonfa5cf462017-04-05 10:37:00 -07004035void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004036 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4037 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4038 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004039}
4040
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004042 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4043 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4044 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4045 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046}
4047
Jason Gerecke489fda82012-09-07 17:19:40 -07004048void TouchInputMapperTest::prepareLocationCalibration() {
4049 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4050}
4051
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052int32_t TouchInputMapperTest::toRawX(float displayX) {
4053 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4054}
4055
4056int32_t TouchInputMapperTest::toRawY(float displayY) {
4057 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4058}
4059
Jason Gerecke489fda82012-09-07 17:19:40 -07004060float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4061 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4062 return rawX;
4063}
4064
4065float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4066 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4067 return rawY;
4068}
4069
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004071 return toDisplayX(rawX, DISPLAY_WIDTH);
4072}
4073
4074float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4075 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076}
4077
4078float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004079 return toDisplayY(rawY, DISPLAY_HEIGHT);
4080}
4081
4082float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4083 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084}
4085
4086
4087// --- SingleTouchInputMapperTest ---
4088
4089class SingleTouchInputMapperTest : public TouchInputMapperTest {
4090protected:
4091 void prepareButtons();
4092 void prepareAxes(int axes);
4093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004094 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4095 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4096 void processUp(SingleTouchInputMapper& mappery);
4097 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4098 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4099 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4100 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4101 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4102 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103};
4104
4105void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004106 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107}
4108
4109void SingleTouchInputMapperTest::prepareAxes(int axes) {
4110 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 }
4114 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004115 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4116 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117 }
4118 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004119 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4120 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121 }
4122 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004123 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4124 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 }
4126 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004127 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4128 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129 }
4130}
4131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004132void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004133 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4134 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4135 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136}
4137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004138void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004139 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4140 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141}
4142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004143void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004144 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145}
4146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004148 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149}
4150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4152 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004153 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154}
4155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004156void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004157 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158}
4159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004160void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4161 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004162 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4163 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164}
4165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004166void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4167 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004168 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169}
4170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004171void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004172 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173}
4174
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176 prepareButtons();
4177 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004178 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004180 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181}
4182
4183TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004184 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4185 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186 prepareButtons();
4187 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004188 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004190 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191}
4192
4193TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 prepareButtons();
4195 prepareAxes(POSITION);
4196 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004197 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004199 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200}
4201
4202TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 prepareButtons();
4204 prepareAxes(POSITION);
4205 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004206 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004208 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209}
4210
4211TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 addConfigurationProperty("touch.deviceType", "touchScreen");
4213 prepareDisplay(DISPLAY_ORIENTATION_0);
4214 prepareButtons();
4215 prepareAxes(POSITION);
4216 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004217 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218
4219 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004220 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
4222 // Virtual key is down.
4223 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4224 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4225 processDown(mapper, x, y);
4226 processSync(mapper);
4227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004229 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230
4231 // Virtual key is up.
4232 processUp(mapper);
4233 processSync(mapper);
4234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004236 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237}
4238
4239TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 addConfigurationProperty("touch.deviceType", "touchScreen");
4241 prepareDisplay(DISPLAY_ORIENTATION_0);
4242 prepareButtons();
4243 prepareAxes(POSITION);
4244 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004245 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246
4247 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004248 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249
4250 // Virtual key is down.
4251 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4252 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4253 processDown(mapper, x, y);
4254 processSync(mapper);
4255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004257 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258
4259 // Virtual key is up.
4260 processUp(mapper);
4261 processSync(mapper);
4262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4263
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004264 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265}
4266
4267TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 addConfigurationProperty("touch.deviceType", "touchScreen");
4269 prepareDisplay(DISPLAY_ORIENTATION_0);
4270 prepareButtons();
4271 prepareAxes(POSITION);
4272 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004273 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
4275 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4276 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004277 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_TRUE(flags[0]);
4279 ASSERT_FALSE(flags[1]);
4280}
4281
4282TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 addConfigurationProperty("touch.deviceType", "touchScreen");
4284 prepareDisplay(DISPLAY_ORIENTATION_0);
4285 prepareButtons();
4286 prepareAxes(POSITION);
4287 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004288 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289
arthurhungdcef2dc2020-08-11 14:47:50 +08004290 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291
4292 NotifyKeyArgs args;
4293
4294 // Press virtual key.
4295 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4296 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4297 processDown(mapper, x, y);
4298 processSync(mapper);
4299
4300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4301 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4302 ASSERT_EQ(DEVICE_ID, args.deviceId);
4303 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4304 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4305 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4306 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4307 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4308 ASSERT_EQ(KEY_HOME, args.scanCode);
4309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4310 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4311
4312 // Release virtual key.
4313 processUp(mapper);
4314 processSync(mapper);
4315
4316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4317 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4318 ASSERT_EQ(DEVICE_ID, args.deviceId);
4319 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4320 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4321 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4322 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4323 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4324 ASSERT_EQ(KEY_HOME, args.scanCode);
4325 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4326 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4327
4328 // Should not have sent any motions.
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4330}
4331
4332TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 addConfigurationProperty("touch.deviceType", "touchScreen");
4334 prepareDisplay(DISPLAY_ORIENTATION_0);
4335 prepareButtons();
4336 prepareAxes(POSITION);
4337 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004338 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339
arthurhungdcef2dc2020-08-11 14:47:50 +08004340 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341
4342 NotifyKeyArgs keyArgs;
4343
4344 // Press virtual key.
4345 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4346 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4347 processDown(mapper, x, y);
4348 processSync(mapper);
4349
4350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4351 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4352 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4353 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4354 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4355 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4356 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4357 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4358 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4359 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4360 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4361
4362 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4363 // into the display area.
4364 y -= 100;
4365 processMove(mapper, x, y);
4366 processSync(mapper);
4367
4368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4369 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4370 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4371 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4372 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4373 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4374 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4375 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4376 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4377 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4378 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4379 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4380
4381 NotifyMotionArgs motionArgs;
4382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4383 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4384 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4385 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4386 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4388 ASSERT_EQ(0, motionArgs.flags);
4389 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4390 ASSERT_EQ(0, motionArgs.buttonState);
4391 ASSERT_EQ(0, motionArgs.edgeFlags);
4392 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4393 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4396 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4397 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4398 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4400
4401 // Keep moving out of bounds. Should generate a pointer move.
4402 y -= 50;
4403 processMove(mapper, x, y);
4404 processSync(mapper);
4405
4406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4408 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4409 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4410 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4412 ASSERT_EQ(0, motionArgs.flags);
4413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4414 ASSERT_EQ(0, motionArgs.buttonState);
4415 ASSERT_EQ(0, motionArgs.edgeFlags);
4416 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4420 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4421 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4422 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4424
4425 // Release out of bounds. Should generate a pointer up.
4426 processUp(mapper);
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_UP, 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 // Should not have sent any more keys or motions.
4449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4451}
4452
4453TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 addConfigurationProperty("touch.deviceType", "touchScreen");
4455 prepareDisplay(DISPLAY_ORIENTATION_0);
4456 prepareButtons();
4457 prepareAxes(POSITION);
4458 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004459 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460
arthurhungdcef2dc2020-08-11 14:47:50 +08004461 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462
4463 NotifyMotionArgs motionArgs;
4464
4465 // Initially go down out of bounds.
4466 int32_t x = -10;
4467 int32_t y = -10;
4468 processDown(mapper, x, y);
4469 processSync(mapper);
4470
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4472
4473 // Move into the display area. Should generate a pointer down.
4474 x = 50;
4475 y = 75;
4476 processMove(mapper, x, y);
4477 processSync(mapper);
4478
4479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4480 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4481 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4482 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4483 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4485 ASSERT_EQ(0, motionArgs.flags);
4486 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4487 ASSERT_EQ(0, motionArgs.buttonState);
4488 ASSERT_EQ(0, motionArgs.edgeFlags);
4489 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4490 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4493 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4494 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4495 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4496 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4497
4498 // Release. Should generate a pointer up.
4499 processUp(mapper);
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_UP, 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 // Should not have sent any more keys or motions.
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4524}
4525
Santos Cordonfa5cf462017-04-05 10:37:00 -07004526TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004527 addConfigurationProperty("touch.deviceType", "touchScreen");
4528 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4529
4530 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4531 prepareButtons();
4532 prepareAxes(POSITION);
4533 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004534 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004535
arthurhungdcef2dc2020-08-11 14:47:50 +08004536 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004537
4538 NotifyMotionArgs motionArgs;
4539
4540 // Down.
4541 int32_t x = 100;
4542 int32_t y = 125;
4543 processDown(mapper, x, y);
4544 processSync(mapper);
4545
4546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4547 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4548 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4549 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4550 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4551 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4552 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4553 ASSERT_EQ(0, motionArgs.flags);
4554 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4555 ASSERT_EQ(0, motionArgs.buttonState);
4556 ASSERT_EQ(0, motionArgs.edgeFlags);
4557 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4558 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4561 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4562 1, 0, 0, 0, 0, 0, 0, 0));
4563 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4564 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4566
4567 // Move.
4568 x += 50;
4569 y += 75;
4570 processMove(mapper, x, y);
4571 processSync(mapper);
4572
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4574 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4575 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4576 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4577 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4578 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4580 ASSERT_EQ(0, motionArgs.flags);
4581 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4582 ASSERT_EQ(0, motionArgs.buttonState);
4583 ASSERT_EQ(0, motionArgs.edgeFlags);
4584 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4585 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4588 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4589 1, 0, 0, 0, 0, 0, 0, 0));
4590 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4591 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4592 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4593
4594 // Up.
4595 processUp(mapper);
4596 processSync(mapper);
4597
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4600 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4601 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4602 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4603 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4605 ASSERT_EQ(0, motionArgs.flags);
4606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4607 ASSERT_EQ(0, motionArgs.buttonState);
4608 ASSERT_EQ(0, motionArgs.edgeFlags);
4609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4610 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4613 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4614 1, 0, 0, 0, 0, 0, 0, 0));
4615 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4616 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4618
4619 // Should not have sent any more keys or motions.
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4622}
4623
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 addConfigurationProperty("touch.deviceType", "touchScreen");
4626 prepareDisplay(DISPLAY_ORIENTATION_0);
4627 prepareButtons();
4628 prepareAxes(POSITION);
4629 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004630 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631
arthurhungdcef2dc2020-08-11 14:47:50 +08004632 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633
4634 NotifyMotionArgs motionArgs;
4635
4636 // Down.
4637 int32_t x = 100;
4638 int32_t y = 125;
4639 processDown(mapper, x, y);
4640 processSync(mapper);
4641
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4643 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4644 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4645 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4646 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4648 ASSERT_EQ(0, motionArgs.flags);
4649 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4650 ASSERT_EQ(0, motionArgs.buttonState);
4651 ASSERT_EQ(0, motionArgs.edgeFlags);
4652 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4653 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4656 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4657 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4658 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4659 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4660
4661 // Move.
4662 x += 50;
4663 y += 75;
4664 processMove(mapper, x, y);
4665 processSync(mapper);
4666
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4668 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4669 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4670 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4671 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4673 ASSERT_EQ(0, motionArgs.flags);
4674 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4675 ASSERT_EQ(0, motionArgs.buttonState);
4676 ASSERT_EQ(0, motionArgs.edgeFlags);
4677 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4678 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4681 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4682 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4683 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4684 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4685
4686 // Up.
4687 processUp(mapper);
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_UP, 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 // Should not have sent any more keys or motions.
4710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4712}
4713
4714TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 addConfigurationProperty("touch.deviceType", "touchScreen");
4716 prepareButtons();
4717 prepareAxes(POSITION);
4718 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004719 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720
4721 NotifyMotionArgs args;
4722
4723 // Rotation 90.
4724 prepareDisplay(DISPLAY_ORIENTATION_90);
4725 processDown(mapper, toRawX(50), toRawY(75));
4726 processSync(mapper);
4727
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4729 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4730 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4731
4732 processUp(mapper);
4733 processSync(mapper);
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4735}
4736
4737TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 addConfigurationProperty("touch.deviceType", "touchScreen");
4739 prepareButtons();
4740 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004741 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742
4743 NotifyMotionArgs args;
4744
4745 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004746 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 prepareDisplay(DISPLAY_ORIENTATION_0);
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 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004760 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761 prepareDisplay(DISPLAY_ORIENTATION_90);
4762 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4763 processSync(mapper);
4764
4765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4766 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4767 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4768
4769 processUp(mapper);
4770 processSync(mapper);
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4772
4773 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004774 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775 prepareDisplay(DISPLAY_ORIENTATION_180);
4776 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4777 processSync(mapper);
4778
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4780 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4781 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4782
4783 processUp(mapper);
4784 processSync(mapper);
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4786
4787 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004788 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 prepareDisplay(DISPLAY_ORIENTATION_270);
4790 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4791 processSync(mapper);
4792
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4794 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4795 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4796
4797 processUp(mapper);
4798 processSync(mapper);
4799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4800}
4801
4802TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 addConfigurationProperty("touch.deviceType", "touchScreen");
4804 prepareDisplay(DISPLAY_ORIENTATION_0);
4805 prepareButtons();
4806 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004807 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808
4809 // These calculations are based on the input device calibration documentation.
4810 int32_t rawX = 100;
4811 int32_t rawY = 200;
4812 int32_t rawPressure = 10;
4813 int32_t rawToolMajor = 12;
4814 int32_t rawDistance = 2;
4815 int32_t rawTiltX = 30;
4816 int32_t rawTiltY = 110;
4817
4818 float x = toDisplayX(rawX);
4819 float y = toDisplayY(rawY);
4820 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4821 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4822 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4823 float distance = float(rawDistance);
4824
4825 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4826 float tiltScale = M_PI / 180;
4827 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4828 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4829 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4830 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4831
4832 processDown(mapper, rawX, rawY);
4833 processPressure(mapper, rawPressure);
4834 processToolMajor(mapper, rawToolMajor);
4835 processDistance(mapper, rawDistance);
4836 processTilt(mapper, rawTiltX, rawTiltY);
4837 processSync(mapper);
4838
4839 NotifyMotionArgs args;
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4842 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4843 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4844}
4845
Jason Gerecke489fda82012-09-07 17:19:40 -07004846TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004847 addConfigurationProperty("touch.deviceType", "touchScreen");
4848 prepareDisplay(DISPLAY_ORIENTATION_0);
4849 prepareLocationCalibration();
4850 prepareButtons();
4851 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004852 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004853
4854 int32_t rawX = 100;
4855 int32_t rawY = 200;
4856
4857 float x = toDisplayX(toCookedX(rawX, rawY));
4858 float y = toDisplayY(toCookedY(rawX, rawY));
4859
4860 processDown(mapper, rawX, rawY);
4861 processSync(mapper);
4862
4863 NotifyMotionArgs args;
4864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4866 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4867}
4868
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 addConfigurationProperty("touch.deviceType", "touchScreen");
4871 prepareDisplay(DISPLAY_ORIENTATION_0);
4872 prepareButtons();
4873 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004874 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875
4876 NotifyMotionArgs motionArgs;
4877 NotifyKeyArgs keyArgs;
4878
4879 processDown(mapper, 100, 200);
4880 processSync(mapper);
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4882 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4883 ASSERT_EQ(0, motionArgs.buttonState);
4884
4885 // press BTN_LEFT, release BTN_LEFT
4886 processKey(mapper, BTN_LEFT, 1);
4887 processSync(mapper);
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4890 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4891
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4894 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4895
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 processKey(mapper, BTN_LEFT, 0);
4897 processSync(mapper);
4898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004899 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004901
4902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004904 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905
4906 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4907 processKey(mapper, BTN_RIGHT, 1);
4908 processKey(mapper, BTN_MIDDLE, 1);
4909 processSync(mapper);
4910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4912 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4913 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_TERTIARY, motionArgs.buttonState);
4918
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4921 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4922 motionArgs.buttonState);
4923
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 processKey(mapper, BTN_RIGHT, 0);
4925 processSync(mapper);
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004927 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004929
4930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004932 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933
4934 processKey(mapper, BTN_MIDDLE, 0);
4935 processSync(mapper);
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004937 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004942 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
4944 // press BTN_BACK, release BTN_BACK
4945 processKey(mapper, BTN_BACK, 1);
4946 processSync(mapper);
4947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4948 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4949 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004950
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004953 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4954
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4956 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4957 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958
4959 processKey(mapper, BTN_BACK, 0);
4960 processSync(mapper);
4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004964
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004967 ASSERT_EQ(0, motionArgs.buttonState);
4968
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4970 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4971 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4972
4973 // press BTN_SIDE, release BTN_SIDE
4974 processKey(mapper, BTN_SIDE, 1);
4975 processSync(mapper);
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4977 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4978 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004979
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004982 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4983
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4985 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4986 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987
4988 processKey(mapper, BTN_SIDE, 0);
4989 processSync(mapper);
4990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004993
4994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004996 ASSERT_EQ(0, motionArgs.buttonState);
4997
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4999 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5000 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5001
5002 // press BTN_FORWARD, release BTN_FORWARD
5003 processKey(mapper, BTN_FORWARD, 1);
5004 processSync(mapper);
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5006 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5007 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005008
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005011 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5012
5013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5014 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5015 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016
5017 processKey(mapper, BTN_FORWARD, 0);
5018 processSync(mapper);
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005022
5023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005025 ASSERT_EQ(0, motionArgs.buttonState);
5026
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5028 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5029 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5030
5031 // press BTN_EXTRA, release BTN_EXTRA
5032 processKey(mapper, BTN_EXTRA, 1);
5033 processSync(mapper);
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5035 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5036 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005037
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005040 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5041
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5043 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5044 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045
5046 processKey(mapper, BTN_EXTRA, 0);
5047 processSync(mapper);
5048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005051
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005054 ASSERT_EQ(0, motionArgs.buttonState);
5055
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5057 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5058 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5059
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5061
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 // press BTN_STYLUS, release BTN_STYLUS
5063 processKey(mapper, BTN_STYLUS, 1);
5064 processSync(mapper);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005067 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5068
5069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5070 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5071 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072
5073 processKey(mapper, BTN_STYLUS, 0);
5074 processSync(mapper);
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005078
5079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005081 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082
5083 // press BTN_STYLUS2, release BTN_STYLUS2
5084 processKey(mapper, BTN_STYLUS2, 1);
5085 processSync(mapper);
5086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005088 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5089
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5091 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5092 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093
5094 processKey(mapper, BTN_STYLUS2, 0);
5095 processSync(mapper);
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005099
5100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005102 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103
5104 // release touch
5105 processUp(mapper);
5106 processSync(mapper);
5107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5108 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5109 ASSERT_EQ(0, motionArgs.buttonState);
5110}
5111
5112TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 addConfigurationProperty("touch.deviceType", "touchScreen");
5114 prepareDisplay(DISPLAY_ORIENTATION_0);
5115 prepareButtons();
5116 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005117 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118
5119 NotifyMotionArgs motionArgs;
5120
5121 // default tool type is finger
5122 processDown(mapper, 100, 200);
5123 processSync(mapper);
5124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5127
5128 // eraser
5129 processKey(mapper, BTN_TOOL_RUBBER, 1);
5130 processSync(mapper);
5131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5134
5135 // stylus
5136 processKey(mapper, BTN_TOOL_RUBBER, 0);
5137 processKey(mapper, BTN_TOOL_PEN, 1);
5138 processSync(mapper);
5139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5142
5143 // brush
5144 processKey(mapper, BTN_TOOL_PEN, 0);
5145 processKey(mapper, BTN_TOOL_BRUSH, 1);
5146 processSync(mapper);
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5150
5151 // pencil
5152 processKey(mapper, BTN_TOOL_BRUSH, 0);
5153 processKey(mapper, BTN_TOOL_PENCIL, 1);
5154 processSync(mapper);
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5158
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005159 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005160 processKey(mapper, BTN_TOOL_PENCIL, 0);
5161 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5162 processSync(mapper);
5163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5166
5167 // mouse
5168 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5169 processKey(mapper, BTN_TOOL_MOUSE, 1);
5170 processSync(mapper);
5171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5174
5175 // lens
5176 processKey(mapper, BTN_TOOL_MOUSE, 0);
5177 processKey(mapper, BTN_TOOL_LENS, 1);
5178 processSync(mapper);
5179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5182
5183 // double-tap
5184 processKey(mapper, BTN_TOOL_LENS, 0);
5185 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5186 processSync(mapper);
5187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5190
5191 // triple-tap
5192 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5193 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5194 processSync(mapper);
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5198
5199 // quad-tap
5200 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5201 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5202 processSync(mapper);
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5206
5207 // finger
5208 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5209 processKey(mapper, BTN_TOOL_FINGER, 1);
5210 processSync(mapper);
5211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5214
5215 // stylus trumps finger
5216 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
5221
5222 // eraser trumps stylus
5223 processKey(mapper, BTN_TOOL_RUBBER, 1);
5224 processSync(mapper);
5225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5228
5229 // mouse trumps eraser
5230 processKey(mapper, BTN_TOOL_MOUSE, 1);
5231 processSync(mapper);
5232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5235
5236 // back to default tool type
5237 processKey(mapper, BTN_TOOL_MOUSE, 0);
5238 processKey(mapper, BTN_TOOL_RUBBER, 0);
5239 processKey(mapper, BTN_TOOL_PEN, 0);
5240 processKey(mapper, BTN_TOOL_FINGER, 0);
5241 processSync(mapper);
5242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5245}
5246
5247TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248 addConfigurationProperty("touch.deviceType", "touchScreen");
5249 prepareDisplay(DISPLAY_ORIENTATION_0);
5250 prepareButtons();
5251 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005252 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005253 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254
5255 NotifyMotionArgs motionArgs;
5256
5257 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5258 processKey(mapper, BTN_TOOL_FINGER, 1);
5259 processMove(mapper, 100, 200);
5260 processSync(mapper);
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5262 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5264 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5265
5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5269 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5270
5271 // move a little
5272 processMove(mapper, 150, 250);
5273 processSync(mapper);
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5278
5279 // down when BTN_TOUCH is pressed, pressure defaults to 1
5280 processKey(mapper, BTN_TOUCH, 1);
5281 processSync(mapper);
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5285 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5286
5287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5290 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5291
5292 // up when BTN_TOUCH is released, hover restored
5293 processKey(mapper, BTN_TOUCH, 0);
5294 processSync(mapper);
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5298 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5299
5300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5301 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5303 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5304
5305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
5311 processKey(mapper, BTN_TOOL_FINGER, 0);
5312 processSync(mapper);
5313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5316 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5317}
5318
5319TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320 addConfigurationProperty("touch.deviceType", "touchScreen");
5321 prepareDisplay(DISPLAY_ORIENTATION_0);
5322 prepareButtons();
5323 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005324 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325
5326 NotifyMotionArgs motionArgs;
5327
5328 // initially hovering because pressure is 0
5329 processDown(mapper, 100, 200);
5330 processPressure(mapper, 0);
5331 processSync(mapper);
5332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5333 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5335 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5336
5337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5340 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5341
5342 // move a little
5343 processMove(mapper, 150, 250);
5344 processSync(mapper);
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5348 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5349
5350 // down when pressure is non-zero
5351 processPressure(mapper, RAW_PRESSURE_MAX);
5352 processSync(mapper);
5353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5354 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5357
5358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5359 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5361 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5362
5363 // up when pressure becomes 0, hover restored
5364 processPressure(mapper, 0);
5365 processSync(mapper);
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5369 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5370
5371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5372 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5374 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5375
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
5382 processUp(mapper);
5383 processSync(mapper);
5384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5385 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5387 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5388}
5389
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390// --- MultiTouchInputMapperTest ---
5391
5392class MultiTouchInputMapperTest : public TouchInputMapperTest {
5393protected:
5394 void prepareAxes(int axes);
5395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005396 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5397 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5398 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5399 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5400 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5401 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5402 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5403 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5404 void processId(MultiTouchInputMapper& mapper, int32_t id);
5405 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5406 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5407 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5408 void processMTSync(MultiTouchInputMapper& mapper);
5409 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410};
5411
5412void MultiTouchInputMapperTest::prepareAxes(int axes) {
5413 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005414 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5415 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 }
5417 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005418 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5419 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005421 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5422 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 }
5424 }
5425 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005426 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5427 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005429 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5430 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 }
5432 }
5433 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005434 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5435 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436 }
5437 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005438 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5439 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 }
5441 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005442 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5443 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 }
5445 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005446 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5447 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 }
5449 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005450 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5451 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452 }
5453 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005454 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 }
5456}
5457
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005458void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5459 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005460 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5461 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462}
5463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005464void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5465 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005466 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467}
5468
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005469void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5470 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005471 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472}
5473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005474void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005475 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476}
5477
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005478void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005479 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480}
5481
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005482void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5483 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005484 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485}
5486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005487void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005488 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489}
5490
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005491void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005492 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493}
5494
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005495void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005496 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497}
5498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005499void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005500 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501}
5502
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005503void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005504 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505}
5506
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005507void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5508 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005509 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510}
5511
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005512void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005513 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514}
5515
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005516void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005517 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518}
5519
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005521 addConfigurationProperty("touch.deviceType", "touchScreen");
5522 prepareDisplay(DISPLAY_ORIENTATION_0);
5523 prepareAxes(POSITION);
5524 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005525 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526
arthurhungdcef2dc2020-08-11 14:47:50 +08005527 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528
5529 NotifyMotionArgs motionArgs;
5530
5531 // Two fingers down at once.
5532 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5533 processPosition(mapper, x1, y1);
5534 processMTSync(mapper);
5535 processPosition(mapper, x2, y2);
5536 processMTSync(mapper);
5537 processSync(mapper);
5538
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5540 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5541 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5542 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5543 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5545 ASSERT_EQ(0, motionArgs.flags);
5546 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5547 ASSERT_EQ(0, motionArgs.buttonState);
5548 ASSERT_EQ(0, motionArgs.edgeFlags);
5549 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5553 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5554 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5555 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5556 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5560 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5561 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5562 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5563 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5564 motionArgs.action);
5565 ASSERT_EQ(0, motionArgs.flags);
5566 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5567 ASSERT_EQ(0, motionArgs.buttonState);
5568 ASSERT_EQ(0, motionArgs.edgeFlags);
5569 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5572 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5575 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5577 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5578 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5579 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5580 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5581
5582 // Move.
5583 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5584 processPosition(mapper, x1, y1);
5585 processMTSync(mapper);
5586 processPosition(mapper, x2, y2);
5587 processMTSync(mapper);
5588 processSync(mapper);
5589
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5592 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5593 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5594 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5596 ASSERT_EQ(0, motionArgs.flags);
5597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5598 ASSERT_EQ(0, motionArgs.buttonState);
5599 ASSERT_EQ(0, motionArgs.edgeFlags);
5600 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5603 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5606 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5608 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5609 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5610 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5612
5613 // First finger up.
5614 x2 += 15; y2 -= 20;
5615 processPosition(mapper, x2, y2);
5616 processMTSync(mapper);
5617 processSync(mapper);
5618
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5620 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5621 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5622 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5623 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5624 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5625 motionArgs.action);
5626 ASSERT_EQ(0, motionArgs.flags);
5627 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5628 ASSERT_EQ(0, motionArgs.buttonState);
5629 ASSERT_EQ(0, motionArgs.edgeFlags);
5630 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5633 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5636 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5638 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5639 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5640 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5641 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5642
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5645 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5646 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5647 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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(1), motionArgs.pointerCount);
5654 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5657 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5658 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5659 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5661
5662 // Move.
5663 x2 += 20; y2 -= 25;
5664 processPosition(mapper, x2, y2);
5665 processMTSync(mapper);
5666 processSync(mapper);
5667
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5670 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5672 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5674 ASSERT_EQ(0, motionArgs.flags);
5675 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5676 ASSERT_EQ(0, motionArgs.buttonState);
5677 ASSERT_EQ(0, motionArgs.edgeFlags);
5678 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5679 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5682 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5683 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5684 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5686
5687 // New finger down.
5688 int32_t x3 = 700, y3 = 300;
5689 processPosition(mapper, x2, y2);
5690 processMTSync(mapper);
5691 processPosition(mapper, x3, y3);
5692 processMTSync(mapper);
5693 processSync(mapper);
5694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5696 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5697 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5698 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5699 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5700 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5701 motionArgs.action);
5702 ASSERT_EQ(0, motionArgs.flags);
5703 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5704 ASSERT_EQ(0, motionArgs.buttonState);
5705 ASSERT_EQ(0, motionArgs.edgeFlags);
5706 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5707 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5709 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5712 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5714 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5715 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5716 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5717 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5718
5719 // Second finger up.
5720 x3 += 30; y3 -= 20;
5721 processPosition(mapper, x3, y3);
5722 processMTSync(mapper);
5723 processSync(mapper);
5724
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5726 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5727 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5728 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5729 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5730 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5731 motionArgs.action);
5732 ASSERT_EQ(0, motionArgs.flags);
5733 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5734 ASSERT_EQ(0, motionArgs.buttonState);
5735 ASSERT_EQ(0, motionArgs.edgeFlags);
5736 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5737 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5739 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5742 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5744 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5745 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5746 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5747 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5751 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5752 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5753 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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(1), motionArgs.pointerCount);
5760 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5763 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5764 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5765 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5766 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5767
5768 // Last finger up.
5769 processMTSync(mapper);
5770 processSync(mapper);
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_UP, 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 // Should not have sent any more keys or motions.
5792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5794}
5795
5796TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797 addConfigurationProperty("touch.deviceType", "touchScreen");
5798 prepareDisplay(DISPLAY_ORIENTATION_0);
5799 prepareAxes(POSITION | ID);
5800 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005801 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802
arthurhungdcef2dc2020-08-11 14:47:50 +08005803 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804
5805 NotifyMotionArgs motionArgs;
5806
5807 // Two fingers down at once.
5808 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5809 processPosition(mapper, x1, y1);
5810 processId(mapper, 1);
5811 processMTSync(mapper);
5812 processPosition(mapper, x2, y2);
5813 processId(mapper, 2);
5814 processMTSync(mapper);
5815 processSync(mapper);
5816
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5818 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5819 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5820 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5823 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5824
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5826 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5827 motionArgs.action);
5828 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5829 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5831 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5834 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5836 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5837
5838 // Move.
5839 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5840 processPosition(mapper, x1, y1);
5841 processId(mapper, 1);
5842 processMTSync(mapper);
5843 processPosition(mapper, x2, y2);
5844 processId(mapper, 2);
5845 processMTSync(mapper);
5846 processSync(mapper);
5847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5850 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5853 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5856 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5858 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5859
5860 // First finger up.
5861 x2 += 15; y2 -= 20;
5862 processPosition(mapper, x2, y2);
5863 processId(mapper, 2);
5864 processMTSync(mapper);
5865 processSync(mapper);
5866
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5868 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5869 motionArgs.action);
5870 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5871 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5873 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5876 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5878 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5879
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5882 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5883 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5886 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5887
5888 // Move.
5889 x2 += 20; y2 -= 25;
5890 processPosition(mapper, x2, y2);
5891 processId(mapper, 2);
5892 processMTSync(mapper);
5893 processSync(mapper);
5894
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5897 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5898 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5901 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5902
5903 // New finger down.
5904 int32_t x3 = 700, y3 = 300;
5905 processPosition(mapper, x2, y2);
5906 processId(mapper, 2);
5907 processMTSync(mapper);
5908 processPosition(mapper, x3, y3);
5909 processId(mapper, 3);
5910 processMTSync(mapper);
5911 processSync(mapper);
5912
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5914 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5915 motionArgs.action);
5916 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5917 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5919 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5922 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5924 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5925
5926 // Second finger up.
5927 x3 += 30; y3 -= 20;
5928 processPosition(mapper, x3, y3);
5929 processId(mapper, 3);
5930 processMTSync(mapper);
5931 processSync(mapper);
5932
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5935 motionArgs.action);
5936 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5937 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5939 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5940 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5942 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5944 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5945
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5948 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5949 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5952 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5953
5954 // Last finger up.
5955 processMTSync(mapper);
5956 processSync(mapper);
5957
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5960 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5961 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5962 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5964 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5965
5966 // Should not have sent any more keys or motions.
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5969}
5970
5971TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972 addConfigurationProperty("touch.deviceType", "touchScreen");
5973 prepareDisplay(DISPLAY_ORIENTATION_0);
5974 prepareAxes(POSITION | ID | SLOT);
5975 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005976 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977
arthurhungdcef2dc2020-08-11 14:47:50 +08005978 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979
5980 NotifyMotionArgs motionArgs;
5981
5982 // Two fingers down at once.
5983 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5984 processPosition(mapper, x1, y1);
5985 processId(mapper, 1);
5986 processSlot(mapper, 1);
5987 processPosition(mapper, x2, y2);
5988 processId(mapper, 2);
5989 processSync(mapper);
5990
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5993 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5994 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5998
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6000 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6001 motionArgs.action);
6002 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6003 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6005 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6006 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6008 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6010 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6011
6012 // Move.
6013 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6014 processSlot(mapper, 0);
6015 processPosition(mapper, x1, y1);
6016 processSlot(mapper, 1);
6017 processPosition(mapper, x2, y2);
6018 processSync(mapper);
6019
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6022 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6023 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6025 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6026 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6028 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6030 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6031
6032 // First finger up.
6033 x2 += 15; y2 -= 20;
6034 processSlot(mapper, 0);
6035 processId(mapper, -1);
6036 processSlot(mapper, 1);
6037 processPosition(mapper, x2, y2);
6038 processSync(mapper);
6039
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6041 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6042 motionArgs.action);
6043 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6044 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6046 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6049 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6051 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6052
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6055 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6056 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6059 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6060
6061 // Move.
6062 x2 += 20; y2 -= 25;
6063 processPosition(mapper, x2, y2);
6064 processSync(mapper);
6065
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6068 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6069 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6072 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6073
6074 // New finger down.
6075 int32_t x3 = 700, y3 = 300;
6076 processPosition(mapper, x2, y2);
6077 processSlot(mapper, 0);
6078 processId(mapper, 3);
6079 processPosition(mapper, x3, y3);
6080 processSync(mapper);
6081
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6084 motionArgs.action);
6085 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6086 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6088 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6091 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6093 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6094
6095 // Second finger up.
6096 x3 += 30; y3 -= 20;
6097 processSlot(mapper, 1);
6098 processId(mapper, -1);
6099 processSlot(mapper, 0);
6100 processPosition(mapper, x3, y3);
6101 processSync(mapper);
6102
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6104 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6105 motionArgs.action);
6106 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6107 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6109 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6112 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6114 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6115
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6118 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6119 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6122 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6123
6124 // Last finger up.
6125 processId(mapper, -1);
6126 processSync(mapper);
6127
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6130 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6131 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6134 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6135
6136 // Should not have sent any more keys or motions.
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6139}
6140
6141TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142 addConfigurationProperty("touch.deviceType", "touchScreen");
6143 prepareDisplay(DISPLAY_ORIENTATION_0);
6144 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006145 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146
6147 // These calculations are based on the input device calibration documentation.
6148 int32_t rawX = 100;
6149 int32_t rawY = 200;
6150 int32_t rawTouchMajor = 7;
6151 int32_t rawTouchMinor = 6;
6152 int32_t rawToolMajor = 9;
6153 int32_t rawToolMinor = 8;
6154 int32_t rawPressure = 11;
6155 int32_t rawDistance = 0;
6156 int32_t rawOrientation = 3;
6157 int32_t id = 5;
6158
6159 float x = toDisplayX(rawX);
6160 float y = toDisplayY(rawY);
6161 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6162 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6163 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6164 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6165 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6166 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6167 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6168 float distance = float(rawDistance);
6169
6170 processPosition(mapper, rawX, rawY);
6171 processTouchMajor(mapper, rawTouchMajor);
6172 processTouchMinor(mapper, rawTouchMinor);
6173 processToolMajor(mapper, rawToolMajor);
6174 processToolMinor(mapper, rawToolMinor);
6175 processPressure(mapper, rawPressure);
6176 processOrientation(mapper, rawOrientation);
6177 processDistance(mapper, rawDistance);
6178 processId(mapper, id);
6179 processMTSync(mapper);
6180 processSync(mapper);
6181
6182 NotifyMotionArgs args;
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6184 ASSERT_EQ(0, args.pointerProperties[0].id);
6185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6186 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6187 orientation, distance));
6188}
6189
6190TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 addConfigurationProperty("touch.deviceType", "touchScreen");
6192 prepareDisplay(DISPLAY_ORIENTATION_0);
6193 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6194 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006195 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196
6197 // These calculations are based on the input device calibration documentation.
6198 int32_t rawX = 100;
6199 int32_t rawY = 200;
6200 int32_t rawTouchMajor = 140;
6201 int32_t rawTouchMinor = 120;
6202 int32_t rawToolMajor = 180;
6203 int32_t rawToolMinor = 160;
6204
6205 float x = toDisplayX(rawX);
6206 float y = toDisplayY(rawY);
6207 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6208 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6209 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6210 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6211 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6212
6213 processPosition(mapper, rawX, rawY);
6214 processTouchMajor(mapper, rawTouchMajor);
6215 processTouchMinor(mapper, rawTouchMinor);
6216 processToolMajor(mapper, rawToolMajor);
6217 processToolMinor(mapper, rawToolMinor);
6218 processMTSync(mapper);
6219 processSync(mapper);
6220
6221 NotifyMotionArgs args;
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6224 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6225}
6226
6227TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228 addConfigurationProperty("touch.deviceType", "touchScreen");
6229 prepareDisplay(DISPLAY_ORIENTATION_0);
6230 prepareAxes(POSITION | TOUCH | TOOL);
6231 addConfigurationProperty("touch.size.calibration", "diameter");
6232 addConfigurationProperty("touch.size.scale", "10");
6233 addConfigurationProperty("touch.size.bias", "160");
6234 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006235 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236
6237 // These calculations are based on the input device calibration documentation.
6238 // Note: We only provide a single common touch/tool value because the device is assumed
6239 // not to emit separate values for each pointer (isSummed = 1).
6240 int32_t rawX = 100;
6241 int32_t rawY = 200;
6242 int32_t rawX2 = 150;
6243 int32_t rawY2 = 250;
6244 int32_t rawTouchMajor = 5;
6245 int32_t rawToolMajor = 8;
6246
6247 float x = toDisplayX(rawX);
6248 float y = toDisplayY(rawY);
6249 float x2 = toDisplayX(rawX2);
6250 float y2 = toDisplayY(rawY2);
6251 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6252 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6253 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6254
6255 processPosition(mapper, rawX, rawY);
6256 processTouchMajor(mapper, rawTouchMajor);
6257 processToolMajor(mapper, rawToolMajor);
6258 processMTSync(mapper);
6259 processPosition(mapper, rawX2, rawY2);
6260 processTouchMajor(mapper, rawTouchMajor);
6261 processToolMajor(mapper, rawToolMajor);
6262 processMTSync(mapper);
6263 processSync(mapper);
6264
6265 NotifyMotionArgs args;
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6268
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6270 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6271 args.action);
6272 ASSERT_EQ(size_t(2), args.pointerCount);
6273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6274 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6276 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6277}
6278
6279TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280 addConfigurationProperty("touch.deviceType", "touchScreen");
6281 prepareDisplay(DISPLAY_ORIENTATION_0);
6282 prepareAxes(POSITION | TOUCH | TOOL);
6283 addConfigurationProperty("touch.size.calibration", "area");
6284 addConfigurationProperty("touch.size.scale", "43");
6285 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006286 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287
6288 // These calculations are based on the input device calibration documentation.
6289 int32_t rawX = 100;
6290 int32_t rawY = 200;
6291 int32_t rawTouchMajor = 5;
6292 int32_t rawToolMajor = 8;
6293
6294 float x = toDisplayX(rawX);
6295 float y = toDisplayY(rawY);
6296 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6297 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6298 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6299
6300 processPosition(mapper, rawX, rawY);
6301 processTouchMajor(mapper, rawTouchMajor);
6302 processToolMajor(mapper, rawToolMajor);
6303 processMTSync(mapper);
6304 processSync(mapper);
6305
6306 NotifyMotionArgs args;
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6309 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6310}
6311
6312TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 addConfigurationProperty("touch.deviceType", "touchScreen");
6314 prepareDisplay(DISPLAY_ORIENTATION_0);
6315 prepareAxes(POSITION | PRESSURE);
6316 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6317 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006318 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319
Michael Wrightaa449c92017-12-13 21:21:43 +00006320 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006321 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006322 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6323 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6324 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6325
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326 // These calculations are based on the input device calibration documentation.
6327 int32_t rawX = 100;
6328 int32_t rawY = 200;
6329 int32_t rawPressure = 60;
6330
6331 float x = toDisplayX(rawX);
6332 float y = toDisplayY(rawY);
6333 float pressure = float(rawPressure) * 0.01f;
6334
6335 processPosition(mapper, rawX, rawY);
6336 processPressure(mapper, rawPressure);
6337 processMTSync(mapper);
6338 processSync(mapper);
6339
6340 NotifyMotionArgs args;
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6343 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6344}
6345
6346TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 addConfigurationProperty("touch.deviceType", "touchScreen");
6348 prepareDisplay(DISPLAY_ORIENTATION_0);
6349 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006350 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351
6352 NotifyMotionArgs motionArgs;
6353 NotifyKeyArgs keyArgs;
6354
6355 processId(mapper, 1);
6356 processPosition(mapper, 100, 200);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6360 ASSERT_EQ(0, motionArgs.buttonState);
6361
6362 // press BTN_LEFT, release BTN_LEFT
6363 processKey(mapper, BTN_LEFT, 1);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6367 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6368
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6371 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6372
Michael Wrightd02c5b62014-02-10 15:10:22 -08006373 processKey(mapper, BTN_LEFT, 0);
6374 processSync(mapper);
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006378
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382
6383 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6384 processKey(mapper, BTN_RIGHT, 1);
6385 processKey(mapper, BTN_MIDDLE, 1);
6386 processSync(mapper);
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6389 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6390 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_TERTIARY, motionArgs.buttonState);
6395
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6398 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6399 motionArgs.buttonState);
6400
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 processKey(mapper, BTN_RIGHT, 0);
6402 processSync(mapper);
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006404 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006409 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410
6411 processKey(mapper, BTN_MIDDLE, 0);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006414 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006419 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006420
6421 // press BTN_BACK, release BTN_BACK
6422 processKey(mapper, BTN_BACK, 1);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6425 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6426 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006430 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6431
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6434 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006435
6436 processKey(mapper, BTN_BACK, 0);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006441
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006444 ASSERT_EQ(0, motionArgs.buttonState);
6445
Michael Wrightd02c5b62014-02-10 15:10:22 -08006446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6447 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6448 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6449
6450 // press BTN_SIDE, release BTN_SIDE
6451 processKey(mapper, BTN_SIDE, 1);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6454 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6455 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6460
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6463 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464
6465 processKey(mapper, BTN_SIDE, 0);
6466 processSync(mapper);
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006470
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006473 ASSERT_EQ(0, motionArgs.buttonState);
6474
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6476 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6477 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6478
6479 // press BTN_FORWARD, release BTN_FORWARD
6480 processKey(mapper, BTN_FORWARD, 1);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6484 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006485
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006488 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6489
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6492 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493
6494 processKey(mapper, BTN_FORWARD, 0);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006502 ASSERT_EQ(0, motionArgs.buttonState);
6503
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6505 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6506 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6507
6508 // press BTN_EXTRA, release BTN_EXTRA
6509 processKey(mapper, BTN_EXTRA, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6512 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6513 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006514
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006517 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6518
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6521 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006522
6523 processKey(mapper, BTN_EXTRA, 0);
6524 processSync(mapper);
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006528
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006531 ASSERT_EQ(0, motionArgs.buttonState);
6532
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6535 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6536
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6538
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539 // press BTN_STYLUS, release BTN_STYLUS
6540 processKey(mapper, BTN_STYLUS, 1);
6541 processSync(mapper);
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006544 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6545
6546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6548 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006549
6550 processKey(mapper, BTN_STYLUS, 0);
6551 processSync(mapper);
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006554 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006555
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006558 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559
6560 // press BTN_STYLUS2, release BTN_STYLUS2
6561 processKey(mapper, BTN_STYLUS2, 1);
6562 processSync(mapper);
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006565 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6566
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6569 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006570
6571 processKey(mapper, BTN_STYLUS2, 0);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006575 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006576
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006579 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006580
6581 // release touch
6582 processId(mapper, -1);
6583 processSync(mapper);
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6585 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6586 ASSERT_EQ(0, motionArgs.buttonState);
6587}
6588
6589TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590 addConfigurationProperty("touch.deviceType", "touchScreen");
6591 prepareDisplay(DISPLAY_ORIENTATION_0);
6592 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006593 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006594
6595 NotifyMotionArgs motionArgs;
6596
6597 // default tool type is finger
6598 processId(mapper, 1);
6599 processPosition(mapper, 100, 200);
6600 processSync(mapper);
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6604
6605 // eraser
6606 processKey(mapper, BTN_TOOL_RUBBER, 1);
6607 processSync(mapper);
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6611
6612 // stylus
6613 processKey(mapper, BTN_TOOL_RUBBER, 0);
6614 processKey(mapper, BTN_TOOL_PEN, 1);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6619
6620 // brush
6621 processKey(mapper, BTN_TOOL_PEN, 0);
6622 processKey(mapper, BTN_TOOL_BRUSH, 1);
6623 processSync(mapper);
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6627
6628 // pencil
6629 processKey(mapper, BTN_TOOL_BRUSH, 0);
6630 processKey(mapper, BTN_TOOL_PENCIL, 1);
6631 processSync(mapper);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6635
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006636 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006637 processKey(mapper, BTN_TOOL_PENCIL, 0);
6638 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6639 processSync(mapper);
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6643
6644 // mouse
6645 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6646 processKey(mapper, BTN_TOOL_MOUSE, 1);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6651
6652 // lens
6653 processKey(mapper, BTN_TOOL_MOUSE, 0);
6654 processKey(mapper, BTN_TOOL_LENS, 1);
6655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6659
6660 // double-tap
6661 processKey(mapper, BTN_TOOL_LENS, 0);
6662 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6666 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6667
6668 // triple-tap
6669 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6670 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6671 processSync(mapper);
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6675
6676 // quad-tap
6677 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6678 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6679 processSync(mapper);
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6683
6684 // finger
6685 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6686 processKey(mapper, BTN_TOOL_FINGER, 1);
6687 processSync(mapper);
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6691
6692 // stylus trumps finger
6693 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6698
6699 // eraser trumps stylus
6700 processKey(mapper, BTN_TOOL_RUBBER, 1);
6701 processSync(mapper);
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6705
6706 // mouse trumps eraser
6707 processKey(mapper, BTN_TOOL_MOUSE, 1);
6708 processSync(mapper);
6709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6712
6713 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6714 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6715 processSync(mapper);
6716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6719
6720 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6721 processToolType(mapper, MT_TOOL_PEN);
6722 processSync(mapper);
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6726
6727 // back to default tool type
6728 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6729 processKey(mapper, BTN_TOOL_MOUSE, 0);
6730 processKey(mapper, BTN_TOOL_RUBBER, 0);
6731 processKey(mapper, BTN_TOOL_PEN, 0);
6732 processKey(mapper, BTN_TOOL_FINGER, 0);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6737}
6738
6739TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006740 addConfigurationProperty("touch.deviceType", "touchScreen");
6741 prepareDisplay(DISPLAY_ORIENTATION_0);
6742 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006743 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006744 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006745
6746 NotifyMotionArgs motionArgs;
6747
6748 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6749 processId(mapper, 1);
6750 processPosition(mapper, 100, 200);
6751 processSync(mapper);
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6755 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6756
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6758 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6760 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6761
6762 // move a little
6763 processPosition(mapper, 150, 250);
6764 processSync(mapper);
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6768 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6769
6770 // down when BTN_TOUCH is pressed, pressure defaults to 1
6771 processKey(mapper, BTN_TOUCH, 1);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6776 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6777
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6779 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6781 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6782
6783 // up when BTN_TOUCH is released, hover restored
6784 processKey(mapper, BTN_TOUCH, 0);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6789 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6790
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6794 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6795
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6797 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
6802 processId(mapper, -1);
6803 processSync(mapper);
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6805 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6807 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6808}
6809
6810TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006811 addConfigurationProperty("touch.deviceType", "touchScreen");
6812 prepareDisplay(DISPLAY_ORIENTATION_0);
6813 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006814 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815
6816 NotifyMotionArgs motionArgs;
6817
6818 // initially hovering because pressure is 0
6819 processId(mapper, 1);
6820 processPosition(mapper, 100, 200);
6821 processPressure(mapper, 0);
6822 processSync(mapper);
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6824 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6826 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6827
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6829 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6831 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6832
6833 // move a little
6834 processPosition(mapper, 150, 250);
6835 processSync(mapper);
6836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6837 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6839 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6840
6841 // down when pressure becomes non-zero
6842 processPressure(mapper, RAW_PRESSURE_MAX);
6843 processSync(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6847 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6848
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6850 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6852 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6853
6854 // up when pressure becomes 0, hover restored
6855 processPressure(mapper, 0);
6856 processSync(mapper);
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6858 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6860 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6861
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6863 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6865 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6866
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6868 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
6873 processId(mapper, -1);
6874 processSync(mapper);
6875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6876 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6878 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6879}
6880
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006881/**
6882 * Set the input device port <--> display port associations, and check that the
6883 * events are routed to the display that matches the display port.
6884 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6885 */
6886TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006887 const std::string usb2 = "USB2";
6888 const uint8_t hdmi1 = 0;
6889 const uint8_t hdmi2 = 1;
6890 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006891 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006892
6893 addConfigurationProperty("touch.deviceType", "touchScreen");
6894 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006895 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006896
6897 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6898 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6899
6900 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6901 // for this input device is specified, and the matching viewport is not present,
6902 // the input device should be disabled (at the mapper level).
6903
6904 // Add viewport for display 2 on hdmi2
6905 prepareSecondaryDisplay(type, hdmi2);
6906 // Send a touch event
6907 processPosition(mapper, 100, 100);
6908 processSync(mapper);
6909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6910
6911 // Add viewport for display 1 on hdmi1
6912 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6913 // Send a touch event again
6914 processPosition(mapper, 100, 100);
6915 processSync(mapper);
6916
6917 NotifyMotionArgs args;
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6919 ASSERT_EQ(DISPLAY_ID, args.displayId);
6920}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006922TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006923 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006924 std::shared_ptr<FakePointerController> fakePointerController =
6925 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006926 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006927 fakePointerController->setPosition(100, 200);
6928 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006929 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6930
Garfield Tan888a6a42020-01-09 11:39:16 -08006931 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006932 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08006933
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006934 prepareDisplay(DISPLAY_ORIENTATION_0);
6935 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006936 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006937
6938 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006939 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006940
6941 NotifyMotionArgs motionArgs;
6942 processPosition(mapper, 100, 100);
6943 processSync(mapper);
6944
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6946 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6947 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6948}
6949
Arthur Hung7c645402019-01-25 17:45:42 +08006950TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6951 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08006952 prepareAxes(POSITION | ID | SLOT);
6953 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006954 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08006955
6956 // Create the second touch screen device, and enable multi fingers.
6957 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08006958 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08006959 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006960 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08006961 std::shared_ptr<InputDevice> device2 =
6962 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
6963 Flags<InputDeviceClass>(0));
6964
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006965 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6966 0 /*flat*/, 0 /*fuzz*/);
6967 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6968 0 /*flat*/, 0 /*fuzz*/);
6969 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6970 0 /*flat*/, 0 /*fuzz*/);
6971 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6972 0 /*flat*/, 0 /*fuzz*/);
6973 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6974 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6975 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08006976
6977 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006978 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08006979 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6980 device2->reset(ARBITRARY_TIME);
6981
6982 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01006983 std::shared_ptr<FakePointerController> fakePointerController =
6984 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08006985 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6986 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6987
6988 // Setup policy for associated displays and show touches.
6989 const uint8_t hdmi1 = 0;
6990 const uint8_t hdmi2 = 1;
6991 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6992 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6993 mFakePolicy->setShowTouches(true);
6994
6995 // Create displays.
6996 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006997 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08006998
6999 // Default device will reconfigure above, need additional reconfiguration for another device.
7000 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007001 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007002
7003 // Two fingers down at default display.
7004 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7005 processPosition(mapper, x1, y1);
7006 processId(mapper, 1);
7007 processSlot(mapper, 1);
7008 processPosition(mapper, x2, y2);
7009 processId(mapper, 2);
7010 processSync(mapper);
7011
7012 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7013 fakePointerController->getSpots().find(DISPLAY_ID);
7014 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7015 ASSERT_EQ(size_t(2), iter->second.size());
7016
7017 // Two fingers down at second display.
7018 processPosition(mapper2, x1, y1);
7019 processId(mapper2, 1);
7020 processSlot(mapper2, 1);
7021 processPosition(mapper2, x2, y2);
7022 processId(mapper2, 2);
7023 processSync(mapper2);
7024
7025 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7026 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7027 ASSERT_EQ(size_t(2), iter->second.size());
7028}
7029
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007030TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007031 prepareAxes(POSITION);
7032 addConfigurationProperty("touch.deviceType", "touchScreen");
7033 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007034 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007035
7036 NotifyMotionArgs motionArgs;
7037 // Unrotated video frame
7038 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7039 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007040 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007041 processPosition(mapper, 100, 200);
7042 processSync(mapper);
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7044 ASSERT_EQ(frames, motionArgs.videoFrames);
7045
7046 // Subsequent touch events should not have any videoframes
7047 // This is implemented separately in FakeEventHub,
7048 // but that should match the behaviour of TouchVideoDevice.
7049 processPosition(mapper, 200, 200);
7050 processSync(mapper);
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7052 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7053}
7054
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007055TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007056 prepareAxes(POSITION);
7057 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007058 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007059 // Unrotated video frame
7060 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7061 NotifyMotionArgs motionArgs;
7062
7063 // Test all 4 orientations
7064 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7065 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7066 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7067 clearViewports();
7068 prepareDisplay(orientation);
7069 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007070 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007071 processPosition(mapper, 100, 200);
7072 processSync(mapper);
7073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7074 frames[0].rotate(orientation);
7075 ASSERT_EQ(frames, motionArgs.videoFrames);
7076 }
7077}
7078
7079TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007080 prepareAxes(POSITION);
7081 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007082 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007083 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7084 // so mix these.
7085 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7086 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7087 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7088 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7089 NotifyMotionArgs motionArgs;
7090
7091 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007092 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007093 processPosition(mapper, 100, 200);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 std::for_each(frames.begin(), frames.end(),
7097 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7098 ASSERT_EQ(frames, motionArgs.videoFrames);
7099}
7100
Arthur Hung9da14732019-09-02 16:16:58 +08007101/**
7102 * If we had defined port associations, but the viewport is not ready, the touch device would be
7103 * expected to be disabled, and it should be enabled after the viewport has found.
7104 */
7105TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007106 constexpr uint8_t hdmi2 = 1;
7107 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007108 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007109
7110 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7111
7112 addConfigurationProperty("touch.deviceType", "touchScreen");
7113 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007114 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007115
7116 ASSERT_EQ(mDevice->isEnabled(), false);
7117
7118 // Add display on hdmi2, the device should be enabled and can receive touch event.
7119 prepareSecondaryDisplay(type, hdmi2);
7120 ASSERT_EQ(mDevice->isEnabled(), true);
7121
7122 // Send a touch event.
7123 processPosition(mapper, 100, 100);
7124 processSync(mapper);
7125
7126 NotifyMotionArgs args;
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7128 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7129}
7130
Arthur Hung6cd19a42019-08-30 19:04:12 +08007131
Arthur Hung6cd19a42019-08-30 19:04:12 +08007132
Arthur Hung421eb1c2020-01-16 00:09:42 +08007133TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007134 addConfigurationProperty("touch.deviceType", "touchScreen");
7135 prepareDisplay(DISPLAY_ORIENTATION_0);
7136 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007137 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007138
7139 NotifyMotionArgs motionArgs;
7140
7141 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7142 // finger down
7143 processId(mapper, 1);
7144 processPosition(mapper, x1, y1);
7145 processSync(mapper);
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7147 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7149
7150 // finger move
7151 processId(mapper, 1);
7152 processPosition(mapper, x2, y2);
7153 processSync(mapper);
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7157
7158 // finger up.
7159 processId(mapper, -1);
7160 processSync(mapper);
7161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7162 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7164
7165 // new finger down
7166 processId(mapper, 1);
7167 processPosition(mapper, x3, y3);
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
7174/**
arthurhungcc7f9802020-04-30 17:55:40 +08007175 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7176 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007177 */
arthurhungcc7f9802020-04-30 17:55:40 +08007178TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007179 addConfigurationProperty("touch.deviceType", "touchScreen");
7180 prepareDisplay(DISPLAY_ORIENTATION_0);
7181 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007183
7184 NotifyMotionArgs motionArgs;
7185
7186 // default tool type is finger
7187 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007188 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007189 processPosition(mapper, x1, y1);
7190 processSync(mapper);
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7194
7195 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7196 processToolType(mapper, MT_TOOL_PALM);
7197 processSync(mapper);
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7199 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7200
7201 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007202 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007203 processPosition(mapper, x2, y2);
7204 processSync(mapper);
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7206
7207 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007208 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007209 processSync(mapper);
7210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7211
7212 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007213 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007214 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007215 processPosition(mapper, x3, y3);
7216 processSync(mapper);
7217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7218 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7220}
7221
arthurhungbf89a482020-04-17 17:37:55 +08007222/**
arthurhungcc7f9802020-04-30 17:55:40 +08007223 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7224 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007225 */
arthurhungcc7f9802020-04-30 17:55:40 +08007226TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007227 addConfigurationProperty("touch.deviceType", "touchScreen");
7228 prepareDisplay(DISPLAY_ORIENTATION_0);
7229 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7230 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7231
7232 NotifyMotionArgs motionArgs;
7233
7234 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007235 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7236 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007237 processPosition(mapper, x1, y1);
7238 processSync(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7242
7243 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007244 processSlot(mapper, SECOND_SLOT);
7245 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007246 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007247 processSync(mapper);
7248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7249 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7250 motionArgs.action);
7251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7252
7253 // If the tool type of the first finger changes to MT_TOOL_PALM,
7254 // we expect to receive ACTION_POINTER_UP with cancel flag.
7255 processSlot(mapper, FIRST_SLOT);
7256 processId(mapper, FIRST_TRACKING_ID);
7257 processToolType(mapper, MT_TOOL_PALM);
7258 processSync(mapper);
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7260 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7261 motionArgs.action);
7262 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7263
7264 // The following MOVE events of second finger should be processed.
7265 processSlot(mapper, SECOND_SLOT);
7266 processId(mapper, SECOND_TRACKING_ID);
7267 processPosition(mapper, x2 + 1, y2 + 1);
7268 processSync(mapper);
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7271 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7272
7273 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7274 // it. Second finger receive move.
7275 processSlot(mapper, FIRST_SLOT);
7276 processId(mapper, INVALID_TRACKING_ID);
7277 processSync(mapper);
7278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7280 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7281
7282 // Second finger keeps moving.
7283 processSlot(mapper, SECOND_SLOT);
7284 processId(mapper, SECOND_TRACKING_ID);
7285 processPosition(mapper, x2 + 2, y2 + 2);
7286 processSync(mapper);
7287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7289 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7290
7291 // Second finger up.
7292 processId(mapper, INVALID_TRACKING_ID);
7293 processSync(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7296 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7297}
7298
7299/**
7300 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7301 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7302 */
7303TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7304 addConfigurationProperty("touch.deviceType", "touchScreen");
7305 prepareDisplay(DISPLAY_ORIENTATION_0);
7306 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7307 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7308
7309 NotifyMotionArgs motionArgs;
7310
7311 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7312 // First finger down.
7313 processId(mapper, FIRST_TRACKING_ID);
7314 processPosition(mapper, x1, y1);
7315 processSync(mapper);
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7319
7320 // Second finger down.
7321 processSlot(mapper, SECOND_SLOT);
7322 processId(mapper, SECOND_TRACKING_ID);
7323 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007324 processSync(mapper);
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7327 motionArgs.action);
7328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7329
arthurhungcc7f9802020-04-30 17:55:40 +08007330 // If the tool type of the first finger changes to MT_TOOL_PALM,
7331 // we expect to receive ACTION_POINTER_UP with cancel flag.
7332 processSlot(mapper, FIRST_SLOT);
7333 processId(mapper, FIRST_TRACKING_ID);
7334 processToolType(mapper, MT_TOOL_PALM);
7335 processSync(mapper);
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7337 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7338 motionArgs.action);
7339 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7340
7341 // Second finger keeps moving.
7342 processSlot(mapper, SECOND_SLOT);
7343 processId(mapper, SECOND_TRACKING_ID);
7344 processPosition(mapper, x2 + 1, y2 + 1);
7345 processSync(mapper);
7346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7348
7349 // second finger becomes palm, receive cancel due to only 1 finger is active.
7350 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007351 processToolType(mapper, MT_TOOL_PALM);
7352 processSync(mapper);
7353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7354 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7355
arthurhungcc7f9802020-04-30 17:55:40 +08007356 // third finger down.
7357 processSlot(mapper, THIRD_SLOT);
7358 processId(mapper, THIRD_TRACKING_ID);
7359 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007360 processPosition(mapper, x3, y3);
7361 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7363 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007365 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7366
7367 // third finger move
7368 processId(mapper, THIRD_TRACKING_ID);
7369 processPosition(mapper, x3 + 1, y3 + 1);
7370 processSync(mapper);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7373
7374 // first finger up, third finger receive move.
7375 processSlot(mapper, FIRST_SLOT);
7376 processId(mapper, INVALID_TRACKING_ID);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7380 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7381
7382 // second finger up, third finger receive move.
7383 processSlot(mapper, SECOND_SLOT);
7384 processId(mapper, INVALID_TRACKING_ID);
7385 processSync(mapper);
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7388 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7389
7390 // third finger up.
7391 processSlot(mapper, THIRD_SLOT);
7392 processId(mapper, INVALID_TRACKING_ID);
7393 processSync(mapper);
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7395 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7396 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7397}
7398
7399/**
7400 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7401 * and the active finger could still be allowed to receive the events
7402 */
7403TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7404 addConfigurationProperty("touch.deviceType", "touchScreen");
7405 prepareDisplay(DISPLAY_ORIENTATION_0);
7406 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7407 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7408
7409 NotifyMotionArgs motionArgs;
7410
7411 // default tool type is finger
7412 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7413 processId(mapper, FIRST_TRACKING_ID);
7414 processPosition(mapper, x1, y1);
7415 processSync(mapper);
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7417 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7419
7420 // Second finger down.
7421 processSlot(mapper, SECOND_SLOT);
7422 processId(mapper, SECOND_TRACKING_ID);
7423 processPosition(mapper, x2, y2);
7424 processSync(mapper);
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7426 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7427 motionArgs.action);
7428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7429
7430 // If the tool type of the second finger changes to MT_TOOL_PALM,
7431 // we expect to receive ACTION_POINTER_UP with cancel flag.
7432 processId(mapper, SECOND_TRACKING_ID);
7433 processToolType(mapper, MT_TOOL_PALM);
7434 processSync(mapper);
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7436 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7437 motionArgs.action);
7438 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7439
7440 // The following MOVE event should be processed.
7441 processSlot(mapper, FIRST_SLOT);
7442 processId(mapper, FIRST_TRACKING_ID);
7443 processPosition(mapper, x1 + 1, y1 + 1);
7444 processSync(mapper);
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7447 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7448
7449 // second finger up.
7450 processSlot(mapper, SECOND_SLOT);
7451 processId(mapper, INVALID_TRACKING_ID);
7452 processSync(mapper);
7453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7455
7456 // first finger keep moving
7457 processSlot(mapper, FIRST_SLOT);
7458 processId(mapper, FIRST_TRACKING_ID);
7459 processPosition(mapper, x1 + 2, y1 + 2);
7460 processSync(mapper);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7463
7464 // first finger up.
7465 processId(mapper, INVALID_TRACKING_ID);
7466 processSync(mapper);
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7468 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7469 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007470}
7471
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007472// --- MultiTouchInputMapperTest_ExternalDevice ---
7473
7474class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7475protected:
Chris Yea52ade12020-08-27 16:49:20 -07007476 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007477};
7478
7479/**
7480 * Expect fallback to internal viewport if device is external and external viewport is not present.
7481 */
7482TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7483 prepareAxes(POSITION);
7484 addConfigurationProperty("touch.deviceType", "touchScreen");
7485 prepareDisplay(DISPLAY_ORIENTATION_0);
7486 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7487
7488 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7489
7490 NotifyMotionArgs motionArgs;
7491
7492 // Expect the event to be sent to the internal viewport,
7493 // because an external viewport is not present.
7494 processPosition(mapper, 100, 100);
7495 processSync(mapper);
7496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7497 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7498
7499 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007500 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007501 processPosition(mapper, 100, 100);
7502 processSync(mapper);
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7504 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7505}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007506
7507/**
7508 * Test touch should not work if outside of surface.
7509 */
7510class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7511protected:
7512 void halfDisplayToCenterHorizontal(int32_t orientation) {
7513 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007514 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007515
7516 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7517 internalViewport->orientation = orientation;
7518 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7519 internalViewport->logicalLeft = 0;
7520 internalViewport->logicalTop = 0;
7521 internalViewport->logicalRight = DISPLAY_HEIGHT;
7522 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7523
7524 internalViewport->physicalLeft = 0;
7525 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7526 internalViewport->physicalRight = DISPLAY_HEIGHT;
7527 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7528
7529 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7530 internalViewport->deviceHeight = DISPLAY_WIDTH;
7531 } else {
7532 internalViewport->logicalLeft = 0;
7533 internalViewport->logicalTop = 0;
7534 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7535 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7536
7537 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7538 internalViewport->physicalTop = 0;
7539 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7540 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7541
7542 internalViewport->deviceWidth = DISPLAY_WIDTH;
7543 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7544 }
7545
7546 mFakePolicy->updateViewport(internalViewport.value());
7547 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7548 }
7549
7550 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7551 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7552 int32_t yExpected) {
7553 // touch on outside area should not work.
7554 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7555 processSync(mapper);
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7557
7558 // touch on inside area should receive the event.
7559 NotifyMotionArgs args;
7560 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7561 processSync(mapper);
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7563 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7564 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7565
7566 // Reset.
7567 mapper.reset(ARBITRARY_TIME);
7568 }
7569};
7570
7571TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7572 addConfigurationProperty("touch.deviceType", "touchScreen");
7573 prepareDisplay(DISPLAY_ORIENTATION_0);
7574 prepareAxes(POSITION);
7575 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7576
7577 // Touch on center of normal display should work.
7578 const int32_t x = DISPLAY_WIDTH / 4;
7579 const int32_t y = DISPLAY_HEIGHT / 2;
7580 processPosition(mapper, toRawX(x), toRawY(y));
7581 processSync(mapper);
7582 NotifyMotionArgs args;
7583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7585 0.0f, 0.0f, 0.0f, 0.0f));
7586 // Reset.
7587 mapper.reset(ARBITRARY_TIME);
7588
7589 // Let physical display be different to device, and make surface and physical could be 1:1.
7590 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7591
7592 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7593 const int32_t yExpected = y;
7594 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7595}
7596
7597TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7598 addConfigurationProperty("touch.deviceType", "touchScreen");
7599 prepareDisplay(DISPLAY_ORIENTATION_0);
7600 prepareAxes(POSITION);
7601 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7602
7603 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7604 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7605
7606 const int32_t x = DISPLAY_WIDTH / 4;
7607 const int32_t y = DISPLAY_HEIGHT / 2;
7608
7609 // expect x/y = swap x/y then reverse y.
7610 const int32_t xExpected = y;
7611 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7612 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7613}
7614
7615TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7616 addConfigurationProperty("touch.deviceType", "touchScreen");
7617 prepareDisplay(DISPLAY_ORIENTATION_0);
7618 prepareAxes(POSITION);
7619 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7620
7621 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7622 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7623
7624 const int32_t x = DISPLAY_WIDTH / 4;
7625 const int32_t y = DISPLAY_HEIGHT / 2;
7626
7627 // expect x/y = swap x/y then reverse x.
7628 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7629 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7630 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7631}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007632
7633TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7634 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7635 std::shared_ptr<FakePointerController> fakePointerController =
7636 std::make_shared<FakePointerController>();
7637 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7638 fakePointerController->setPosition(0, 0);
7639 fakePointerController->setButtonState(0);
7640
7641 // prepare device and capture
7642 prepareDisplay(DISPLAY_ORIENTATION_0);
7643 prepareAxes(POSITION | ID | SLOT);
7644 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7645 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7646 mFakePolicy->setPointerCapture(true);
7647 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7648 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7649
7650 // captured touchpad should be a touchpad source
7651 NotifyDeviceResetArgs resetArgs;
7652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7653 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7654
7655 // run captured pointer tests - note that this is unscaled, so input listener events should be
7656 // identical to what the hardware sends (accounting for any
7657 // calibration).
7658 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007659 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007660 processId(mapper, 1);
7661 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7662 processKey(mapper, BTN_TOUCH, 1);
7663 processSync(mapper);
7664
7665 // expect coord[0] to contain initial location of touch 0
7666 NotifyMotionArgs args;
7667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7669 ASSERT_EQ(1U, args.pointerCount);
7670 ASSERT_EQ(0, args.pointerProperties[0].id);
7671 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7672 ASSERT_NO_FATAL_FAILURE(
7673 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7674
7675 // FINGER 1 DOWN
7676 processSlot(mapper, 1);
7677 processId(mapper, 2);
7678 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7679 processSync(mapper);
7680
7681 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007683 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7684 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007685 ASSERT_EQ(2U, args.pointerCount);
7686 ASSERT_EQ(0, args.pointerProperties[0].id);
7687 ASSERT_EQ(1, args.pointerProperties[1].id);
7688 ASSERT_NO_FATAL_FAILURE(
7689 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7690 ASSERT_NO_FATAL_FAILURE(
7691 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7692
7693 // FINGER 1 MOVE
7694 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7695 processSync(mapper);
7696
7697 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7698 // from move
7699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7701 ASSERT_NO_FATAL_FAILURE(
7702 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7703 ASSERT_NO_FATAL_FAILURE(
7704 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7705
7706 // FINGER 0 MOVE
7707 processSlot(mapper, 0);
7708 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7709 processSync(mapper);
7710
7711 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7714 ASSERT_NO_FATAL_FAILURE(
7715 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7716 ASSERT_NO_FATAL_FAILURE(
7717 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7718
7719 // BUTTON DOWN
7720 processKey(mapper, BTN_LEFT, 1);
7721 processSync(mapper);
7722
7723 // touchinputmapper design sends a move before button press
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7728
7729 // BUTTON UP
7730 processKey(mapper, BTN_LEFT, 0);
7731 processSync(mapper);
7732
7733 // touchinputmapper design sends a move after button release
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7738
7739 // FINGER 0 UP
7740 processId(mapper, -1);
7741 processSync(mapper);
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7743 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7744
7745 // FINGER 1 MOVE
7746 processSlot(mapper, 1);
7747 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7748 processSync(mapper);
7749
7750 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7753 ASSERT_EQ(1U, args.pointerCount);
7754 ASSERT_EQ(1, args.pointerProperties[0].id);
7755 ASSERT_NO_FATAL_FAILURE(
7756 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7757
7758 // FINGER 1 UP
7759 processId(mapper, -1);
7760 processKey(mapper, BTN_TOUCH, 0);
7761 processSync(mapper);
7762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7763 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7764
7765 // non captured touchpad should be a mouse source
7766 mFakePolicy->setPointerCapture(false);
7767 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7769 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7770}
7771
7772TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7773 std::shared_ptr<FakePointerController> fakePointerController =
7774 std::make_shared<FakePointerController>();
7775 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7776 fakePointerController->setPosition(0, 0);
7777 fakePointerController->setButtonState(0);
7778
7779 // prepare device and capture
7780 prepareDisplay(DISPLAY_ORIENTATION_0);
7781 prepareAxes(POSITION | ID | SLOT);
7782 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7783 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7784 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7785 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7786 // run uncaptured pointer tests - pushes out generic events
7787 // FINGER 0 DOWN
7788 processId(mapper, 3);
7789 processPosition(mapper, 100, 100);
7790 processKey(mapper, BTN_TOUCH, 1);
7791 processSync(mapper);
7792
7793 // start at (100,100), cursor should be at (0,0) * scale
7794 NotifyMotionArgs args;
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7797 ASSERT_NO_FATAL_FAILURE(
7798 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7799
7800 // FINGER 0 MOVE
7801 processPosition(mapper, 200, 200);
7802 processSync(mapper);
7803
7804 // compute scaling to help with touch position checking
7805 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7806 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7807 float scale =
7808 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7809
7810 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7812 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7814 0, 0, 0, 0, 0, 0, 0));
7815}
7816
7817TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7818 std::shared_ptr<FakePointerController> fakePointerController =
7819 std::make_shared<FakePointerController>();
7820
7821 prepareDisplay(DISPLAY_ORIENTATION_0);
7822 prepareAxes(POSITION | ID | SLOT);
7823 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7824 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7825 mFakePolicy->setPointerCapture(false);
7826 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7827
7828 // uncaptured touchpad should be a pointer device
7829 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7830
7831 // captured touchpad should be a touchpad device
7832 mFakePolicy->setPointerCapture(true);
7833 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7834 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7835}
7836
Michael Wrightd02c5b62014-02-10 15:10:22 -08007837} // namespace android