blob: 5a4c881485ec9523ab91696527887e080535eeb3 [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>
Michael Wrightdde67b82020-10-27 16:09:22 +000036#include "input/DisplayViewport.h"
37#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010038
Michael Wrightd02c5b62014-02-10 15:10:22 -080039namespace android {
40
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070041using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070042using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070043
44// Timeout for waiting for an expected event
45static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
46
Michael Wrightd02c5b62014-02-10 15:10:22 -080047// An arbitrary time value.
48static const nsecs_t ARBITRARY_TIME = 1234;
49
50// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080051static constexpr int32_t DISPLAY_ID = 0;
52static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
53static constexpr int32_t DISPLAY_WIDTH = 480;
54static constexpr int32_t DISPLAY_HEIGHT = 800;
55static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
56static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
57static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070058static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070059static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080060
arthurhungcc7f9802020-04-30 17:55:40 +080061static constexpr int32_t FIRST_SLOT = 0;
62static constexpr int32_t SECOND_SLOT = 1;
63static constexpr int32_t THIRD_SLOT = 2;
64static constexpr int32_t INVALID_TRACKING_ID = -1;
65static constexpr int32_t FIRST_TRACKING_ID = 0;
66static constexpr int32_t SECOND_TRACKING_ID = 1;
67static constexpr int32_t THIRD_TRACKING_ID = 2;
68
Michael Wrightd02c5b62014-02-10 15:10:22 -080069// Error tolerance for floating point assertions.
70static const float EPSILON = 0.001f;
71
72template<typename T>
73static inline T min(T a, T b) {
74 return a < b ? a : b;
75}
76
77static inline float avg(float x, float y) {
78 return (x + y) / 2;
79}
80
81
82// --- FakePointerController ---
83
84class FakePointerController : public PointerControllerInterface {
85 bool mHaveBounds;
86 float mMinX, mMinY, mMaxX, mMaxY;
87 float mX, mY;
88 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080089 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080090
Michael Wrightd02c5b62014-02-10 15:10:22 -080091public:
92 FakePointerController() :
93 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080094 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080095 }
96
Michael Wright17db18e2020-06-26 20:51:44 +010097 virtual ~FakePointerController() {}
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099 void setBounds(float minX, float minY, float maxX, float maxY) {
100 mHaveBounds = true;
101 mMinX = minX;
102 mMinY = minY;
103 mMaxX = maxX;
104 mMaxY = maxY;
105 }
106
Chris Yea52ade12020-08-27 16:49:20 -0700107 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108 mX = x;
109 mY = y;
110 }
111
Chris Yea52ade12020-08-27 16:49:20 -0700112 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
Chris Yea52ade12020-08-27 16:49:20 -0700114 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Chris Yea52ade12020-08-27 16:49:20 -0700116 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117 *outX = mX;
118 *outY = mY;
119 }
120
Chris Yea52ade12020-08-27 16:49:20 -0700121 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800122
Chris Yea52ade12020-08-27 16:49:20 -0700123 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800124 mDisplayId = viewport.displayId;
125 }
126
Arthur Hung7c645402019-01-25 17:45:42 +0800127 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
128 return mSpotsByDisplay;
129 }
130
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131private:
Chris Yea52ade12020-08-27 16:49:20 -0700132 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133 *outMinX = mMinX;
134 *outMinY = mMinY;
135 *outMaxX = mMaxX;
136 *outMaxY = mMaxY;
137 return mHaveBounds;
138 }
139
Chris Yea52ade12020-08-27 16:49:20 -0700140 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141 mX += deltaX;
142 if (mX < mMinX) mX = mMinX;
143 if (mX > mMaxX) mX = mMaxX;
144 mY += deltaY;
145 if (mY < mMinY) mY = mMinY;
146 if (mY > mMaxY) mY = mMaxY;
147 }
148
Chris Yea52ade12020-08-27 16:49:20 -0700149 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150
Chris Yea52ade12020-08-27 16:49:20 -0700151 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152
Chris Yea52ade12020-08-27 16:49:20 -0700153 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154
Chris Yea52ade12020-08-27 16:49:20 -0700155 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
156 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800157 std::vector<int32_t> newSpots;
158 // Add spots for fingers that are down.
159 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
160 uint32_t id = idBits.clearFirstMarkedBit();
161 newSpots.push_back(id);
162 }
163
164 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800168
169 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170};
171
172
173// --- FakeInputReaderPolicy ---
174
175class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700176 std::mutex mLock;
177 std::condition_variable mDevicesChangedCondition;
178
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100180 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700181 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
182 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100183 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700184 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186protected:
Chris Yea52ade12020-08-27 16:49:20 -0700187 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
189public:
190 FakeInputReaderPolicy() {
191 }
192
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700193 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800194 waitForInputDevices([](bool devicesChanged) {
195 if (!devicesChanged) {
196 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
197 }
198 });
199 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700200
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800201 void assertInputDevicesNotChanged() {
202 waitForInputDevices([](bool devicesChanged) {
203 if (devicesChanged) {
204 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
205 }
206 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700207 }
208
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700209 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100210 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100211 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700212 }
213
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700214 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
215 return mConfig.getDisplayViewportByUniqueId(uniqueId);
216 }
217 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
218 return mConfig.getDisplayViewportByType(type);
219 }
220
221 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
222 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700223 }
224
225 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700226 const std::string& uniqueId, std::optional<uint8_t> physicalPort,
227 ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700228 const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700229 orientation, uniqueId, physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700230 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100231 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232 }
233
Arthur Hung6cd19a42019-08-30 19:04:12 +0800234 bool updateViewport(const DisplayViewport& viewport) {
235 size_t count = mViewports.size();
236 for (size_t i = 0; i < count; i++) {
237 const DisplayViewport& currentViewport = mViewports[i];
238 if (currentViewport.displayId == viewport.displayId) {
239 mViewports[i] = viewport;
240 mConfig.setDisplayViewports(mViewports);
241 return true;
242 }
243 }
244 // no viewport found.
245 return false;
246 }
247
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100248 void addExcludedDeviceName(const std::string& deviceName) {
249 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 }
251
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700252 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
253 mConfig.portAssociations.insert({inputPort, displayPort});
254 }
255
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000256 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700257
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000258 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700259
Michael Wright17db18e2020-06-26 20:51:44 +0100260 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
261 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262 }
263
264 const InputReaderConfiguration* getReaderConfiguration() const {
265 return &mConfig;
266 }
267
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800268 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 return mInputDevices;
270 }
271
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100272 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700273 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700274 return transform;
275 }
276
277 void setTouchAffineTransformation(const TouchAffineTransformation t) {
278 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800279 }
280
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800281 void setPointerCapture(bool enabled) {
282 mConfig.pointerCapture = enabled;
283 }
284
Arthur Hung7c645402019-01-25 17:45:42 +0800285 void setShowTouches(bool enabled) {
286 mConfig.showTouches = enabled;
287 }
288
Garfield Tan888a6a42020-01-09 11:39:16 -0800289 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
290 mConfig.defaultPointerDisplayId = pointerDisplayId;
291 }
292
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800293 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
294
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700296 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700297 int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
298 ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700299 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
300 || orientation == DISPLAY_ORIENTATION_270);
301 DisplayViewport v;
302 v.displayId = displayId;
303 v.orientation = orientation;
304 v.logicalLeft = 0;
305 v.logicalTop = 0;
306 v.logicalRight = isRotated ? height : width;
307 v.logicalBottom = isRotated ? width : height;
308 v.physicalLeft = 0;
309 v.physicalTop = 0;
310 v.physicalRight = isRotated ? height : width;
311 v.physicalBottom = isRotated ? width : height;
312 v.deviceWidth = isRotated ? height : width;
313 v.deviceHeight = isRotated ? width : height;
314 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700315 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100316 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700317 return v;
318 }
319
Chris Yea52ade12020-08-27 16:49:20 -0700320 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 *outConfig = mConfig;
322 }
323
Chris Yea52ade12020-08-27 16:49:20 -0700324 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100325 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 }
327
Chris Yea52ade12020-08-27 16:49:20 -0700328 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700329 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700331 mInputDevicesChanged = true;
332 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 }
334
Chris Yea52ade12020-08-27 16:49:20 -0700335 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
336 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700337 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
339
Chris Yea52ade12020-08-27 16:49:20 -0700340 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800341
342 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
343 std::unique_lock<std::mutex> lock(mLock);
344 base::ScopedLockAssertion assumeLocked(mLock);
345
346 const bool devicesChanged =
347 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
348 return mInputDevicesChanged;
349 });
350 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
351 mInputDevicesChanged = false;
352 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353};
354
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355// --- FakeEventHub ---
356
357class FakeEventHub : public EventHubInterface {
358 struct KeyInfo {
359 int32_t keyCode;
360 uint32_t flags;
361 };
362
363 struct Device {
364 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700365 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 PropertyMap configuration;
367 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
368 KeyedVector<int, bool> relativeAxes;
369 KeyedVector<int32_t, int32_t> keyCodeStates;
370 KeyedVector<int32_t, int32_t> scanCodeStates;
371 KeyedVector<int32_t, int32_t> switchStates;
372 KeyedVector<int32_t, int32_t> absoluteAxisValue;
373 KeyedVector<int32_t, KeyInfo> keysByScanCode;
374 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
375 KeyedVector<int32_t, bool> leds;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800376 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700377 bool enabled;
378
379 status_t enable() {
380 enabled = true;
381 return OK;
382 }
383
384 status_t disable() {
385 enabled = false;
386 return OK;
387 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388
Chris Ye1b0c7342020-07-28 21:57:03 -0700389 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390 };
391
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700392 std::mutex mLock;
393 std::condition_variable mEventsCondition;
394
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100396 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700397 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600398 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700400public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 virtual ~FakeEventHub() {
402 for (size_t i = 0; i < mDevices.size(); i++) {
403 delete mDevices.valueAt(i);
404 }
405 }
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 FakeEventHub() { }
408
Chris Ye1b0c7342020-07-28 21:57:03 -0700409 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 Device* device = new Device(classes);
411 device->identifier.name = name;
412 mDevices.add(deviceId, device);
413
414 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
415 }
416
417 void removeDevice(int32_t deviceId) {
418 delete mDevices.valueFor(deviceId);
419 mDevices.removeItem(deviceId);
420
421 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
422 }
423
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700424 bool isDeviceEnabled(int32_t deviceId) {
425 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700426 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700427 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
428 return false;
429 }
430 return device->enabled;
431 }
432
433 status_t enableDevice(int32_t deviceId) {
434 status_t result;
435 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700436 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
438 return BAD_VALUE;
439 }
440 if (device->enabled) {
441 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
442 return OK;
443 }
444 result = device->enable();
445 return result;
446 }
447
448 status_t disableDevice(int32_t deviceId) {
449 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700450 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700451 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
452 return BAD_VALUE;
453 }
454 if (!device->enabled) {
455 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
456 return OK;
457 }
458 return device->disable();
459 }
460
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 void finishDeviceScan() {
462 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
463 }
464
465 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
466 Device* device = getDevice(deviceId);
467 device->configuration.addProperty(key, value);
468 }
469
470 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
471 Device* device = getDevice(deviceId);
472 device->configuration.addAll(configuration);
473 }
474
475 void addAbsoluteAxis(int32_t deviceId, int axis,
476 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
477 Device* device = getDevice(deviceId);
478
479 RawAbsoluteAxisInfo info;
480 info.valid = true;
481 info.minValue = minValue;
482 info.maxValue = maxValue;
483 info.flat = flat;
484 info.fuzz = fuzz;
485 info.resolution = resolution;
486 device->absoluteAxes.add(axis, info);
487 }
488
489 void addRelativeAxis(int32_t deviceId, int32_t axis) {
490 Device* device = getDevice(deviceId);
491 device->relativeAxes.add(axis, true);
492 }
493
494 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
495 Device* device = getDevice(deviceId);
496 device->keyCodeStates.replaceValueFor(keyCode, state);
497 }
498
499 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
500 Device* device = getDevice(deviceId);
501 device->scanCodeStates.replaceValueFor(scanCode, state);
502 }
503
504 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
505 Device* device = getDevice(deviceId);
506 device->switchStates.replaceValueFor(switchCode, state);
507 }
508
509 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
510 Device* device = getDevice(deviceId);
511 device->absoluteAxisValue.replaceValueFor(axis, value);
512 }
513
514 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
515 int32_t keyCode, uint32_t flags) {
516 Device* device = getDevice(deviceId);
517 KeyInfo info;
518 info.keyCode = keyCode;
519 info.flags = flags;
520 if (scanCode) {
521 device->keysByScanCode.add(scanCode, info);
522 }
523 if (usageCode) {
524 device->keysByUsageCode.add(usageCode, info);
525 }
526 }
527
528 void addLed(int32_t deviceId, int32_t led, bool initialState) {
529 Device* device = getDevice(deviceId);
530 device->leds.add(led, initialState);
531 }
532
533 bool getLedState(int32_t deviceId, int32_t led) {
534 Device* device = getDevice(deviceId);
535 return device->leds.valueFor(led);
536 }
537
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100538 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 return mExcludedDevices;
540 }
541
542 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
543 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800544 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 }
546
547 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
548 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700549 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 RawEvent event;
551 event.when = when;
552 event.deviceId = deviceId;
553 event.type = type;
554 event.code = code;
555 event.value = value;
556 mEvents.push_back(event);
557
558 if (type == EV_ABS) {
559 setAbsoluteAxisValue(deviceId, code, value);
560 }
561 }
562
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600563 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
564 std::vector<TouchVideoFrame>> videoFrames) {
565 mVideoFrames = std::move(videoFrames);
566 }
567
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700569 std::unique_lock<std::mutex> lock(mLock);
570 base::ScopedLockAssertion assumeLocked(mLock);
571 const bool queueIsEmpty =
572 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
573 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
574 if (!queueIsEmpty) {
575 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
576 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 }
578
579private:
580 Device* getDevice(int32_t deviceId) const {
581 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100582 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 }
584
Chris Yea52ade12020-08-27 16:49:20 -0700585 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700587 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Chris Yea52ade12020-08-27 16:49:20 -0700590 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 Device* device = getDevice(deviceId);
592 return device ? device->identifier : InputDeviceIdentifier();
593 }
594
Chris Yea52ade12020-08-27 16:49:20 -0700595 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
Chris Yea52ade12020-08-27 16:49:20 -0700597 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598 Device* device = getDevice(deviceId);
599 if (device) {
600 *outConfiguration = device->configuration;
601 }
602 }
603
Chris Yea52ade12020-08-27 16:49:20 -0700604 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
605 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800607 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608 ssize_t index = device->absoluteAxes.indexOfKey(axis);
609 if (index >= 0) {
610 *outAxisInfo = device->absoluteAxes.valueAt(index);
611 return OK;
612 }
613 }
614 outAxisInfo->clear();
615 return -1;
616 }
617
Chris Yea52ade12020-08-27 16:49:20 -0700618 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 Device* device = getDevice(deviceId);
620 if (device) {
621 return device->relativeAxes.indexOfKey(axis) >= 0;
622 }
623 return false;
624 }
625
Chris Yea52ade12020-08-27 16:49:20 -0700626 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627
Chris Yea52ade12020-08-27 16:49:20 -0700628 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
629 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 Device* device = getDevice(deviceId);
631 if (device) {
632 const KeyInfo* key = getKey(device, scanCode, usageCode);
633 if (key) {
634 if (outKeycode) {
635 *outKeycode = key->keyCode;
636 }
637 if (outFlags) {
638 *outFlags = key->flags;
639 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700640 if (outMetaState) {
641 *outMetaState = metaState;
642 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 return OK;
644 }
645 }
646 return NAME_NOT_FOUND;
647 }
648
649 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
650 if (usageCode) {
651 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
652 if (index >= 0) {
653 return &device->keysByUsageCode.valueAt(index);
654 }
655 }
656 if (scanCode) {
657 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
658 if (index >= 0) {
659 return &device->keysByScanCode.valueAt(index);
660 }
661 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700662 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 }
664
Chris Yea52ade12020-08-27 16:49:20 -0700665 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666
Chris Yea52ade12020-08-27 16:49:20 -0700667 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 mExcludedDevices = devices;
669 }
670
Chris Yea52ade12020-08-27 16:49:20 -0700671 size_t getEvents(int, RawEvent* buffer, size_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700672 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673 if (mEvents.empty()) {
674 return 0;
675 }
676
677 *buffer = *mEvents.begin();
678 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700679 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 return 1;
681 }
682
Chris Yea52ade12020-08-27 16:49:20 -0700683 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600684 auto it = mVideoFrames.find(deviceId);
685 if (it != mVideoFrames.end()) {
686 std::vector<TouchVideoFrame> frames = std::move(it->second);
687 mVideoFrames.erase(deviceId);
688 return frames;
689 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800690 return {};
691 }
692
Chris Yea52ade12020-08-27 16:49:20 -0700693 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 Device* device = getDevice(deviceId);
695 if (device) {
696 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
697 if (index >= 0) {
698 return device->scanCodeStates.valueAt(index);
699 }
700 }
701 return AKEY_STATE_UNKNOWN;
702 }
703
Chris Yea52ade12020-08-27 16:49:20 -0700704 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 Device* device = getDevice(deviceId);
706 if (device) {
707 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
708 if (index >= 0) {
709 return device->keyCodeStates.valueAt(index);
710 }
711 }
712 return AKEY_STATE_UNKNOWN;
713 }
714
Chris Yea52ade12020-08-27 16:49:20 -0700715 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800716 Device* device = getDevice(deviceId);
717 if (device) {
718 ssize_t index = device->switchStates.indexOfKey(sw);
719 if (index >= 0) {
720 return device->switchStates.valueAt(index);
721 }
722 }
723 return AKEY_STATE_UNKNOWN;
724 }
725
Chris Yea52ade12020-08-27 16:49:20 -0700726 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
727 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 Device* device = getDevice(deviceId);
729 if (device) {
730 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
731 if (index >= 0) {
732 *outValue = device->absoluteAxisValue.valueAt(index);
733 return OK;
734 }
735 }
736 *outValue = 0;
737 return -1;
738 }
739
Chris Yea52ade12020-08-27 16:49:20 -0700740 // Return true if the device has non-empty key layout.
741 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
742 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743 bool result = false;
744 Device* device = getDevice(deviceId);
745 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700746 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747 for (size_t i = 0; i < numCodes; i++) {
748 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
749 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
750 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 }
752 }
753 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
754 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
755 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 }
757 }
758 }
759 }
760 return result;
761 }
762
Chris Yea52ade12020-08-27 16:49:20 -0700763 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 Device* device = getDevice(deviceId);
765 if (device) {
766 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
767 return index >= 0;
768 }
769 return false;
770 }
771
Chris Yea52ade12020-08-27 16:49:20 -0700772 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 Device* device = getDevice(deviceId);
774 return device && device->leds.indexOfKey(led) >= 0;
775 }
776
Chris Yea52ade12020-08-27 16:49:20 -0700777 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800778 Device* device = getDevice(deviceId);
779 if (device) {
780 ssize_t index = device->leds.indexOfKey(led);
781 if (index >= 0) {
782 device->leds.replaceValueAt(led, on);
783 } else {
784 ADD_FAILURE()
785 << "Attempted to set the state of an LED that the EventHub declared "
786 "was not present. led=" << led;
787 }
788 }
789 }
790
Chris Yea52ade12020-08-27 16:49:20 -0700791 void getVirtualKeyDefinitions(
792 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793 outVirtualKeys.clear();
794
795 Device* device = getDevice(deviceId);
796 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800797 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 }
799 }
800
Chris Yea52ade12020-08-27 16:49:20 -0700801 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700802 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 }
804
Chris Yea52ade12020-08-27 16:49:20 -0700805 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806 return false;
807 }
808
Chris Yea52ade12020-08-27 16:49:20 -0700809 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
Chris Yea52ade12020-08-27 16:49:20 -0700811 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100813 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 return false;
815 }
816
Chris Yea52ade12020-08-27 16:49:20 -0700817 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818
Chris Yea52ade12020-08-27 16:49:20 -0700819 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820
Chris Yea52ade12020-08-27 16:49:20 -0700821 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822
Chris Yea52ade12020-08-27 16:49:20 -0700823 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824};
825
Michael Wrightd02c5b62014-02-10 15:10:22 -0800826// --- FakeInputMapper ---
827
828class FakeInputMapper : public InputMapper {
829 uint32_t mSources;
830 int32_t mKeyboardType;
831 int32_t mMetaState;
832 KeyedVector<int32_t, int32_t> mKeyCodeStates;
833 KeyedVector<int32_t, int32_t> mScanCodeStates;
834 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800835 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700837 std::mutex mLock;
838 std::condition_variable mStateChangedCondition;
839 bool mConfigureWasCalled GUARDED_BY(mLock);
840 bool mResetWasCalled GUARDED_BY(mLock);
841 bool mProcessWasCalled GUARDED_BY(mLock);
842 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843
Arthur Hungc23540e2018-11-29 20:42:11 +0800844 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800846 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
847 : InputMapper(deviceContext),
848 mSources(sources),
849 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800851 mConfigureWasCalled(false),
852 mResetWasCalled(false),
853 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854
Chris Yea52ade12020-08-27 16:49:20 -0700855 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856
857 void setKeyboardType(int32_t keyboardType) {
858 mKeyboardType = keyboardType;
859 }
860
861 void setMetaState(int32_t metaState) {
862 mMetaState = metaState;
863 }
864
865 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700866 std::unique_lock<std::mutex> lock(mLock);
867 base::ScopedLockAssertion assumeLocked(mLock);
868 const bool configureCalled =
869 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
870 return mConfigureWasCalled;
871 });
872 if (!configureCalled) {
873 FAIL() << "Expected configure() to have been called.";
874 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 mConfigureWasCalled = false;
876 }
877
878 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700879 std::unique_lock<std::mutex> lock(mLock);
880 base::ScopedLockAssertion assumeLocked(mLock);
881 const bool resetCalled =
882 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
883 return mResetWasCalled;
884 });
885 if (!resetCalled) {
886 FAIL() << "Expected reset() to have been called.";
887 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800888 mResetWasCalled = false;
889 }
890
Yi Kong9b14ac62018-07-17 13:48:38 -0700891 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700892 std::unique_lock<std::mutex> lock(mLock);
893 base::ScopedLockAssertion assumeLocked(mLock);
894 const bool processCalled =
895 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
896 return mProcessWasCalled;
897 });
898 if (!processCalled) {
899 FAIL() << "Expected process() to have been called.";
900 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901 if (outLastEvent) {
902 *outLastEvent = mLastEvent;
903 }
904 mProcessWasCalled = false;
905 }
906
907 void setKeyCodeState(int32_t keyCode, int32_t state) {
908 mKeyCodeStates.replaceValueFor(keyCode, state);
909 }
910
911 void setScanCodeState(int32_t scanCode, int32_t state) {
912 mScanCodeStates.replaceValueFor(scanCode, state);
913 }
914
915 void setSwitchState(int32_t switchCode, int32_t state) {
916 mSwitchStates.replaceValueFor(switchCode, state);
917 }
918
919 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800920 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 }
922
923private:
Chris Yea52ade12020-08-27 16:49:20 -0700924 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925
Chris Yea52ade12020-08-27 16:49:20 -0700926 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 InputMapper::populateDeviceInfo(deviceInfo);
928
929 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
930 deviceInfo->setKeyboardType(mKeyboardType);
931 }
932 }
933
Chris Yea52ade12020-08-27 16:49:20 -0700934 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700935 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800937
938 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800939 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800940 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
941 mViewport = config->getDisplayViewportByPort(*displayPort);
942 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700943
944 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 }
946
Chris Yea52ade12020-08-27 16:49:20 -0700947 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700948 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700950 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951 }
952
Chris Yea52ade12020-08-27 16:49:20 -0700953 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700954 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 mLastEvent = *rawEvent;
956 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700957 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 }
959
Chris Yea52ade12020-08-27 16:49:20 -0700960 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
962 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
963 }
964
Chris Yea52ade12020-08-27 16:49:20 -0700965 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
967 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
968 }
969
Chris Yea52ade12020-08-27 16:49:20 -0700970 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971 ssize_t index = mSwitchStates.indexOfKey(switchCode);
972 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
973 }
974
Chris Yea52ade12020-08-27 16:49:20 -0700975 // Return true if the device has non-empty key layout.
976 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
977 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978 for (size_t i = 0; i < numCodes; i++) {
979 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
980 if (keyCodes[i] == mSupportedKeyCodes[j]) {
981 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 }
983 }
984 }
Chris Yea52ade12020-08-27 16:49:20 -0700985 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986 return result;
987 }
988
989 virtual int32_t getMetaState() {
990 return mMetaState;
991 }
992
993 virtual void fadePointer() {
994 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800995
996 virtual std::optional<int32_t> getAssociatedDisplay() {
997 if (mViewport) {
998 return std::make_optional(mViewport->displayId);
999 }
1000 return std::nullopt;
1001 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002};
1003
1004
1005// --- InstrumentedInputReader ---
1006
1007class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001008 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009
1010public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001011 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1012 const sp<InputReaderPolicyInterface>& policy,
1013 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001014 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001016 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001018 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001020 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001021 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022 InputDeviceIdentifier identifier;
1023 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001024 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001026 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001027 }
1028
Prabir Pradhan28efc192019-11-05 01:10:04 +00001029 // Make the protected loopOnce method accessible to tests.
1030 using InputReader::loopOnce;
1031
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001033 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1034 const InputDeviceIdentifier& identifier)
1035 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001036 if (!mNextDevices.empty()) {
1037 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1038 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 return device;
1040 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001041 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042 }
1043
arthurhungdcef2dc2020-08-11 14:47:50 +08001044 // --- FakeInputReaderContext ---
1045 class FakeInputReaderContext : public ContextImpl {
1046 int32_t mGlobalMetaState;
1047 bool mUpdateGlobalMetaStateWasCalled;
1048 int32_t mGeneration;
1049
1050 public:
1051 FakeInputReaderContext(InputReader* reader)
1052 : ContextImpl(reader),
1053 mGlobalMetaState(0),
1054 mUpdateGlobalMetaStateWasCalled(false),
1055 mGeneration(1) {}
1056
1057 virtual ~FakeInputReaderContext() {}
1058
1059 void assertUpdateGlobalMetaStateWasCalled() {
1060 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1061 << "Expected updateGlobalMetaState() to have been called.";
1062 mUpdateGlobalMetaStateWasCalled = false;
1063 }
1064
1065 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1066
1067 uint32_t getGeneration() { return mGeneration; }
1068
1069 void updateGlobalMetaState() override {
1070 mUpdateGlobalMetaStateWasCalled = true;
1071 ContextImpl::updateGlobalMetaState();
1072 }
1073
1074 int32_t getGlobalMetaState() override {
1075 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1076 }
1077
1078 int32_t bumpGeneration() override {
1079 mGeneration = ContextImpl::bumpGeneration();
1080 return mGeneration;
1081 }
1082 } mFakeContext;
1083
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001085
1086public:
1087 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088};
1089
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001090// --- InputReaderPolicyTest ---
1091class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001092protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001093 sp<FakeInputReaderPolicy> mFakePolicy;
1094
Chris Yea52ade12020-08-27 16:49:20 -07001095 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1096 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001097};
1098
1099/**
1100 * Check that empty set of viewports is an acceptable configuration.
1101 * Also try to get internal viewport two different ways - by type and by uniqueId.
1102 *
1103 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1104 * Such configuration is not currently allowed.
1105 */
1106TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001107 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001108
1109 // We didn't add any viewports yet, so there shouldn't be any.
1110 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001111 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001112 ASSERT_FALSE(internalViewport);
1113
1114 // Add an internal viewport, then clear it
1115 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001116 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1117 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001118
1119 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001120 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001121 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001122 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001123
1124 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001125 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001126 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001127 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001128
1129 mFakePolicy->clearViewports();
1130 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001131 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001132 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001133 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001134 ASSERT_FALSE(internalViewport);
1135}
1136
1137TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1138 const std::string internalUniqueId = "local:0";
1139 const std::string externalUniqueId = "local:1";
1140 const std::string virtualUniqueId1 = "virtual:2";
1141 const std::string virtualUniqueId2 = "virtual:3";
1142 constexpr int32_t virtualDisplayId1 = 2;
1143 constexpr int32_t virtualDisplayId2 = 3;
1144
1145 // Add an internal viewport
1146 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001147 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1148 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001149 // Add an external viewport
1150 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001151 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1152 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001153 // Add an virtual viewport
1154 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001155 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1156 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001157 // Add another virtual viewport
1158 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001159 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1160 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001161
1162 // Check matching by type for internal
1163 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001164 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001165 ASSERT_TRUE(internalViewport);
1166 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1167
1168 // Check matching by type for external
1169 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001170 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001171 ASSERT_TRUE(externalViewport);
1172 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1173
1174 // Check matching by uniqueId for virtual viewport #1
1175 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001176 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001177 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001178 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1180 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1181
1182 // Check matching by uniqueId for virtual viewport #2
1183 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001184 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001186 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1188 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1189}
1190
1191
1192/**
1193 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1194 * that lookup works by checking display id.
1195 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1196 */
1197TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1198 const std::string uniqueId1 = "uniqueId1";
1199 const std::string uniqueId2 = "uniqueId2";
1200 constexpr int32_t displayId1 = 2;
1201 constexpr int32_t displayId2 = 3;
1202
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001203 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1204 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001205 for (const ViewportType& type : types) {
1206 mFakePolicy->clearViewports();
1207 // Add a viewport
1208 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001209 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210 // Add another viewport
1211 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001212 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001213
1214 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001215 std::optional<DisplayViewport> viewport1 =
1216 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001217 ASSERT_TRUE(viewport1);
1218 ASSERT_EQ(displayId1, viewport1->displayId);
1219 ASSERT_EQ(type, viewport1->type);
1220
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001221 std::optional<DisplayViewport> viewport2 =
1222 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001223 ASSERT_TRUE(viewport2);
1224 ASSERT_EQ(displayId2, viewport2->displayId);
1225 ASSERT_EQ(type, viewport2->type);
1226
1227 // When there are multiple viewports of the same kind, and uniqueId is not specified
1228 // in the call to getDisplayViewport, then that situation is not supported.
1229 // The viewports can be stored in any order, so we cannot rely on the order, since that
1230 // is just implementation detail.
1231 // However, we can check that it still returns *a* viewport, we just cannot assert
1232 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001233 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001234 ASSERT_TRUE(someViewport);
1235 }
1236}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001237
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001238/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001239 * When we have multiple internal displays make sure we always return the default display when
1240 * querying by type.
1241 */
1242TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1243 const std::string uniqueId1 = "uniqueId1";
1244 const std::string uniqueId2 = "uniqueId2";
1245 constexpr int32_t nonDefaultDisplayId = 2;
1246 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1247 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1248
1249 // Add the default display first and ensure it gets returned.
1250 mFakePolicy->clearViewports();
1251 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1252 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT,
1253 ViewportType::INTERNAL);
1254 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1255 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT,
1256 ViewportType::INTERNAL);
1257
1258 std::optional<DisplayViewport> viewport =
1259 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1260 ASSERT_TRUE(viewport);
1261 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1262 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1263
1264 // Add the default display second to make sure order doesn't matter.
1265 mFakePolicy->clearViewports();
1266 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1267 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT,
1268 ViewportType::INTERNAL);
1269 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1270 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT,
1271 ViewportType::INTERNAL);
1272
1273 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1274 ASSERT_TRUE(viewport);
1275 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1276 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1277}
1278
1279/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001280 * Check getDisplayViewportByPort
1281 */
1282TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001283 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001284 const std::string uniqueId1 = "uniqueId1";
1285 const std::string uniqueId2 = "uniqueId2";
1286 constexpr int32_t displayId1 = 1;
1287 constexpr int32_t displayId2 = 2;
1288 const uint8_t hdmi1 = 0;
1289 const uint8_t hdmi2 = 1;
1290 const uint8_t hdmi3 = 2;
1291
1292 mFakePolicy->clearViewports();
1293 // Add a viewport that's associated with some display port that's not of interest.
1294 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1295 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1296 // Add another viewport, connected to HDMI1 port
1297 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1298 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1299
1300 // Check that correct display viewport was returned by comparing the display ports.
1301 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1302 ASSERT_TRUE(hdmi1Viewport);
1303 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1304 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1305
1306 // Check that we can still get the same viewport using the uniqueId
1307 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1308 ASSERT_TRUE(hdmi1Viewport);
1309 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1310 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1311 ASSERT_EQ(type, hdmi1Viewport->type);
1312
1313 // Check that we cannot find a port with "HDMI2", because we never added one
1314 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1315 ASSERT_FALSE(hdmi2Viewport);
1316}
1317
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318// --- InputReaderTest ---
1319
1320class InputReaderTest : public testing::Test {
1321protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001322 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001323 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001324 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001325 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326
Chris Yea52ade12020-08-27 16:49:20 -07001327 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001328 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001330 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001331
Prabir Pradhan28efc192019-11-05 01:10:04 +00001332 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1333 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001334 }
1335
Chris Yea52ade12020-08-27 16:49:20 -07001336 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001337 mFakeListener.clear();
1338 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 }
1340
Chris Ye1b0c7342020-07-28 21:57:03 -07001341 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001342 const PropertyMap* configuration) {
1343 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001344
1345 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001346 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001347 }
1348 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001349 mReader->loopOnce();
1350 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001351 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1352 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001353 }
1354
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001355 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001356 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001357 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001358 }
1359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001360 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001361 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001362 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001363 }
1364
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001365 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001366 const std::string& name,
1367 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001368 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001369 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1370 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001371 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001372 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001373 return mapper;
1374 }
1375};
1376
Chris Ye98d3f532020-10-01 21:48:59 -07001377TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001378 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1379 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1380 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381
Chris Ye98d3f532020-10-01 21:48:59 -07001382 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001384 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001385 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1387 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1388 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001389}
1390
1391TEST_F(InputReaderTest, PolicyGetInputDevices) {
1392 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1393 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1394 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001395
1396 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001397 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001398 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001399 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001400 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1402 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1403 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1404}
1405
Chris Yee7310032020-09-22 15:36:28 -07001406TEST_F(InputReaderTest, GetMergedInputDevices) {
1407 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1408 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1409 // Add two subdevices to device
1410 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1411 // Must add at least one mapper or the device will be ignored!
1412 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1413 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1414
1415 // Push same device instance for next device to be added, so they'll have same identifier.
1416 mReader->pushNextDevice(device);
1417 mReader->pushNextDevice(device);
1418 ASSERT_NO_FATAL_FAILURE(
1419 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1420 ASSERT_NO_FATAL_FAILURE(
1421 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1422
1423 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001424 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001425}
1426
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001427TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
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;
1431 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001432 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001433 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001434 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001435 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001436
Yi Kong9b14ac62018-07-17 13:48:38 -07001437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001438
1439 NotifyDeviceResetArgs resetArgs;
1440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001441 ASSERT_EQ(deviceId, resetArgs.deviceId);
1442
1443 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001444 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001445 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001446
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001448 ASSERT_EQ(deviceId, resetArgs.deviceId);
1449 ASSERT_EQ(device->isEnabled(), false);
1450
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001451 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001452 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001455 ASSERT_EQ(device->isEnabled(), false);
1456
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001457 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001458 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001460 ASSERT_EQ(deviceId, resetArgs.deviceId);
1461 ASSERT_EQ(device->isEnabled(), true);
1462}
1463
Michael Wrightd02c5b62014-02-10 15:10:22 -08001464TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001465 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001466 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001467 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001468 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001469 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001470 AINPUT_SOURCE_KEYBOARD, nullptr);
1471 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001472
1473 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1474 AINPUT_SOURCE_ANY, AKEYCODE_A))
1475 << "Should return unknown when the device id is >= 0 but unknown.";
1476
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001477 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1478 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1479 << "Should return unknown when the device id is valid but the sources are not "
1480 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001482 ASSERT_EQ(AKEY_STATE_DOWN,
1483 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1484 AKEYCODE_A))
1485 << "Should return value provided by mapper when device id is valid and the device "
1486 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001487
1488 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1489 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1490 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1491
1492 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1493 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1494 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1495}
1496
1497TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001498 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001499 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001500 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001501 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001502 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001503 AINPUT_SOURCE_KEYBOARD, nullptr);
1504 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001505
1506 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1507 AINPUT_SOURCE_ANY, KEY_A))
1508 << "Should return unknown when the device id is >= 0 but unknown.";
1509
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001510 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1511 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1512 << "Should return unknown when the device id is valid but the sources are not "
1513 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001515 ASSERT_EQ(AKEY_STATE_DOWN,
1516 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1517 KEY_A))
1518 << "Should return value provided by mapper when device id is valid and the device "
1519 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001520
1521 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1522 AINPUT_SOURCE_TRACKBALL, KEY_A))
1523 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1524
1525 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1526 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1527 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1528}
1529
1530TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001531 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001532 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001533 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001534 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001535 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001536 AINPUT_SOURCE_KEYBOARD, nullptr);
1537 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001538
1539 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1540 AINPUT_SOURCE_ANY, SW_LID))
1541 << "Should return unknown when the device id is >= 0 but unknown.";
1542
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001543 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1544 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1545 << "Should return unknown when the device id is valid but the sources are not "
1546 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001548 ASSERT_EQ(AKEY_STATE_DOWN,
1549 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1550 SW_LID))
1551 << "Should return value provided by mapper when device id is valid and the device "
1552 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553
1554 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1555 AINPUT_SOURCE_TRACKBALL, SW_LID))
1556 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1557
1558 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1559 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1560 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1561}
1562
1563TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001564 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001565 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001566 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001567 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001568 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001569 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001570
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001571 mapper.addSupportedKeyCode(AKEYCODE_A);
1572 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573
1574 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1575 uint8_t flags[4] = { 0, 0, 0, 1 };
1576
1577 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1578 << "Should return false when device id is >= 0 but unknown.";
1579 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1580
1581 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001582 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1583 << "Should return false when device id is valid but the sources are not supported by "
1584 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1586
1587 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001588 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1589 keyCodes, flags))
1590 << "Should return value provided by mapper when device id is valid and the device "
1591 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001592 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1593
1594 flags[3] = 1;
1595 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1596 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1597 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1598
1599 flags[3] = 1;
1600 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1601 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1602 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1603}
1604
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001605TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001607 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001608
1609 NotifyConfigurationChangedArgs args;
1610
1611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1612 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1613}
1614
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001615TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001616 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001617 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001618 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001619 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001620 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001621 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001623 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001624 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1626
1627 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001628 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631 ASSERT_EQ(EV_KEY, event.type);
1632 ASSERT_EQ(KEY_A, event.code);
1633 ASSERT_EQ(1, event.value);
1634}
1635
Garfield Tan1c7bc862020-01-28 13:24:04 -08001636TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001637 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001638 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001639 constexpr int32_t eventHubId = 1;
1640 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001641 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001643 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001645
1646 NotifyDeviceResetArgs resetArgs;
1647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001648 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001650 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001651 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001653 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001654 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001655
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001656 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001657 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001659 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001660 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001661
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001662 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001663 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001665 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001666 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001667}
1668
Garfield Tan1c7bc862020-01-28 13:24:04 -08001669TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1670 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001671 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001672 constexpr int32_t eventHubId = 1;
1673 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1674 // Must add at least one mapper or the device will be ignored!
1675 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001676 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001677 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1678
1679 NotifyDeviceResetArgs resetArgs;
1680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1681 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1682}
1683
Arthur Hungc23540e2018-11-29 20:42:11 +08001684TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001686 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001688 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001689 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1690 FakeInputMapper& mapper =
1691 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001692 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001693
1694 const uint8_t hdmi1 = 1;
1695
1696 // Associated touch screen with second display.
1697 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1698
1699 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001700 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001701 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001702 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1703 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001704 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001705 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1706 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001707 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001708 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001709
1710 // Add the device, and make sure all of the callbacks are triggered.
1711 // The device is added after the input port associations are processed since
1712 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001716 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001717
Arthur Hung2c9a3342019-07-23 14:18:59 +08001718 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001719 ASSERT_EQ(deviceId, device->getId());
1720 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1721 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001722
1723 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001724 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001725 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001726 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001727}
1728
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001729TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1730 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1731 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1732 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1733 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1734 // Must add at least one mapper or the device will be ignored!
1735 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1736 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1737 mReader->pushNextDevice(device);
1738 mReader->pushNextDevice(device);
1739 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1740 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1741
1742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1743
1744 NotifyDeviceResetArgs resetArgs;
1745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1746 ASSERT_EQ(deviceId, resetArgs.deviceId);
1747 ASSERT_TRUE(device->isEnabled());
1748 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1749 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1750
1751 disableDevice(deviceId);
1752 mReader->loopOnce();
1753
1754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1755 ASSERT_EQ(deviceId, resetArgs.deviceId);
1756 ASSERT_FALSE(device->isEnabled());
1757 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1758 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1759
1760 enableDevice(deviceId);
1761 mReader->loopOnce();
1762
1763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1764 ASSERT_EQ(deviceId, resetArgs.deviceId);
1765 ASSERT_TRUE(device->isEnabled());
1766 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1767 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1768}
1769
1770TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1771 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1772 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1773 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1774 // Add two subdevices to device
1775 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1776 FakeInputMapper& mapperDevice1 =
1777 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1778 FakeInputMapper& mapperDevice2 =
1779 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1780 mReader->pushNextDevice(device);
1781 mReader->pushNextDevice(device);
1782 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1783 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1784
1785 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1786 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1787
1788 ASSERT_EQ(AKEY_STATE_DOWN,
1789 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1790 ASSERT_EQ(AKEY_STATE_DOWN,
1791 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1792 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1793 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1794}
1795
Prabir Pradhan7e186182020-11-10 13:56:45 -08001796TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1797 NotifyPointerCaptureChangedArgs args;
1798
1799 mFakePolicy->setPointerCapture(true);
1800 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1801 mReader->loopOnce();
1802 mFakeListener->assertNotifyCaptureWasCalled(&args);
1803 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1804
1805 mFakePolicy->setPointerCapture(false);
1806 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1807 mReader->loopOnce();
1808 mFakeListener->assertNotifyCaptureWasCalled(&args);
1809 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1810
1811 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1812 // does not change.
1813 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1814 mReader->loopOnce();
1815 mFakeListener->assertNotifyCaptureWasCalled(&args);
1816 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1817}
1818
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001819// --- InputReaderIntegrationTest ---
1820
1821// These tests create and interact with the InputReader only through its interface.
1822// The InputReader is started during SetUp(), which starts its processing in its own
1823// thread. The tests use linux uinput to emulate input devices.
1824// NOTE: Interacting with the physical device while these tests are running may cause
1825// the tests to fail.
1826class InputReaderIntegrationTest : public testing::Test {
1827protected:
1828 sp<TestInputListener> mTestListener;
1829 sp<FakeInputReaderPolicy> mFakePolicy;
1830 sp<InputReaderInterface> mReader;
1831
Chris Yea52ade12020-08-27 16:49:20 -07001832 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001833 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001834 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1835 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001836
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001837 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001838 ASSERT_EQ(mReader->start(), OK);
1839
1840 // Since this test is run on a real device, all the input devices connected
1841 // to the test device will show up in mReader. We wait for those input devices to
1842 // show up before beginning the tests.
1843 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1844 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1845 }
1846
Chris Yea52ade12020-08-27 16:49:20 -07001847 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001848 ASSERT_EQ(mReader->stop(), OK);
1849 mTestListener.clear();
1850 mFakePolicy.clear();
1851 }
1852};
1853
1854TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1855 // An invalid input device that is only used for this test.
1856 class InvalidUinputDevice : public UinputDevice {
1857 public:
1858 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1859
1860 private:
1861 void configureDevice(int fd, uinput_user_dev* device) override {}
1862 };
1863
1864 const size_t numDevices = mFakePolicy->getInputDevices().size();
1865
1866 // UinputDevice does not set any event or key bits, so InputReader should not
1867 // consider it as a valid device.
1868 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1869 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1870 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1871 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1872
1873 invalidDevice.reset();
1874 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1875 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1876 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1877}
1878
1879TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1880 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1881
1882 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1883 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1884 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1885 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1886
1887 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07001888 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
1889 const auto& it =
1890 std::find_if(inputDevices.begin(), inputDevices.end(),
1891 [&keyboard](const InputDeviceInfo& info) {
1892 return info.getIdentifier().name == keyboard->getName();
1893 });
1894
1895 ASSERT_NE(it, inputDevices.end());
1896 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
1897 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
1898 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001899
1900 keyboard.reset();
1901 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1902 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1903 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1904}
1905
1906TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1907 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1908 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1909
1910 NotifyConfigurationChangedArgs configChangedArgs;
1911 ASSERT_NO_FATAL_FAILURE(
1912 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001913 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001914 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1915
1916 NotifyKeyArgs keyArgs;
1917 keyboard->pressAndReleaseHomeKey();
1918 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1919 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001920 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001921 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001922 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1923 prevTimestamp = keyArgs.eventTime;
1924
1925 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1926 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001927 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001928 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1929}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001931/**
1932 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1933 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1934 * are passed to the listener.
1935 */
1936static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1937TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1938 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1939 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1940 NotifyKeyArgs keyArgs;
1941
1942 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1943 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1944 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1945 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1946
1947 controller->pressAndReleaseKey(BTN_GEAR_UP);
1948 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1949 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1950 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1951}
1952
Arthur Hungaab25622020-01-16 11:22:11 +08001953// --- TouchProcessTest ---
1954class TouchIntegrationTest : public InputReaderIntegrationTest {
1955protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001956 const std::string UNIQUE_ID = "local:0";
1957
Chris Yea52ade12020-08-27 16:49:20 -07001958 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08001959 InputReaderIntegrationTest::SetUp();
1960 // At least add an internal display.
1961 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1962 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001963 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001964
1965 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1966 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1967 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1968 }
1969
1970 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1971 int32_t orientation, const std::string& uniqueId,
1972 std::optional<uint8_t> physicalPort,
1973 ViewportType viewportType) {
1974 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1975 physicalPort, viewportType);
1976 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1977 }
1978
1979 std::unique_ptr<UinputTouchScreen> mDevice;
1980};
1981
1982TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1983 NotifyMotionArgs args;
1984 const Point centerPoint = mDevice->getCenterPoint();
1985
1986 // ACTION_DOWN
1987 mDevice->sendDown(centerPoint);
1988 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1990
1991 // ACTION_MOVE
1992 mDevice->sendMove(centerPoint + Point(1, 1));
1993 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1995
1996 // ACTION_UP
1997 mDevice->sendUp();
1998 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1999 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2000}
2001
2002TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2003 NotifyMotionArgs args;
2004 const Point centerPoint = mDevice->getCenterPoint();
2005
2006 // ACTION_DOWN
2007 mDevice->sendDown(centerPoint);
2008 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2009 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2010
2011 // ACTION_POINTER_DOWN (Second slot)
2012 const Point secondPoint = centerPoint + Point(100, 100);
2013 mDevice->sendSlot(SECOND_SLOT);
2014 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2015 mDevice->sendDown(secondPoint + Point(1, 1));
2016 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2017 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2018 args.action);
2019
2020 // ACTION_MOVE (Second slot)
2021 mDevice->sendMove(secondPoint);
2022 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2024
2025 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002026 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002027 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002028 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002029 args.action);
2030
2031 // ACTION_UP
2032 mDevice->sendSlot(FIRST_SLOT);
2033 mDevice->sendUp();
2034 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2035 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2036}
2037
2038TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2039 NotifyMotionArgs args;
2040 const Point centerPoint = mDevice->getCenterPoint();
2041
2042 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002043 mDevice->sendSlot(FIRST_SLOT);
2044 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002045 mDevice->sendDown(centerPoint);
2046 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2048
arthurhungcc7f9802020-04-30 17:55:40 +08002049 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002050 const Point secondPoint = centerPoint + Point(100, 100);
2051 mDevice->sendSlot(SECOND_SLOT);
2052 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2053 mDevice->sendDown(secondPoint);
2054 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2055 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2056 args.action);
2057
arthurhungcc7f9802020-04-30 17:55:40 +08002058 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002059 mDevice->sendMove(secondPoint + Point(1, 1));
2060 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2062
arthurhungcc7f9802020-04-30 17:55:40 +08002063 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2064 // a palm event.
2065 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002066 mDevice->sendToolType(MT_TOOL_PALM);
2067 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002068 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2069 args.action);
2070 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002071
arthurhungcc7f9802020-04-30 17:55:40 +08002072 // Send up to second slot, expect first slot send moving.
2073 mDevice->sendPointerUp();
2074 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002076
arthurhungcc7f9802020-04-30 17:55:40 +08002077 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002078 mDevice->sendSlot(FIRST_SLOT);
2079 mDevice->sendUp();
2080
arthurhungcc7f9802020-04-30 17:55:40 +08002081 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2082 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002083}
2084
Michael Wrightd02c5b62014-02-10 15:10:22 -08002085// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002086class InputDeviceTest : public testing::Test {
2087protected:
2088 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002089 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002090 static const int32_t DEVICE_ID;
2091 static const int32_t DEVICE_GENERATION;
2092 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002093 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002094 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002095
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002096 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002097 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002098 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002099 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002100 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002101
Chris Yea52ade12020-08-27 16:49:20 -07002102 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002103 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002104 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002105 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002106 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2107 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002108 InputDeviceIdentifier identifier;
2109 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002110 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002111 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002112 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002113 mReader->pushNextDevice(mDevice);
2114 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2115 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002116 }
2117
Chris Yea52ade12020-08-27 16:49:20 -07002118 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002119 mFakeListener.clear();
2120 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002121 }
2122};
2123
2124const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002125const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002126const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002127const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2128const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002129const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2130 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002131const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002132
2133TEST_F(InputDeviceTest, ImmutableProperties) {
2134 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002135 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002136 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002137}
2138
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002139TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2140 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002141}
2142
Michael Wrightd02c5b62014-02-10 15:10:22 -08002143TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2144 // Configuration.
2145 InputReaderConfiguration config;
2146 mDevice->configure(ARBITRARY_TIME, &config, 0);
2147
2148 // Reset.
2149 mDevice->reset(ARBITRARY_TIME);
2150
2151 NotifyDeviceResetArgs resetArgs;
2152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2153 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2154 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2155
2156 // Metadata.
2157 ASSERT_TRUE(mDevice->isIgnored());
2158 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2159
2160 InputDeviceInfo info;
2161 mDevice->getDeviceInfo(&info);
2162 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002163 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002164 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2165 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2166
2167 // State queries.
2168 ASSERT_EQ(0, mDevice->getMetaState());
2169
2170 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2171 << "Ignored device should return unknown key code state.";
2172 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2173 << "Ignored device should return unknown scan code state.";
2174 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2175 << "Ignored device should return unknown switch state.";
2176
2177 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2178 uint8_t flags[2] = { 0, 1 };
2179 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2180 << "Ignored device should never mark any key codes.";
2181 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2182 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2183}
2184
2185TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2186 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002187 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002188
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002189 FakeInputMapper& mapper1 =
2190 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002191 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2192 mapper1.setMetaState(AMETA_ALT_ON);
2193 mapper1.addSupportedKeyCode(AKEYCODE_A);
2194 mapper1.addSupportedKeyCode(AKEYCODE_B);
2195 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2196 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2197 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2198 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2199 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002200
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002201 FakeInputMapper& mapper2 =
2202 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002203 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002204
2205 InputReaderConfiguration config;
2206 mDevice->configure(ARBITRARY_TIME, &config, 0);
2207
2208 String8 propertyValue;
2209 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2210 << "Device should have read configuration during configuration phase.";
2211 ASSERT_STREQ("value", propertyValue.string());
2212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002213 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2214 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002215
2216 // Reset
2217 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002218 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2219 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220
2221 NotifyDeviceResetArgs resetArgs;
2222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2223 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2224 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2225
2226 // Metadata.
2227 ASSERT_FALSE(mDevice->isIgnored());
2228 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2229
2230 InputDeviceInfo info;
2231 mDevice->getDeviceInfo(&info);
2232 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002233 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002234 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2235 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2236
2237 // State queries.
2238 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2239 << "Should query mappers and combine meta states.";
2240
2241 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2242 << "Should return unknown key code state when source not supported.";
2243 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2244 << "Should return unknown scan code state when source not supported.";
2245 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2246 << "Should return unknown switch state when source not supported.";
2247
2248 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2249 << "Should query mapper when source is supported.";
2250 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2251 << "Should query mapper when source is supported.";
2252 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2253 << "Should query mapper when source is supported.";
2254
2255 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2256 uint8_t flags[4] = { 0, 0, 0, 1 };
2257 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2258 << "Should do nothing when source is unsupported.";
2259 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2260 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2261 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2262 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2263
2264 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2265 << "Should query mapper when source is supported.";
2266 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2267 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2268 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2269 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2270
2271 // Event handling.
2272 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002273 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002274 mDevice->process(&event, 1);
2275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002276 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2277 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002278}
2279
Arthur Hung2c9a3342019-07-23 14:18:59 +08002280// A single input device is associated with a specific display. Check that:
2281// 1. Device is disabled if the viewport corresponding to the associated display is not found
2282// 2. Device is disabled when setEnabled API is called
2283TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002284 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002285
2286 // First Configuration.
2287 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2288
2289 // Device should be enabled by default.
2290 ASSERT_TRUE(mDevice->isEnabled());
2291
2292 // Prepare associated info.
2293 constexpr uint8_t hdmi = 1;
2294 const std::string UNIQUE_ID = "local:1";
2295
2296 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2297 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2298 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2299 // Device should be disabled because it is associated with a specific display via
2300 // input port <-> display port association, but the corresponding display is not found
2301 ASSERT_FALSE(mDevice->isEnabled());
2302
2303 // Prepare displays.
2304 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002305 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002306 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2307 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2308 ASSERT_TRUE(mDevice->isEnabled());
2309
2310 // Device should be disabled after set disable.
2311 mFakePolicy->addDisabledDevice(mDevice->getId());
2312 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2313 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2314 ASSERT_FALSE(mDevice->isEnabled());
2315
2316 // Device should still be disabled even found the associated display.
2317 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2318 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2319 ASSERT_FALSE(mDevice->isEnabled());
2320}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002321
2322// --- InputMapperTest ---
2323
2324class InputMapperTest : public testing::Test {
2325protected:
2326 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002327 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002328 static const int32_t DEVICE_ID;
2329 static const int32_t DEVICE_GENERATION;
2330 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002331 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002332 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002333
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002334 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002336 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002337 std::unique_ptr<InstrumentedInputReader> mReader;
2338 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002339
Chris Ye1b0c7342020-07-28 21:57:03 -07002340 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002341 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002342 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002343 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002344 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2345 mFakeListener);
2346 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002347 }
2348
Chris Yea52ade12020-08-27 16:49:20 -07002349 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002350
Chris Yea52ade12020-08-27 16:49:20 -07002351 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352 mFakeListener.clear();
2353 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002354 }
2355
2356 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002357 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358 }
2359
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002360 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002361 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002362 mReader->requestRefreshConfiguration(changes);
2363 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002364 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002365 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2366 }
2367
arthurhungdcef2dc2020-08-11 14:47:50 +08002368 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2369 const std::string& location, int32_t eventHubId,
2370 Flags<InputDeviceClass> classes) {
2371 InputDeviceIdentifier identifier;
2372 identifier.name = name;
2373 identifier.location = location;
2374 std::shared_ptr<InputDevice> device =
2375 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2376 identifier);
2377 mReader->pushNextDevice(device);
2378 mFakeEventHub->addDevice(eventHubId, name, classes);
2379 mReader->loopOnce();
2380 return device;
2381 }
2382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002383 template <class T, typename... Args>
2384 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002385 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002386 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002388 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002389 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390 }
2391
2392 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002393 int32_t orientation, const std::string& uniqueId,
2394 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002395 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002396 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002397 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2398 }
2399
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002400 void clearViewports() {
2401 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 }
2403
arthurhungdcef2dc2020-08-11 14:47:50 +08002404 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405 RawEvent event;
2406 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002407 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 event.type = type;
2409 event.code = code;
2410 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002411 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002412 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002413 }
2414
2415 static void assertMotionRange(const InputDeviceInfo& info,
2416 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2417 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002418 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2420 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2421 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2422 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2423 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2424 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2425 }
2426
2427 static void assertPointerCoords(const PointerCoords& coords,
2428 float x, float y, float pressure, float size,
2429 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2430 float orientation, float distance) {
2431 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2432 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2433 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2434 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2435 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2436 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2437 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2438 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2439 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2440 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2441 }
2442
Michael Wright17db18e2020-06-26 20:51:44 +01002443 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002445 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446 ASSERT_NEAR(x, actualX, 1);
2447 ASSERT_NEAR(y, actualY, 1);
2448 }
2449};
2450
2451const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002452const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002453const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2455const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002456const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2457 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002458const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459
2460// --- SwitchInputMapperTest ---
2461
2462class SwitchInputMapperTest : public InputMapperTest {
2463protected:
2464};
2465
2466TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002467 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002469 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470}
2471
2472TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002473 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002475 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002476 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002477
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002478 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002479 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480}
2481
2482TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002483 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002484
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002485 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2486 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2487 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2488 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489
2490 NotifySwitchArgs args;
2491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2492 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002493 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2494 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495 args.switchMask);
2496 ASSERT_EQ(uint32_t(0), args.policyFlags);
2497}
2498
2499
2500// --- KeyboardInputMapperTest ---
2501
2502class KeyboardInputMapperTest : public InputMapperTest {
2503protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002504 const std::string UNIQUE_ID = "local:0";
2505
2506 void prepareDisplay(int32_t orientation);
2507
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002508 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002509 int32_t originalKeyCode, int32_t rotatedKeyCode,
2510 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511};
2512
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002513/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2514 * orientation.
2515 */
2516void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002517 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2518 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002519}
2520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002521void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002522 int32_t originalScanCode, int32_t originalKeyCode,
2523 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 NotifyKeyArgs args;
2525
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002526 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2528 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2529 ASSERT_EQ(originalScanCode, args.scanCode);
2530 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002531 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002532
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002533 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2535 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2536 ASSERT_EQ(originalScanCode, args.scanCode);
2537 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002538 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002539}
2540
Michael Wrightd02c5b62014-02-10 15:10:22 -08002541TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002542 KeyboardInputMapper& mapper =
2543 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2544 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002546 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002547}
2548
2549TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2550 const int32_t USAGE_A = 0x070004;
2551 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002552 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2553 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002554 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2555 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2556 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002557
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002558 KeyboardInputMapper& mapper =
2559 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2560 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002561 // Initial metastate to AMETA_NONE.
2562 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2563 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564
2565 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002566 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 NotifyKeyArgs args;
2568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2569 ASSERT_EQ(DEVICE_ID, args.deviceId);
2570 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2571 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2572 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2573 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2574 ASSERT_EQ(KEY_HOME, args.scanCode);
2575 ASSERT_EQ(AMETA_NONE, args.metaState);
2576 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2577 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2578 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2579
2580 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002581 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2583 ASSERT_EQ(DEVICE_ID, args.deviceId);
2584 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2585 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2586 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2587 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2588 ASSERT_EQ(KEY_HOME, args.scanCode);
2589 ASSERT_EQ(AMETA_NONE, args.metaState);
2590 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2591 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2592 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2593
2594 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002595 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2596 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2598 ASSERT_EQ(DEVICE_ID, args.deviceId);
2599 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2600 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2601 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2602 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2603 ASSERT_EQ(0, args.scanCode);
2604 ASSERT_EQ(AMETA_NONE, args.metaState);
2605 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2606 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2607 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2608
2609 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002610 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2611 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2613 ASSERT_EQ(DEVICE_ID, args.deviceId);
2614 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2615 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2616 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2617 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2618 ASSERT_EQ(0, args.scanCode);
2619 ASSERT_EQ(AMETA_NONE, args.metaState);
2620 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2621 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2622 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2623
2624 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002625 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2626 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2628 ASSERT_EQ(DEVICE_ID, args.deviceId);
2629 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2630 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2631 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2632 ASSERT_EQ(0, args.keyCode);
2633 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2634 ASSERT_EQ(AMETA_NONE, args.metaState);
2635 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2636 ASSERT_EQ(0U, args.policyFlags);
2637 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2638
2639 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002640 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2641 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2643 ASSERT_EQ(DEVICE_ID, args.deviceId);
2644 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2645 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2646 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2647 ASSERT_EQ(0, args.keyCode);
2648 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2649 ASSERT_EQ(AMETA_NONE, args.metaState);
2650 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2651 ASSERT_EQ(0U, args.policyFlags);
2652 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2653}
2654
2655TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002656 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2657 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002658 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2659 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2660 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002662 KeyboardInputMapper& mapper =
2663 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2664 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
arthurhungc903df12020-08-11 15:08:42 +08002666 // Initial metastate to AMETA_NONE.
2667 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2668 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669
2670 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002671 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 NotifyKeyArgs args;
2673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2674 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002675 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002676 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677
2678 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002679 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2681 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002682 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683
2684 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002685 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2687 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002688 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689
2690 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002691 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2693 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002694 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002695 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696}
2697
2698TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002699 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2700 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2701 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002704 KeyboardInputMapper& mapper =
2705 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2706 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002708 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2710 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2711 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2712 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2713 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2714 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2715 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2716 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2717}
2718
2719TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002720 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2722 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2723 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002724
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002726 KeyboardInputMapper& mapper =
2727 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2728 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002730 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002731 ASSERT_NO_FATAL_FAILURE(
2732 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2733 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2734 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2735 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2736 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2737 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2738 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002740 clearViewports();
2741 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002742 ASSERT_NO_FATAL_FAILURE(
2743 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2744 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2745 AKEYCODE_DPAD_UP, DISPLAY_ID));
2746 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2747 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2748 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2749 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002750
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002751 clearViewports();
2752 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002753 ASSERT_NO_FATAL_FAILURE(
2754 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2755 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2756 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2757 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2758 AKEYCODE_DPAD_UP, DISPLAY_ID));
2759 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2760 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002762 clearViewports();
2763 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002764 ASSERT_NO_FATAL_FAILURE(
2765 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2766 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2767 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2768 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2769 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2770 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2771 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772
2773 // Special case: if orientation changes while key is down, we still emit the same keycode
2774 // in the key up as we did in the key down.
2775 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002776 clearViewports();
2777 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002778 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2780 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2781 ASSERT_EQ(KEY_UP, args.scanCode);
2782 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2783
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002784 clearViewports();
2785 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002786 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2788 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2789 ASSERT_EQ(KEY_UP, args.scanCode);
2790 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2791}
2792
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002793TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2794 // If the keyboard is not orientation aware,
2795 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002796 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002797
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002798 KeyboardInputMapper& mapper =
2799 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2800 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002801 NotifyKeyArgs args;
2802
2803 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002804 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002806 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2808 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2809
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002810 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002811 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002813 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2815 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2816}
2817
2818TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2819 // If the keyboard is orientation aware,
2820 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002821 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002822
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002823 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002824 KeyboardInputMapper& mapper =
2825 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2826 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002827 NotifyKeyArgs args;
2828
2829 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2830 // ^--- already checked by the previous test
2831
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002832 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002833 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002834 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002836 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2838 ASSERT_EQ(DISPLAY_ID, args.displayId);
2839
2840 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002841 clearViewports();
2842 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002843 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002844 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002846 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2848 ASSERT_EQ(newDisplayId, args.displayId);
2849}
2850
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002852 KeyboardInputMapper& mapper =
2853 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2854 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002856 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002857 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002859 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002860 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861}
2862
2863TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002864 KeyboardInputMapper& mapper =
2865 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2866 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002868 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002869 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002870
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002871 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002872 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002873}
2874
2875TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002876 KeyboardInputMapper& mapper =
2877 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2878 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002880 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881
2882 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2883 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002884 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885 ASSERT_TRUE(flags[0]);
2886 ASSERT_FALSE(flags[1]);
2887}
2888
2889TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002890 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2891 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2892 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2893 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2894 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2895 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002896
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002897 KeyboardInputMapper& mapper =
2898 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2899 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07002900 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08002901 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2902 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903
2904 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002905 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2906 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2907 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002908
2909 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002910 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2911 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002912 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2913 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2914 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002915 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916
2917 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002918 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2919 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002920 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2921 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2922 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002923 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924
2925 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002926 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2927 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002928 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2929 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2930 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002931 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932
2933 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002934 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2935 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002936 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2937 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2938 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002939 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940
2941 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002942 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2943 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002944 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2945 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2946 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002947 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948
2949 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002950 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2951 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002952 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2953 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2954 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002955 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956}
2957
Chris Yea52ade12020-08-27 16:49:20 -07002958TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
2959 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
2960 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
2961 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
2962 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
2963
2964 KeyboardInputMapper& mapper =
2965 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2966 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
2967
2968 // Initial metastate should be AMETA_NONE as no meta keys added.
2969 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2970 // Meta state should be AMETA_NONE after reset
2971 mapper.reset(ARBITRARY_TIME);
2972 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2973 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
2974 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
2975 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2976
2977 NotifyKeyArgs args;
2978 // Press button "A"
2979 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
2980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2981 ASSERT_EQ(AMETA_NONE, args.metaState);
2982 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2983 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2984 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2985
2986 // Button up.
2987 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
2988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(AMETA_NONE, args.metaState);
2990 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2991 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2992 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2993}
2994
Arthur Hung2c9a3342019-07-23 14:18:59 +08002995TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2996 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002997 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2998 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2999 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3000 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003001
3002 // keyboard 2.
3003 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003004 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003005 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003006 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003007 std::shared_ptr<InputDevice> device2 =
3008 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3009 Flags<InputDeviceClass>(0));
3010
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003011 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3012 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3013 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3014 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003015
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003016 KeyboardInputMapper& mapper =
3017 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3018 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003020 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003021 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003022 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003023 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3024 device2->reset(ARBITRARY_TIME);
3025
3026 // Prepared displays and associated info.
3027 constexpr uint8_t hdmi1 = 0;
3028 constexpr uint8_t hdmi2 = 1;
3029 const std::string SECONDARY_UNIQUE_ID = "local:1";
3030
3031 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3032 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3033
3034 // No associated display viewport found, should disable the device.
3035 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3036 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3037 ASSERT_FALSE(device2->isEnabled());
3038
3039 // Prepare second display.
3040 constexpr int32_t newDisplayId = 2;
3041 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003042 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003043 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003044 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003045 // Default device will reconfigure above, need additional reconfiguration for another device.
3046 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3047 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3048
3049 // Device should be enabled after the associated display is found.
3050 ASSERT_TRUE(mDevice->isEnabled());
3051 ASSERT_TRUE(device2->isEnabled());
3052
3053 // Test pad key events
3054 ASSERT_NO_FATAL_FAILURE(
3055 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3056 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3057 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3058 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3059 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3060 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3061 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3062
3063 ASSERT_NO_FATAL_FAILURE(
3064 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3065 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3066 AKEYCODE_DPAD_RIGHT, newDisplayId));
3067 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3068 AKEYCODE_DPAD_DOWN, newDisplayId));
3069 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3070 AKEYCODE_DPAD_LEFT, newDisplayId));
3071}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072
arthurhungc903df12020-08-11 15:08:42 +08003073TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3074 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3075 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3076 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3077 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3078 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3079 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3080
3081 KeyboardInputMapper& mapper =
3082 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3083 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3084 // Initial metastate to AMETA_NONE.
3085 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3086 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3087
3088 // Initialization should have turned all of the lights off.
3089 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3090 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3091 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3092
3093 // Toggle caps lock on.
3094 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3095 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3096 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3097 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3098
3099 // Toggle num lock on.
3100 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3101 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3102 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3103 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3104
3105 // Toggle scroll lock on.
3106 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3107 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3108 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3109 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3110
3111 mFakeEventHub->removeDevice(EVENTHUB_ID);
3112 mReader->loopOnce();
3113
3114 // keyboard 2 should default toggle keys.
3115 const std::string USB2 = "USB2";
3116 const std::string DEVICE_NAME2 = "KEYBOARD2";
3117 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3118 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3119 std::shared_ptr<InputDevice> device2 =
3120 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3121 Flags<InputDeviceClass>(0));
3122 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3123 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3124 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3125 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3126 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3127 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3128
arthurhung6fe95782020-10-05 22:41:16 +08003129 KeyboardInputMapper& mapper2 =
3130 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3131 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003132 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3133 device2->reset(ARBITRARY_TIME);
3134
3135 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3136 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3137 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003138 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3139 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003140}
3141
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003142// --- KeyboardInputMapperTest_ExternalDevice ---
3143
3144class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3145protected:
Chris Yea52ade12020-08-27 16:49:20 -07003146 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003147};
3148
3149TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003150 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3151 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003152
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003153 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3154 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3155 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3156 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003158 KeyboardInputMapper& mapper =
3159 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3160 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003161
3162 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3163 NotifyKeyArgs args;
3164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3165 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3166
3167 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3169 ASSERT_EQ(uint32_t(0), args.policyFlags);
3170
3171 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3173 ASSERT_EQ(uint32_t(0), args.policyFlags);
3174
3175 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3177 ASSERT_EQ(uint32_t(0), args.policyFlags);
3178
3179 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3181 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3182
3183 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3185 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3186}
3187
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003188TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003189 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003190
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003191 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3193 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003194
Powei Fengd041c5d2019-05-03 17:11:33 -07003195 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003196 KeyboardInputMapper& mapper =
3197 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3198 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003199
3200 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3201 NotifyKeyArgs args;
3202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3203 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3204
3205 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3207 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3208
3209 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3211 ASSERT_EQ(uint32_t(0), args.policyFlags);
3212
3213 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3215 ASSERT_EQ(uint32_t(0), args.policyFlags);
3216
3217 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3219 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3220
3221 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3223 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3224}
3225
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226// --- CursorInputMapperTest ---
3227
3228class CursorInputMapperTest : public InputMapperTest {
3229protected:
3230 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3231
Michael Wright17db18e2020-06-26 20:51:44 +01003232 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233
Chris Yea52ade12020-08-27 16:49:20 -07003234 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235 InputMapperTest::SetUp();
3236
Michael Wright17db18e2020-06-26 20:51:44 +01003237 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003238 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239 }
3240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003241 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3242 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003243
3244 void prepareDisplay(int32_t orientation) {
3245 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003246 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003247 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3248 orientation, uniqueId, NO_PORT, viewportType);
3249 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250};
3251
3252const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3253
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003254void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3255 int32_t originalY, int32_t rotatedX,
3256 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257 NotifyMotionArgs args;
3258
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003259 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3260 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3261 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3265 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3266 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3267 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3268}
3269
3270TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003274 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275}
3276
3277TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003279 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282}
3283
3284TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003286 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287
3288 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003289 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
3291 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003292 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3293 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3295 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3296
3297 // When the bounds are set, then there should be a valid motion range.
3298 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3299
3300 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003301 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302
3303 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3304 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3305 1, 800 - 1, 0.0f, 0.0f));
3306 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3307 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3308 2, 480 - 1, 0.0f, 0.0f));
3309 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3310 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3311 0.0f, 1.0f, 0.0f, 0.0f));
3312}
3313
3314TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003316 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317
3318 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320
3321 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3322 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3323 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3324 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3325 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3326 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3327 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3328 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3329 0.0f, 1.0f, 0.0f, 0.0f));
3330}
3331
3332TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
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
arthurhungdcef2dc2020-08-11 14:47:50 +08003336 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337
3338 NotifyMotionArgs args;
3339
3340 // Button press.
3341 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003342 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3343 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3345 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3346 ASSERT_EQ(DEVICE_ID, args.deviceId);
3347 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3348 ASSERT_EQ(uint32_t(0), args.policyFlags);
3349 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3350 ASSERT_EQ(0, args.flags);
3351 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3352 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3353 ASSERT_EQ(0, args.edgeFlags);
3354 ASSERT_EQ(uint32_t(1), args.pointerCount);
3355 ASSERT_EQ(0, args.pointerProperties[0].id);
3356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3358 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3359 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3360 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3361 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3362
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3364 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3365 ASSERT_EQ(DEVICE_ID, args.deviceId);
3366 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3367 ASSERT_EQ(uint32_t(0), args.policyFlags);
3368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3369 ASSERT_EQ(0, args.flags);
3370 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3371 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3372 ASSERT_EQ(0, args.edgeFlags);
3373 ASSERT_EQ(uint32_t(1), args.pointerCount);
3374 ASSERT_EQ(0, args.pointerProperties[0].id);
3375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3377 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3378 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3379 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3380 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3381
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003383 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3384 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3386 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3387 ASSERT_EQ(DEVICE_ID, args.deviceId);
3388 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3389 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3391 ASSERT_EQ(0, args.flags);
3392 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3393 ASSERT_EQ(0, args.buttonState);
3394 ASSERT_EQ(0, args.edgeFlags);
3395 ASSERT_EQ(uint32_t(1), args.pointerCount);
3396 ASSERT_EQ(0, args.pointerProperties[0].id);
3397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3399 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3400 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3401 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3402 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3403
3404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3405 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3406 ASSERT_EQ(DEVICE_ID, args.deviceId);
3407 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3408 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3410 ASSERT_EQ(0, args.flags);
3411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3412 ASSERT_EQ(0, args.buttonState);
3413 ASSERT_EQ(0, args.edgeFlags);
3414 ASSERT_EQ(uint32_t(1), args.pointerCount);
3415 ASSERT_EQ(0, args.pointerProperties[0].id);
3416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3418 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3419 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3420 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3421 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3422}
3423
3424TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003426 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427
3428 NotifyMotionArgs args;
3429
3430 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003431 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3432 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3436 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3437
3438 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003439 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3440 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3444 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3445}
3446
3447TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003449 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450
3451 NotifyMotionArgs args;
3452
3453 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003454 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3455 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3457 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3459 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3460
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3464 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3465
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003467 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3468 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3472 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3473
3474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3477 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3478}
3479
3480TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
3484 NotifyMotionArgs args;
3485
3486 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003487 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3488 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3489 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3490 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3492 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3494 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3495 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3496
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3500 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3501 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3502
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003504 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3505 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3506 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3510 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3511 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3512
3513 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003514 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3515 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3519 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3520
3521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3524 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3525}
3526
3527TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003531 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3539 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3540}
3541
3542TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543 addConfigurationProperty("cursor.mode", "navigation");
3544 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003545 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003547 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3555 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3556
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003557 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3559 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3564 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3565 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3566
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003567 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3569 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3570 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3571 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3575 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3576
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003577 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3579 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3580 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3581 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3582 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3583 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3584 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3585 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3586}
3587
3588TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003590 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591
3592 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3593 mFakePointerController->setPosition(100, 200);
3594 mFakePointerController->setButtonState(0);
3595
3596 NotifyMotionArgs motionArgs;
3597 NotifyKeyArgs keyArgs;
3598
3599 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003600 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3601 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3603 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3604 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3605 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3607 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3608
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3610 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3611 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3612 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3614 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3615
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003616 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3617 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_EQ(0, motionArgs.buttonState);
3621 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3623 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3624
3625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003626 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_EQ(0, motionArgs.buttonState);
3628 ASSERT_EQ(0, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003634 ASSERT_EQ(0, motionArgs.buttonState);
3635 ASSERT_EQ(0, 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
3639 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003640 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3641 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3642 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3644 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3645 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3646 motionArgs.buttonState);
3647 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3648 mFakePointerController->getButtonState());
3649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3650 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3651
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3653 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3654 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3655 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3656 mFakePointerController->getButtonState());
3657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3658 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3659
3660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3662 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3663 motionArgs.buttonState);
3664 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3665 mFakePointerController->getButtonState());
3666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3667 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3668
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003669 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3670 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003672 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3674 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3676 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3677
3678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003680 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3681 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3683 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3684
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003685 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3686 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3689 ASSERT_EQ(0, motionArgs.buttonState);
3690 ASSERT_EQ(0, mFakePointerController->getButtonState());
3691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3692 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 -08003693 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3694 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003695
3696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697 ASSERT_EQ(0, motionArgs.buttonState);
3698 ASSERT_EQ(0, mFakePointerController->getButtonState());
3699 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3701 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003702
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3704 ASSERT_EQ(0, motionArgs.buttonState);
3705 ASSERT_EQ(0, mFakePointerController->getButtonState());
3706 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3708 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3709
3710 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003711 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3712 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3714 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3715 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003716
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003718 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3720 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3722 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3723
3724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3726 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3727 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3729 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3730
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003731 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3732 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003734 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735 ASSERT_EQ(0, motionArgs.buttonState);
3736 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3738 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3739
3740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003742 ASSERT_EQ(0, motionArgs.buttonState);
3743 ASSERT_EQ(0, mFakePointerController->getButtonState());
3744
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3746 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3748 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3749 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3750
3751 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003752 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3753 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3755 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3756 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003757
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3761 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3763 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3764
3765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3766 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3767 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3768 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3770 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3771
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003772 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3773 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003775 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776 ASSERT_EQ(0, motionArgs.buttonState);
3777 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3779 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 -08003780
3781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3783 ASSERT_EQ(0, motionArgs.buttonState);
3784 ASSERT_EQ(0, mFakePointerController->getButtonState());
3785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3786 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3787
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3789 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3790 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3791
3792 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003793 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3794 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3796 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3797 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003798
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003800 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3802 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3804 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3805
3806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3807 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3808 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3809 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3811 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3812
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003813 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3814 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003816 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817 ASSERT_EQ(0, motionArgs.buttonState);
3818 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3820 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 -08003821
3822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3824 ASSERT_EQ(0, motionArgs.buttonState);
3825 ASSERT_EQ(0, mFakePointerController->getButtonState());
3826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3827 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3828
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3830 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3831 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3832
3833 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003834 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3835 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3837 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3838 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003839
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003841 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3843 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3845 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3846
3847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3848 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3849 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3850 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3852 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3853
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003854 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3855 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003857 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 ASSERT_EQ(0, motionArgs.buttonState);
3859 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3861 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 -08003862
3863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3864 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3865 ASSERT_EQ(0, motionArgs.buttonState);
3866 ASSERT_EQ(0, mFakePointerController->getButtonState());
3867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3868 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3869
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3871 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3872 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3873}
3874
3875TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003877 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878
3879 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3880 mFakePointerController->setPosition(100, 200);
3881 mFakePointerController->setButtonState(0);
3882
3883 NotifyMotionArgs args;
3884
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003885 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3886 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3887 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003889 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3890 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3892 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 +01003893 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003894}
3895
3896TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003897 addConfigurationProperty("cursor.mode", "pointer");
3898 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003899 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003900
3901 NotifyDeviceResetArgs resetArgs;
3902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3903 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3904 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3905
3906 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3907 mFakePointerController->setPosition(100, 200);
3908 mFakePointerController->setButtonState(0);
3909
3910 NotifyMotionArgs args;
3911
3912 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003913 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3914 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3915 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3917 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3920 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 +01003921 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003922
3923 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003924 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3925 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3927 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3928 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3930 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3932 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3935 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3936
3937 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003938 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3939 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3941 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3944 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3946 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3947 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3949 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3950
3951 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003952 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3953 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3954 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3956 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3959 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 +01003960 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003961
3962 // Disable pointer capture and check that the device generation got bumped
3963 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08003964 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003965 mFakePolicy->setPointerCapture(false);
3966 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08003967 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003968
3969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3970 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3971 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3972
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003973 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3974 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3975 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3977 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3980 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 +01003981 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982}
3983
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003984TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003985 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003986
Garfield Tan888a6a42020-01-09 11:39:16 -08003987 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003988 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003989 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3990 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003991 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003992 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3993 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3994
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003995 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3996 mFakePointerController->setPosition(100, 200);
3997 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003998
3999 NotifyMotionArgs args;
4000 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4001 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4002 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4004 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4005 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4007 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 +01004008 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004009 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4010}
4011
Michael Wrightd02c5b62014-02-10 15:10:22 -08004012// --- TouchInputMapperTest ---
4013
4014class TouchInputMapperTest : public InputMapperTest {
4015protected:
4016 static const int32_t RAW_X_MIN;
4017 static const int32_t RAW_X_MAX;
4018 static const int32_t RAW_Y_MIN;
4019 static const int32_t RAW_Y_MAX;
4020 static const int32_t RAW_TOUCH_MIN;
4021 static const int32_t RAW_TOUCH_MAX;
4022 static const int32_t RAW_TOOL_MIN;
4023 static const int32_t RAW_TOOL_MAX;
4024 static const int32_t RAW_PRESSURE_MIN;
4025 static const int32_t RAW_PRESSURE_MAX;
4026 static const int32_t RAW_ORIENTATION_MIN;
4027 static const int32_t RAW_ORIENTATION_MAX;
4028 static const int32_t RAW_DISTANCE_MIN;
4029 static const int32_t RAW_DISTANCE_MAX;
4030 static const int32_t RAW_TILT_MIN;
4031 static const int32_t RAW_TILT_MAX;
4032 static const int32_t RAW_ID_MIN;
4033 static const int32_t RAW_ID_MAX;
4034 static const int32_t RAW_SLOT_MIN;
4035 static const int32_t RAW_SLOT_MAX;
4036 static const float X_PRECISION;
4037 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004038 static const float X_PRECISION_VIRTUAL;
4039 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040
4041 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004042 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043
4044 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4045
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004046 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004047 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004048
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 enum Axes {
4050 POSITION = 1 << 0,
4051 TOUCH = 1 << 1,
4052 TOOL = 1 << 2,
4053 PRESSURE = 1 << 3,
4054 ORIENTATION = 1 << 4,
4055 MINOR = 1 << 5,
4056 ID = 1 << 6,
4057 DISTANCE = 1 << 7,
4058 TILT = 1 << 8,
4059 SLOT = 1 << 9,
4060 TOOL_TYPE = 1 << 10,
4061 };
4062
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004063 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4064 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004065 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004067 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 int32_t toRawX(float displayX);
4069 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004070 float toCookedX(float rawX, float rawY);
4071 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004073 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004075 float toDisplayY(int32_t rawY, int32_t displayHeight);
4076
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077};
4078
4079const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4080const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4081const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4082const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4083const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4084const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4085const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4086const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004087const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4088const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4090const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4091const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4092const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4093const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4094const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4095const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4096const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4097const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4098const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4099const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4100const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004101const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4102 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4103const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4104 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004105const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4106 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107
4108const float TouchInputMapperTest::GEOMETRIC_SCALE =
4109 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4110 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4111
4112const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4113 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4114 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4115};
4116
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004117void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004118 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4119 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004120}
4121
4122void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4123 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4124 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125}
4126
Santos Cordonfa5cf462017-04-05 10:37:00 -07004127void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004128 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4129 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4130 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004131}
4132
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004134 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4135 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138}
4139
Jason Gerecke489fda82012-09-07 17:19:40 -07004140void TouchInputMapperTest::prepareLocationCalibration() {
4141 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4142}
4143
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144int32_t TouchInputMapperTest::toRawX(float displayX) {
4145 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4146}
4147
4148int32_t TouchInputMapperTest::toRawY(float displayY) {
4149 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4150}
4151
Jason Gerecke489fda82012-09-07 17:19:40 -07004152float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4153 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4154 return rawX;
4155}
4156
4157float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4158 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4159 return rawY;
4160}
4161
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004163 return toDisplayX(rawX, DISPLAY_WIDTH);
4164}
4165
4166float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4167 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168}
4169
4170float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004171 return toDisplayY(rawY, DISPLAY_HEIGHT);
4172}
4173
4174float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4175 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176}
4177
4178
4179// --- SingleTouchInputMapperTest ---
4180
4181class SingleTouchInputMapperTest : public TouchInputMapperTest {
4182protected:
4183 void prepareButtons();
4184 void prepareAxes(int axes);
4185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004186 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4187 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4188 void processUp(SingleTouchInputMapper& mappery);
4189 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4190 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4191 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4192 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4193 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4194 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195};
4196
4197void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004198 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199}
4200
4201void SingleTouchInputMapperTest::prepareAxes(int axes) {
4202 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4204 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 }
4206 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004207 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4208 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 }
4210 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004211 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4212 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 }
4214 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004215 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4216 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 }
4218 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4220 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221 }
4222}
4223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004225 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4226 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4227 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228}
4229
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004230void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004231 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4232 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233}
4234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004235void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004236 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237}
4238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004240 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241}
4242
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004243void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4244 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004245 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246}
4247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004248void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004249 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250}
4251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004252void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4253 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004254 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4255 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256}
4257
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004258void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4259 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004260 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261}
4262
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004263void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004264 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265}
4266
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 prepareButtons();
4269 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004270 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004272 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273}
4274
4275TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004276 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4277 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 prepareButtons();
4279 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004280 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004282 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283}
4284
4285TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 prepareButtons();
4287 prepareAxes(POSITION);
4288 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004289 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004291 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292}
4293
4294TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 prepareButtons();
4296 prepareAxes(POSITION);
4297 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004298 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004300 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301}
4302
4303TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 addConfigurationProperty("touch.deviceType", "touchScreen");
4305 prepareDisplay(DISPLAY_ORIENTATION_0);
4306 prepareButtons();
4307 prepareAxes(POSITION);
4308 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004309 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310
4311 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004312 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313
4314 // Virtual key is down.
4315 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4316 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4317 processDown(mapper, x, y);
4318 processSync(mapper);
4319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004321 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322
4323 // Virtual key is up.
4324 processUp(mapper);
4325 processSync(mapper);
4326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4327
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004328 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329}
4330
4331TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 addConfigurationProperty("touch.deviceType", "touchScreen");
4333 prepareDisplay(DISPLAY_ORIENTATION_0);
4334 prepareButtons();
4335 prepareAxes(POSITION);
4336 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004337 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338
4339 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004340 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341
4342 // Virtual key is down.
4343 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4344 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4345 processDown(mapper, x, y);
4346 processSync(mapper);
4347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4348
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004349 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
4351 // Virtual key is up.
4352 processUp(mapper);
4353 processSync(mapper);
4354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4355
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004356 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357}
4358
4359TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 addConfigurationProperty("touch.deviceType", "touchScreen");
4361 prepareDisplay(DISPLAY_ORIENTATION_0);
4362 prepareButtons();
4363 prepareAxes(POSITION);
4364 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004365 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366
4367 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4368 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004369 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_TRUE(flags[0]);
4371 ASSERT_FALSE(flags[1]);
4372}
4373
4374TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 addConfigurationProperty("touch.deviceType", "touchScreen");
4376 prepareDisplay(DISPLAY_ORIENTATION_0);
4377 prepareButtons();
4378 prepareAxes(POSITION);
4379 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004380 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381
arthurhungdcef2dc2020-08-11 14:47:50 +08004382 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383
4384 NotifyKeyArgs args;
4385
4386 // Press virtual key.
4387 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4388 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4389 processDown(mapper, x, y);
4390 processSync(mapper);
4391
4392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4393 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4394 ASSERT_EQ(DEVICE_ID, args.deviceId);
4395 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4396 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4397 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4398 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4399 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4400 ASSERT_EQ(KEY_HOME, args.scanCode);
4401 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4402 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4403
4404 // Release virtual key.
4405 processUp(mapper);
4406 processSync(mapper);
4407
4408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4409 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4410 ASSERT_EQ(DEVICE_ID, args.deviceId);
4411 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4412 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4413 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4414 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4415 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4416 ASSERT_EQ(KEY_HOME, args.scanCode);
4417 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4418 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4419
4420 // Should not have sent any motions.
4421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4422}
4423
4424TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 addConfigurationProperty("touch.deviceType", "touchScreen");
4426 prepareDisplay(DISPLAY_ORIENTATION_0);
4427 prepareButtons();
4428 prepareAxes(POSITION);
4429 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004430 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431
arthurhungdcef2dc2020-08-11 14:47:50 +08004432 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433
4434 NotifyKeyArgs keyArgs;
4435
4436 // Press virtual key.
4437 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4438 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4439 processDown(mapper, x, y);
4440 processSync(mapper);
4441
4442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4443 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4444 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4445 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4446 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4447 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4448 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4449 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4450 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4451 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4452 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4453
4454 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4455 // into the display area.
4456 y -= 100;
4457 processMove(mapper, x, y);
4458 processSync(mapper);
4459
4460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4461 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4462 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4463 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4464 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4465 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4466 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4467 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4468 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4469 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4470 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4471 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4472
4473 NotifyMotionArgs motionArgs;
4474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4476 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4478 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4480 ASSERT_EQ(0, motionArgs.flags);
4481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4482 ASSERT_EQ(0, motionArgs.buttonState);
4483 ASSERT_EQ(0, motionArgs.edgeFlags);
4484 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4488 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4489 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4490 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4491 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4492
4493 // Keep moving out of bounds. Should generate a pointer move.
4494 y -= 50;
4495 processMove(mapper, x, y);
4496 processSync(mapper);
4497
4498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4500 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4501 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4502 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4504 ASSERT_EQ(0, motionArgs.flags);
4505 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4506 ASSERT_EQ(0, motionArgs.buttonState);
4507 ASSERT_EQ(0, motionArgs.edgeFlags);
4508 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4509 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4512 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4513 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4514 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4515 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4516
4517 // Release out of bounds. Should generate a pointer up.
4518 processUp(mapper);
4519 processSync(mapper);
4520
4521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4523 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4524 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4525 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4526 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4527 ASSERT_EQ(0, motionArgs.flags);
4528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4529 ASSERT_EQ(0, motionArgs.buttonState);
4530 ASSERT_EQ(0, motionArgs.edgeFlags);
4531 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4535 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4536 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4537 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4538 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4539
4540 // Should not have sent any more keys or motions.
4541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4543}
4544
4545TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546 addConfigurationProperty("touch.deviceType", "touchScreen");
4547 prepareDisplay(DISPLAY_ORIENTATION_0);
4548 prepareButtons();
4549 prepareAxes(POSITION);
4550 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004551 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552
arthurhungdcef2dc2020-08-11 14:47:50 +08004553 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554
4555 NotifyMotionArgs motionArgs;
4556
4557 // Initially go down out of bounds.
4558 int32_t x = -10;
4559 int32_t y = -10;
4560 processDown(mapper, x, y);
4561 processSync(mapper);
4562
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4564
4565 // Move into the display area. Should generate a pointer down.
4566 x = 50;
4567 y = 75;
4568 processMove(mapper, x, y);
4569 processSync(mapper);
4570
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4572 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4573 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4574 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4575 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4576 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4577 ASSERT_EQ(0, motionArgs.flags);
4578 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4579 ASSERT_EQ(0, motionArgs.buttonState);
4580 ASSERT_EQ(0, motionArgs.edgeFlags);
4581 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4582 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4585 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4586 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4587 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4588 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4589
4590 // Release. Should generate a pointer up.
4591 processUp(mapper);
4592 processSync(mapper);
4593
4594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4595 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4596 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4597 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4598 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4600 ASSERT_EQ(0, motionArgs.flags);
4601 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4602 ASSERT_EQ(0, motionArgs.buttonState);
4603 ASSERT_EQ(0, motionArgs.edgeFlags);
4604 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4608 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4609 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4610 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4612
4613 // Should not have sent any more keys or motions.
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4616}
4617
Santos Cordonfa5cf462017-04-05 10:37:00 -07004618TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004619 addConfigurationProperty("touch.deviceType", "touchScreen");
4620 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4621
4622 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4623 prepareButtons();
4624 prepareAxes(POSITION);
4625 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004626 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004627
arthurhungdcef2dc2020-08-11 14:47:50 +08004628 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004629
4630 NotifyMotionArgs motionArgs;
4631
4632 // Down.
4633 int32_t x = 100;
4634 int32_t y = 125;
4635 processDown(mapper, x, y);
4636 processSync(mapper);
4637
4638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4639 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4640 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4641 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4642 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4643 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4644 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4645 ASSERT_EQ(0, motionArgs.flags);
4646 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4647 ASSERT_EQ(0, motionArgs.buttonState);
4648 ASSERT_EQ(0, motionArgs.edgeFlags);
4649 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4653 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4654 1, 0, 0, 0, 0, 0, 0, 0));
4655 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4656 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4657 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4658
4659 // Move.
4660 x += 50;
4661 y += 75;
4662 processMove(mapper, x, y);
4663 processSync(mapper);
4664
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4666 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4667 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4668 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4669 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4670 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4672 ASSERT_EQ(0, motionArgs.flags);
4673 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4674 ASSERT_EQ(0, motionArgs.buttonState);
4675 ASSERT_EQ(0, motionArgs.edgeFlags);
4676 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4677 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4680 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4681 1, 0, 0, 0, 0, 0, 0, 0));
4682 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4683 ASSERT_NEAR(Y_PRECISION_VIRTUAL, 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(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4694 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4695 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4696 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4697 ASSERT_EQ(0, motionArgs.flags);
4698 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4699 ASSERT_EQ(0, motionArgs.buttonState);
4700 ASSERT_EQ(0, motionArgs.edgeFlags);
4701 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4702 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4705 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4706 1, 0, 0, 0, 0, 0, 0, 0));
4707 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4708 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4709 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4710
4711 // Should not have sent any more keys or motions.
4712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4714}
4715
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 addConfigurationProperty("touch.deviceType", "touchScreen");
4718 prepareDisplay(DISPLAY_ORIENTATION_0);
4719 prepareButtons();
4720 prepareAxes(POSITION);
4721 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004722 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723
arthurhungdcef2dc2020-08-11 14:47:50 +08004724 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725
4726 NotifyMotionArgs motionArgs;
4727
4728 // Down.
4729 int32_t x = 100;
4730 int32_t y = 125;
4731 processDown(mapper, x, y);
4732 processSync(mapper);
4733
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4736 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4737 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4738 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4739 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4740 ASSERT_EQ(0, motionArgs.flags);
4741 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4742 ASSERT_EQ(0, motionArgs.buttonState);
4743 ASSERT_EQ(0, motionArgs.edgeFlags);
4744 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4745 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4748 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4749 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4750 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4751 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4752
4753 // Move.
4754 x += 50;
4755 y += 75;
4756 processMove(mapper, x, y);
4757 processSync(mapper);
4758
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4761 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4762 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4763 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4765 ASSERT_EQ(0, motionArgs.flags);
4766 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4767 ASSERT_EQ(0, motionArgs.buttonState);
4768 ASSERT_EQ(0, motionArgs.edgeFlags);
4769 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4770 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4773 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4774 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4775 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4776 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4777
4778 // Up.
4779 processUp(mapper);
4780 processSync(mapper);
4781
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4783 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4784 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4785 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4786 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4788 ASSERT_EQ(0, motionArgs.flags);
4789 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4790 ASSERT_EQ(0, motionArgs.buttonState);
4791 ASSERT_EQ(0, motionArgs.edgeFlags);
4792 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4793 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4796 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4797 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4798 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4799 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4800
4801 // Should not have sent any more keys or motions.
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4804}
4805
4806TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807 addConfigurationProperty("touch.deviceType", "touchScreen");
4808 prepareButtons();
4809 prepareAxes(POSITION);
4810 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004811 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812
4813 NotifyMotionArgs args;
4814
4815 // Rotation 90.
4816 prepareDisplay(DISPLAY_ORIENTATION_90);
4817 processDown(mapper, toRawX(50), toRawY(75));
4818 processSync(mapper);
4819
4820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4821 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4822 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4823
4824 processUp(mapper);
4825 processSync(mapper);
4826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4827}
4828
4829TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 addConfigurationProperty("touch.deviceType", "touchScreen");
4831 prepareButtons();
4832 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004833 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834
4835 NotifyMotionArgs args;
4836
4837 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004838 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 prepareDisplay(DISPLAY_ORIENTATION_0);
4840 processDown(mapper, toRawX(50), toRawY(75));
4841 processSync(mapper);
4842
4843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4844 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4845 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4846
4847 processUp(mapper);
4848 processSync(mapper);
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4850
4851 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004852 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 prepareDisplay(DISPLAY_ORIENTATION_90);
4854 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4855 processSync(mapper);
4856
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4858 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4859 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4860
4861 processUp(mapper);
4862 processSync(mapper);
4863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4864
4865 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004866 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867 prepareDisplay(DISPLAY_ORIENTATION_180);
4868 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4869 processSync(mapper);
4870
4871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4872 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4873 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4874
4875 processUp(mapper);
4876 processSync(mapper);
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4878
4879 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004880 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 prepareDisplay(DISPLAY_ORIENTATION_270);
4882 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4883 processSync(mapper);
4884
4885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4886 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4887 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4888
4889 processUp(mapper);
4890 processSync(mapper);
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4892}
4893
4894TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895 addConfigurationProperty("touch.deviceType", "touchScreen");
4896 prepareDisplay(DISPLAY_ORIENTATION_0);
4897 prepareButtons();
4898 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004899 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900
4901 // These calculations are based on the input device calibration documentation.
4902 int32_t rawX = 100;
4903 int32_t rawY = 200;
4904 int32_t rawPressure = 10;
4905 int32_t rawToolMajor = 12;
4906 int32_t rawDistance = 2;
4907 int32_t rawTiltX = 30;
4908 int32_t rawTiltY = 110;
4909
4910 float x = toDisplayX(rawX);
4911 float y = toDisplayY(rawY);
4912 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4913 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4914 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4915 float distance = float(rawDistance);
4916
4917 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4918 float tiltScale = M_PI / 180;
4919 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4920 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4921 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4922 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4923
4924 processDown(mapper, rawX, rawY);
4925 processPressure(mapper, rawPressure);
4926 processToolMajor(mapper, rawToolMajor);
4927 processDistance(mapper, rawDistance);
4928 processTilt(mapper, rawTiltX, rawTiltY);
4929 processSync(mapper);
4930
4931 NotifyMotionArgs args;
4932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4934 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4935 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4936}
4937
Jason Gerecke489fda82012-09-07 17:19:40 -07004938TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004939 addConfigurationProperty("touch.deviceType", "touchScreen");
4940 prepareDisplay(DISPLAY_ORIENTATION_0);
4941 prepareLocationCalibration();
4942 prepareButtons();
4943 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004944 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004945
4946 int32_t rawX = 100;
4947 int32_t rawY = 200;
4948
4949 float x = toDisplayX(toCookedX(rawX, rawY));
4950 float y = toDisplayY(toCookedY(rawX, rawY));
4951
4952 processDown(mapper, rawX, rawY);
4953 processSync(mapper);
4954
4955 NotifyMotionArgs args;
4956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4958 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4959}
4960
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 addConfigurationProperty("touch.deviceType", "touchScreen");
4963 prepareDisplay(DISPLAY_ORIENTATION_0);
4964 prepareButtons();
4965 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004966 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967
4968 NotifyMotionArgs motionArgs;
4969 NotifyKeyArgs keyArgs;
4970
4971 processDown(mapper, 100, 200);
4972 processSync(mapper);
4973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4974 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4975 ASSERT_EQ(0, motionArgs.buttonState);
4976
4977 // press BTN_LEFT, release BTN_LEFT
4978 processKey(mapper, BTN_LEFT, 1);
4979 processSync(mapper);
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4982 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4983
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4985 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4986 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4987
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988 processKey(mapper, BTN_LEFT, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997
4998 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4999 processKey(mapper, BTN_RIGHT, 1);
5000 processKey(mapper, BTN_MIDDLE, 1);
5001 processSync(mapper);
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5004 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5005 motionArgs.buttonState);
5006
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5008 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5009 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5010
5011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5013 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5014 motionArgs.buttonState);
5015
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016 processKey(mapper, BTN_RIGHT, 0);
5017 processSync(mapper);
5018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005019 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005021
5022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005024 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025
5026 processKey(mapper, BTN_MIDDLE, 0);
5027 processSync(mapper);
5028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005031
5032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005034 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035
5036 // press BTN_BACK, release BTN_BACK
5037 processKey(mapper, BTN_BACK, 1);
5038 processSync(mapper);
5039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5040 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5041 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005042
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005045 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5046
5047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5049 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050
5051 processKey(mapper, BTN_BACK, 0);
5052 processSync(mapper);
5053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005056
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005059 ASSERT_EQ(0, motionArgs.buttonState);
5060
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5062 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5063 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5064
5065 // press BTN_SIDE, release BTN_SIDE
5066 processKey(mapper, BTN_SIDE, 1);
5067 processSync(mapper);
5068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5069 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5070 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005071
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005074 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5075
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5078 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079
5080 processKey(mapper, BTN_SIDE, 0);
5081 processSync(mapper);
5082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005085
5086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005088 ASSERT_EQ(0, motionArgs.buttonState);
5089
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5091 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5092 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5093
5094 // press BTN_FORWARD, release BTN_FORWARD
5095 processKey(mapper, BTN_FORWARD, 1);
5096 processSync(mapper);
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5098 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5099 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005100
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005103 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5104
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5107 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108
5109 processKey(mapper, BTN_FORWARD, 0);
5110 processSync(mapper);
5111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005112 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005114
5115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005117 ASSERT_EQ(0, motionArgs.buttonState);
5118
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5120 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5121 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5122
5123 // press BTN_EXTRA, release BTN_EXTRA
5124 processKey(mapper, BTN_EXTRA, 1);
5125 processSync(mapper);
5126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5127 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5128 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005129
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005132 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5133
5134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5136 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137
5138 processKey(mapper, BTN_EXTRA, 0);
5139 processSync(mapper);
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005143
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005146 ASSERT_EQ(0, motionArgs.buttonState);
5147
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5149 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5150 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5151
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5153
Michael Wrightd02c5b62014-02-10 15:10:22 -08005154 // press BTN_STYLUS, release BTN_STYLUS
5155 processKey(mapper, BTN_STYLUS, 1);
5156 processSync(mapper);
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005159 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5160
5161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5163 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164
5165 processKey(mapper, BTN_STYLUS, 0);
5166 processSync(mapper);
5167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005168 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005170
5171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005173 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174
5175 // press BTN_STYLUS2, release BTN_STYLUS2
5176 processKey(mapper, BTN_STYLUS2, 1);
5177 processSync(mapper);
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005180 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5181
5182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5184 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185
5186 processKey(mapper, BTN_STYLUS2, 0);
5187 processSync(mapper);
5188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005190 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005191
5192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005194 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195
5196 // release touch
5197 processUp(mapper);
5198 processSync(mapper);
5199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5200 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5201 ASSERT_EQ(0, motionArgs.buttonState);
5202}
5203
5204TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205 addConfigurationProperty("touch.deviceType", "touchScreen");
5206 prepareDisplay(DISPLAY_ORIENTATION_0);
5207 prepareButtons();
5208 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005209 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210
5211 NotifyMotionArgs motionArgs;
5212
5213 // default tool type is finger
5214 processDown(mapper, 100, 200);
5215 processSync(mapper);
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5217 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5219
5220 // eraser
5221 processKey(mapper, BTN_TOOL_RUBBER, 1);
5222 processSync(mapper);
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5226
5227 // stylus
5228 processKey(mapper, BTN_TOOL_RUBBER, 0);
5229 processKey(mapper, BTN_TOOL_PEN, 1);
5230 processSync(mapper);
5231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5234
5235 // brush
5236 processKey(mapper, BTN_TOOL_PEN, 0);
5237 processKey(mapper, BTN_TOOL_BRUSH, 1);
5238 processSync(mapper);
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5242
5243 // pencil
5244 processKey(mapper, BTN_TOOL_BRUSH, 0);
5245 processKey(mapper, BTN_TOOL_PENCIL, 1);
5246 processSync(mapper);
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5250
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005251 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252 processKey(mapper, BTN_TOOL_PENCIL, 0);
5253 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5254 processSync(mapper);
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5258
5259 // mouse
5260 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5261 processKey(mapper, BTN_TOOL_MOUSE, 1);
5262 processSync(mapper);
5263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5265 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5266
5267 // lens
5268 processKey(mapper, BTN_TOOL_MOUSE, 0);
5269 processKey(mapper, BTN_TOOL_LENS, 1);
5270 processSync(mapper);
5271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5274
5275 // double-tap
5276 processKey(mapper, BTN_TOOL_LENS, 0);
5277 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5278 processSync(mapper);
5279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5282
5283 // triple-tap
5284 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5285 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5286 processSync(mapper);
5287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5290
5291 // quad-tap
5292 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5293 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5294 processSync(mapper);
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5298
5299 // finger
5300 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5301 processKey(mapper, BTN_TOOL_FINGER, 1);
5302 processSync(mapper);
5303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5306
5307 // stylus trumps finger
5308 processKey(mapper, BTN_TOOL_PEN, 1);
5309 processSync(mapper);
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5313
5314 // eraser trumps stylus
5315 processKey(mapper, BTN_TOOL_RUBBER, 1);
5316 processSync(mapper);
5317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5320
5321 // mouse trumps eraser
5322 processKey(mapper, BTN_TOOL_MOUSE, 1);
5323 processSync(mapper);
5324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5327
5328 // back to default tool type
5329 processKey(mapper, BTN_TOOL_MOUSE, 0);
5330 processKey(mapper, BTN_TOOL_RUBBER, 0);
5331 processKey(mapper, BTN_TOOL_PEN, 0);
5332 processKey(mapper, BTN_TOOL_FINGER, 0);
5333 processSync(mapper);
5334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5337}
5338
5339TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 addConfigurationProperty("touch.deviceType", "touchScreen");
5341 prepareDisplay(DISPLAY_ORIENTATION_0);
5342 prepareButtons();
5343 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005344 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005345 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346
5347 NotifyMotionArgs motionArgs;
5348
5349 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5350 processKey(mapper, BTN_TOOL_FINGER, 1);
5351 processMove(mapper, 100, 200);
5352 processSync(mapper);
5353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5354 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5357
5358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5359 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5361 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5362
5363 // move a little
5364 processMove(mapper, 150, 250);
5365 processSync(mapper);
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5367 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5369 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5370
5371 // down when BTN_TOUCH is pressed, pressure defaults to 1
5372 processKey(mapper, BTN_TOUCH, 1);
5373 processSync(mapper);
5374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5377 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5378
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5380 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5382 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5383
5384 // up when BTN_TOUCH is released, hover restored
5385 processKey(mapper, BTN_TOUCH, 0);
5386 processSync(mapper);
5387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5388 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5390 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5391
5392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5393 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5395 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5396
5397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5398 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5400 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5401
5402 // exit hover when pointer goes away
5403 processKey(mapper, BTN_TOOL_FINGER, 0);
5404 processSync(mapper);
5405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5406 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5408 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5409}
5410
5411TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 addConfigurationProperty("touch.deviceType", "touchScreen");
5413 prepareDisplay(DISPLAY_ORIENTATION_0);
5414 prepareButtons();
5415 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417
5418 NotifyMotionArgs motionArgs;
5419
5420 // initially hovering because pressure is 0
5421 processDown(mapper, 100, 200);
5422 processPressure(mapper, 0);
5423 processSync(mapper);
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5427 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5428
5429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5430 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5432 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5433
5434 // move a little
5435 processMove(mapper, 150, 250);
5436 processSync(mapper);
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5438 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5440 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5441
5442 // down when pressure is non-zero
5443 processPressure(mapper, RAW_PRESSURE_MAX);
5444 processSync(mapper);
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5448 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5449
5450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5451 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5453 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5454
5455 // up when pressure becomes 0, hover restored
5456 processPressure(mapper, 0);
5457 processSync(mapper);
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5459 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5461 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5462
5463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5464 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5466 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5467
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5469 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5471 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5472
5473 // exit hover when pointer goes away
5474 processUp(mapper);
5475 processSync(mapper);
5476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5477 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5479 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5480}
5481
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482// --- MultiTouchInputMapperTest ---
5483
5484class MultiTouchInputMapperTest : public TouchInputMapperTest {
5485protected:
5486 void prepareAxes(int axes);
5487
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005488 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5489 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5490 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5491 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5492 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5493 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5494 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5495 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5496 void processId(MultiTouchInputMapper& mapper, int32_t id);
5497 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5498 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5499 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5500 void processMTSync(MultiTouchInputMapper& mapper);
5501 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502};
5503
5504void MultiTouchInputMapperTest::prepareAxes(int axes) {
5505 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5507 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 }
5509 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5511 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005513 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5514 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 }
5516 }
5517 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005518 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5519 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005521 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5522 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523 }
5524 }
5525 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005526 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5527 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 }
5529 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005530 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5531 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532 }
5533 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005534 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5535 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536 }
5537 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005538 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5539 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005540 }
5541 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005542 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5543 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 }
5545 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005546 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 }
5548}
5549
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005550void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5551 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005552 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5553 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554}
5555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005556void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5557 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005558 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559}
5560
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005561void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5562 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005563 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005564}
5565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005566void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005567 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568}
5569
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005570void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005571 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572}
5573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005574void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5575 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005576 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577}
5578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005579void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005580 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581}
5582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005583void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005584 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585}
5586
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005587void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005588 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589}
5590
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005591void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005592 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593}
5594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005595void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005596 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597}
5598
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005599void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5600 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005601 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602}
5603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005604void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005605 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606}
5607
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005608void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005609 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610}
5611
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613 addConfigurationProperty("touch.deviceType", "touchScreen");
5614 prepareDisplay(DISPLAY_ORIENTATION_0);
5615 prepareAxes(POSITION);
5616 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005617 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005618
arthurhungdcef2dc2020-08-11 14:47:50 +08005619 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620
5621 NotifyMotionArgs motionArgs;
5622
5623 // Two fingers down at once.
5624 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5625 processPosition(mapper, x1, y1);
5626 processMTSync(mapper);
5627 processPosition(mapper, x2, y2);
5628 processMTSync(mapper);
5629 processSync(mapper);
5630
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5632 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5633 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5634 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5635 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5636 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5637 ASSERT_EQ(0, motionArgs.flags);
5638 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5639 ASSERT_EQ(0, motionArgs.buttonState);
5640 ASSERT_EQ(0, motionArgs.edgeFlags);
5641 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5642 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5645 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5646 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5647 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5648 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5649
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5652 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5653 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5654 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5655 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5656 motionArgs.action);
5657 ASSERT_EQ(0, motionArgs.flags);
5658 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5659 ASSERT_EQ(0, motionArgs.buttonState);
5660 ASSERT_EQ(0, motionArgs.edgeFlags);
5661 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5664 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5667 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5669 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5670 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5671 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5672 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5673
5674 // Move.
5675 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5676 processPosition(mapper, x1, y1);
5677 processMTSync(mapper);
5678 processPosition(mapper, x2, y2);
5679 processMTSync(mapper);
5680 processSync(mapper);
5681
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5684 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5685 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5686 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5688 ASSERT_EQ(0, motionArgs.flags);
5689 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5690 ASSERT_EQ(0, motionArgs.buttonState);
5691 ASSERT_EQ(0, motionArgs.edgeFlags);
5692 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5693 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5695 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5698 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5700 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5701 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5702 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5703 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5704
5705 // First finger up.
5706 x2 += 15; y2 -= 20;
5707 processPosition(mapper, x2, y2);
5708 processMTSync(mapper);
5709 processSync(mapper);
5710
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5713 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5714 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5715 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5716 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5717 motionArgs.action);
5718 ASSERT_EQ(0, motionArgs.flags);
5719 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5720 ASSERT_EQ(0, motionArgs.buttonState);
5721 ASSERT_EQ(0, motionArgs.edgeFlags);
5722 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5723 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5725 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5728 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5730 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5731 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5732 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5733 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5734
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5737 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5738 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5739 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5741 ASSERT_EQ(0, motionArgs.flags);
5742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5743 ASSERT_EQ(0, motionArgs.buttonState);
5744 ASSERT_EQ(0, motionArgs.edgeFlags);
5745 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5746 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5749 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5750 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5751 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5753
5754 // Move.
5755 x2 += 20; y2 -= 25;
5756 processPosition(mapper, x2, y2);
5757 processMTSync(mapper);
5758 processSync(mapper);
5759
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5761 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5762 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5763 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5764 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5766 ASSERT_EQ(0, motionArgs.flags);
5767 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5768 ASSERT_EQ(0, motionArgs.buttonState);
5769 ASSERT_EQ(0, motionArgs.edgeFlags);
5770 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5771 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5774 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5775 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5776 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5777 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5778
5779 // New finger down.
5780 int32_t x3 = 700, y3 = 300;
5781 processPosition(mapper, x2, y2);
5782 processMTSync(mapper);
5783 processPosition(mapper, x3, y3);
5784 processMTSync(mapper);
5785 processSync(mapper);
5786
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5788 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5789 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5790 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5791 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5792 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5793 motionArgs.action);
5794 ASSERT_EQ(0, motionArgs.flags);
5795 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5796 ASSERT_EQ(0, motionArgs.buttonState);
5797 ASSERT_EQ(0, motionArgs.edgeFlags);
5798 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5799 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5801 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5804 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5806 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5807 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5808 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5810
5811 // Second finger up.
5812 x3 += 30; y3 -= 20;
5813 processPosition(mapper, x3, y3);
5814 processMTSync(mapper);
5815 processSync(mapper);
5816
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5818 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5819 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5820 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5821 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5822 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5823 motionArgs.action);
5824 ASSERT_EQ(0, motionArgs.flags);
5825 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5826 ASSERT_EQ(0, motionArgs.buttonState);
5827 ASSERT_EQ(0, motionArgs.edgeFlags);
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(x3), toDisplayY(y3), 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 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5838 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5839 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5840
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5842 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5843 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5844 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5845 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5847 ASSERT_EQ(0, motionArgs.flags);
5848 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5849 ASSERT_EQ(0, motionArgs.buttonState);
5850 ASSERT_EQ(0, motionArgs.edgeFlags);
5851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5856 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5857 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5858 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5859
5860 // Last finger up.
5861 processMTSync(mapper);
5862 processSync(mapper);
5863
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5865 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5866 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5867 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5868 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5869 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5870 ASSERT_EQ(0, motionArgs.flags);
5871 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5872 ASSERT_EQ(0, motionArgs.buttonState);
5873 ASSERT_EQ(0, motionArgs.edgeFlags);
5874 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5875 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5876 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5878 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5879 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5880 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5881 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5882
5883 // Should not have sent any more keys or motions.
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5886}
5887
5888TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889 addConfigurationProperty("touch.deviceType", "touchScreen");
5890 prepareDisplay(DISPLAY_ORIENTATION_0);
5891 prepareAxes(POSITION | ID);
5892 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005893 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894
arthurhungdcef2dc2020-08-11 14:47:50 +08005895 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896
5897 NotifyMotionArgs motionArgs;
5898
5899 // Two fingers down at once.
5900 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5901 processPosition(mapper, x1, y1);
5902 processId(mapper, 1);
5903 processMTSync(mapper);
5904 processPosition(mapper, x2, y2);
5905 processId(mapper, 2);
5906 processMTSync(mapper);
5907 processSync(mapper);
5908
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5911 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5912 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5915 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5916
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5919 motionArgs.action);
5920 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5923 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5926 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5928 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5929
5930 // Move.
5931 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5932 processPosition(mapper, x1, y1);
5933 processId(mapper, 1);
5934 processMTSync(mapper);
5935 processPosition(mapper, x2, y2);
5936 processId(mapper, 2);
5937 processMTSync(mapper);
5938 processSync(mapper);
5939
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5942 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5943 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5945 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5948 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5950 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5951
5952 // First finger up.
5953 x2 += 15; y2 -= 20;
5954 processPosition(mapper, x2, y2);
5955 processId(mapper, 2);
5956 processMTSync(mapper);
5957 processSync(mapper);
5958
5959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5960 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5961 motionArgs.action);
5962 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5963 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5965 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5968 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5970 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5971
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5974 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5975 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5978 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5979
5980 // Move.
5981 x2 += 20; y2 -= 25;
5982 processPosition(mapper, x2, y2);
5983 processId(mapper, 2);
5984 processMTSync(mapper);
5985 processSync(mapper);
5986
5987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5988 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5989 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5990 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5993 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5994
5995 // New finger down.
5996 int32_t x3 = 700, y3 = 300;
5997 processPosition(mapper, x2, y2);
5998 processId(mapper, 2);
5999 processMTSync(mapper);
6000 processPosition(mapper, x3, y3);
6001 processId(mapper, 3);
6002 processMTSync(mapper);
6003 processSync(mapper);
6004
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6007 motionArgs.action);
6008 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6009 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6011 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6014 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6016 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6017
6018 // Second finger up.
6019 x3 += 30; y3 -= 20;
6020 processPosition(mapper, x3, y3);
6021 processId(mapper, 3);
6022 processMTSync(mapper);
6023 processSync(mapper);
6024
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6027 motionArgs.action);
6028 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6029 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6031 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6034 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6036 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6037
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6040 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6041 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6044 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6045
6046 // Last finger up.
6047 processMTSync(mapper);
6048 processSync(mapper);
6049
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6052 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6053 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6056 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6057
6058 // Should not have sent any more keys or motions.
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6061}
6062
6063TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 addConfigurationProperty("touch.deviceType", "touchScreen");
6065 prepareDisplay(DISPLAY_ORIENTATION_0);
6066 prepareAxes(POSITION | ID | SLOT);
6067 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006068 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069
arthurhungdcef2dc2020-08-11 14:47:50 +08006070 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
6072 NotifyMotionArgs motionArgs;
6073
6074 // Two fingers down at once.
6075 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6076 processPosition(mapper, x1, y1);
6077 processId(mapper, 1);
6078 processSlot(mapper, 1);
6079 processPosition(mapper, x2, y2);
6080 processId(mapper, 2);
6081 processSync(mapper);
6082
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6085 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6086 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6089 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6090
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6093 motionArgs.action);
6094 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6095 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6097 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6100 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6102 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6103
6104 // Move.
6105 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6106 processSlot(mapper, 0);
6107 processPosition(mapper, x1, y1);
6108 processSlot(mapper, 1);
6109 processPosition(mapper, x2, y2);
6110 processSync(mapper);
6111
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6114 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6115 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6117 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6120 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6122 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6123
6124 // First finger up.
6125 x2 += 15; y2 -= 20;
6126 processSlot(mapper, 0);
6127 processId(mapper, -1);
6128 processSlot(mapper, 1);
6129 processPosition(mapper, x2, y2);
6130 processSync(mapper);
6131
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6133 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6134 motionArgs.action);
6135 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6136 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6138 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6141 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6143 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6144
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6147 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6148 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6151 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6152
6153 // Move.
6154 x2 += 20; y2 -= 25;
6155 processPosition(mapper, x2, y2);
6156 processSync(mapper);
6157
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6160 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6161 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6164 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6165
6166 // New finger down.
6167 int32_t x3 = 700, y3 = 300;
6168 processPosition(mapper, x2, y2);
6169 processSlot(mapper, 0);
6170 processId(mapper, 3);
6171 processPosition(mapper, x3, y3);
6172 processSync(mapper);
6173
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6175 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6176 motionArgs.action);
6177 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6178 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6180 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6183 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6185 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6186
6187 // Second finger up.
6188 x3 += 30; y3 -= 20;
6189 processSlot(mapper, 1);
6190 processId(mapper, -1);
6191 processSlot(mapper, 0);
6192 processPosition(mapper, x3, y3);
6193 processSync(mapper);
6194
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6196 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6197 motionArgs.action);
6198 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6199 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6201 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6202 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6204 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6206 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6210 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6211 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6214 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6215
6216 // Last finger up.
6217 processId(mapper, -1);
6218 processSync(mapper);
6219
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6222 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6223 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6226 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6227
6228 // Should not have sent any more keys or motions.
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6231}
6232
6233TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 addConfigurationProperty("touch.deviceType", "touchScreen");
6235 prepareDisplay(DISPLAY_ORIENTATION_0);
6236 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006237 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238
6239 // These calculations are based on the input device calibration documentation.
6240 int32_t rawX = 100;
6241 int32_t rawY = 200;
6242 int32_t rawTouchMajor = 7;
6243 int32_t rawTouchMinor = 6;
6244 int32_t rawToolMajor = 9;
6245 int32_t rawToolMinor = 8;
6246 int32_t rawPressure = 11;
6247 int32_t rawDistance = 0;
6248 int32_t rawOrientation = 3;
6249 int32_t id = 5;
6250
6251 float x = toDisplayX(rawX);
6252 float y = toDisplayY(rawY);
6253 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6254 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6255 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6256 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6257 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6258 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6259 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6260 float distance = float(rawDistance);
6261
6262 processPosition(mapper, rawX, rawY);
6263 processTouchMajor(mapper, rawTouchMajor);
6264 processTouchMinor(mapper, rawTouchMinor);
6265 processToolMajor(mapper, rawToolMajor);
6266 processToolMinor(mapper, rawToolMinor);
6267 processPressure(mapper, rawPressure);
6268 processOrientation(mapper, rawOrientation);
6269 processDistance(mapper, rawDistance);
6270 processId(mapper, id);
6271 processMTSync(mapper);
6272 processSync(mapper);
6273
6274 NotifyMotionArgs args;
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6276 ASSERT_EQ(0, args.pointerProperties[0].id);
6277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6278 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6279 orientation, distance));
6280}
6281
6282TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 addConfigurationProperty("touch.deviceType", "touchScreen");
6284 prepareDisplay(DISPLAY_ORIENTATION_0);
6285 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6286 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006287 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288
6289 // These calculations are based on the input device calibration documentation.
6290 int32_t rawX = 100;
6291 int32_t rawY = 200;
6292 int32_t rawTouchMajor = 140;
6293 int32_t rawTouchMinor = 120;
6294 int32_t rawToolMajor = 180;
6295 int32_t rawToolMinor = 160;
6296
6297 float x = toDisplayX(rawX);
6298 float y = toDisplayY(rawY);
6299 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6300 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6301 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6302 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6303 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6304
6305 processPosition(mapper, rawX, rawY);
6306 processTouchMajor(mapper, rawTouchMajor);
6307 processTouchMinor(mapper, rawTouchMinor);
6308 processToolMajor(mapper, rawToolMajor);
6309 processToolMinor(mapper, rawToolMinor);
6310 processMTSync(mapper);
6311 processSync(mapper);
6312
6313 NotifyMotionArgs args;
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6316 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6317}
6318
6319TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320 addConfigurationProperty("touch.deviceType", "touchScreen");
6321 prepareDisplay(DISPLAY_ORIENTATION_0);
6322 prepareAxes(POSITION | TOUCH | TOOL);
6323 addConfigurationProperty("touch.size.calibration", "diameter");
6324 addConfigurationProperty("touch.size.scale", "10");
6325 addConfigurationProperty("touch.size.bias", "160");
6326 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006327 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328
6329 // These calculations are based on the input device calibration documentation.
6330 // Note: We only provide a single common touch/tool value because the device is assumed
6331 // not to emit separate values for each pointer (isSummed = 1).
6332 int32_t rawX = 100;
6333 int32_t rawY = 200;
6334 int32_t rawX2 = 150;
6335 int32_t rawY2 = 250;
6336 int32_t rawTouchMajor = 5;
6337 int32_t rawToolMajor = 8;
6338
6339 float x = toDisplayX(rawX);
6340 float y = toDisplayY(rawY);
6341 float x2 = toDisplayX(rawX2);
6342 float y2 = toDisplayY(rawY2);
6343 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6344 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6345 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6346
6347 processPosition(mapper, rawX, rawY);
6348 processTouchMajor(mapper, rawTouchMajor);
6349 processToolMajor(mapper, rawToolMajor);
6350 processMTSync(mapper);
6351 processPosition(mapper, rawX2, rawY2);
6352 processTouchMajor(mapper, rawTouchMajor);
6353 processToolMajor(mapper, rawToolMajor);
6354 processMTSync(mapper);
6355 processSync(mapper);
6356
6357 NotifyMotionArgs args;
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6362 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6363 args.action);
6364 ASSERT_EQ(size_t(2), args.pointerCount);
6365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6366 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6368 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6369}
6370
6371TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 addConfigurationProperty("touch.deviceType", "touchScreen");
6373 prepareDisplay(DISPLAY_ORIENTATION_0);
6374 prepareAxes(POSITION | TOUCH | TOOL);
6375 addConfigurationProperty("touch.size.calibration", "area");
6376 addConfigurationProperty("touch.size.scale", "43");
6377 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006378 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379
6380 // These calculations are based on the input device calibration documentation.
6381 int32_t rawX = 100;
6382 int32_t rawY = 200;
6383 int32_t rawTouchMajor = 5;
6384 int32_t rawToolMajor = 8;
6385
6386 float x = toDisplayX(rawX);
6387 float y = toDisplayY(rawY);
6388 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6389 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6390 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6391
6392 processPosition(mapper, rawX, rawY);
6393 processTouchMajor(mapper, rawTouchMajor);
6394 processToolMajor(mapper, rawToolMajor);
6395 processMTSync(mapper);
6396 processSync(mapper);
6397
6398 NotifyMotionArgs args;
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6401 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6402}
6403
6404TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405 addConfigurationProperty("touch.deviceType", "touchScreen");
6406 prepareDisplay(DISPLAY_ORIENTATION_0);
6407 prepareAxes(POSITION | PRESSURE);
6408 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6409 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006410 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411
Michael Wrightaa449c92017-12-13 21:21:43 +00006412 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006413 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006414 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6415 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6416 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6417
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 // These calculations are based on the input device calibration documentation.
6419 int32_t rawX = 100;
6420 int32_t rawY = 200;
6421 int32_t rawPressure = 60;
6422
6423 float x = toDisplayX(rawX);
6424 float y = toDisplayY(rawY);
6425 float pressure = float(rawPressure) * 0.01f;
6426
6427 processPosition(mapper, rawX, rawY);
6428 processPressure(mapper, rawPressure);
6429 processMTSync(mapper);
6430 processSync(mapper);
6431
6432 NotifyMotionArgs args;
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6435 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6436}
6437
6438TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439 addConfigurationProperty("touch.deviceType", "touchScreen");
6440 prepareDisplay(DISPLAY_ORIENTATION_0);
6441 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006442 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443
6444 NotifyMotionArgs motionArgs;
6445 NotifyKeyArgs keyArgs;
6446
6447 processId(mapper, 1);
6448 processPosition(mapper, 100, 200);
6449 processSync(mapper);
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6452 ASSERT_EQ(0, motionArgs.buttonState);
6453
6454 // press BTN_LEFT, release BTN_LEFT
6455 processKey(mapper, BTN_LEFT, 1);
6456 processSync(mapper);
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6459 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6460
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6463 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6464
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465 processKey(mapper, BTN_LEFT, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006474
6475 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6476 processKey(mapper, BTN_RIGHT, 1);
6477 processKey(mapper, BTN_MIDDLE, 1);
6478 processSync(mapper);
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6481 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6482 motionArgs.buttonState);
6483
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6486 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6487
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6491 motionArgs.buttonState);
6492
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493 processKey(mapper, BTN_RIGHT, 0);
6494 processSync(mapper);
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006497 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006498
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006501 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006502
6503 processKey(mapper, BTN_MIDDLE, 0);
6504 processSync(mapper);
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006507 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006508
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006511 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006512
6513 // press BTN_BACK, release BTN_BACK
6514 processKey(mapper, BTN_BACK, 1);
6515 processSync(mapper);
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6517 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6518 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006519
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006522 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6523
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6526 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527
6528 processKey(mapper, BTN_BACK, 0);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006533
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006536 ASSERT_EQ(0, motionArgs.buttonState);
6537
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6539 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6540 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6541
6542 // press BTN_SIDE, release BTN_SIDE
6543 processKey(mapper, BTN_SIDE, 1);
6544 processSync(mapper);
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6546 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6547 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006548
Michael Wrightd02c5b62014-02-10 15:10:22 -08006549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006551 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6552
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6554 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6555 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006556
6557 processKey(mapper, BTN_SIDE, 0);
6558 processSync(mapper);
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006560 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006561 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006562
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006565 ASSERT_EQ(0, motionArgs.buttonState);
6566
Michael Wrightd02c5b62014-02-10 15:10:22 -08006567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6568 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6569 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6570
6571 // press BTN_FORWARD, release BTN_FORWARD
6572 processKey(mapper, BTN_FORWARD, 1);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6575 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6576 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006577
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006580 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6581
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6584 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006585
6586 processKey(mapper, BTN_FORWARD, 0);
6587 processSync(mapper);
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006589 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006591
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006594 ASSERT_EQ(0, motionArgs.buttonState);
6595
Michael Wrightd02c5b62014-02-10 15:10:22 -08006596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6597 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6598 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6599
6600 // press BTN_EXTRA, release BTN_EXTRA
6601 processKey(mapper, BTN_EXTRA, 1);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6604 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6605 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006606
Michael Wrightd02c5b62014-02-10 15:10:22 -08006607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006609 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6610
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6613 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006614
6615 processKey(mapper, BTN_EXTRA, 0);
6616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006619 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006620
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006623 ASSERT_EQ(0, motionArgs.buttonState);
6624
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6626 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6627 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6628
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6630
Michael Wrightd02c5b62014-02-10 15:10:22 -08006631 // press BTN_STYLUS, release BTN_STYLUS
6632 processKey(mapper, BTN_STYLUS, 1);
6633 processSync(mapper);
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006636 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6637
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6640 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006641
6642 processKey(mapper, BTN_STYLUS, 0);
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006645 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006647
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006650 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006651
6652 // press BTN_STYLUS2, release BTN_STYLUS2
6653 processKey(mapper, BTN_STYLUS2, 1);
6654 processSync(mapper);
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006657 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6658
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6661 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006662
6663 processKey(mapper, BTN_STYLUS2, 0);
6664 processSync(mapper);
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006667 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006668
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006671 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006672
6673 // release touch
6674 processId(mapper, -1);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6678 ASSERT_EQ(0, motionArgs.buttonState);
6679}
6680
6681TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006682 addConfigurationProperty("touch.deviceType", "touchScreen");
6683 prepareDisplay(DISPLAY_ORIENTATION_0);
6684 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006685 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006686
6687 NotifyMotionArgs motionArgs;
6688
6689 // default tool type is finger
6690 processId(mapper, 1);
6691 processPosition(mapper, 100, 200);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6696
6697 // eraser
6698 processKey(mapper, BTN_TOOL_RUBBER, 1);
6699 processSync(mapper);
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6703
6704 // stylus
6705 processKey(mapper, BTN_TOOL_RUBBER, 0);
6706 processKey(mapper, BTN_TOOL_PEN, 1);
6707 processSync(mapper);
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6711
6712 // brush
6713 processKey(mapper, BTN_TOOL_PEN, 0);
6714 processKey(mapper, BTN_TOOL_BRUSH, 1);
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_STYLUS, motionArgs.pointerProperties[0].toolType);
6719
6720 // pencil
6721 processKey(mapper, BTN_TOOL_BRUSH, 0);
6722 processKey(mapper, BTN_TOOL_PENCIL, 1);
6723 processSync(mapper);
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6727
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006728 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006729 processKey(mapper, BTN_TOOL_PENCIL, 0);
6730 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6731 processSync(mapper);
6732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6735
6736 // mouse
6737 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6738 processKey(mapper, BTN_TOOL_MOUSE, 1);
6739 processSync(mapper);
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6743
6744 // lens
6745 processKey(mapper, BTN_TOOL_MOUSE, 0);
6746 processKey(mapper, BTN_TOOL_LENS, 1);
6747 processSync(mapper);
6748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6751
6752 // double-tap
6753 processKey(mapper, BTN_TOOL_LENS, 0);
6754 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6755 processSync(mapper);
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6759
6760 // triple-tap
6761 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6762 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6763 processSync(mapper);
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6767
6768 // quad-tap
6769 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6770 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6771 processSync(mapper);
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6775
6776 // finger
6777 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6778 processKey(mapper, BTN_TOOL_FINGER, 1);
6779 processSync(mapper);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6783
6784 // stylus trumps finger
6785 processKey(mapper, BTN_TOOL_PEN, 1);
6786 processSync(mapper);
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6790
6791 // eraser trumps stylus
6792 processKey(mapper, BTN_TOOL_RUBBER, 1);
6793 processSync(mapper);
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6796 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6797
6798 // mouse trumps eraser
6799 processKey(mapper, BTN_TOOL_MOUSE, 1);
6800 processSync(mapper);
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6804
6805 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6806 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6811
6812 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6813 processToolType(mapper, MT_TOOL_PEN);
6814 processSync(mapper);
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6818
6819 // back to default tool type
6820 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6821 processKey(mapper, BTN_TOOL_MOUSE, 0);
6822 processKey(mapper, BTN_TOOL_RUBBER, 0);
6823 processKey(mapper, BTN_TOOL_PEN, 0);
6824 processKey(mapper, BTN_TOOL_FINGER, 0);
6825 processSync(mapper);
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6828 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6829}
6830
6831TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006832 addConfigurationProperty("touch.deviceType", "touchScreen");
6833 prepareDisplay(DISPLAY_ORIENTATION_0);
6834 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006835 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006836 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006837
6838 NotifyMotionArgs motionArgs;
6839
6840 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6841 processId(mapper, 1);
6842 processPosition(mapper, 100, 200);
6843 processSync(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6847 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6848
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6850 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6852 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6853
6854 // move a little
6855 processPosition(mapper, 150, 250);
6856 processSync(mapper);
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6858 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6860 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6861
6862 // down when BTN_TOUCH is pressed, pressure defaults to 1
6863 processKey(mapper, BTN_TOUCH, 1);
6864 processSync(mapper);
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6866 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6868 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6869
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6871 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6873 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6874
6875 // up when BTN_TOUCH is released, hover restored
6876 processKey(mapper, BTN_TOUCH, 0);
6877 processSync(mapper);
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6879 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6881 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6882
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6884 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6886 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6887
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6889 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6891 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6892
6893 // exit hover when pointer goes away
6894 processId(mapper, -1);
6895 processSync(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6897 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6899 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6900}
6901
6902TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006903 addConfigurationProperty("touch.deviceType", "touchScreen");
6904 prepareDisplay(DISPLAY_ORIENTATION_0);
6905 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006906 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006907
6908 NotifyMotionArgs motionArgs;
6909
6910 // initially hovering because pressure is 0
6911 processId(mapper, 1);
6912 processPosition(mapper, 100, 200);
6913 processPressure(mapper, 0);
6914 processSync(mapper);
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6916 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6918 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6919
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6921 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6923 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6924
6925 // move a little
6926 processPosition(mapper, 150, 250);
6927 processSync(mapper);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6929 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6931 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6932
6933 // down when pressure becomes non-zero
6934 processPressure(mapper, RAW_PRESSURE_MAX);
6935 processSync(mapper);
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6937 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6939 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6940
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6944 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6945
6946 // up when pressure becomes 0, hover restored
6947 processPressure(mapper, 0);
6948 processSync(mapper);
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6950 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6952 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6953
6954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6957 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6958
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6962 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6963
6964 // exit hover when pointer goes away
6965 processId(mapper, -1);
6966 processSync(mapper);
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6968 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6970 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6971}
6972
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006973/**
6974 * Set the input device port <--> display port associations, and check that the
6975 * events are routed to the display that matches the display port.
6976 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6977 */
6978TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006979 const std::string usb2 = "USB2";
6980 const uint8_t hdmi1 = 0;
6981 const uint8_t hdmi2 = 1;
6982 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006983 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006984
6985 addConfigurationProperty("touch.deviceType", "touchScreen");
6986 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006987 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006988
6989 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6990 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6991
6992 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6993 // for this input device is specified, and the matching viewport is not present,
6994 // the input device should be disabled (at the mapper level).
6995
6996 // Add viewport for display 2 on hdmi2
6997 prepareSecondaryDisplay(type, hdmi2);
6998 // Send a touch event
6999 processPosition(mapper, 100, 100);
7000 processSync(mapper);
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7002
7003 // Add viewport for display 1 on hdmi1
7004 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7005 // Send a touch event again
7006 processPosition(mapper, 100, 100);
7007 processSync(mapper);
7008
7009 NotifyMotionArgs args;
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7011 ASSERT_EQ(DISPLAY_ID, args.displayId);
7012}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007014TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007015 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007016 std::shared_ptr<FakePointerController> fakePointerController =
7017 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007018 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007019 fakePointerController->setPosition(100, 200);
7020 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007021 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7022
Garfield Tan888a6a42020-01-09 11:39:16 -08007023 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007024 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007025
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007026 prepareDisplay(DISPLAY_ORIENTATION_0);
7027 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007028 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007029
7030 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007031 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007032
7033 NotifyMotionArgs motionArgs;
7034 processPosition(mapper, 100, 100);
7035 processSync(mapper);
7036
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7039 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7040}
7041
Arthur Hung7c645402019-01-25 17:45:42 +08007042TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7043 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007044 prepareAxes(POSITION | ID | SLOT);
7045 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007046 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007047
7048 // Create the second touch screen device, and enable multi fingers.
7049 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007050 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007051 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007052 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007053 std::shared_ptr<InputDevice> device2 =
7054 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7055 Flags<InputDeviceClass>(0));
7056
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007057 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7058 0 /*flat*/, 0 /*fuzz*/);
7059 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7060 0 /*flat*/, 0 /*fuzz*/);
7061 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7062 0 /*flat*/, 0 /*fuzz*/);
7063 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7064 0 /*flat*/, 0 /*fuzz*/);
7065 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7066 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7067 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007068
7069 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007070 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007071 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7072 device2->reset(ARBITRARY_TIME);
7073
7074 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007075 std::shared_ptr<FakePointerController> fakePointerController =
7076 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007077 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7078 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7079
7080 // Setup policy for associated displays and show touches.
7081 const uint8_t hdmi1 = 0;
7082 const uint8_t hdmi2 = 1;
7083 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7084 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7085 mFakePolicy->setShowTouches(true);
7086
7087 // Create displays.
7088 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007089 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007090
7091 // Default device will reconfigure above, need additional reconfiguration for another device.
7092 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007093 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007094
7095 // Two fingers down at default display.
7096 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7097 processPosition(mapper, x1, y1);
7098 processId(mapper, 1);
7099 processSlot(mapper, 1);
7100 processPosition(mapper, x2, y2);
7101 processId(mapper, 2);
7102 processSync(mapper);
7103
7104 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7105 fakePointerController->getSpots().find(DISPLAY_ID);
7106 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7107 ASSERT_EQ(size_t(2), iter->second.size());
7108
7109 // Two fingers down at second display.
7110 processPosition(mapper2, x1, y1);
7111 processId(mapper2, 1);
7112 processSlot(mapper2, 1);
7113 processPosition(mapper2, x2, y2);
7114 processId(mapper2, 2);
7115 processSync(mapper2);
7116
7117 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7118 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7119 ASSERT_EQ(size_t(2), iter->second.size());
7120}
7121
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007122TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007123 prepareAxes(POSITION);
7124 addConfigurationProperty("touch.deviceType", "touchScreen");
7125 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007126 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007127
7128 NotifyMotionArgs motionArgs;
7129 // Unrotated video frame
7130 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7131 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007132 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007133 processPosition(mapper, 100, 200);
7134 processSync(mapper);
7135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7136 ASSERT_EQ(frames, motionArgs.videoFrames);
7137
7138 // Subsequent touch events should not have any videoframes
7139 // This is implemented separately in FakeEventHub,
7140 // but that should match the behaviour of TouchVideoDevice.
7141 processPosition(mapper, 200, 200);
7142 processSync(mapper);
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7144 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7145}
7146
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007147TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007148 prepareAxes(POSITION);
7149 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007150 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007151 // Unrotated video frame
7152 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7153 NotifyMotionArgs motionArgs;
7154
7155 // Test all 4 orientations
7156 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7157 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7158 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7159 clearViewports();
7160 prepareDisplay(orientation);
7161 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007162 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007163 processPosition(mapper, 100, 200);
7164 processSync(mapper);
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7166 frames[0].rotate(orientation);
7167 ASSERT_EQ(frames, motionArgs.videoFrames);
7168 }
7169}
7170
7171TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007172 prepareAxes(POSITION);
7173 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007174 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007175 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7176 // so mix these.
7177 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7178 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7179 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7180 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7181 NotifyMotionArgs motionArgs;
7182
7183 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007184 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007185 processPosition(mapper, 100, 200);
7186 processSync(mapper);
7187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7188 std::for_each(frames.begin(), frames.end(),
7189 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7190 ASSERT_EQ(frames, motionArgs.videoFrames);
7191}
7192
Arthur Hung9da14732019-09-02 16:16:58 +08007193/**
7194 * If we had defined port associations, but the viewport is not ready, the touch device would be
7195 * expected to be disabled, and it should be enabled after the viewport has found.
7196 */
7197TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007198 constexpr uint8_t hdmi2 = 1;
7199 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007200 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007201
7202 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7203
7204 addConfigurationProperty("touch.deviceType", "touchScreen");
7205 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007206 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007207
7208 ASSERT_EQ(mDevice->isEnabled(), false);
7209
7210 // Add display on hdmi2, the device should be enabled and can receive touch event.
7211 prepareSecondaryDisplay(type, hdmi2);
7212 ASSERT_EQ(mDevice->isEnabled(), true);
7213
7214 // Send a touch event.
7215 processPosition(mapper, 100, 100);
7216 processSync(mapper);
7217
7218 NotifyMotionArgs args;
7219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7220 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7221}
7222
Arthur Hung6cd19a42019-08-30 19:04:12 +08007223
Arthur Hung6cd19a42019-08-30 19:04:12 +08007224
Arthur Hung421eb1c2020-01-16 00:09:42 +08007225TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007226 addConfigurationProperty("touch.deviceType", "touchScreen");
7227 prepareDisplay(DISPLAY_ORIENTATION_0);
7228 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007229 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007230
7231 NotifyMotionArgs motionArgs;
7232
7233 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7234 // finger down
7235 processId(mapper, 1);
7236 processPosition(mapper, x1, y1);
7237 processSync(mapper);
7238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7239 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7241
7242 // finger move
7243 processId(mapper, 1);
7244 processPosition(mapper, x2, y2);
7245 processSync(mapper);
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7249
7250 // finger up.
7251 processId(mapper, -1);
7252 processSync(mapper);
7253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7254 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7256
7257 // new finger down
7258 processId(mapper, 1);
7259 processPosition(mapper, x3, y3);
7260 processSync(mapper);
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7264}
7265
7266/**
arthurhungcc7f9802020-04-30 17:55:40 +08007267 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7268 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007269 */
arthurhungcc7f9802020-04-30 17:55:40 +08007270TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007271 addConfigurationProperty("touch.deviceType", "touchScreen");
7272 prepareDisplay(DISPLAY_ORIENTATION_0);
7273 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007274 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007275
7276 NotifyMotionArgs motionArgs;
7277
7278 // default tool type is finger
7279 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007280 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007281 processPosition(mapper, x1, y1);
7282 processSync(mapper);
7283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7284 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7286
7287 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7288 processToolType(mapper, MT_TOOL_PALM);
7289 processSync(mapper);
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7291 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7292
7293 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007294 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007295 processPosition(mapper, x2, y2);
7296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7298
7299 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007300 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007301 processSync(mapper);
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7303
7304 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007305 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007306 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007307 processPosition(mapper, x3, y3);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7310 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7312}
7313
arthurhungbf89a482020-04-17 17:37:55 +08007314/**
arthurhungcc7f9802020-04-30 17:55:40 +08007315 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7316 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007317 */
arthurhungcc7f9802020-04-30 17:55:40 +08007318TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007319 addConfigurationProperty("touch.deviceType", "touchScreen");
7320 prepareDisplay(DISPLAY_ORIENTATION_0);
7321 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7322 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7323
7324 NotifyMotionArgs motionArgs;
7325
7326 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007327 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7328 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007329 processPosition(mapper, x1, y1);
7330 processSync(mapper);
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7332 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7334
7335 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007336 processSlot(mapper, SECOND_SLOT);
7337 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007338 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007339 processSync(mapper);
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7341 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7342 motionArgs.action);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7344
7345 // If the tool type of the first finger changes to MT_TOOL_PALM,
7346 // we expect to receive ACTION_POINTER_UP with cancel flag.
7347 processSlot(mapper, FIRST_SLOT);
7348 processId(mapper, FIRST_TRACKING_ID);
7349 processToolType(mapper, MT_TOOL_PALM);
7350 processSync(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7353 motionArgs.action);
7354 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7355
7356 // The following MOVE events of second finger should be processed.
7357 processSlot(mapper, SECOND_SLOT);
7358 processId(mapper, SECOND_TRACKING_ID);
7359 processPosition(mapper, x2 + 1, y2 + 1);
7360 processSync(mapper);
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7363 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7364
7365 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7366 // it. Second finger receive move.
7367 processSlot(mapper, FIRST_SLOT);
7368 processId(mapper, INVALID_TRACKING_ID);
7369 processSync(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7372 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7373
7374 // Second finger keeps moving.
7375 processSlot(mapper, SECOND_SLOT);
7376 processId(mapper, SECOND_TRACKING_ID);
7377 processPosition(mapper, x2 + 2, y2 + 2);
7378 processSync(mapper);
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7381 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7382
7383 // Second finger up.
7384 processId(mapper, INVALID_TRACKING_ID);
7385 processSync(mapper);
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7387 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7388 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7389}
7390
7391/**
7392 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7393 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7394 */
7395TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7396 addConfigurationProperty("touch.deviceType", "touchScreen");
7397 prepareDisplay(DISPLAY_ORIENTATION_0);
7398 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7399 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7400
7401 NotifyMotionArgs motionArgs;
7402
7403 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7404 // First finger down.
7405 processId(mapper, FIRST_TRACKING_ID);
7406 processPosition(mapper, x1, y1);
7407 processSync(mapper);
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7409 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7411
7412 // Second finger down.
7413 processSlot(mapper, SECOND_SLOT);
7414 processId(mapper, SECOND_TRACKING_ID);
7415 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007416 processSync(mapper);
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7418 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7419 motionArgs.action);
7420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7421
arthurhungcc7f9802020-04-30 17:55:40 +08007422 // If the tool type of the first finger changes to MT_TOOL_PALM,
7423 // we expect to receive ACTION_POINTER_UP with cancel flag.
7424 processSlot(mapper, FIRST_SLOT);
7425 processId(mapper, FIRST_TRACKING_ID);
7426 processToolType(mapper, MT_TOOL_PALM);
7427 processSync(mapper);
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7430 motionArgs.action);
7431 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7432
7433 // Second finger keeps moving.
7434 processSlot(mapper, SECOND_SLOT);
7435 processId(mapper, SECOND_TRACKING_ID);
7436 processPosition(mapper, x2 + 1, y2 + 1);
7437 processSync(mapper);
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7440
7441 // second finger becomes palm, receive cancel due to only 1 finger is active.
7442 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007443 processToolType(mapper, MT_TOOL_PALM);
7444 processSync(mapper);
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7446 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7447
arthurhungcc7f9802020-04-30 17:55:40 +08007448 // third finger down.
7449 processSlot(mapper, THIRD_SLOT);
7450 processId(mapper, THIRD_TRACKING_ID);
7451 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007452 processPosition(mapper, x3, y3);
7453 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007457 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7458
7459 // third finger move
7460 processId(mapper, THIRD_TRACKING_ID);
7461 processPosition(mapper, x3 + 1, y3 + 1);
7462 processSync(mapper);
7463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7465
7466 // first finger up, third finger receive move.
7467 processSlot(mapper, FIRST_SLOT);
7468 processId(mapper, INVALID_TRACKING_ID);
7469 processSync(mapper);
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7472 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7473
7474 // second finger up, third finger receive move.
7475 processSlot(mapper, SECOND_SLOT);
7476 processId(mapper, INVALID_TRACKING_ID);
7477 processSync(mapper);
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7480 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7481
7482 // third finger up.
7483 processSlot(mapper, THIRD_SLOT);
7484 processId(mapper, INVALID_TRACKING_ID);
7485 processSync(mapper);
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7488 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7489}
7490
7491/**
7492 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7493 * and the active finger could still be allowed to receive the events
7494 */
7495TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7496 addConfigurationProperty("touch.deviceType", "touchScreen");
7497 prepareDisplay(DISPLAY_ORIENTATION_0);
7498 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7499 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7500
7501 NotifyMotionArgs motionArgs;
7502
7503 // default tool type is finger
7504 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7505 processId(mapper, FIRST_TRACKING_ID);
7506 processPosition(mapper, x1, y1);
7507 processSync(mapper);
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7511
7512 // Second finger down.
7513 processSlot(mapper, SECOND_SLOT);
7514 processId(mapper, SECOND_TRACKING_ID);
7515 processPosition(mapper, x2, y2);
7516 processSync(mapper);
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7518 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7519 motionArgs.action);
7520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7521
7522 // If the tool type of the second finger changes to MT_TOOL_PALM,
7523 // we expect to receive ACTION_POINTER_UP with cancel flag.
7524 processId(mapper, SECOND_TRACKING_ID);
7525 processToolType(mapper, MT_TOOL_PALM);
7526 processSync(mapper);
7527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7528 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7529 motionArgs.action);
7530 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7531
7532 // The following MOVE event should be processed.
7533 processSlot(mapper, FIRST_SLOT);
7534 processId(mapper, FIRST_TRACKING_ID);
7535 processPosition(mapper, x1 + 1, y1 + 1);
7536 processSync(mapper);
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7539 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7540
7541 // second finger up.
7542 processSlot(mapper, SECOND_SLOT);
7543 processId(mapper, INVALID_TRACKING_ID);
7544 processSync(mapper);
7545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7547
7548 // first finger keep moving
7549 processSlot(mapper, FIRST_SLOT);
7550 processId(mapper, FIRST_TRACKING_ID);
7551 processPosition(mapper, x1 + 2, y1 + 2);
7552 processSync(mapper);
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7555
7556 // first finger up.
7557 processId(mapper, INVALID_TRACKING_ID);
7558 processSync(mapper);
7559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7560 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7561 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007562}
7563
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007564// --- MultiTouchInputMapperTest_ExternalDevice ---
7565
7566class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7567protected:
Chris Yea52ade12020-08-27 16:49:20 -07007568 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007569};
7570
7571/**
7572 * Expect fallback to internal viewport if device is external and external viewport is not present.
7573 */
7574TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7575 prepareAxes(POSITION);
7576 addConfigurationProperty("touch.deviceType", "touchScreen");
7577 prepareDisplay(DISPLAY_ORIENTATION_0);
7578 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7579
7580 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7581
7582 NotifyMotionArgs motionArgs;
7583
7584 // Expect the event to be sent to the internal viewport,
7585 // because an external viewport is not present.
7586 processPosition(mapper, 100, 100);
7587 processSync(mapper);
7588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7589 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7590
7591 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007592 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007593 processPosition(mapper, 100, 100);
7594 processSync(mapper);
7595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7596 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7597}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007598
7599/**
7600 * Test touch should not work if outside of surface.
7601 */
7602class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7603protected:
7604 void halfDisplayToCenterHorizontal(int32_t orientation) {
7605 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007606 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007607
7608 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7609 internalViewport->orientation = orientation;
7610 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7611 internalViewport->logicalLeft = 0;
7612 internalViewport->logicalTop = 0;
7613 internalViewport->logicalRight = DISPLAY_HEIGHT;
7614 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7615
7616 internalViewport->physicalLeft = 0;
7617 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7618 internalViewport->physicalRight = DISPLAY_HEIGHT;
7619 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7620
7621 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7622 internalViewport->deviceHeight = DISPLAY_WIDTH;
7623 } else {
7624 internalViewport->logicalLeft = 0;
7625 internalViewport->logicalTop = 0;
7626 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7627 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7628
7629 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7630 internalViewport->physicalTop = 0;
7631 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7632 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7633
7634 internalViewport->deviceWidth = DISPLAY_WIDTH;
7635 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7636 }
7637
7638 mFakePolicy->updateViewport(internalViewport.value());
7639 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7640 }
7641
7642 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7643 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7644 int32_t yExpected) {
7645 // touch on outside area should not work.
7646 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7647 processSync(mapper);
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7649
7650 // touch on inside area should receive the event.
7651 NotifyMotionArgs args;
7652 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7653 processSync(mapper);
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7655 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7656 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7657
7658 // Reset.
7659 mapper.reset(ARBITRARY_TIME);
7660 }
7661};
7662
7663TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7664 addConfigurationProperty("touch.deviceType", "touchScreen");
7665 prepareDisplay(DISPLAY_ORIENTATION_0);
7666 prepareAxes(POSITION);
7667 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7668
7669 // Touch on center of normal display should work.
7670 const int32_t x = DISPLAY_WIDTH / 4;
7671 const int32_t y = DISPLAY_HEIGHT / 2;
7672 processPosition(mapper, toRawX(x), toRawY(y));
7673 processSync(mapper);
7674 NotifyMotionArgs args;
7675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7677 0.0f, 0.0f, 0.0f, 0.0f));
7678 // Reset.
7679 mapper.reset(ARBITRARY_TIME);
7680
7681 // Let physical display be different to device, and make surface and physical could be 1:1.
7682 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7683
7684 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7685 const int32_t yExpected = y;
7686 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7687}
7688
7689TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7690 addConfigurationProperty("touch.deviceType", "touchScreen");
7691 prepareDisplay(DISPLAY_ORIENTATION_0);
7692 prepareAxes(POSITION);
7693 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7694
7695 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7696 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7697
7698 const int32_t x = DISPLAY_WIDTH / 4;
7699 const int32_t y = DISPLAY_HEIGHT / 2;
7700
7701 // expect x/y = swap x/y then reverse y.
7702 const int32_t xExpected = y;
7703 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7704 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7705}
7706
7707TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7708 addConfigurationProperty("touch.deviceType", "touchScreen");
7709 prepareDisplay(DISPLAY_ORIENTATION_0);
7710 prepareAxes(POSITION);
7711 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7712
7713 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7714 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7715
7716 const int32_t x = DISPLAY_WIDTH / 4;
7717 const int32_t y = DISPLAY_HEIGHT / 2;
7718
7719 // expect x/y = swap x/y then reverse x.
7720 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7721 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7722 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7723}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007724
7725TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7726 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7727 std::shared_ptr<FakePointerController> fakePointerController =
7728 std::make_shared<FakePointerController>();
7729 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7730 fakePointerController->setPosition(0, 0);
7731 fakePointerController->setButtonState(0);
7732
7733 // prepare device and capture
7734 prepareDisplay(DISPLAY_ORIENTATION_0);
7735 prepareAxes(POSITION | ID | SLOT);
7736 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7737 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7738 mFakePolicy->setPointerCapture(true);
7739 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7740 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7741
7742 // captured touchpad should be a touchpad source
7743 NotifyDeviceResetArgs resetArgs;
7744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7745 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7746
Chris Yef74dc422020-09-02 22:41:50 -07007747 InputDeviceInfo deviceInfo;
7748 mDevice->getDeviceInfo(&deviceInfo);
7749
7750 const InputDeviceInfo::MotionRange* relRangeX =
7751 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
7752 ASSERT_NE(relRangeX, nullptr);
7753 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
7754 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
7755 const InputDeviceInfo::MotionRange* relRangeY =
7756 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
7757 ASSERT_NE(relRangeY, nullptr);
7758 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
7759 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
7760
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007761 // run captured pointer tests - note that this is unscaled, so input listener events should be
7762 // identical to what the hardware sends (accounting for any
7763 // calibration).
7764 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007765 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007766 processId(mapper, 1);
7767 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7768 processKey(mapper, BTN_TOUCH, 1);
7769 processSync(mapper);
7770
7771 // expect coord[0] to contain initial location of touch 0
7772 NotifyMotionArgs args;
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7774 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7775 ASSERT_EQ(1U, args.pointerCount);
7776 ASSERT_EQ(0, args.pointerProperties[0].id);
7777 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7778 ASSERT_NO_FATAL_FAILURE(
7779 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7780
7781 // FINGER 1 DOWN
7782 processSlot(mapper, 1);
7783 processId(mapper, 2);
7784 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7785 processSync(mapper);
7786
7787 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007789 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7790 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007791 ASSERT_EQ(2U, args.pointerCount);
7792 ASSERT_EQ(0, args.pointerProperties[0].id);
7793 ASSERT_EQ(1, args.pointerProperties[1].id);
7794 ASSERT_NO_FATAL_FAILURE(
7795 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7796 ASSERT_NO_FATAL_FAILURE(
7797 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7798
7799 // FINGER 1 MOVE
7800 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7801 processSync(mapper);
7802
7803 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7804 // from move
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7807 ASSERT_NO_FATAL_FAILURE(
7808 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7809 ASSERT_NO_FATAL_FAILURE(
7810 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7811
7812 // FINGER 0 MOVE
7813 processSlot(mapper, 0);
7814 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7815 processSync(mapper);
7816
7817 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7820 ASSERT_NO_FATAL_FAILURE(
7821 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7822 ASSERT_NO_FATAL_FAILURE(
7823 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7824
7825 // BUTTON DOWN
7826 processKey(mapper, BTN_LEFT, 1);
7827 processSync(mapper);
7828
7829 // touchinputmapper design sends a move before button press
7830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7834
7835 // BUTTON UP
7836 processKey(mapper, BTN_LEFT, 0);
7837 processSync(mapper);
7838
7839 // touchinputmapper design sends a move after button release
7840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7844
7845 // FINGER 0 UP
7846 processId(mapper, -1);
7847 processSync(mapper);
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7849 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7850
7851 // FINGER 1 MOVE
7852 processSlot(mapper, 1);
7853 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7854 processSync(mapper);
7855
7856 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7859 ASSERT_EQ(1U, args.pointerCount);
7860 ASSERT_EQ(1, args.pointerProperties[0].id);
7861 ASSERT_NO_FATAL_FAILURE(
7862 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7863
7864 // FINGER 1 UP
7865 processId(mapper, -1);
7866 processKey(mapper, BTN_TOUCH, 0);
7867 processSync(mapper);
7868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7869 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7870
7871 // non captured touchpad should be a mouse source
7872 mFakePolicy->setPointerCapture(false);
7873 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7875 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7876}
7877
7878TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7879 std::shared_ptr<FakePointerController> fakePointerController =
7880 std::make_shared<FakePointerController>();
7881 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7882 fakePointerController->setPosition(0, 0);
7883 fakePointerController->setButtonState(0);
7884
7885 // prepare device and capture
7886 prepareDisplay(DISPLAY_ORIENTATION_0);
7887 prepareAxes(POSITION | ID | SLOT);
7888 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7889 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7890 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7891 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7892 // run uncaptured pointer tests - pushes out generic events
7893 // FINGER 0 DOWN
7894 processId(mapper, 3);
7895 processPosition(mapper, 100, 100);
7896 processKey(mapper, BTN_TOUCH, 1);
7897 processSync(mapper);
7898
7899 // start at (100,100), cursor should be at (0,0) * scale
7900 NotifyMotionArgs args;
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7902 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7903 ASSERT_NO_FATAL_FAILURE(
7904 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7905
7906 // FINGER 0 MOVE
7907 processPosition(mapper, 200, 200);
7908 processSync(mapper);
7909
7910 // compute scaling to help with touch position checking
7911 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7912 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7913 float scale =
7914 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7915
7916 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7920 0, 0, 0, 0, 0, 0, 0));
7921}
7922
7923TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7924 std::shared_ptr<FakePointerController> fakePointerController =
7925 std::make_shared<FakePointerController>();
7926
7927 prepareDisplay(DISPLAY_ORIENTATION_0);
7928 prepareAxes(POSITION | ID | SLOT);
7929 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7930 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7931 mFakePolicy->setPointerCapture(false);
7932 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7933
7934 // uncaptured touchpad should be a pointer device
7935 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7936
7937 // captured touchpad should be a touchpad device
7938 mFakePolicy->setPointerCapture(true);
7939 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7940 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7941}
7942
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943} // namespace android