blob: a72d5c386f65c80466a8bbeea99bd2b4c76ba682 [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:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001033 virtual std::shared_ptr<InputDevice> createDeviceLocked(
1034 int32_t eventHubId, const InputDeviceIdentifier& identifier) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001035 if (!mNextDevices.empty()) {
1036 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1037 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038 return device;
1039 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001040 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041 }
1042
arthurhungdcef2dc2020-08-11 14:47:50 +08001043 // --- FakeInputReaderContext ---
1044 class FakeInputReaderContext : public ContextImpl {
1045 int32_t mGlobalMetaState;
1046 bool mUpdateGlobalMetaStateWasCalled;
1047 int32_t mGeneration;
1048
1049 public:
1050 FakeInputReaderContext(InputReader* reader)
1051 : ContextImpl(reader),
1052 mGlobalMetaState(0),
1053 mUpdateGlobalMetaStateWasCalled(false),
1054 mGeneration(1) {}
1055
1056 virtual ~FakeInputReaderContext() {}
1057
1058 void assertUpdateGlobalMetaStateWasCalled() {
1059 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1060 << "Expected updateGlobalMetaState() to have been called.";
1061 mUpdateGlobalMetaStateWasCalled = false;
1062 }
1063
1064 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1065
1066 uint32_t getGeneration() { return mGeneration; }
1067
1068 void updateGlobalMetaState() override {
1069 mUpdateGlobalMetaStateWasCalled = true;
1070 ContextImpl::updateGlobalMetaState();
1071 }
1072
1073 int32_t getGlobalMetaState() override {
1074 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1075 }
1076
1077 int32_t bumpGeneration() override {
1078 mGeneration = ContextImpl::bumpGeneration();
1079 return mGeneration;
1080 }
1081 } mFakeContext;
1082
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001084
1085public:
1086 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087};
1088
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001089// --- InputReaderPolicyTest ---
1090class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001091protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001092 sp<FakeInputReaderPolicy> mFakePolicy;
1093
Chris Yea52ade12020-08-27 16:49:20 -07001094 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1095 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001096};
1097
1098/**
1099 * Check that empty set of viewports is an acceptable configuration.
1100 * Also try to get internal viewport two different ways - by type and by uniqueId.
1101 *
1102 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1103 * Such configuration is not currently allowed.
1104 */
1105TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001106 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001107
1108 // We didn't add any viewports yet, so there shouldn't be any.
1109 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001110 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001111 ASSERT_FALSE(internalViewport);
1112
1113 // Add an internal viewport, then clear it
1114 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001115 DISPLAY_ORIENTATION_0, uniqueId, NO_PORT,
1116 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001117
1118 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001119 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001120 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001121 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001122
1123 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001124 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001125 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001126 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001127
1128 mFakePolicy->clearViewports();
1129 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001130 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001131 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001132 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001133 ASSERT_FALSE(internalViewport);
1134}
1135
1136TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1137 const std::string internalUniqueId = "local:0";
1138 const std::string externalUniqueId = "local:1";
1139 const std::string virtualUniqueId1 = "virtual:2";
1140 const std::string virtualUniqueId2 = "virtual:3";
1141 constexpr int32_t virtualDisplayId1 = 2;
1142 constexpr int32_t virtualDisplayId2 = 3;
1143
1144 // Add an internal viewport
1145 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001146 DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT,
1147 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001148 // Add an external viewport
1149 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001150 DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT,
1151 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001152 // Add an virtual viewport
1153 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001154 DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT,
1155 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001156 // Add another virtual viewport
1157 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001158 DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT,
1159 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001160
1161 // Check matching by type for internal
1162 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001163 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001164 ASSERT_TRUE(internalViewport);
1165 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1166
1167 // Check matching by type for external
1168 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001169 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001170 ASSERT_TRUE(externalViewport);
1171 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1172
1173 // Check matching by uniqueId for virtual viewport #1
1174 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001175 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001176 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001177 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001178 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1179 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1180
1181 // Check matching by uniqueId for virtual viewport #2
1182 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001183 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001184 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001185 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001186 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1187 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1188}
1189
1190
1191/**
1192 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1193 * that lookup works by checking display id.
1194 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1195 */
1196TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1197 const std::string uniqueId1 = "uniqueId1";
1198 const std::string uniqueId2 = "uniqueId2";
1199 constexpr int32_t displayId1 = 2;
1200 constexpr int32_t displayId2 = 3;
1201
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001202 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1203 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001204 for (const ViewportType& type : types) {
1205 mFakePolicy->clearViewports();
1206 // Add a viewport
1207 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001208 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001209 // Add another viewport
1210 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001211 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001212
1213 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001214 std::optional<DisplayViewport> viewport1 =
1215 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001216 ASSERT_TRUE(viewport1);
1217 ASSERT_EQ(displayId1, viewport1->displayId);
1218 ASSERT_EQ(type, viewport1->type);
1219
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001220 std::optional<DisplayViewport> viewport2 =
1221 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001222 ASSERT_TRUE(viewport2);
1223 ASSERT_EQ(displayId2, viewport2->displayId);
1224 ASSERT_EQ(type, viewport2->type);
1225
1226 // When there are multiple viewports of the same kind, and uniqueId is not specified
1227 // in the call to getDisplayViewport, then that situation is not supported.
1228 // The viewports can be stored in any order, so we cannot rely on the order, since that
1229 // is just implementation detail.
1230 // However, we can check that it still returns *a* viewport, we just cannot assert
1231 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001232 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001233 ASSERT_TRUE(someViewport);
1234 }
1235}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001237/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001238 * When we have multiple internal displays make sure we always return the default display when
1239 * querying by type.
1240 */
1241TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1242 const std::string uniqueId1 = "uniqueId1";
1243 const std::string uniqueId2 = "uniqueId2";
1244 constexpr int32_t nonDefaultDisplayId = 2;
1245 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1246 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1247
1248 // Add the default display first and ensure it gets returned.
1249 mFakePolicy->clearViewports();
1250 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1251 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT,
1252 ViewportType::INTERNAL);
1253 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1254 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT,
1255 ViewportType::INTERNAL);
1256
1257 std::optional<DisplayViewport> viewport =
1258 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1259 ASSERT_TRUE(viewport);
1260 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1261 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1262
1263 // Add the default display second to make sure order doesn't matter.
1264 mFakePolicy->clearViewports();
1265 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1266 DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT,
1267 ViewportType::INTERNAL);
1268 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1269 DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT,
1270 ViewportType::INTERNAL);
1271
1272 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1273 ASSERT_TRUE(viewport);
1274 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1275 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1276}
1277
1278/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001279 * Check getDisplayViewportByPort
1280 */
1281TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001282 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001283 const std::string uniqueId1 = "uniqueId1";
1284 const std::string uniqueId2 = "uniqueId2";
1285 constexpr int32_t displayId1 = 1;
1286 constexpr int32_t displayId2 = 2;
1287 const uint8_t hdmi1 = 0;
1288 const uint8_t hdmi2 = 1;
1289 const uint8_t hdmi3 = 2;
1290
1291 mFakePolicy->clearViewports();
1292 // Add a viewport that's associated with some display port that's not of interest.
1293 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1294 DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1295 // Add another viewport, connected to HDMI1 port
1296 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1297 DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1298
1299 // Check that correct display viewport was returned by comparing the display ports.
1300 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1301 ASSERT_TRUE(hdmi1Viewport);
1302 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1303 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1304
1305 // Check that we can still get the same viewport using the uniqueId
1306 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1307 ASSERT_TRUE(hdmi1Viewport);
1308 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1309 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1310 ASSERT_EQ(type, hdmi1Viewport->type);
1311
1312 // Check that we cannot find a port with "HDMI2", because we never added one
1313 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1314 ASSERT_FALSE(hdmi2Viewport);
1315}
1316
Michael Wrightd02c5b62014-02-10 15:10:22 -08001317// --- InputReaderTest ---
1318
1319class InputReaderTest : public testing::Test {
1320protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001321 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001323 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001324 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325
Chris Yea52ade12020-08-27 16:49:20 -07001326 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001327 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001329 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330
Prabir Pradhan28efc192019-11-05 01:10:04 +00001331 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1332 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001333 }
1334
Chris Yea52ade12020-08-27 16:49:20 -07001335 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001336 mFakeListener.clear();
1337 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001338 }
1339
Chris Ye1b0c7342020-07-28 21:57:03 -07001340 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001341 const PropertyMap* configuration) {
1342 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001343
1344 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001345 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001346 }
1347 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001348 mReader->loopOnce();
1349 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001350 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1351 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001352 }
1353
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001354 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001355 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001356 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001357 }
1358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001359 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001360 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001361 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001362 }
1363
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001364 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001365 const std::string& name,
1366 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001367 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001368 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1369 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001370 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001371 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001372 return mapper;
1373 }
1374};
1375
Chris Ye98d3f532020-10-01 21:48:59 -07001376TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001377 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1378 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1379 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380
Chris Ye98d3f532020-10-01 21:48:59 -07001381 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001382 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001383 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001384 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001385 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1386 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1387 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001388}
1389
1390TEST_F(InputReaderTest, PolicyGetInputDevices) {
1391 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1392 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1393 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001394
1395 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001396 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001398 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001399 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001400 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1401 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1402 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1403}
1404
Chris Yee7310032020-09-22 15:36:28 -07001405TEST_F(InputReaderTest, GetMergedInputDevices) {
1406 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1407 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1408 // Add two subdevices to device
1409 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1410 // Must add at least one mapper or the device will be ignored!
1411 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1412 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1413
1414 // Push same device instance for next device to be added, so they'll have same identifier.
1415 mReader->pushNextDevice(device);
1416 mReader->pushNextDevice(device);
1417 ASSERT_NO_FATAL_FAILURE(
1418 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1419 ASSERT_NO_FATAL_FAILURE(
1420 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1421
1422 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001423 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001424}
1425
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001426TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001427 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001428 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001429 constexpr int32_t eventHubId = 1;
1430 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001431 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001432 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001433 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001434 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001435
Yi Kong9b14ac62018-07-17 13:48:38 -07001436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001437
1438 NotifyDeviceResetArgs resetArgs;
1439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001440 ASSERT_EQ(deviceId, resetArgs.deviceId);
1441
1442 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001443 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001444 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001445
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001447 ASSERT_EQ(deviceId, resetArgs.deviceId);
1448 ASSERT_EQ(device->isEnabled(), false);
1449
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001450 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001451 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001454 ASSERT_EQ(device->isEnabled(), false);
1455
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001456 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001457 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001459 ASSERT_EQ(deviceId, resetArgs.deviceId);
1460 ASSERT_EQ(device->isEnabled(), true);
1461}
1462
Michael Wrightd02c5b62014-02-10 15:10:22 -08001463TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001464 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001465 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001466 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001467 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001468 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001469 AINPUT_SOURCE_KEYBOARD, nullptr);
1470 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001471
1472 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1473 AINPUT_SOURCE_ANY, AKEYCODE_A))
1474 << "Should return unknown when the device id is >= 0 but unknown.";
1475
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001476 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1477 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1478 << "Should return unknown when the device id is valid but the sources are not "
1479 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001480
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001481 ASSERT_EQ(AKEY_STATE_DOWN,
1482 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1483 AKEYCODE_A))
1484 << "Should return value provided by mapper when device id is valid and the device "
1485 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001486
1487 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1488 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1489 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1490
1491 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1492 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1493 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1494}
1495
1496TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001497 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001498 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001499 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001500 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001501 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001502 AINPUT_SOURCE_KEYBOARD, nullptr);
1503 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504
1505 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1506 AINPUT_SOURCE_ANY, KEY_A))
1507 << "Should return unknown when the device id is >= 0 but unknown.";
1508
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001509 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1510 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1511 << "Should return unknown when the device id is valid but the sources are not "
1512 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001513
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001514 ASSERT_EQ(AKEY_STATE_DOWN,
1515 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1516 KEY_A))
1517 << "Should return value provided by mapper when device id is valid and the device "
1518 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519
1520 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1521 AINPUT_SOURCE_TRACKBALL, KEY_A))
1522 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1523
1524 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1525 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1526 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1527}
1528
1529TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001530 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001531 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001532 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001533 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001534 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001535 AINPUT_SOURCE_KEYBOARD, nullptr);
1536 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001537
1538 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1539 AINPUT_SOURCE_ANY, SW_LID))
1540 << "Should return unknown when the device id is >= 0 but unknown.";
1541
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001542 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1543 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1544 << "Should return unknown when the device id is valid but the sources are not "
1545 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001546
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001547 ASSERT_EQ(AKEY_STATE_DOWN,
1548 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1549 SW_LID))
1550 << "Should return value provided by mapper when device id is valid and the device "
1551 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001552
1553 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1554 AINPUT_SOURCE_TRACKBALL, SW_LID))
1555 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1556
1557 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1558 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1559 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1560}
1561
1562TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001563 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001564 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001565 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001566 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001567 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001568 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001569
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001570 mapper.addSupportedKeyCode(AKEYCODE_A);
1571 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001572
1573 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1574 uint8_t flags[4] = { 0, 0, 0, 1 };
1575
1576 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1577 << "Should return false when device id is >= 0 but unknown.";
1578 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1579
1580 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001581 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1582 << "Should return false when device id is valid but the sources are not supported by "
1583 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1585
1586 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1588 keyCodes, flags))
1589 << "Should return value provided by mapper when device id is valid and the device "
1590 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1592
1593 flags[3] = 1;
1594 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1595 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1596 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1597
1598 flags[3] = 1;
1599 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1600 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1601 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1602}
1603
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001604TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001606 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607
1608 NotifyConfigurationChangedArgs args;
1609
1610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1611 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1612}
1613
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001614TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001616 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001617 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001618 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001619 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001620 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001622 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001623 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001624 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1625
1626 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 ASSERT_EQ(EV_KEY, event.type);
1631 ASSERT_EQ(KEY_A, event.code);
1632 ASSERT_EQ(1, event.value);
1633}
1634
Garfield Tan1c7bc862020-01-28 13:24:04 -08001635TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001636 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001637 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001638 constexpr int32_t eventHubId = 1;
1639 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001640 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001642 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001644
1645 NotifyDeviceResetArgs resetArgs;
1646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001647 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001648
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001649 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001650 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001652 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001653 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001654
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001655 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001656 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001658 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001659 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001661 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001662 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001664 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001665 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001666}
1667
Garfield Tan1c7bc862020-01-28 13:24:04 -08001668TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1669 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001670 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001671 constexpr int32_t eventHubId = 1;
1672 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1673 // Must add at least one mapper or the device will be ignored!
1674 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001675 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001676 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1677
1678 NotifyDeviceResetArgs resetArgs;
1679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1680 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1681}
1682
Arthur Hungc23540e2018-11-29 20:42:11 +08001683TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001684 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001685 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001687 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001688 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1689 FakeInputMapper& mapper =
1690 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001691 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001692
1693 const uint8_t hdmi1 = 1;
1694
1695 // Associated touch screen with second display.
1696 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1697
1698 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001699 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001700 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001701 DISPLAY_ORIENTATION_0, "local:0", NO_PORT,
1702 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001703 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001704 DISPLAY_ORIENTATION_0, "local:1", hdmi1,
1705 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001706 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001707 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001708
1709 // Add the device, and make sure all of the callbacks are triggered.
1710 // The device is added after the input port associations are processed since
1711 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001715 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001716
Arthur Hung2c9a3342019-07-23 14:18:59 +08001717 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001718 ASSERT_EQ(deviceId, device->getId());
1719 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1720 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001721
1722 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001724 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001725 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001726}
1727
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001728TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1729 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1730 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1731 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1732 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1733 // Must add at least one mapper or the device will be ignored!
1734 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1735 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1736 mReader->pushNextDevice(device);
1737 mReader->pushNextDevice(device);
1738 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1739 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1740
1741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1742
1743 NotifyDeviceResetArgs resetArgs;
1744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1745 ASSERT_EQ(deviceId, resetArgs.deviceId);
1746 ASSERT_TRUE(device->isEnabled());
1747 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1748 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1749
1750 disableDevice(deviceId);
1751 mReader->loopOnce();
1752
1753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1754 ASSERT_EQ(deviceId, resetArgs.deviceId);
1755 ASSERT_FALSE(device->isEnabled());
1756 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1757 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1758
1759 enableDevice(deviceId);
1760 mReader->loopOnce();
1761
1762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1763 ASSERT_EQ(deviceId, resetArgs.deviceId);
1764 ASSERT_TRUE(device->isEnabled());
1765 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1766 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1767}
1768
1769TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1770 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1771 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1772 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1773 // Add two subdevices to device
1774 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1775 FakeInputMapper& mapperDevice1 =
1776 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1777 FakeInputMapper& mapperDevice2 =
1778 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1779 mReader->pushNextDevice(device);
1780 mReader->pushNextDevice(device);
1781 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1782 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1783
1784 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1785 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1786
1787 ASSERT_EQ(AKEY_STATE_DOWN,
1788 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1789 ASSERT_EQ(AKEY_STATE_DOWN,
1790 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1791 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1792 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1793}
1794
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001795// --- InputReaderIntegrationTest ---
1796
1797// These tests create and interact with the InputReader only through its interface.
1798// The InputReader is started during SetUp(), which starts its processing in its own
1799// thread. The tests use linux uinput to emulate input devices.
1800// NOTE: Interacting with the physical device while these tests are running may cause
1801// the tests to fail.
1802class InputReaderIntegrationTest : public testing::Test {
1803protected:
1804 sp<TestInputListener> mTestListener;
1805 sp<FakeInputReaderPolicy> mFakePolicy;
1806 sp<InputReaderInterface> mReader;
1807
Chris Yea52ade12020-08-27 16:49:20 -07001808 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001809 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001810 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1811 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001812
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001813 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001814 ASSERT_EQ(mReader->start(), OK);
1815
1816 // Since this test is run on a real device, all the input devices connected
1817 // to the test device will show up in mReader. We wait for those input devices to
1818 // show up before beginning the tests.
1819 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1820 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1821 }
1822
Chris Yea52ade12020-08-27 16:49:20 -07001823 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001824 ASSERT_EQ(mReader->stop(), OK);
1825 mTestListener.clear();
1826 mFakePolicy.clear();
1827 }
1828};
1829
1830TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1831 // An invalid input device that is only used for this test.
1832 class InvalidUinputDevice : public UinputDevice {
1833 public:
1834 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1835
1836 private:
1837 void configureDevice(int fd, uinput_user_dev* device) override {}
1838 };
1839
1840 const size_t numDevices = mFakePolicy->getInputDevices().size();
1841
1842 // UinputDevice does not set any event or key bits, so InputReader should not
1843 // consider it as a valid device.
1844 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1845 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1846 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1847 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1848
1849 invalidDevice.reset();
1850 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1851 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1852 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1853}
1854
1855TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1856 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1857
1858 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1859 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1860 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1861 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1862
1863 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07001864 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
1865 const auto& it =
1866 std::find_if(inputDevices.begin(), inputDevices.end(),
1867 [&keyboard](const InputDeviceInfo& info) {
1868 return info.getIdentifier().name == keyboard->getName();
1869 });
1870
1871 ASSERT_NE(it, inputDevices.end());
1872 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
1873 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
1874 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001875
1876 keyboard.reset();
1877 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1878 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1879 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1880}
1881
1882TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1883 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1884 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1885
1886 NotifyConfigurationChangedArgs configChangedArgs;
1887 ASSERT_NO_FATAL_FAILURE(
1888 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001889 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001890 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1891
1892 NotifyKeyArgs keyArgs;
1893 keyboard->pressAndReleaseHomeKey();
1894 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1895 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001896 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001897 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001898 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1899 prevTimestamp = keyArgs.eventTime;
1900
1901 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1902 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001903 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001904 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1905}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001906
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001907/**
1908 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1909 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1910 * are passed to the listener.
1911 */
1912static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1913TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1914 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1915 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1916 NotifyKeyArgs keyArgs;
1917
1918 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1919 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1920 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1921 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1922
1923 controller->pressAndReleaseKey(BTN_GEAR_UP);
1924 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1925 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1926 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1927}
1928
Arthur Hungaab25622020-01-16 11:22:11 +08001929// --- TouchProcessTest ---
1930class TouchIntegrationTest : public InputReaderIntegrationTest {
1931protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001932 const std::string UNIQUE_ID = "local:0";
1933
Chris Yea52ade12020-08-27 16:49:20 -07001934 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08001935 InputReaderIntegrationTest::SetUp();
1936 // At least add an internal display.
1937 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1938 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001939 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001940
1941 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1942 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1943 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1944 }
1945
1946 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1947 int32_t orientation, const std::string& uniqueId,
1948 std::optional<uint8_t> physicalPort,
1949 ViewportType viewportType) {
1950 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1951 physicalPort, viewportType);
1952 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1953 }
1954
1955 std::unique_ptr<UinputTouchScreen> mDevice;
1956};
1957
1958TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1959 NotifyMotionArgs args;
1960 const Point centerPoint = mDevice->getCenterPoint();
1961
1962 // ACTION_DOWN
1963 mDevice->sendDown(centerPoint);
1964 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1965 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1966
1967 // ACTION_MOVE
1968 mDevice->sendMove(centerPoint + Point(1, 1));
1969 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1971
1972 // ACTION_UP
1973 mDevice->sendUp();
1974 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1975 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1976}
1977
1978TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1979 NotifyMotionArgs args;
1980 const Point centerPoint = mDevice->getCenterPoint();
1981
1982 // ACTION_DOWN
1983 mDevice->sendDown(centerPoint);
1984 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1985 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1986
1987 // ACTION_POINTER_DOWN (Second slot)
1988 const Point secondPoint = centerPoint + Point(100, 100);
1989 mDevice->sendSlot(SECOND_SLOT);
1990 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1991 mDevice->sendDown(secondPoint + Point(1, 1));
1992 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1993 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1994 args.action);
1995
1996 // ACTION_MOVE (Second slot)
1997 mDevice->sendMove(secondPoint);
1998 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2000
2001 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002002 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002003 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002004 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002005 args.action);
2006
2007 // ACTION_UP
2008 mDevice->sendSlot(FIRST_SLOT);
2009 mDevice->sendUp();
2010 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2011 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2012}
2013
2014TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2015 NotifyMotionArgs args;
2016 const Point centerPoint = mDevice->getCenterPoint();
2017
2018 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002019 mDevice->sendSlot(FIRST_SLOT);
2020 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002021 mDevice->sendDown(centerPoint);
2022 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2023 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2024
arthurhungcc7f9802020-04-30 17:55:40 +08002025 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002026 const Point secondPoint = centerPoint + Point(100, 100);
2027 mDevice->sendSlot(SECOND_SLOT);
2028 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2029 mDevice->sendDown(secondPoint);
2030 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2031 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2032 args.action);
2033
arthurhungcc7f9802020-04-30 17:55:40 +08002034 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002035 mDevice->sendMove(secondPoint + Point(1, 1));
2036 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2038
arthurhungcc7f9802020-04-30 17:55:40 +08002039 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2040 // a palm event.
2041 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002042 mDevice->sendToolType(MT_TOOL_PALM);
2043 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002044 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2045 args.action);
2046 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002047
arthurhungcc7f9802020-04-30 17:55:40 +08002048 // Send up to second slot, expect first slot send moving.
2049 mDevice->sendPointerUp();
2050 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002052
arthurhungcc7f9802020-04-30 17:55:40 +08002053 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002054 mDevice->sendSlot(FIRST_SLOT);
2055 mDevice->sendUp();
2056
arthurhungcc7f9802020-04-30 17:55:40 +08002057 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2058 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002059}
2060
Michael Wrightd02c5b62014-02-10 15:10:22 -08002061// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002062class InputDeviceTest : public testing::Test {
2063protected:
2064 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002065 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002066 static const int32_t DEVICE_ID;
2067 static const int32_t DEVICE_GENERATION;
2068 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002069 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002070 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002071
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002072 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002073 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002074 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002075 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002076 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002077
Chris Yea52ade12020-08-27 16:49:20 -07002078 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002079 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002080 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002081 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002082 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2083 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002084 InputDeviceIdentifier identifier;
2085 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002086 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002087 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002088 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002089 mReader->pushNextDevice(mDevice);
2090 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2091 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002092 }
2093
Chris Yea52ade12020-08-27 16:49:20 -07002094 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002095 mFakeListener.clear();
2096 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002097 }
2098};
2099
2100const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002101const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002102const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002103const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2104const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002105const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2106 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002107const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002108
2109TEST_F(InputDeviceTest, ImmutableProperties) {
2110 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002111 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002112 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002113}
2114
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002115TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2116 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002117}
2118
Michael Wrightd02c5b62014-02-10 15:10:22 -08002119TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2120 // Configuration.
2121 InputReaderConfiguration config;
2122 mDevice->configure(ARBITRARY_TIME, &config, 0);
2123
2124 // Reset.
2125 mDevice->reset(ARBITRARY_TIME);
2126
2127 NotifyDeviceResetArgs resetArgs;
2128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2129 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2130 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2131
2132 // Metadata.
2133 ASSERT_TRUE(mDevice->isIgnored());
2134 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2135
2136 InputDeviceInfo info;
2137 mDevice->getDeviceInfo(&info);
2138 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002139 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002140 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2141 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2142
2143 // State queries.
2144 ASSERT_EQ(0, mDevice->getMetaState());
2145
2146 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2147 << "Ignored device should return unknown key code state.";
2148 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2149 << "Ignored device should return unknown scan code state.";
2150 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2151 << "Ignored device should return unknown switch state.";
2152
2153 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2154 uint8_t flags[2] = { 0, 1 };
2155 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2156 << "Ignored device should never mark any key codes.";
2157 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2158 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2159}
2160
2161TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2162 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002163 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002164
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002165 FakeInputMapper& mapper1 =
2166 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002167 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2168 mapper1.setMetaState(AMETA_ALT_ON);
2169 mapper1.addSupportedKeyCode(AKEYCODE_A);
2170 mapper1.addSupportedKeyCode(AKEYCODE_B);
2171 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2172 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2173 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2174 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2175 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002176
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002177 FakeInputMapper& mapper2 =
2178 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002179 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002180
2181 InputReaderConfiguration config;
2182 mDevice->configure(ARBITRARY_TIME, &config, 0);
2183
2184 String8 propertyValue;
2185 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2186 << "Device should have read configuration during configuration phase.";
2187 ASSERT_STREQ("value", propertyValue.string());
2188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002189 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2190 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002191
2192 // Reset
2193 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002194 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2195 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002196
2197 NotifyDeviceResetArgs resetArgs;
2198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2199 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2200 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2201
2202 // Metadata.
2203 ASSERT_FALSE(mDevice->isIgnored());
2204 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2205
2206 InputDeviceInfo info;
2207 mDevice->getDeviceInfo(&info);
2208 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002209 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2211 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2212
2213 // State queries.
2214 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2215 << "Should query mappers and combine meta states.";
2216
2217 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2218 << "Should return unknown key code state when source not supported.";
2219 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2220 << "Should return unknown scan code state when source not supported.";
2221 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2222 << "Should return unknown switch state when source not supported.";
2223
2224 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2225 << "Should query mapper when source is supported.";
2226 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2227 << "Should query mapper when source is supported.";
2228 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2229 << "Should query mapper when source is supported.";
2230
2231 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2232 uint8_t flags[4] = { 0, 0, 0, 1 };
2233 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2234 << "Should do nothing when source is unsupported.";
2235 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2236 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2237 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2238 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2239
2240 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2241 << "Should query mapper when source is supported.";
2242 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2243 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2244 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2245 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2246
2247 // Event handling.
2248 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002249 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002250 mDevice->process(&event, 1);
2251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002252 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2253 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002254}
2255
Arthur Hung2c9a3342019-07-23 14:18:59 +08002256// A single input device is associated with a specific display. Check that:
2257// 1. Device is disabled if the viewport corresponding to the associated display is not found
2258// 2. Device is disabled when setEnabled API is called
2259TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002260 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002261
2262 // First Configuration.
2263 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2264
2265 // Device should be enabled by default.
2266 ASSERT_TRUE(mDevice->isEnabled());
2267
2268 // Prepare associated info.
2269 constexpr uint8_t hdmi = 1;
2270 const std::string UNIQUE_ID = "local:1";
2271
2272 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2273 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2274 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2275 // Device should be disabled because it is associated with a specific display via
2276 // input port <-> display port association, but the corresponding display is not found
2277 ASSERT_FALSE(mDevice->isEnabled());
2278
2279 // Prepare displays.
2280 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002281 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002282 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2283 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2284 ASSERT_TRUE(mDevice->isEnabled());
2285
2286 // Device should be disabled after set disable.
2287 mFakePolicy->addDisabledDevice(mDevice->getId());
2288 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2289 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2290 ASSERT_FALSE(mDevice->isEnabled());
2291
2292 // Device should still be disabled even found the associated display.
2293 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2294 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2295 ASSERT_FALSE(mDevice->isEnabled());
2296}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002297
2298// --- InputMapperTest ---
2299
2300class InputMapperTest : public testing::Test {
2301protected:
2302 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002303 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002304 static const int32_t DEVICE_ID;
2305 static const int32_t DEVICE_GENERATION;
2306 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002307 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002308 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002309
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002310 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002311 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002312 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002313 std::unique_ptr<InstrumentedInputReader> mReader;
2314 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002315
Chris Ye1b0c7342020-07-28 21:57:03 -07002316 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002317 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002318 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002319 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002320 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2321 mFakeListener);
2322 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323 }
2324
Chris Yea52ade12020-08-27 16:49:20 -07002325 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002326
Chris Yea52ade12020-08-27 16:49:20 -07002327 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002328 mFakeListener.clear();
2329 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002330 }
2331
2332 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002333 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002334 }
2335
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002336 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002337 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002338 mReader->requestRefreshConfiguration(changes);
2339 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002340 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002341 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2342 }
2343
arthurhungdcef2dc2020-08-11 14:47:50 +08002344 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2345 const std::string& location, int32_t eventHubId,
2346 Flags<InputDeviceClass> classes) {
2347 InputDeviceIdentifier identifier;
2348 identifier.name = name;
2349 identifier.location = location;
2350 std::shared_ptr<InputDevice> device =
2351 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2352 identifier);
2353 mReader->pushNextDevice(device);
2354 mFakeEventHub->addDevice(eventHubId, name, classes);
2355 mReader->loopOnce();
2356 return device;
2357 }
2358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002359 template <class T, typename... Args>
2360 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002361 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002362 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002364 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002365 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002366 }
2367
2368 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002369 int32_t orientation, const std::string& uniqueId,
2370 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002371 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002372 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002373 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2374 }
2375
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002376 void clearViewports() {
2377 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378 }
2379
arthurhungdcef2dc2020-08-11 14:47:50 +08002380 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 RawEvent event;
2382 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002383 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384 event.type = type;
2385 event.code = code;
2386 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002387 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002388 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 }
2390
2391 static void assertMotionRange(const InputDeviceInfo& info,
2392 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2393 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002394 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2396 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2397 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2398 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2399 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2400 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2401 }
2402
2403 static void assertPointerCoords(const PointerCoords& coords,
2404 float x, float y, float pressure, float size,
2405 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2406 float orientation, float distance) {
2407 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2408 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2409 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2410 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2411 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2412 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2413 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2414 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2415 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2416 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2417 }
2418
Michael Wright17db18e2020-06-26 20:51:44 +01002419 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002421 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422 ASSERT_NEAR(x, actualX, 1);
2423 ASSERT_NEAR(y, actualY, 1);
2424 }
2425};
2426
2427const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002428const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002429const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2431const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002432const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2433 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002434const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435
2436// --- SwitchInputMapperTest ---
2437
2438class SwitchInputMapperTest : public InputMapperTest {
2439protected:
2440};
2441
2442TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002443 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002445 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446}
2447
2448TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002449 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002451 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002452 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002453
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002454 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002455 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456}
2457
2458TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002459 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002461 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2462 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2463 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2464 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465
2466 NotifySwitchArgs args;
2467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2468 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002469 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2470 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471 args.switchMask);
2472 ASSERT_EQ(uint32_t(0), args.policyFlags);
2473}
2474
2475
2476// --- KeyboardInputMapperTest ---
2477
2478class KeyboardInputMapperTest : public InputMapperTest {
2479protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002480 const std::string UNIQUE_ID = "local:0";
2481
2482 void prepareDisplay(int32_t orientation);
2483
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002484 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002485 int32_t originalKeyCode, int32_t rotatedKeyCode,
2486 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002487};
2488
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002489/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2490 * orientation.
2491 */
2492void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002493 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2494 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002495}
2496
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002497void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002498 int32_t originalScanCode, int32_t originalKeyCode,
2499 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002500 NotifyKeyArgs args;
2501
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002502 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2504 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2505 ASSERT_EQ(originalScanCode, args.scanCode);
2506 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002507 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002509 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2511 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2512 ASSERT_EQ(originalScanCode, args.scanCode);
2513 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002514 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515}
2516
Michael Wrightd02c5b62014-02-10 15:10:22 -08002517TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002518 KeyboardInputMapper& mapper =
2519 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2520 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002522 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523}
2524
2525TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2526 const int32_t USAGE_A = 0x070004;
2527 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002528 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2529 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002530 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2531 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2532 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002534 KeyboardInputMapper& mapper =
2535 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2536 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002537 // Initial metastate to AMETA_NONE.
2538 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2539 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540
2541 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002542 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543 NotifyKeyArgs args;
2544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2545 ASSERT_EQ(DEVICE_ID, args.deviceId);
2546 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2547 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2548 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2549 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2550 ASSERT_EQ(KEY_HOME, args.scanCode);
2551 ASSERT_EQ(AMETA_NONE, args.metaState);
2552 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2553 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2554 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2555
2556 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002557 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2559 ASSERT_EQ(DEVICE_ID, args.deviceId);
2560 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2561 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2562 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2563 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2564 ASSERT_EQ(KEY_HOME, args.scanCode);
2565 ASSERT_EQ(AMETA_NONE, args.metaState);
2566 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2567 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2568 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2569
2570 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002571 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2572 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2574 ASSERT_EQ(DEVICE_ID, args.deviceId);
2575 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2576 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2577 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2578 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2579 ASSERT_EQ(0, args.scanCode);
2580 ASSERT_EQ(AMETA_NONE, args.metaState);
2581 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2582 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2583 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2584
2585 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002586 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2587 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2589 ASSERT_EQ(DEVICE_ID, args.deviceId);
2590 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2591 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2592 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2593 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2594 ASSERT_EQ(0, args.scanCode);
2595 ASSERT_EQ(AMETA_NONE, args.metaState);
2596 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2597 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2598 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2599
2600 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002601 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2602 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2604 ASSERT_EQ(DEVICE_ID, args.deviceId);
2605 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2606 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2607 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2608 ASSERT_EQ(0, args.keyCode);
2609 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2610 ASSERT_EQ(AMETA_NONE, args.metaState);
2611 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2612 ASSERT_EQ(0U, args.policyFlags);
2613 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2614
2615 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002616 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2617 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2619 ASSERT_EQ(DEVICE_ID, args.deviceId);
2620 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2621 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2622 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2623 ASSERT_EQ(0, args.keyCode);
2624 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2625 ASSERT_EQ(AMETA_NONE, args.metaState);
2626 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2627 ASSERT_EQ(0U, args.policyFlags);
2628 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2629}
2630
2631TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002632 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002634 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2635 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2636 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002638 KeyboardInputMapper& mapper =
2639 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2640 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641
arthurhungc903df12020-08-11 15:08:42 +08002642 // Initial metastate to AMETA_NONE.
2643 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2644 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645
2646 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002647 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648 NotifyKeyArgs args;
2649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2650 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002651 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002652 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
2654 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002655 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2657 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002658 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659
2660 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002661 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2663 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002664 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
2666 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002667 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2669 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002670 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002671 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672}
2673
2674TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002675 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2677 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2678 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002680 KeyboardInputMapper& mapper =
2681 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2682 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002684 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2686 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2687 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2688 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2689 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2690 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2691 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2692 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2693}
2694
2695TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002696 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2697 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2698 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2699 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002702 KeyboardInputMapper& mapper =
2703 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2704 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002706 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002707 ASSERT_NO_FATAL_FAILURE(
2708 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2709 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2710 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2711 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2712 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2713 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2714 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002716 clearViewports();
2717 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002718 ASSERT_NO_FATAL_FAILURE(
2719 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2720 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2721 AKEYCODE_DPAD_UP, DISPLAY_ID));
2722 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2723 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2724 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2725 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002727 clearViewports();
2728 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002729 ASSERT_NO_FATAL_FAILURE(
2730 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2731 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2732 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2733 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2734 AKEYCODE_DPAD_UP, DISPLAY_ID));
2735 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2736 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002738 clearViewports();
2739 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002740 ASSERT_NO_FATAL_FAILURE(
2741 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2742 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2743 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2744 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2745 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2746 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2747 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748
2749 // Special case: if orientation changes while key is down, we still emit the same keycode
2750 // in the key up as we did in the key down.
2751 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002752 clearViewports();
2753 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002754 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2756 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2757 ASSERT_EQ(KEY_UP, args.scanCode);
2758 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2759
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002760 clearViewports();
2761 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002762 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2764 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2765 ASSERT_EQ(KEY_UP, args.scanCode);
2766 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2767}
2768
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002769TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2770 // If the keyboard is not orientation aware,
2771 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002772 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002774 KeyboardInputMapper& mapper =
2775 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2776 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002777 NotifyKeyArgs args;
2778
2779 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002780 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002782 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2784 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2785
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002786 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002787 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002789 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2791 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2792}
2793
2794TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2795 // If the keyboard is orientation aware,
2796 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002797 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002798
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002799 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002800 KeyboardInputMapper& mapper =
2801 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2802 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002803 NotifyKeyArgs args;
2804
2805 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2806 // ^--- already checked by the previous test
2807
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002808 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002809 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002810 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002812 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2814 ASSERT_EQ(DISPLAY_ID, args.displayId);
2815
2816 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002817 clearViewports();
2818 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002819 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002820 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002822 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2824 ASSERT_EQ(newDisplayId, args.displayId);
2825}
2826
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002828 KeyboardInputMapper& mapper =
2829 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2830 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002832 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002833 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002835 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002836 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837}
2838
2839TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002840 KeyboardInputMapper& mapper =
2841 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2842 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002844 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002845 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002847 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002848 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849}
2850
2851TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
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->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857
2858 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2859 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002860 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861 ASSERT_TRUE(flags[0]);
2862 ASSERT_FALSE(flags[1]);
2863}
2864
2865TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002866 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2867 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2868 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2869 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2870 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002873 KeyboardInputMapper& mapper =
2874 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2875 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07002876 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08002877 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2878 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879
2880 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002881 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2882 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2883 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002884
2885 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002886 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2887 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002888 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2889 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2890 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002891 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892
2893 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002894 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2895 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002896 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2897 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2898 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002899 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002900
2901 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002902 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2903 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002904 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2905 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2906 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002907 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002908
2909 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002910 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2911 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002912 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2913 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2914 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002915 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916
2917 // Toggle num lock off.
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_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2921 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2922 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002923 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924
2925 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002926 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2927 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002928 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2929 ASSERT_FALSE(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_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932}
2933
Chris Yea52ade12020-08-27 16:49:20 -07002934TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
2935 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
2936 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
2937 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
2938 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
2939
2940 KeyboardInputMapper& mapper =
2941 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2942 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
2943
2944 // Initial metastate should be AMETA_NONE as no meta keys added.
2945 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2946 // Meta state should be AMETA_NONE after reset
2947 mapper.reset(ARBITRARY_TIME);
2948 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2949 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
2950 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
2951 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2952
2953 NotifyKeyArgs args;
2954 // Press button "A"
2955 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
2956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2957 ASSERT_EQ(AMETA_NONE, args.metaState);
2958 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2959 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2960 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2961
2962 // Button up.
2963 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
2964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2965 ASSERT_EQ(AMETA_NONE, args.metaState);
2966 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2967 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2968 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2969}
2970
Arthur Hung2c9a3342019-07-23 14:18:59 +08002971TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2972 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002973 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2974 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2975 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2976 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002977
2978 // keyboard 2.
2979 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08002980 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002981 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002982 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08002983 std::shared_ptr<InputDevice> device2 =
2984 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
2985 Flags<InputDeviceClass>(0));
2986
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002987 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2988 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2989 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2990 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002991
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002992 KeyboardInputMapper& mapper =
2993 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2994 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002996 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002997 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002998 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002999 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3000 device2->reset(ARBITRARY_TIME);
3001
3002 // Prepared displays and associated info.
3003 constexpr uint8_t hdmi1 = 0;
3004 constexpr uint8_t hdmi2 = 1;
3005 const std::string SECONDARY_UNIQUE_ID = "local:1";
3006
3007 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3008 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3009
3010 // No associated display viewport found, should disable the device.
3011 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3012 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3013 ASSERT_FALSE(device2->isEnabled());
3014
3015 // Prepare second display.
3016 constexpr int32_t newDisplayId = 2;
3017 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003018 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003019 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003020 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003021 // Default device will reconfigure above, need additional reconfiguration for another device.
3022 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3023 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3024
3025 // Device should be enabled after the associated display is found.
3026 ASSERT_TRUE(mDevice->isEnabled());
3027 ASSERT_TRUE(device2->isEnabled());
3028
3029 // Test pad key events
3030 ASSERT_NO_FATAL_FAILURE(
3031 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3032 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3033 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3034 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3035 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3036 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3037 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3038
3039 ASSERT_NO_FATAL_FAILURE(
3040 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3041 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3042 AKEYCODE_DPAD_RIGHT, newDisplayId));
3043 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3044 AKEYCODE_DPAD_DOWN, newDisplayId));
3045 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3046 AKEYCODE_DPAD_LEFT, newDisplayId));
3047}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048
arthurhungc903df12020-08-11 15:08:42 +08003049TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3050 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3051 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3052 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3054 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3055 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3056
3057 KeyboardInputMapper& mapper =
3058 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3059 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3060 // Initial metastate to AMETA_NONE.
3061 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3062 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3063
3064 // Initialization should have turned all of the lights off.
3065 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3066 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3067 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3068
3069 // Toggle caps lock on.
3070 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3071 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3072 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3073 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3074
3075 // Toggle num lock on.
3076 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3077 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3078 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3079 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3080
3081 // Toggle scroll lock on.
3082 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3083 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3084 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3085 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3086
3087 mFakeEventHub->removeDevice(EVENTHUB_ID);
3088 mReader->loopOnce();
3089
3090 // keyboard 2 should default toggle keys.
3091 const std::string USB2 = "USB2";
3092 const std::string DEVICE_NAME2 = "KEYBOARD2";
3093 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3094 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3095 std::shared_ptr<InputDevice> device2 =
3096 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3097 Flags<InputDeviceClass>(0));
3098 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3099 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3100 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3101 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3102 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3103 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3104
arthurhung6fe95782020-10-05 22:41:16 +08003105 KeyboardInputMapper& mapper2 =
3106 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3107 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003108 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3109 device2->reset(ARBITRARY_TIME);
3110
3111 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3112 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3113 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003114 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3115 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003116}
3117
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003118// --- KeyboardInputMapperTest_ExternalDevice ---
3119
3120class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3121protected:
Chris Yea52ade12020-08-27 16:49:20 -07003122 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003123};
3124
3125TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003126 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3127 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003128
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003129 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3130 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3131 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3132 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003134 KeyboardInputMapper& mapper =
3135 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3136 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003137
3138 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3139 NotifyKeyArgs args;
3140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3141 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3142
3143 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3145 ASSERT_EQ(uint32_t(0), args.policyFlags);
3146
3147 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3149 ASSERT_EQ(uint32_t(0), args.policyFlags);
3150
3151 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3153 ASSERT_EQ(uint32_t(0), args.policyFlags);
3154
3155 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3157 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3158
3159 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3161 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3162}
3163
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003164TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003165 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003166
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003167 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3168 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003170
Powei Fengd041c5d2019-05-03 17:11:33 -07003171 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003172 KeyboardInputMapper& mapper =
3173 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3174 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003175
3176 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3177 NotifyKeyArgs args;
3178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3179 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3180
3181 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3183 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3184
3185 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3187 ASSERT_EQ(uint32_t(0), args.policyFlags);
3188
3189 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3191 ASSERT_EQ(uint32_t(0), args.policyFlags);
3192
3193 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3195 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3196
3197 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3199 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3200}
3201
Michael Wrightd02c5b62014-02-10 15:10:22 -08003202// --- CursorInputMapperTest ---
3203
3204class CursorInputMapperTest : public InputMapperTest {
3205protected:
3206 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3207
Michael Wright17db18e2020-06-26 20:51:44 +01003208 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209
Chris Yea52ade12020-08-27 16:49:20 -07003210 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003211 InputMapperTest::SetUp();
3212
Michael Wright17db18e2020-06-26 20:51:44 +01003213 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003214 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215 }
3216
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3218 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003219
3220 void prepareDisplay(int32_t orientation) {
3221 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003222 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003223 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3224 orientation, uniqueId, NO_PORT, viewportType);
3225 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226};
3227
3228const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3229
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003230void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3231 int32_t originalY, int32_t rotatedX,
3232 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 NotifyMotionArgs args;
3234
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003235 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3236 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3237 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3241 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3242 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3243 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3244}
3245
3246TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251}
3252
3253TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003255 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258}
3259
3260TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003262 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263
3264 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266
3267 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003268 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3269 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3271 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3272
3273 // When the bounds are set, then there should be a valid motion range.
3274 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3275
3276 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003277 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
3279 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3280 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3281 1, 800 - 1, 0.0f, 0.0f));
3282 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3283 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3284 2, 480 - 1, 0.0f, 0.0f));
3285 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3286 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3287 0.0f, 1.0f, 0.0f, 0.0f));
3288}
3289
3290TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003292 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
3294 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003295 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
3297 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3298 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3299 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3300 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3301 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3302 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3303 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3304 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3305 0.0f, 1.0f, 0.0f, 0.0f));
3306}
3307
3308TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003310 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311
arthurhungdcef2dc2020-08-11 14:47:50 +08003312 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313
3314 NotifyMotionArgs args;
3315
3316 // Button press.
3317 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003318 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3319 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3321 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3322 ASSERT_EQ(DEVICE_ID, args.deviceId);
3323 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3324 ASSERT_EQ(uint32_t(0), args.policyFlags);
3325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3326 ASSERT_EQ(0, args.flags);
3327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3328 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3329 ASSERT_EQ(0, args.edgeFlags);
3330 ASSERT_EQ(uint32_t(1), args.pointerCount);
3331 ASSERT_EQ(0, args.pointerProperties[0].id);
3332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3334 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3335 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3336 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3337 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3338
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3340 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3341 ASSERT_EQ(DEVICE_ID, args.deviceId);
3342 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3343 ASSERT_EQ(uint32_t(0), args.policyFlags);
3344 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3345 ASSERT_EQ(0, args.flags);
3346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3347 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3348 ASSERT_EQ(0, args.edgeFlags);
3349 ASSERT_EQ(uint32_t(1), args.pointerCount);
3350 ASSERT_EQ(0, args.pointerProperties[0].id);
3351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3353 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3354 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3355 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3356 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3357
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003359 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3360 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3362 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3363 ASSERT_EQ(DEVICE_ID, args.deviceId);
3364 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3365 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3367 ASSERT_EQ(0, args.flags);
3368 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3369 ASSERT_EQ(0, args.buttonState);
3370 ASSERT_EQ(0, args.edgeFlags);
3371 ASSERT_EQ(uint32_t(1), args.pointerCount);
3372 ASSERT_EQ(0, args.pointerProperties[0].id);
3373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3375 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3376 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3377 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3378 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3379
3380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3381 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3382 ASSERT_EQ(DEVICE_ID, args.deviceId);
3383 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3384 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3386 ASSERT_EQ(0, args.flags);
3387 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3388 ASSERT_EQ(0, args.buttonState);
3389 ASSERT_EQ(0, args.edgeFlags);
3390 ASSERT_EQ(uint32_t(1), args.pointerCount);
3391 ASSERT_EQ(0, args.pointerProperties[0].id);
3392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3394 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3395 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3396 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3397 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3398}
3399
3400TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003402 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403
3404 NotifyMotionArgs args;
3405
3406 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003407 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3408 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3412 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3413
3414 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003415 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3416 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3420 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3421}
3422
3423TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003425 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426
3427 NotifyMotionArgs args;
3428
3429 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003430 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3431 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3433 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3435 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3436
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3440 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3441
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003443 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3444 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003446 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3448 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3449
3450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3453 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3454}
3455
3456TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003458 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459
3460 NotifyMotionArgs args;
3461
3462 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003463 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3464 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3465 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3466 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3468 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3470 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3471 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3472
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3476 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3477 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3478
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003480 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3481 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3482 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3486 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3487 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3488
3489 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003490 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3491 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3495 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3496
3497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3500 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3501}
3502
3503TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003505 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003507 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3516}
3517
3518TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519 addConfigurationProperty("cursor.mode", "navigation");
3520 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003523 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3532
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003533 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3539 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3540 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3541 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3542
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003543 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3545 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3546 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3548 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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003553 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3555 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3556 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3557 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3558 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}
3563
3564TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003566 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567
3568 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3569 mFakePointerController->setPosition(100, 200);
3570 mFakePointerController->setButtonState(0);
3571
3572 NotifyMotionArgs motionArgs;
3573 NotifyKeyArgs keyArgs;
3574
3575 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003576 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3577 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3579 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3580 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3581 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3583 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3584
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3587 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3588 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3590 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3591
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003592 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3593 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596 ASSERT_EQ(0, motionArgs.buttonState);
3597 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3599 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3600
3601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003602 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603 ASSERT_EQ(0, motionArgs.buttonState);
3604 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3606 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3607
3608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003610 ASSERT_EQ(0, motionArgs.buttonState);
3611 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3613 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3614
3615 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003616 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3617 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3618 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3620 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3621 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3622 motionArgs.buttonState);
3623 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3624 mFakePointerController->getButtonState());
3625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3626 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3627
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3630 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3631 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3632 mFakePointerController->getButtonState());
3633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3634 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3635
3636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3637 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3638 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3639 motionArgs.buttonState);
3640 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3641 mFakePointerController->getButtonState());
3642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3643 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3644
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003645 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3646 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003648 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3650 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3652 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3653
3654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003656 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3657 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3659 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3660
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003661 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3662 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003664 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3665 ASSERT_EQ(0, motionArgs.buttonState);
3666 ASSERT_EQ(0, mFakePointerController->getButtonState());
3667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3668 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 -08003669 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3670 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003671
3672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673 ASSERT_EQ(0, motionArgs.buttonState);
3674 ASSERT_EQ(0, mFakePointerController->getButtonState());
3675 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3677 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003678
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3680 ASSERT_EQ(0, motionArgs.buttonState);
3681 ASSERT_EQ(0, mFakePointerController->getButtonState());
3682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3684 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3685
3686 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003687 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3688 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3690 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3691 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003692
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3696 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3698 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3699
3700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3701 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3702 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3703 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3705 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3706
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003707 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3708 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003710 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711 ASSERT_EQ(0, motionArgs.buttonState);
3712 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3714 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3715
3716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003718 ASSERT_EQ(0, motionArgs.buttonState);
3719 ASSERT_EQ(0, mFakePointerController->getButtonState());
3720
Michael Wrightd02c5b62014-02-10 15:10:22 -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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3724 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3725 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3726
3727 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003728 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3729 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3731 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3732 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003733
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3737 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3739 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3740
3741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3742 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3743 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3744 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
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
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003748 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3749 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003751 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752 ASSERT_EQ(0, motionArgs.buttonState);
3753 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3755 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 -08003756
3757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3758 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3759 ASSERT_EQ(0, motionArgs.buttonState);
3760 ASSERT_EQ(0, mFakePointerController->getButtonState());
3761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3762 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3763
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3765 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3766 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3767
3768 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003769 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3770 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3772 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3773 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003774
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3778 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3780 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3781
3782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3784 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3785 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3787 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3788
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003789 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3790 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003792 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793 ASSERT_EQ(0, motionArgs.buttonState);
3794 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3796 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 -08003797
3798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3799 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3800 ASSERT_EQ(0, motionArgs.buttonState);
3801 ASSERT_EQ(0, mFakePointerController->getButtonState());
3802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3803 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3804
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3806 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3807 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3808
3809 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003810 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3811 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3813 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3814 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003815
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003817 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3819 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3821 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3822
3823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3825 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3826 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3828 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3829
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003830 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3831 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834 ASSERT_EQ(0, motionArgs.buttonState);
3835 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3837 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 -08003838
3839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3840 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3841 ASSERT_EQ(0, motionArgs.buttonState);
3842 ASSERT_EQ(0, mFakePointerController->getButtonState());
3843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3844 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3845
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3847 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3848 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3849}
3850
3851TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003853 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854
3855 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3856 mFakePointerController->setPosition(100, 200);
3857 mFakePointerController->setButtonState(0);
3858
3859 NotifyMotionArgs args;
3860
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003861 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3862 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3863 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003865 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3866 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3868 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 +01003869 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003870}
3871
3872TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003873 addConfigurationProperty("cursor.mode", "pointer");
3874 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003876
3877 NotifyDeviceResetArgs resetArgs;
3878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3879 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3880 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3881
3882 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3883 mFakePointerController->setPosition(100, 200);
3884 mFakePointerController->setButtonState(0);
3885
3886 NotifyMotionArgs args;
3887
3888 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003889 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3890 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3891 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3893 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3896 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 +01003897 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003898
3899 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003900 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3901 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3903 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3904 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3906 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3908 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3911 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3912
3913 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003914 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3915 process(mapper, ARBITRARY_TIME + 2, 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_BUTTON_RELEASE, args.action);
3919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3920 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3922 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3923 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3925 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3926
3927 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003928 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3929 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3930 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3932 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3933 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3935 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 +01003936 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003937
3938 // Disable pointer capture and check that the device generation got bumped
3939 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08003940 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003941 mFakePolicy->setPointerCapture(false);
3942 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08003943 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003944
3945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3946 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3947 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3948
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003949 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3950 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3951 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3953 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3956 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 +01003957 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958}
3959
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003960TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003961 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003962
Garfield Tan888a6a42020-01-09 11:39:16 -08003963 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003964 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003965 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3966 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003967 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003968 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3969 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3970
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003971 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3972 mFakePointerController->setPosition(100, 200);
3973 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003974
3975 NotifyMotionArgs args;
3976 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3977 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3978 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3980 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3981 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3983 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 +01003984 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003985 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3986}
3987
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988// --- TouchInputMapperTest ---
3989
3990class TouchInputMapperTest : public InputMapperTest {
3991protected:
3992 static const int32_t RAW_X_MIN;
3993 static const int32_t RAW_X_MAX;
3994 static const int32_t RAW_Y_MIN;
3995 static const int32_t RAW_Y_MAX;
3996 static const int32_t RAW_TOUCH_MIN;
3997 static const int32_t RAW_TOUCH_MAX;
3998 static const int32_t RAW_TOOL_MIN;
3999 static const int32_t RAW_TOOL_MAX;
4000 static const int32_t RAW_PRESSURE_MIN;
4001 static const int32_t RAW_PRESSURE_MAX;
4002 static const int32_t RAW_ORIENTATION_MIN;
4003 static const int32_t RAW_ORIENTATION_MAX;
4004 static const int32_t RAW_DISTANCE_MIN;
4005 static const int32_t RAW_DISTANCE_MAX;
4006 static const int32_t RAW_TILT_MIN;
4007 static const int32_t RAW_TILT_MAX;
4008 static const int32_t RAW_ID_MIN;
4009 static const int32_t RAW_ID_MAX;
4010 static const int32_t RAW_SLOT_MIN;
4011 static const int32_t RAW_SLOT_MAX;
4012 static const float X_PRECISION;
4013 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004014 static const float X_PRECISION_VIRTUAL;
4015 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016
4017 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004018 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019
4020 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4021
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004022 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004023 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004024
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 enum Axes {
4026 POSITION = 1 << 0,
4027 TOUCH = 1 << 1,
4028 TOOL = 1 << 2,
4029 PRESSURE = 1 << 3,
4030 ORIENTATION = 1 << 4,
4031 MINOR = 1 << 5,
4032 ID = 1 << 6,
4033 DISTANCE = 1 << 7,
4034 TILT = 1 << 8,
4035 SLOT = 1 << 9,
4036 TOOL_TYPE = 1 << 10,
4037 };
4038
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004039 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4040 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004041 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004043 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 int32_t toRawX(float displayX);
4045 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004046 float toCookedX(float rawX, float rawY);
4047 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004049 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004051 float toDisplayY(int32_t rawY, int32_t displayHeight);
4052
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053};
4054
4055const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4056const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4057const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4058const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4059const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4060const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4061const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4062const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004063const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4064const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4066const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4067const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4068const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4069const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4070const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4071const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4072const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4073const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4074const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4075const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4076const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004077const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4078 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4079const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4080 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004081const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4082 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083
4084const float TouchInputMapperTest::GEOMETRIC_SCALE =
4085 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4086 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4087
4088const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4089 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4090 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4091};
4092
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004093void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004094 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4095 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004096}
4097
4098void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4099 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4100 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101}
4102
Santos Cordonfa5cf462017-04-05 10:37:00 -07004103void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004104 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4105 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4106 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004107}
4108
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004110 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4111 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4112 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4113 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114}
4115
Jason Gerecke489fda82012-09-07 17:19:40 -07004116void TouchInputMapperTest::prepareLocationCalibration() {
4117 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4118}
4119
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120int32_t TouchInputMapperTest::toRawX(float displayX) {
4121 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4122}
4123
4124int32_t TouchInputMapperTest::toRawY(float displayY) {
4125 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4126}
4127
Jason Gerecke489fda82012-09-07 17:19:40 -07004128float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4129 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4130 return rawX;
4131}
4132
4133float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4134 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4135 return rawY;
4136}
4137
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004139 return toDisplayX(rawX, DISPLAY_WIDTH);
4140}
4141
4142float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4143 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144}
4145
4146float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004147 return toDisplayY(rawY, DISPLAY_HEIGHT);
4148}
4149
4150float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4151 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152}
4153
4154
4155// --- SingleTouchInputMapperTest ---
4156
4157class SingleTouchInputMapperTest : public TouchInputMapperTest {
4158protected:
4159 void prepareButtons();
4160 void prepareAxes(int axes);
4161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004162 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4163 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4164 void processUp(SingleTouchInputMapper& mappery);
4165 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4166 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4167 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4168 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4169 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4170 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171};
4172
4173void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004174 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175}
4176
4177void SingleTouchInputMapperTest::prepareAxes(int axes) {
4178 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004179 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4180 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 }
4182 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004183 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4184 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185 }
4186 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004187 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4188 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 }
4190 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004191 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4192 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 }
4194 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004195 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4196 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197 }
4198}
4199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004200void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004201 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4202 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4203 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204}
4205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004206void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004207 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4208 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209}
4210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004211void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004212 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213}
4214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004215void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004216 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217}
4218
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004219void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4220 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004221 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222}
4223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004225 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226}
4227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004228void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4229 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004230 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4231 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232}
4233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4235 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004236 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237}
4238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004240 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241}
4242
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 prepareButtons();
4245 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004246 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004248 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249}
4250
4251TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004252 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4253 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 prepareButtons();
4255 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004256 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004258 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259}
4260
4261TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 prepareButtons();
4263 prepareAxes(POSITION);
4264 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004265 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004267 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268}
4269
4270TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 prepareButtons();
4272 prepareAxes(POSITION);
4273 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004274 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004276 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277}
4278
4279TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 addConfigurationProperty("touch.deviceType", "touchScreen");
4281 prepareDisplay(DISPLAY_ORIENTATION_0);
4282 prepareButtons();
4283 prepareAxes(POSITION);
4284 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004285 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286
4287 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004288 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289
4290 // Virtual key is down.
4291 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4292 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4293 processDown(mapper, x, y);
4294 processSync(mapper);
4295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4296
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004297 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298
4299 // Virtual key is up.
4300 processUp(mapper);
4301 processSync(mapper);
4302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4303
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004304 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305}
4306
4307TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 addConfigurationProperty("touch.deviceType", "touchScreen");
4309 prepareDisplay(DISPLAY_ORIENTATION_0);
4310 prepareButtons();
4311 prepareAxes(POSITION);
4312 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004313 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314
4315 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004316 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317
4318 // Virtual key is down.
4319 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4320 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4321 processDown(mapper, x, y);
4322 processSync(mapper);
4323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004325 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326
4327 // Virtual key is up.
4328 processUp(mapper);
4329 processSync(mapper);
4330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4331
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004332 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333}
4334
4335TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 addConfigurationProperty("touch.deviceType", "touchScreen");
4337 prepareDisplay(DISPLAY_ORIENTATION_0);
4338 prepareButtons();
4339 prepareAxes(POSITION);
4340 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004341 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342
4343 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4344 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004345 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 ASSERT_TRUE(flags[0]);
4347 ASSERT_FALSE(flags[1]);
4348}
4349
4350TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 addConfigurationProperty("touch.deviceType", "touchScreen");
4352 prepareDisplay(DISPLAY_ORIENTATION_0);
4353 prepareButtons();
4354 prepareAxes(POSITION);
4355 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004356 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357
arthurhungdcef2dc2020-08-11 14:47:50 +08004358 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359
4360 NotifyKeyArgs args;
4361
4362 // Press virtual key.
4363 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4364 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4365 processDown(mapper, x, y);
4366 processSync(mapper);
4367
4368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4369 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4370 ASSERT_EQ(DEVICE_ID, args.deviceId);
4371 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4372 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4373 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4374 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4375 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4376 ASSERT_EQ(KEY_HOME, args.scanCode);
4377 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4378 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4379
4380 // Release virtual key.
4381 processUp(mapper);
4382 processSync(mapper);
4383
4384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4385 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4386 ASSERT_EQ(DEVICE_ID, args.deviceId);
4387 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4388 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4389 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4390 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4391 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4392 ASSERT_EQ(KEY_HOME, args.scanCode);
4393 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4394 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4395
4396 // Should not have sent any motions.
4397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4398}
4399
4400TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 addConfigurationProperty("touch.deviceType", "touchScreen");
4402 prepareDisplay(DISPLAY_ORIENTATION_0);
4403 prepareButtons();
4404 prepareAxes(POSITION);
4405 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004406 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
arthurhungdcef2dc2020-08-11 14:47:50 +08004408 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409
4410 NotifyKeyArgs keyArgs;
4411
4412 // Press virtual key.
4413 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4414 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4415 processDown(mapper, x, y);
4416 processSync(mapper);
4417
4418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4419 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4420 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4421 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4422 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4423 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4424 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4425 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4426 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4427 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4428 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4429
4430 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4431 // into the display area.
4432 y -= 100;
4433 processMove(mapper, x, y);
4434 processSync(mapper);
4435
4436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4437 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4438 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4439 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4440 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4441 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4442 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4443 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4444 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4445 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4447 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4448
4449 NotifyMotionArgs motionArgs;
4450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4451 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4452 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4453 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4454 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4456 ASSERT_EQ(0, motionArgs.flags);
4457 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4458 ASSERT_EQ(0, motionArgs.buttonState);
4459 ASSERT_EQ(0, motionArgs.edgeFlags);
4460 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4464 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4465 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4466 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4468
4469 // Keep moving out of bounds. Should generate a pointer move.
4470 y -= 50;
4471 processMove(mapper, x, y);
4472 processSync(mapper);
4473
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_MOVE, 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 // Release out of bounds. Should generate a pointer up.
4494 processUp(mapper);
4495 processSync(mapper);
4496
4497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4498 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4499 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4500 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4501 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4503 ASSERT_EQ(0, motionArgs.flags);
4504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4505 ASSERT_EQ(0, motionArgs.buttonState);
4506 ASSERT_EQ(0, motionArgs.edgeFlags);
4507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4511 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4512 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4513 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4514 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4515
4516 // Should not have sent any more keys or motions.
4517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4519}
4520
4521TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 addConfigurationProperty("touch.deviceType", "touchScreen");
4523 prepareDisplay(DISPLAY_ORIENTATION_0);
4524 prepareButtons();
4525 prepareAxes(POSITION);
4526 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004527 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528
arthurhungdcef2dc2020-08-11 14:47:50 +08004529 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530
4531 NotifyMotionArgs motionArgs;
4532
4533 // Initially go down out of bounds.
4534 int32_t x = -10;
4535 int32_t y = -10;
4536 processDown(mapper, x, y);
4537 processSync(mapper);
4538
4539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4540
4541 // Move into the display area. Should generate a pointer down.
4542 x = 50;
4543 y = 75;
4544 processMove(mapper, x, y);
4545 processSync(mapper);
4546
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4548 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4549 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4550 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4551 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4552 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4553 ASSERT_EQ(0, motionArgs.flags);
4554 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4555 ASSERT_EQ(0, motionArgs.buttonState);
4556 ASSERT_EQ(0, motionArgs.edgeFlags);
4557 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4558 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4561 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4562 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4563 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4564 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4565
4566 // Release. Should generate a pointer up.
4567 processUp(mapper);
4568 processSync(mapper);
4569
4570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4571 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4572 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4573 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4574 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4576 ASSERT_EQ(0, motionArgs.flags);
4577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4578 ASSERT_EQ(0, motionArgs.buttonState);
4579 ASSERT_EQ(0, motionArgs.edgeFlags);
4580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4584 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4585 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4586 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4588
4589 // Should not have sent any more keys or motions.
4590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4592}
4593
Santos Cordonfa5cf462017-04-05 10:37:00 -07004594TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004595 addConfigurationProperty("touch.deviceType", "touchScreen");
4596 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4597
4598 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4599 prepareButtons();
4600 prepareAxes(POSITION);
4601 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004602 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004603
arthurhungdcef2dc2020-08-11 14:47:50 +08004604 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004605
4606 NotifyMotionArgs motionArgs;
4607
4608 // Down.
4609 int32_t x = 100;
4610 int32_t y = 125;
4611 processDown(mapper, x, y);
4612 processSync(mapper);
4613
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4615 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4616 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4617 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4618 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4619 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4620 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4621 ASSERT_EQ(0, motionArgs.flags);
4622 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4623 ASSERT_EQ(0, motionArgs.buttonState);
4624 ASSERT_EQ(0, motionArgs.edgeFlags);
4625 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4626 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4629 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4630 1, 0, 0, 0, 0, 0, 0, 0));
4631 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4632 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4633 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4634
4635 // Move.
4636 x += 50;
4637 y += 75;
4638 processMove(mapper, x, y);
4639 processSync(mapper);
4640
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4644 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4645 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4646 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4648 ASSERT_EQ(0, motionArgs.flags);
4649 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4650 ASSERT_EQ(0, motionArgs.buttonState);
4651 ASSERT_EQ(0, motionArgs.edgeFlags);
4652 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4653 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4656 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4657 1, 0, 0, 0, 0, 0, 0, 0));
4658 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4659 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4661
4662 // Up.
4663 processUp(mapper);
4664 processSync(mapper);
4665
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4667 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4668 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4669 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4670 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4671 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4672 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4673 ASSERT_EQ(0, motionArgs.flags);
4674 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4675 ASSERT_EQ(0, motionArgs.buttonState);
4676 ASSERT_EQ(0, motionArgs.edgeFlags);
4677 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4678 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4681 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4682 1, 0, 0, 0, 0, 0, 0, 0));
4683 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4684 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4686
4687 // Should not have sent any more keys or motions.
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4690}
4691
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 addConfigurationProperty("touch.deviceType", "touchScreen");
4694 prepareDisplay(DISPLAY_ORIENTATION_0);
4695 prepareButtons();
4696 prepareAxes(POSITION);
4697 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004698 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699
arthurhungdcef2dc2020-08-11 14:47:50 +08004700 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701
4702 NotifyMotionArgs motionArgs;
4703
4704 // Down.
4705 int32_t x = 100;
4706 int32_t y = 125;
4707 processDown(mapper, x, y);
4708 processSync(mapper);
4709
4710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4711 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4712 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4713 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4714 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4715 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4716 ASSERT_EQ(0, motionArgs.flags);
4717 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4718 ASSERT_EQ(0, motionArgs.buttonState);
4719 ASSERT_EQ(0, motionArgs.edgeFlags);
4720 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4724 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4725 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4726 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4728
4729 // Move.
4730 x += 50;
4731 y += 75;
4732 processMove(mapper, x, y);
4733 processSync(mapper);
4734
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4737 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4738 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4739 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4741 ASSERT_EQ(0, motionArgs.flags);
4742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4743 ASSERT_EQ(0, motionArgs.buttonState);
4744 ASSERT_EQ(0, motionArgs.edgeFlags);
4745 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4746 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4749 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4750 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4751 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4753
4754 // Up.
4755 processUp(mapper);
4756 processSync(mapper);
4757
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4759 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4760 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4761 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4762 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4763 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4764 ASSERT_EQ(0, motionArgs.flags);
4765 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4766 ASSERT_EQ(0, motionArgs.buttonState);
4767 ASSERT_EQ(0, motionArgs.edgeFlags);
4768 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4769 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4772 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4773 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4774 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4775 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4776
4777 // Should not have sent any more keys or motions.
4778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4780}
4781
4782TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 addConfigurationProperty("touch.deviceType", "touchScreen");
4784 prepareButtons();
4785 prepareAxes(POSITION);
4786 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004787 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788
4789 NotifyMotionArgs args;
4790
4791 // Rotation 90.
4792 prepareDisplay(DISPLAY_ORIENTATION_90);
4793 processDown(mapper, toRawX(50), toRawY(75));
4794 processSync(mapper);
4795
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4797 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4798 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4799
4800 processUp(mapper);
4801 processSync(mapper);
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4803}
4804
4805TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 addConfigurationProperty("touch.deviceType", "touchScreen");
4807 prepareButtons();
4808 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004809 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810
4811 NotifyMotionArgs args;
4812
4813 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004814 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 prepareDisplay(DISPLAY_ORIENTATION_0);
4816 processDown(mapper, toRawX(50), toRawY(75));
4817 processSync(mapper);
4818
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4820 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4821 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4822
4823 processUp(mapper);
4824 processSync(mapper);
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4826
4827 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004828 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 prepareDisplay(DISPLAY_ORIENTATION_90);
4830 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4831 processSync(mapper);
4832
4833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4834 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4835 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4836
4837 processUp(mapper);
4838 processSync(mapper);
4839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4840
4841 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004842 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 prepareDisplay(DISPLAY_ORIENTATION_180);
4844 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4845 processSync(mapper);
4846
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4848 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4849 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4850
4851 processUp(mapper);
4852 processSync(mapper);
4853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4854
4855 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004856 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857 prepareDisplay(DISPLAY_ORIENTATION_270);
4858 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4859 processSync(mapper);
4860
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4862 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4863 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4864
4865 processUp(mapper);
4866 processSync(mapper);
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4868}
4869
4870TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 addConfigurationProperty("touch.deviceType", "touchScreen");
4872 prepareDisplay(DISPLAY_ORIENTATION_0);
4873 prepareButtons();
4874 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004875 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876
4877 // These calculations are based on the input device calibration documentation.
4878 int32_t rawX = 100;
4879 int32_t rawY = 200;
4880 int32_t rawPressure = 10;
4881 int32_t rawToolMajor = 12;
4882 int32_t rawDistance = 2;
4883 int32_t rawTiltX = 30;
4884 int32_t rawTiltY = 110;
4885
4886 float x = toDisplayX(rawX);
4887 float y = toDisplayY(rawY);
4888 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4889 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4890 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4891 float distance = float(rawDistance);
4892
4893 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4894 float tiltScale = M_PI / 180;
4895 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4896 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4897 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4898 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4899
4900 processDown(mapper, rawX, rawY);
4901 processPressure(mapper, rawPressure);
4902 processToolMajor(mapper, rawToolMajor);
4903 processDistance(mapper, rawDistance);
4904 processTilt(mapper, rawTiltX, rawTiltY);
4905 processSync(mapper);
4906
4907 NotifyMotionArgs args;
4908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4910 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4911 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4912}
4913
Jason Gerecke489fda82012-09-07 17:19:40 -07004914TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004915 addConfigurationProperty("touch.deviceType", "touchScreen");
4916 prepareDisplay(DISPLAY_ORIENTATION_0);
4917 prepareLocationCalibration();
4918 prepareButtons();
4919 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004920 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004921
4922 int32_t rawX = 100;
4923 int32_t rawY = 200;
4924
4925 float x = toDisplayX(toCookedX(rawX, rawY));
4926 float y = toDisplayY(toCookedY(rawX, rawY));
4927
4928 processDown(mapper, rawX, rawY);
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, 1, 0, 0, 0, 0, 0, 0, 0));
4935}
4936
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 addConfigurationProperty("touch.deviceType", "touchScreen");
4939 prepareDisplay(DISPLAY_ORIENTATION_0);
4940 prepareButtons();
4941 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
4944 NotifyMotionArgs motionArgs;
4945 NotifyKeyArgs keyArgs;
4946
4947 processDown(mapper, 100, 200);
4948 processSync(mapper);
4949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4950 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4951 ASSERT_EQ(0, motionArgs.buttonState);
4952
4953 // press BTN_LEFT, release BTN_LEFT
4954 processKey(mapper, BTN_LEFT, 1);
4955 processSync(mapper);
4956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4958 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4959
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4962 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4963
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 processKey(mapper, BTN_LEFT, 0);
4965 processSync(mapper);
4966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004967 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004969
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004972 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973
4974 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4975 processKey(mapper, BTN_RIGHT, 1);
4976 processKey(mapper, BTN_MIDDLE, 1);
4977 processSync(mapper);
4978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4980 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4981 motionArgs.buttonState);
4982
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4985 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4986
4987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4989 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4990 motionArgs.buttonState);
4991
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 processKey(mapper, BTN_RIGHT, 0);
4993 processSync(mapper);
4994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004997
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005000 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001
5002 processKey(mapper, BTN_MIDDLE, 0);
5003 processSync(mapper);
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005007
5008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005010 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011
5012 // press BTN_BACK, release BTN_BACK
5013 processKey(mapper, BTN_BACK, 1);
5014 processSync(mapper);
5015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5016 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5017 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005018
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005021 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5022
5023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5025 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026
5027 processKey(mapper, BTN_BACK, 0);
5028 processSync(mapper);
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005031 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005032
5033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005035 ASSERT_EQ(0, motionArgs.buttonState);
5036
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5038 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5039 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5040
5041 // press BTN_SIDE, release BTN_SIDE
5042 processKey(mapper, BTN_SIDE, 1);
5043 processSync(mapper);
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5045 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5046 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005047
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005050 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5051
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5054 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055
5056 processKey(mapper, BTN_SIDE, 0);
5057 processSync(mapper);
5058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005059 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005061
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005064 ASSERT_EQ(0, motionArgs.buttonState);
5065
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5067 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5068 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5069
5070 // press BTN_FORWARD, release BTN_FORWARD
5071 processKey(mapper, BTN_FORWARD, 1);
5072 processSync(mapper);
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5074 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5075 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005076
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005079 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5080
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5083 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084
5085 processKey(mapper, BTN_FORWARD, 0);
5086 processSync(mapper);
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005090
5091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005093 ASSERT_EQ(0, motionArgs.buttonState);
5094
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5096 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5097 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5098
5099 // press BTN_EXTRA, release BTN_EXTRA
5100 processKey(mapper, BTN_EXTRA, 1);
5101 processSync(mapper);
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5103 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5104 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005105
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005108 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5109
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5112 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113
5114 processKey(mapper, BTN_EXTRA, 0);
5115 processSync(mapper);
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005119
5120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005122 ASSERT_EQ(0, motionArgs.buttonState);
5123
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5125 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5126 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5127
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5129
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 // press BTN_STYLUS, release BTN_STYLUS
5131 processKey(mapper, BTN_STYLUS, 1);
5132 processSync(mapper);
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005135 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5136
5137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5139 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140
5141 processKey(mapper, BTN_STYLUS, 0);
5142 processSync(mapper);
5143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005144 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005146
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005149 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150
5151 // press BTN_STYLUS2, release BTN_STYLUS2
5152 processKey(mapper, BTN_STYLUS2, 1);
5153 processSync(mapper);
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005156 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5157
5158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5160 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161
5162 processKey(mapper, BTN_STYLUS2, 0);
5163 processSync(mapper);
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005170 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171
5172 // release touch
5173 processUp(mapper);
5174 processSync(mapper);
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5176 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5177 ASSERT_EQ(0, motionArgs.buttonState);
5178}
5179
5180TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181 addConfigurationProperty("touch.deviceType", "touchScreen");
5182 prepareDisplay(DISPLAY_ORIENTATION_0);
5183 prepareButtons();
5184 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005185 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005186
5187 NotifyMotionArgs motionArgs;
5188
5189 // default tool type is finger
5190 processDown(mapper, 100, 200);
5191 processSync(mapper);
5192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5193 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5195
5196 // eraser
5197 processKey(mapper, BTN_TOOL_RUBBER, 1);
5198 processSync(mapper);
5199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5202
5203 // stylus
5204 processKey(mapper, BTN_TOOL_RUBBER, 0);
5205 processKey(mapper, BTN_TOOL_PEN, 1);
5206 processSync(mapper);
5207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5210
5211 // brush
5212 processKey(mapper, BTN_TOOL_PEN, 0);
5213 processKey(mapper, BTN_TOOL_BRUSH, 1);
5214 processSync(mapper);
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5218
5219 // pencil
5220 processKey(mapper, BTN_TOOL_BRUSH, 0);
5221 processKey(mapper, BTN_TOOL_PENCIL, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
5226
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005227 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228 processKey(mapper, BTN_TOOL_PENCIL, 0);
5229 processKey(mapper, BTN_TOOL_AIRBRUSH, 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 // mouse
5236 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5237 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
5242
5243 // lens
5244 processKey(mapper, BTN_TOOL_MOUSE, 0);
5245 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
5250
5251 // double-tap
5252 processKey(mapper, BTN_TOOL_LENS, 0);
5253 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
5258
5259 // triple-tap
5260 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5261 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
5266
5267 // quad-tap
5268 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5269 processKey(mapper, BTN_TOOL_QUADTAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
5274
5275 // finger
5276 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5277 processKey(mapper, BTN_TOOL_FINGER, 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 // stylus trumps finger
5284 processKey(mapper, BTN_TOOL_PEN, 1);
5285 processSync(mapper);
5286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5289
5290 // eraser trumps stylus
5291 processKey(mapper, BTN_TOOL_RUBBER, 1);
5292 processSync(mapper);
5293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5296
5297 // mouse trumps eraser
5298 processKey(mapper, BTN_TOOL_MOUSE, 1);
5299 processSync(mapper);
5300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5303
5304 // back to default tool type
5305 processKey(mapper, BTN_TOOL_MOUSE, 0);
5306 processKey(mapper, BTN_TOOL_RUBBER, 0);
5307 processKey(mapper, BTN_TOOL_PEN, 0);
5308 processKey(mapper, BTN_TOOL_FINGER, 0);
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_FINGER, motionArgs.pointerProperties[0].toolType);
5313}
5314
5315TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316 addConfigurationProperty("touch.deviceType", "touchScreen");
5317 prepareDisplay(DISPLAY_ORIENTATION_0);
5318 prepareButtons();
5319 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005320 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005321 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322
5323 NotifyMotionArgs motionArgs;
5324
5325 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5326 processKey(mapper, BTN_TOOL_FINGER, 1);
5327 processMove(mapper, 100, 200);
5328 processSync(mapper);
5329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5332 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5333
5334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5337 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5338
5339 // move a little
5340 processMove(mapper, 150, 250);
5341 processSync(mapper);
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5345 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5346
5347 // down when BTN_TOUCH is pressed, pressure defaults to 1
5348 processKey(mapper, BTN_TOUCH, 1);
5349 processSync(mapper);
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5353 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5354
5355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5358 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5359
5360 // up when BTN_TOUCH is released, hover restored
5361 processKey(mapper, BTN_TOUCH, 0);
5362 processSync(mapper);
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5366 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5367
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5371 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5372
5373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5376 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5377
5378 // exit hover when pointer goes away
5379 processKey(mapper, BTN_TOOL_FINGER, 0);
5380 processSync(mapper);
5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5384 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5385}
5386
5387TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388 addConfigurationProperty("touch.deviceType", "touchScreen");
5389 prepareDisplay(DISPLAY_ORIENTATION_0);
5390 prepareButtons();
5391 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005392 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393
5394 NotifyMotionArgs motionArgs;
5395
5396 // initially hovering because pressure is 0
5397 processDown(mapper, 100, 200);
5398 processPressure(mapper, 0);
5399 processSync(mapper);
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5401 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5403 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5404
5405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5406 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5408 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5409
5410 // move a little
5411 processMove(mapper, 150, 250);
5412 processSync(mapper);
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5416 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5417
5418 // down when pressure is non-zero
5419 processPressure(mapper, RAW_PRESSURE_MAX);
5420 processSync(mapper);
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5424 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5425
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5427 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5429 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5430
5431 // up when pressure becomes 0, hover restored
5432 processPressure(mapper, 0);
5433 processSync(mapper);
5434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5435 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5437 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5442 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5443
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5447 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5448
5449 // exit hover when pointer goes away
5450 processUp(mapper);
5451 processSync(mapper);
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5455 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5456}
5457
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458// --- MultiTouchInputMapperTest ---
5459
5460class MultiTouchInputMapperTest : public TouchInputMapperTest {
5461protected:
5462 void prepareAxes(int axes);
5463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005464 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5465 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5466 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5467 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5468 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5469 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5470 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5471 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5472 void processId(MultiTouchInputMapper& mapper, int32_t id);
5473 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5474 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5475 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5476 void processMTSync(MultiTouchInputMapper& mapper);
5477 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478};
5479
5480void MultiTouchInputMapperTest::prepareAxes(int axes) {
5481 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005482 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5483 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484 }
5485 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5487 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005489 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5490 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 }
5492 }
5493 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005494 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5495 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005497 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5498 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 }
5500 }
5501 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005502 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5503 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504 }
5505 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5507 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 }
5509 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5511 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512 }
5513 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005514 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5515 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 }
5517 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005518 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5519 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520 }
5521 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005522 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523 }
5524}
5525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005526void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5527 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005528 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5529 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530}
5531
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005532void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5533 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005534 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005535}
5536
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005537void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5538 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005539 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005540}
5541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005542void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005543 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544}
5545
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005546void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005547 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548}
5549
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005550void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5551 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005552 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553}
5554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005555void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005556 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005557}
5558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005559void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005560 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561}
5562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005563void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005564 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565}
5566
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005567void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005568 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569}
5570
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005571void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005572 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573}
5574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005575void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5576 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005577 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578}
5579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005580void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005581 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582}
5583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005584void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005585 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586}
5587
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589 addConfigurationProperty("touch.deviceType", "touchScreen");
5590 prepareDisplay(DISPLAY_ORIENTATION_0);
5591 prepareAxes(POSITION);
5592 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005593 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594
arthurhungdcef2dc2020-08-11 14:47:50 +08005595 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596
5597 NotifyMotionArgs motionArgs;
5598
5599 // Two fingers down at once.
5600 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5601 processPosition(mapper, x1, y1);
5602 processMTSync(mapper);
5603 processPosition(mapper, x2, y2);
5604 processMTSync(mapper);
5605 processSync(mapper);
5606
5607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5608 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5609 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5610 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5611 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5612 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5613 ASSERT_EQ(0, motionArgs.flags);
5614 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5615 ASSERT_EQ(0, motionArgs.buttonState);
5616 ASSERT_EQ(0, motionArgs.edgeFlags);
5617 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5618 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5621 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5622 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5623 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5624 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5625
5626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5628 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5629 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5630 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5631 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5632 motionArgs.action);
5633 ASSERT_EQ(0, motionArgs.flags);
5634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5635 ASSERT_EQ(0, motionArgs.buttonState);
5636 ASSERT_EQ(0, motionArgs.edgeFlags);
5637 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5638 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5640 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5643 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5645 toDisplayX(x2), toDisplayY(y2), 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 // Move.
5651 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5652 processPosition(mapper, x1, y1);
5653 processMTSync(mapper);
5654 processPosition(mapper, x2, y2);
5655 processMTSync(mapper);
5656 processSync(mapper);
5657
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5659 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5660 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5661 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5662 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5663 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5664 ASSERT_EQ(0, motionArgs.flags);
5665 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5666 ASSERT_EQ(0, motionArgs.buttonState);
5667 ASSERT_EQ(0, motionArgs.edgeFlags);
5668 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5669 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5671 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5674 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5676 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5677 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5678 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5679 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5680
5681 // First finger up.
5682 x2 += 15; y2 -= 20;
5683 processPosition(mapper, x2, y2);
5684 processMTSync(mapper);
5685 processSync(mapper);
5686
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5689 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5690 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5691 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5692 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5693 motionArgs.action);
5694 ASSERT_EQ(0, motionArgs.flags);
5695 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5696 ASSERT_EQ(0, motionArgs.buttonState);
5697 ASSERT_EQ(0, motionArgs.edgeFlags);
5698 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5701 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5704 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5706 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5707 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5708 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5709 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
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_MOVE, motionArgs.action);
5717 ASSERT_EQ(0, motionArgs.flags);
5718 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5719 ASSERT_EQ(0, motionArgs.buttonState);
5720 ASSERT_EQ(0, motionArgs.edgeFlags);
5721 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5722 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5725 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5726 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5727 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5728 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5729
5730 // Move.
5731 x2 += 20; y2 -= 25;
5732 processPosition(mapper, x2, y2);
5733 processMTSync(mapper);
5734 processSync(mapper);
5735
5736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5737 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5738 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5739 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5740 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5742 ASSERT_EQ(0, motionArgs.flags);
5743 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5744 ASSERT_EQ(0, motionArgs.buttonState);
5745 ASSERT_EQ(0, motionArgs.edgeFlags);
5746 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5747 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5750 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5751 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5752 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5753 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5754
5755 // New finger down.
5756 int32_t x3 = 700, y3 = 300;
5757 processPosition(mapper, x2, y2);
5758 processMTSync(mapper);
5759 processPosition(mapper, x3, y3);
5760 processMTSync(mapper);
5761 processSync(mapper);
5762
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5764 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5765 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5766 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5767 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5768 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5769 motionArgs.action);
5770 ASSERT_EQ(0, motionArgs.flags);
5771 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5772 ASSERT_EQ(0, motionArgs.buttonState);
5773 ASSERT_EQ(0, motionArgs.edgeFlags);
5774 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5777 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5780 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5782 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5783 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5784 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5785 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5786
5787 // Second finger up.
5788 x3 += 30; y3 -= 20;
5789 processPosition(mapper, x3, y3);
5790 processMTSync(mapper);
5791 processSync(mapper);
5792
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5794 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5795 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5796 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5797 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5798 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5799 motionArgs.action);
5800 ASSERT_EQ(0, motionArgs.flags);
5801 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5802 ASSERT_EQ(0, motionArgs.buttonState);
5803 ASSERT_EQ(0, motionArgs.edgeFlags);
5804 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5805 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5807 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5810 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5812 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5813 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5814 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5815 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
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_MOVE, motionArgs.action);
5823 ASSERT_EQ(0, motionArgs.flags);
5824 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5825 ASSERT_EQ(0, motionArgs.buttonState);
5826 ASSERT_EQ(0, motionArgs.edgeFlags);
5827 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5828 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5831 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5832 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5833 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5834 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5835
5836 // Last finger up.
5837 processMTSync(mapper);
5838 processSync(mapper);
5839
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5841 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5842 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5843 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5844 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5845 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5846 ASSERT_EQ(0, motionArgs.flags);
5847 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5848 ASSERT_EQ(0, motionArgs.buttonState);
5849 ASSERT_EQ(0, motionArgs.edgeFlags);
5850 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5854 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5855 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5856 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5858
5859 // Should not have sent any more keys or motions.
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5862}
5863
5864TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865 addConfigurationProperty("touch.deviceType", "touchScreen");
5866 prepareDisplay(DISPLAY_ORIENTATION_0);
5867 prepareAxes(POSITION | ID);
5868 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005869 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870
arthurhungdcef2dc2020-08-11 14:47:50 +08005871 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872
5873 NotifyMotionArgs motionArgs;
5874
5875 // Two fingers down at once.
5876 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5877 processPosition(mapper, x1, y1);
5878 processId(mapper, 1);
5879 processMTSync(mapper);
5880 processPosition(mapper, x2, y2);
5881 processId(mapper, 2);
5882 processMTSync(mapper);
5883 processSync(mapper);
5884
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5886 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5887 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5888 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5891 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5892
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5894 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5895 motionArgs.action);
5896 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5897 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5899 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5902 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5904 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5905
5906 // Move.
5907 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5908 processPosition(mapper, x1, y1);
5909 processId(mapper, 1);
5910 processMTSync(mapper);
5911 processPosition(mapper, x2, y2);
5912 processId(mapper, 2);
5913 processMTSync(mapper);
5914 processSync(mapper);
5915
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5918 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5919 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5921 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5924 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5926 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5927
5928 // First finger up.
5929 x2 += 15; y2 -= 20;
5930 processPosition(mapper, x2, y2);
5931 processId(mapper, 2);
5932 processMTSync(mapper);
5933 processSync(mapper);
5934
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5937 motionArgs.action);
5938 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5939 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5940 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5941 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5942 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5944 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5946 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5947
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5950 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5951 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5954 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5955
5956 // Move.
5957 x2 += 20; y2 -= 25;
5958 processPosition(mapper, x2, y2);
5959 processId(mapper, 2);
5960 processMTSync(mapper);
5961 processSync(mapper);
5962
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5965 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5966 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5969 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5970
5971 // New finger down.
5972 int32_t x3 = 700, y3 = 300;
5973 processPosition(mapper, x2, y2);
5974 processId(mapper, 2);
5975 processMTSync(mapper);
5976 processPosition(mapper, x3, y3);
5977 processId(mapper, 3);
5978 processMTSync(mapper);
5979 processSync(mapper);
5980
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5983 motionArgs.action);
5984 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5985 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5986 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5987 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5990 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5992 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5993
5994 // Second finger up.
5995 x3 += 30; y3 -= 20;
5996 processPosition(mapper, x3, y3);
5997 processId(mapper, 3);
5998 processMTSync(mapper);
5999 processSync(mapper);
6000
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6003 motionArgs.action);
6004 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6005 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6006 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6007 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6010 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6012 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6016 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6020 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6021
6022 // Last finger up.
6023 processMTSync(mapper);
6024 processSync(mapper);
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6028 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6029 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6032 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6033
6034 // Should not have sent any more keys or motions.
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6037}
6038
6039TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 addConfigurationProperty("touch.deviceType", "touchScreen");
6041 prepareDisplay(DISPLAY_ORIENTATION_0);
6042 prepareAxes(POSITION | ID | SLOT);
6043 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006044 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045
arthurhungdcef2dc2020-08-11 14:47:50 +08006046 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047
6048 NotifyMotionArgs motionArgs;
6049
6050 // Two fingers down at once.
6051 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6052 processPosition(mapper, x1, y1);
6053 processId(mapper, 1);
6054 processSlot(mapper, 1);
6055 processPosition(mapper, x2, y2);
6056 processId(mapper, 2);
6057 processSync(mapper);
6058
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6061 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6062 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6065 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6066
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6069 motionArgs.action);
6070 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6071 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6073 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6076 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6078 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6079
6080 // Move.
6081 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6082 processSlot(mapper, 0);
6083 processPosition(mapper, x1, y1);
6084 processSlot(mapper, 1);
6085 processPosition(mapper, x2, y2);
6086 processSync(mapper);
6087
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6090 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6093 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6096 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6098 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6099
6100 // First finger up.
6101 x2 += 15; y2 -= 20;
6102 processSlot(mapper, 0);
6103 processId(mapper, -1);
6104 processSlot(mapper, 1);
6105 processPosition(mapper, x2, y2);
6106 processSync(mapper);
6107
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6109 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6110 motionArgs.action);
6111 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6112 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6114 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6117 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6119 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6120
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6123 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6124 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6127 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6128
6129 // Move.
6130 x2 += 20; y2 -= 25;
6131 processPosition(mapper, x2, y2);
6132 processSync(mapper);
6133
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6136 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6137 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6140 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6141
6142 // New finger down.
6143 int32_t x3 = 700, y3 = 300;
6144 processPosition(mapper, x2, y2);
6145 processSlot(mapper, 0);
6146 processId(mapper, 3);
6147 processPosition(mapper, x3, y3);
6148 processSync(mapper);
6149
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6151 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6152 motionArgs.action);
6153 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6154 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6156 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6159 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6161 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6162
6163 // Second finger up.
6164 x3 += 30; y3 -= 20;
6165 processSlot(mapper, 1);
6166 processId(mapper, -1);
6167 processSlot(mapper, 0);
6168 processPosition(mapper, x3, y3);
6169 processSync(mapper);
6170
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6173 motionArgs.action);
6174 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6177 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6180 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6182 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6183
6184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6186 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6187 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6190 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6191
6192 // Last finger up.
6193 processId(mapper, -1);
6194 processSync(mapper);
6195
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6198 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6199 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6202 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6203
6204 // Should not have sent any more keys or motions.
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6207}
6208
6209TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 addConfigurationProperty("touch.deviceType", "touchScreen");
6211 prepareDisplay(DISPLAY_ORIENTATION_0);
6212 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006213 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214
6215 // These calculations are based on the input device calibration documentation.
6216 int32_t rawX = 100;
6217 int32_t rawY = 200;
6218 int32_t rawTouchMajor = 7;
6219 int32_t rawTouchMinor = 6;
6220 int32_t rawToolMajor = 9;
6221 int32_t rawToolMinor = 8;
6222 int32_t rawPressure = 11;
6223 int32_t rawDistance = 0;
6224 int32_t rawOrientation = 3;
6225 int32_t id = 5;
6226
6227 float x = toDisplayX(rawX);
6228 float y = toDisplayY(rawY);
6229 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6230 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6231 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6232 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6233 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6234 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6235 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6236 float distance = float(rawDistance);
6237
6238 processPosition(mapper, rawX, rawY);
6239 processTouchMajor(mapper, rawTouchMajor);
6240 processTouchMinor(mapper, rawTouchMinor);
6241 processToolMajor(mapper, rawToolMajor);
6242 processToolMinor(mapper, rawToolMinor);
6243 processPressure(mapper, rawPressure);
6244 processOrientation(mapper, rawOrientation);
6245 processDistance(mapper, rawDistance);
6246 processId(mapper, id);
6247 processMTSync(mapper);
6248 processSync(mapper);
6249
6250 NotifyMotionArgs args;
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6252 ASSERT_EQ(0, args.pointerProperties[0].id);
6253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6254 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6255 orientation, distance));
6256}
6257
6258TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259 addConfigurationProperty("touch.deviceType", "touchScreen");
6260 prepareDisplay(DISPLAY_ORIENTATION_0);
6261 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6262 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006263 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264
6265 // These calculations are based on the input device calibration documentation.
6266 int32_t rawX = 100;
6267 int32_t rawY = 200;
6268 int32_t rawTouchMajor = 140;
6269 int32_t rawTouchMinor = 120;
6270 int32_t rawToolMajor = 180;
6271 int32_t rawToolMinor = 160;
6272
6273 float x = toDisplayX(rawX);
6274 float y = toDisplayY(rawY);
6275 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6276 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6277 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6278 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6279 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6280
6281 processPosition(mapper, rawX, rawY);
6282 processTouchMajor(mapper, rawTouchMajor);
6283 processTouchMinor(mapper, rawTouchMinor);
6284 processToolMajor(mapper, rawToolMajor);
6285 processToolMinor(mapper, rawToolMinor);
6286 processMTSync(mapper);
6287 processSync(mapper);
6288
6289 NotifyMotionArgs args;
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6292 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6293}
6294
6295TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 addConfigurationProperty("touch.deviceType", "touchScreen");
6297 prepareDisplay(DISPLAY_ORIENTATION_0);
6298 prepareAxes(POSITION | TOUCH | TOOL);
6299 addConfigurationProperty("touch.size.calibration", "diameter");
6300 addConfigurationProperty("touch.size.scale", "10");
6301 addConfigurationProperty("touch.size.bias", "160");
6302 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006303 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304
6305 // These calculations are based on the input device calibration documentation.
6306 // Note: We only provide a single common touch/tool value because the device is assumed
6307 // not to emit separate values for each pointer (isSummed = 1).
6308 int32_t rawX = 100;
6309 int32_t rawY = 200;
6310 int32_t rawX2 = 150;
6311 int32_t rawY2 = 250;
6312 int32_t rawTouchMajor = 5;
6313 int32_t rawToolMajor = 8;
6314
6315 float x = toDisplayX(rawX);
6316 float y = toDisplayY(rawY);
6317 float x2 = toDisplayX(rawX2);
6318 float y2 = toDisplayY(rawY2);
6319 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6320 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6321 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6322
6323 processPosition(mapper, rawX, rawY);
6324 processTouchMajor(mapper, rawTouchMajor);
6325 processToolMajor(mapper, rawToolMajor);
6326 processMTSync(mapper);
6327 processPosition(mapper, rawX2, rawY2);
6328 processTouchMajor(mapper, rawTouchMajor);
6329 processToolMajor(mapper, rawToolMajor);
6330 processMTSync(mapper);
6331 processSync(mapper);
6332
6333 NotifyMotionArgs args;
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6336
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6338 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6339 args.action);
6340 ASSERT_EQ(size_t(2), args.pointerCount);
6341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6342 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6344 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6345}
6346
6347TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 addConfigurationProperty("touch.deviceType", "touchScreen");
6349 prepareDisplay(DISPLAY_ORIENTATION_0);
6350 prepareAxes(POSITION | TOUCH | TOOL);
6351 addConfigurationProperty("touch.size.calibration", "area");
6352 addConfigurationProperty("touch.size.scale", "43");
6353 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006354 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355
6356 // These calculations are based on the input device calibration documentation.
6357 int32_t rawX = 100;
6358 int32_t rawY = 200;
6359 int32_t rawTouchMajor = 5;
6360 int32_t rawToolMajor = 8;
6361
6362 float x = toDisplayX(rawX);
6363 float y = toDisplayY(rawY);
6364 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6365 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6366 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6367
6368 processPosition(mapper, rawX, rawY);
6369 processTouchMajor(mapper, rawTouchMajor);
6370 processToolMajor(mapper, rawToolMajor);
6371 processMTSync(mapper);
6372 processSync(mapper);
6373
6374 NotifyMotionArgs args;
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6377 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6378}
6379
6380TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381 addConfigurationProperty("touch.deviceType", "touchScreen");
6382 prepareDisplay(DISPLAY_ORIENTATION_0);
6383 prepareAxes(POSITION | PRESSURE);
6384 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6385 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006386 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387
Michael Wrightaa449c92017-12-13 21:21:43 +00006388 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006389 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006390 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6391 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6392 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6393
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394 // These calculations are based on the input device calibration documentation.
6395 int32_t rawX = 100;
6396 int32_t rawY = 200;
6397 int32_t rawPressure = 60;
6398
6399 float x = toDisplayX(rawX);
6400 float y = toDisplayY(rawY);
6401 float pressure = float(rawPressure) * 0.01f;
6402
6403 processPosition(mapper, rawX, rawY);
6404 processPressure(mapper, rawPressure);
6405 processMTSync(mapper);
6406 processSync(mapper);
6407
6408 NotifyMotionArgs args;
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6411 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6412}
6413
6414TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 addConfigurationProperty("touch.deviceType", "touchScreen");
6416 prepareDisplay(DISPLAY_ORIENTATION_0);
6417 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006418 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419
6420 NotifyMotionArgs motionArgs;
6421 NotifyKeyArgs keyArgs;
6422
6423 processId(mapper, 1);
6424 processPosition(mapper, 100, 200);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6428 ASSERT_EQ(0, motionArgs.buttonState);
6429
6430 // press BTN_LEFT, release BTN_LEFT
6431 processKey(mapper, BTN_LEFT, 1);
6432 processSync(mapper);
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6435 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6436
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6439 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6440
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 processKey(mapper, BTN_LEFT, 0);
6442 processSync(mapper);
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006444 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006445 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006446
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006449 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450
6451 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6452 processKey(mapper, BTN_RIGHT, 1);
6453 processKey(mapper, BTN_MIDDLE, 1);
6454 processSync(mapper);
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6457 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6458 motionArgs.buttonState);
6459
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6462 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6463
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6466 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6467 motionArgs.buttonState);
6468
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 processKey(mapper, BTN_RIGHT, 0);
6470 processSync(mapper);
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006472 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006474
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478
6479 processKey(mapper, BTN_MIDDLE, 0);
6480 processSync(mapper);
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006484
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006487 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006488
6489 // press BTN_BACK, release BTN_BACK
6490 processKey(mapper, BTN_BACK, 1);
6491 processSync(mapper);
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6493 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6494 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006495
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006498 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6501 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6502 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503
6504 processKey(mapper, BTN_BACK, 0);
6505 processSync(mapper);
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006508 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006509
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006512 ASSERT_EQ(0, motionArgs.buttonState);
6513
Michael Wrightd02c5b62014-02-10 15:10:22 -08006514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6515 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6516 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6517
6518 // press BTN_SIDE, release BTN_SIDE
6519 processKey(mapper, BTN_SIDE, 1);
6520 processSync(mapper);
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6522 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6523 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006524
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006527 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6528
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6531 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532
6533 processKey(mapper, BTN_SIDE, 0);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006538
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006541 ASSERT_EQ(0, motionArgs.buttonState);
6542
Michael Wrightd02c5b62014-02-10 15:10:22 -08006543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6544 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6545 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6546
6547 // press BTN_FORWARD, release BTN_FORWARD
6548 processKey(mapper, BTN_FORWARD, 1);
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6551 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6552 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006553
Michael Wrightd02c5b62014-02-10 15:10:22 -08006554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006556 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6557
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6560 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006561
6562 processKey(mapper, BTN_FORWARD, 0);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006566 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006567
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006570 ASSERT_EQ(0, motionArgs.buttonState);
6571
Michael Wrightd02c5b62014-02-10 15:10:22 -08006572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6573 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6574 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6575
6576 // press BTN_EXTRA, release BTN_EXTRA
6577 processKey(mapper, BTN_EXTRA, 1);
6578 processSync(mapper);
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6580 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6581 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006582
Michael Wrightd02c5b62014-02-10 15:10:22 -08006583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006585 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6586
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6589 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590
6591 processKey(mapper, BTN_EXTRA, 0);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006595 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006596
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006599 ASSERT_EQ(0, motionArgs.buttonState);
6600
Michael Wrightd02c5b62014-02-10 15:10:22 -08006601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6602 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6603 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6604
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6606
Michael Wrightd02c5b62014-02-10 15:10:22 -08006607 // press BTN_STYLUS, release BTN_STYLUS
6608 processKey(mapper, BTN_STYLUS, 1);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006612 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6613
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6616 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006617
6618 processKey(mapper, BTN_STYLUS, 0);
6619 processSync(mapper);
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006622 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006623
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006626 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006627
6628 // press BTN_STYLUS2, release BTN_STYLUS2
6629 processKey(mapper, BTN_STYLUS2, 1);
6630 processSync(mapper);
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006633 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6634
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6637 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006638
6639 processKey(mapper, BTN_STYLUS2, 0);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006642 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006643 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006644
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006647 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006648
6649 // release touch
6650 processId(mapper, -1);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6654 ASSERT_EQ(0, motionArgs.buttonState);
6655}
6656
6657TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006658 addConfigurationProperty("touch.deviceType", "touchScreen");
6659 prepareDisplay(DISPLAY_ORIENTATION_0);
6660 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006661 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006662
6663 NotifyMotionArgs motionArgs;
6664
6665 // default tool type is finger
6666 processId(mapper, 1);
6667 processPosition(mapper, 100, 200);
6668 processSync(mapper);
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6670 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6672
6673 // eraser
6674 processKey(mapper, BTN_TOOL_RUBBER, 1);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6679
6680 // stylus
6681 processKey(mapper, BTN_TOOL_RUBBER, 0);
6682 processKey(mapper, BTN_TOOL_PEN, 1);
6683 processSync(mapper);
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6685 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6687
6688 // brush
6689 processKey(mapper, BTN_TOOL_PEN, 0);
6690 processKey(mapper, BTN_TOOL_BRUSH, 1);
6691 processSync(mapper);
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6695
6696 // pencil
6697 processKey(mapper, BTN_TOOL_BRUSH, 0);
6698 processKey(mapper, BTN_TOOL_PENCIL, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6703
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006704 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705 processKey(mapper, BTN_TOOL_PENCIL, 0);
6706 processKey(mapper, BTN_TOOL_AIRBRUSH, 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 // mouse
6713 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6714 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
6719
6720 // lens
6721 processKey(mapper, BTN_TOOL_MOUSE, 0);
6722 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
6727
6728 // double-tap
6729 processKey(mapper, BTN_TOOL_LENS, 0);
6730 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6735
6736 // triple-tap
6737 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6738 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6743
6744 // quad-tap
6745 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6746 processKey(mapper, BTN_TOOL_QUADTAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6751
6752 // finger
6753 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6754 processKey(mapper, BTN_TOOL_FINGER, 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 // stylus trumps finger
6761 processKey(mapper, BTN_TOOL_PEN, 1);
6762 processSync(mapper);
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6766
6767 // eraser trumps stylus
6768 processKey(mapper, BTN_TOOL_RUBBER, 1);
6769 processSync(mapper);
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6773
6774 // mouse trumps eraser
6775 processKey(mapper, BTN_TOOL_MOUSE, 1);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6780
6781 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6782 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6783 processSync(mapper);
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6787
6788 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6789 processToolType(mapper, MT_TOOL_PEN);
6790 processSync(mapper);
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6794
6795 // back to default tool type
6796 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6797 processKey(mapper, BTN_TOOL_MOUSE, 0);
6798 processKey(mapper, BTN_TOOL_RUBBER, 0);
6799 processKey(mapper, BTN_TOOL_PEN, 0);
6800 processKey(mapper, BTN_TOOL_FINGER, 0);
6801 processSync(mapper);
6802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6805}
6806
6807TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006808 addConfigurationProperty("touch.deviceType", "touchScreen");
6809 prepareDisplay(DISPLAY_ORIENTATION_0);
6810 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006811 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006812 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006813
6814 NotifyMotionArgs motionArgs;
6815
6816 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6817 processId(mapper, 1);
6818 processPosition(mapper, 100, 200);
6819 processSync(mapper);
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6821 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6823 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6824
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6826 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6828 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6829
6830 // move a little
6831 processPosition(mapper, 150, 250);
6832 processSync(mapper);
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6836 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6837
6838 // down when BTN_TOUCH is pressed, pressure defaults to 1
6839 processKey(mapper, BTN_TOUCH, 1);
6840 processSync(mapper);
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6842 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6844 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6845
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6847 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6849 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6850
6851 // up when BTN_TOUCH is released, hover restored
6852 processKey(mapper, BTN_TOUCH, 0);
6853 processSync(mapper);
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6855 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6857 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6858
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6860 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6862 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6863
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6865 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6867 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6868
6869 // exit hover when pointer goes away
6870 processId(mapper, -1);
6871 processSync(mapper);
6872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6873 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6875 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6876}
6877
6878TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006879 addConfigurationProperty("touch.deviceType", "touchScreen");
6880 prepareDisplay(DISPLAY_ORIENTATION_0);
6881 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006882 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006883
6884 NotifyMotionArgs motionArgs;
6885
6886 // initially hovering because pressure is 0
6887 processId(mapper, 1);
6888 processPosition(mapper, 100, 200);
6889 processPressure(mapper, 0);
6890 processSync(mapper);
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6894 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6895
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6897 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6899 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6900
6901 // move a little
6902 processPosition(mapper, 150, 250);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6907 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6908
6909 // down when pressure becomes non-zero
6910 processPressure(mapper, RAW_PRESSURE_MAX);
6911 processSync(mapper);
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6913 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6915 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6916
6917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6918 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6920 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6921
6922 // up when pressure becomes 0, hover restored
6923 processPressure(mapper, 0);
6924 processSync(mapper);
6925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6926 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6928 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6929
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6933 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6934
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6938 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6939
6940 // exit hover when pointer goes away
6941 processId(mapper, -1);
6942 processSync(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6944 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6946 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6947}
6948
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006949/**
6950 * Set the input device port <--> display port associations, and check that the
6951 * events are routed to the display that matches the display port.
6952 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6953 */
6954TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006955 const std::string usb2 = "USB2";
6956 const uint8_t hdmi1 = 0;
6957 const uint8_t hdmi2 = 1;
6958 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006959 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006960
6961 addConfigurationProperty("touch.deviceType", "touchScreen");
6962 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006963 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006964
6965 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6966 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6967
6968 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6969 // for this input device is specified, and the matching viewport is not present,
6970 // the input device should be disabled (at the mapper level).
6971
6972 // Add viewport for display 2 on hdmi2
6973 prepareSecondaryDisplay(type, hdmi2);
6974 // Send a touch event
6975 processPosition(mapper, 100, 100);
6976 processSync(mapper);
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6978
6979 // Add viewport for display 1 on hdmi1
6980 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6981 // Send a touch event again
6982 processPosition(mapper, 100, 100);
6983 processSync(mapper);
6984
6985 NotifyMotionArgs args;
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6987 ASSERT_EQ(DISPLAY_ID, args.displayId);
6988}
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006990TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08006991 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01006992 std::shared_ptr<FakePointerController> fakePointerController =
6993 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08006994 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006995 fakePointerController->setPosition(100, 200);
6996 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08006997 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6998
Garfield Tan888a6a42020-01-09 11:39:16 -08006999 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007000 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007001
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007002 prepareDisplay(DISPLAY_ORIENTATION_0);
7003 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007004 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007005
7006 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007007 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007008
7009 NotifyMotionArgs motionArgs;
7010 processPosition(mapper, 100, 100);
7011 processSync(mapper);
7012
7013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7014 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7015 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7016}
7017
Arthur Hung7c645402019-01-25 17:45:42 +08007018TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7019 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007020 prepareAxes(POSITION | ID | SLOT);
7021 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007022 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007023
7024 // Create the second touch screen device, and enable multi fingers.
7025 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007026 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007027 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007028 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007029 std::shared_ptr<InputDevice> device2 =
7030 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7031 Flags<InputDeviceClass>(0));
7032
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007033 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7034 0 /*flat*/, 0 /*fuzz*/);
7035 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7036 0 /*flat*/, 0 /*fuzz*/);
7037 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7038 0 /*flat*/, 0 /*fuzz*/);
7039 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7040 0 /*flat*/, 0 /*fuzz*/);
7041 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7042 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7043 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007044
7045 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007046 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007047 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7048 device2->reset(ARBITRARY_TIME);
7049
7050 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007051 std::shared_ptr<FakePointerController> fakePointerController =
7052 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007053 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7054 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7055
7056 // Setup policy for associated displays and show touches.
7057 const uint8_t hdmi1 = 0;
7058 const uint8_t hdmi2 = 1;
7059 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7060 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7061 mFakePolicy->setShowTouches(true);
7062
7063 // Create displays.
7064 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007065 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007066
7067 // Default device will reconfigure above, need additional reconfiguration for another device.
7068 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007069 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007070
7071 // Two fingers down at default display.
7072 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7073 processPosition(mapper, x1, y1);
7074 processId(mapper, 1);
7075 processSlot(mapper, 1);
7076 processPosition(mapper, x2, y2);
7077 processId(mapper, 2);
7078 processSync(mapper);
7079
7080 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7081 fakePointerController->getSpots().find(DISPLAY_ID);
7082 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7083 ASSERT_EQ(size_t(2), iter->second.size());
7084
7085 // Two fingers down at second display.
7086 processPosition(mapper2, x1, y1);
7087 processId(mapper2, 1);
7088 processSlot(mapper2, 1);
7089 processPosition(mapper2, x2, y2);
7090 processId(mapper2, 2);
7091 processSync(mapper2);
7092
7093 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7094 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7095 ASSERT_EQ(size_t(2), iter->second.size());
7096}
7097
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007098TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007099 prepareAxes(POSITION);
7100 addConfigurationProperty("touch.deviceType", "touchScreen");
7101 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007102 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007103
7104 NotifyMotionArgs motionArgs;
7105 // Unrotated video frame
7106 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7107 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007108 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007109 processPosition(mapper, 100, 200);
7110 processSync(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7112 ASSERT_EQ(frames, motionArgs.videoFrames);
7113
7114 // Subsequent touch events should not have any videoframes
7115 // This is implemented separately in FakeEventHub,
7116 // but that should match the behaviour of TouchVideoDevice.
7117 processPosition(mapper, 200, 200);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7120 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7121}
7122
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007123TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007124 prepareAxes(POSITION);
7125 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007126 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007127 // Unrotated video frame
7128 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7129 NotifyMotionArgs motionArgs;
7130
7131 // Test all 4 orientations
7132 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7133 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7134 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7135 clearViewports();
7136 prepareDisplay(orientation);
7137 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007138 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007139 processPosition(mapper, 100, 200);
7140 processSync(mapper);
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7142 frames[0].rotate(orientation);
7143 ASSERT_EQ(frames, motionArgs.videoFrames);
7144 }
7145}
7146
7147TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
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 frames. There's no rule that they must all have the same dimensions,
7152 // so mix these.
7153 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7154 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7155 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7156 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7157 NotifyMotionArgs motionArgs;
7158
7159 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007160 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007161 processPosition(mapper, 100, 200);
7162 processSync(mapper);
7163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7164 std::for_each(frames.begin(), frames.end(),
7165 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7166 ASSERT_EQ(frames, motionArgs.videoFrames);
7167}
7168
Arthur Hung9da14732019-09-02 16:16:58 +08007169/**
7170 * If we had defined port associations, but the viewport is not ready, the touch device would be
7171 * expected to be disabled, and it should be enabled after the viewport has found.
7172 */
7173TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007174 constexpr uint8_t hdmi2 = 1;
7175 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007176 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007177
7178 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7179
7180 addConfigurationProperty("touch.deviceType", "touchScreen");
7181 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007183
7184 ASSERT_EQ(mDevice->isEnabled(), false);
7185
7186 // Add display on hdmi2, the device should be enabled and can receive touch event.
7187 prepareSecondaryDisplay(type, hdmi2);
7188 ASSERT_EQ(mDevice->isEnabled(), true);
7189
7190 // Send a touch event.
7191 processPosition(mapper, 100, 100);
7192 processSync(mapper);
7193
7194 NotifyMotionArgs args;
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7196 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7197}
7198
Arthur Hung6cd19a42019-08-30 19:04:12 +08007199
Arthur Hung6cd19a42019-08-30 19:04:12 +08007200
Arthur Hung421eb1c2020-01-16 00:09:42 +08007201TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007202 addConfigurationProperty("touch.deviceType", "touchScreen");
7203 prepareDisplay(DISPLAY_ORIENTATION_0);
7204 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007206
7207 NotifyMotionArgs motionArgs;
7208
7209 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7210 // finger down
7211 processId(mapper, 1);
7212 processPosition(mapper, x1, y1);
7213 processSync(mapper);
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7215 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7217
7218 // finger move
7219 processId(mapper, 1);
7220 processPosition(mapper, x2, y2);
7221 processSync(mapper);
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7225
7226 // finger up.
7227 processId(mapper, -1);
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7232
7233 // new finger down
7234 processId(mapper, 1);
7235 processPosition(mapper, x3, y3);
7236 processSync(mapper);
7237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7238 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7240}
7241
7242/**
arthurhungcc7f9802020-04-30 17:55:40 +08007243 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7244 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007245 */
arthurhungcc7f9802020-04-30 17:55:40 +08007246TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007247 addConfigurationProperty("touch.deviceType", "touchScreen");
7248 prepareDisplay(DISPLAY_ORIENTATION_0);
7249 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007250 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007251
7252 NotifyMotionArgs motionArgs;
7253
7254 // default tool type is finger
7255 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007256 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007257 processPosition(mapper, x1, y1);
7258 processSync(mapper);
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7262
7263 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7264 processToolType(mapper, MT_TOOL_PALM);
7265 processSync(mapper);
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7267 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7268
7269 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007270 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007271 processPosition(mapper, x2, y2);
7272 processSync(mapper);
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7274
7275 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007276 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007277 processSync(mapper);
7278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7279
7280 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007281 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007282 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007283 processPosition(mapper, x3, y3);
7284 processSync(mapper);
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7286 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7288}
7289
arthurhungbf89a482020-04-17 17:37:55 +08007290/**
arthurhungcc7f9802020-04-30 17:55:40 +08007291 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7292 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007293 */
arthurhungcc7f9802020-04-30 17:55:40 +08007294TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007295 addConfigurationProperty("touch.deviceType", "touchScreen");
7296 prepareDisplay(DISPLAY_ORIENTATION_0);
7297 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7298 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7299
7300 NotifyMotionArgs motionArgs;
7301
7302 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007303 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7304 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007305 processPosition(mapper, x1, y1);
7306 processSync(mapper);
7307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7308 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7310
7311 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007312 processSlot(mapper, SECOND_SLOT);
7313 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007314 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007315 processSync(mapper);
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7317 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7318 motionArgs.action);
7319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7320
7321 // If the tool type of the first finger changes to MT_TOOL_PALM,
7322 // we expect to receive ACTION_POINTER_UP with cancel flag.
7323 processSlot(mapper, FIRST_SLOT);
7324 processId(mapper, FIRST_TRACKING_ID);
7325 processToolType(mapper, MT_TOOL_PALM);
7326 processSync(mapper);
7327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7328 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7329 motionArgs.action);
7330 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7331
7332 // The following MOVE events of second finger should be processed.
7333 processSlot(mapper, SECOND_SLOT);
7334 processId(mapper, SECOND_TRACKING_ID);
7335 processPosition(mapper, x2 + 1, y2 + 1);
7336 processSync(mapper);
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7339 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7340
7341 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7342 // it. Second finger receive move.
7343 processSlot(mapper, FIRST_SLOT);
7344 processId(mapper, INVALID_TRACKING_ID);
7345 processSync(mapper);
7346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7348 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7349
7350 // Second finger keeps moving.
7351 processSlot(mapper, SECOND_SLOT);
7352 processId(mapper, SECOND_TRACKING_ID);
7353 processPosition(mapper, x2 + 2, y2 + 2);
7354 processSync(mapper);
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7357 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7358
7359 // Second finger up.
7360 processId(mapper, INVALID_TRACKING_ID);
7361 processSync(mapper);
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7363 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7364 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7365}
7366
7367/**
7368 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7369 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7370 */
7371TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7372 addConfigurationProperty("touch.deviceType", "touchScreen");
7373 prepareDisplay(DISPLAY_ORIENTATION_0);
7374 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7375 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7376
7377 NotifyMotionArgs motionArgs;
7378
7379 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7380 // First finger down.
7381 processId(mapper, FIRST_TRACKING_ID);
7382 processPosition(mapper, x1, y1);
7383 processSync(mapper);
7384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7385 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387
7388 // Second finger down.
7389 processSlot(mapper, SECOND_SLOT);
7390 processId(mapper, SECOND_TRACKING_ID);
7391 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007392 processSync(mapper);
7393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7394 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7395 motionArgs.action);
7396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7397
arthurhungcc7f9802020-04-30 17:55:40 +08007398 // If the tool type of the first finger changes to MT_TOOL_PALM,
7399 // we expect to receive ACTION_POINTER_UP with cancel flag.
7400 processSlot(mapper, FIRST_SLOT);
7401 processId(mapper, FIRST_TRACKING_ID);
7402 processToolType(mapper, MT_TOOL_PALM);
7403 processSync(mapper);
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7405 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7406 motionArgs.action);
7407 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7408
7409 // Second finger keeps moving.
7410 processSlot(mapper, SECOND_SLOT);
7411 processId(mapper, SECOND_TRACKING_ID);
7412 processPosition(mapper, x2 + 1, y2 + 1);
7413 processSync(mapper);
7414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7416
7417 // second finger becomes palm, receive cancel due to only 1 finger is active.
7418 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007419 processToolType(mapper, MT_TOOL_PALM);
7420 processSync(mapper);
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7422 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7423
arthurhungcc7f9802020-04-30 17:55:40 +08007424 // third finger down.
7425 processSlot(mapper, THIRD_SLOT);
7426 processId(mapper, THIRD_TRACKING_ID);
7427 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007428 processPosition(mapper, x3, y3);
7429 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007433 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7434
7435 // third finger move
7436 processId(mapper, THIRD_TRACKING_ID);
7437 processPosition(mapper, x3 + 1, y3 + 1);
7438 processSync(mapper);
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7441
7442 // first finger up, third finger receive move.
7443 processSlot(mapper, FIRST_SLOT);
7444 processId(mapper, INVALID_TRACKING_ID);
7445 processSync(mapper);
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7448 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7449
7450 // second finger up, third finger receive move.
7451 processSlot(mapper, SECOND_SLOT);
7452 processId(mapper, INVALID_TRACKING_ID);
7453 processSync(mapper);
7454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7456 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7457
7458 // third finger up.
7459 processSlot(mapper, THIRD_SLOT);
7460 processId(mapper, INVALID_TRACKING_ID);
7461 processSync(mapper);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7463 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7464 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7465}
7466
7467/**
7468 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7469 * and the active finger could still be allowed to receive the events
7470 */
7471TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7472 addConfigurationProperty("touch.deviceType", "touchScreen");
7473 prepareDisplay(DISPLAY_ORIENTATION_0);
7474 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7475 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7476
7477 NotifyMotionArgs motionArgs;
7478
7479 // default tool type is finger
7480 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7481 processId(mapper, FIRST_TRACKING_ID);
7482 processPosition(mapper, x1, y1);
7483 processSync(mapper);
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7485 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7487
7488 // Second finger down.
7489 processSlot(mapper, SECOND_SLOT);
7490 processId(mapper, SECOND_TRACKING_ID);
7491 processPosition(mapper, x2, y2);
7492 processSync(mapper);
7493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7494 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7495 motionArgs.action);
7496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7497
7498 // If the tool type of the second finger changes to MT_TOOL_PALM,
7499 // we expect to receive ACTION_POINTER_UP with cancel flag.
7500 processId(mapper, SECOND_TRACKING_ID);
7501 processToolType(mapper, MT_TOOL_PALM);
7502 processSync(mapper);
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7504 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7505 motionArgs.action);
7506 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7507
7508 // The following MOVE event should be processed.
7509 processSlot(mapper, FIRST_SLOT);
7510 processId(mapper, FIRST_TRACKING_ID);
7511 processPosition(mapper, x1 + 1, y1 + 1);
7512 processSync(mapper);
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7515 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7516
7517 // second finger up.
7518 processSlot(mapper, SECOND_SLOT);
7519 processId(mapper, INVALID_TRACKING_ID);
7520 processSync(mapper);
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7523
7524 // first finger keep moving
7525 processSlot(mapper, FIRST_SLOT);
7526 processId(mapper, FIRST_TRACKING_ID);
7527 processPosition(mapper, x1 + 2, y1 + 2);
7528 processSync(mapper);
7529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7531
7532 // first finger up.
7533 processId(mapper, INVALID_TRACKING_ID);
7534 processSync(mapper);
7535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7536 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7537 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007538}
7539
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007540// --- MultiTouchInputMapperTest_ExternalDevice ---
7541
7542class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7543protected:
Chris Yea52ade12020-08-27 16:49:20 -07007544 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007545};
7546
7547/**
7548 * Expect fallback to internal viewport if device is external and external viewport is not present.
7549 */
7550TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7551 prepareAxes(POSITION);
7552 addConfigurationProperty("touch.deviceType", "touchScreen");
7553 prepareDisplay(DISPLAY_ORIENTATION_0);
7554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7555
7556 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7557
7558 NotifyMotionArgs motionArgs;
7559
7560 // Expect the event to be sent to the internal viewport,
7561 // because an external viewport is not present.
7562 processPosition(mapper, 100, 100);
7563 processSync(mapper);
7564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7565 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7566
7567 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007568 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007569 processPosition(mapper, 100, 100);
7570 processSync(mapper);
7571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7572 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7573}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007574
7575/**
7576 * Test touch should not work if outside of surface.
7577 */
7578class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7579protected:
7580 void halfDisplayToCenterHorizontal(int32_t orientation) {
7581 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007582 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007583
7584 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7585 internalViewport->orientation = orientation;
7586 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7587 internalViewport->logicalLeft = 0;
7588 internalViewport->logicalTop = 0;
7589 internalViewport->logicalRight = DISPLAY_HEIGHT;
7590 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7591
7592 internalViewport->physicalLeft = 0;
7593 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7594 internalViewport->physicalRight = DISPLAY_HEIGHT;
7595 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7596
7597 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7598 internalViewport->deviceHeight = DISPLAY_WIDTH;
7599 } else {
7600 internalViewport->logicalLeft = 0;
7601 internalViewport->logicalTop = 0;
7602 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7603 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7604
7605 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7606 internalViewport->physicalTop = 0;
7607 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7608 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7609
7610 internalViewport->deviceWidth = DISPLAY_WIDTH;
7611 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7612 }
7613
7614 mFakePolicy->updateViewport(internalViewport.value());
7615 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7616 }
7617
7618 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7619 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7620 int32_t yExpected) {
7621 // touch on outside area should not work.
7622 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7623 processSync(mapper);
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7625
7626 // touch on inside area should receive the event.
7627 NotifyMotionArgs args;
7628 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7629 processSync(mapper);
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7631 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7632 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7633
7634 // Reset.
7635 mapper.reset(ARBITRARY_TIME);
7636 }
7637};
7638
7639TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7640 addConfigurationProperty("touch.deviceType", "touchScreen");
7641 prepareDisplay(DISPLAY_ORIENTATION_0);
7642 prepareAxes(POSITION);
7643 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7644
7645 // Touch on center of normal display should work.
7646 const int32_t x = DISPLAY_WIDTH / 4;
7647 const int32_t y = DISPLAY_HEIGHT / 2;
7648 processPosition(mapper, toRawX(x), toRawY(y));
7649 processSync(mapper);
7650 NotifyMotionArgs args;
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7653 0.0f, 0.0f, 0.0f, 0.0f));
7654 // Reset.
7655 mapper.reset(ARBITRARY_TIME);
7656
7657 // Let physical display be different to device, and make surface and physical could be 1:1.
7658 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7659
7660 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7661 const int32_t yExpected = y;
7662 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7663}
7664
7665TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7666 addConfigurationProperty("touch.deviceType", "touchScreen");
7667 prepareDisplay(DISPLAY_ORIENTATION_0);
7668 prepareAxes(POSITION);
7669 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7670
7671 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7672 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7673
7674 const int32_t x = DISPLAY_WIDTH / 4;
7675 const int32_t y = DISPLAY_HEIGHT / 2;
7676
7677 // expect x/y = swap x/y then reverse y.
7678 const int32_t xExpected = y;
7679 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7680 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7681}
7682
7683TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7684 addConfigurationProperty("touch.deviceType", "touchScreen");
7685 prepareDisplay(DISPLAY_ORIENTATION_0);
7686 prepareAxes(POSITION);
7687 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7688
7689 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7690 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7691
7692 const int32_t x = DISPLAY_WIDTH / 4;
7693 const int32_t y = DISPLAY_HEIGHT / 2;
7694
7695 // expect x/y = swap x/y then reverse x.
7696 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7697 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7698 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7699}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007700
7701TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7702 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7703 std::shared_ptr<FakePointerController> fakePointerController =
7704 std::make_shared<FakePointerController>();
7705 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7706 fakePointerController->setPosition(0, 0);
7707 fakePointerController->setButtonState(0);
7708
7709 // prepare device and capture
7710 prepareDisplay(DISPLAY_ORIENTATION_0);
7711 prepareAxes(POSITION | ID | SLOT);
7712 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7713 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7714 mFakePolicy->setPointerCapture(true);
7715 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7717
7718 // captured touchpad should be a touchpad source
7719 NotifyDeviceResetArgs resetArgs;
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7721 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7722
Chris Yef74dc422020-09-02 22:41:50 -07007723 InputDeviceInfo deviceInfo;
7724 mDevice->getDeviceInfo(&deviceInfo);
7725
7726 const InputDeviceInfo::MotionRange* relRangeX =
7727 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
7728 ASSERT_NE(relRangeX, nullptr);
7729 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
7730 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
7731 const InputDeviceInfo::MotionRange* relRangeY =
7732 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
7733 ASSERT_NE(relRangeY, nullptr);
7734 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
7735 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
7736
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007737 // run captured pointer tests - note that this is unscaled, so input listener events should be
7738 // identical to what the hardware sends (accounting for any
7739 // calibration).
7740 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007741 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007742 processId(mapper, 1);
7743 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7744 processKey(mapper, BTN_TOUCH, 1);
7745 processSync(mapper);
7746
7747 // expect coord[0] to contain initial location of touch 0
7748 NotifyMotionArgs args;
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7750 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7751 ASSERT_EQ(1U, args.pointerCount);
7752 ASSERT_EQ(0, args.pointerProperties[0].id);
7753 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7754 ASSERT_NO_FATAL_FAILURE(
7755 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7756
7757 // FINGER 1 DOWN
7758 processSlot(mapper, 1);
7759 processId(mapper, 2);
7760 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7761 processSync(mapper);
7762
7763 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007765 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7766 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007767 ASSERT_EQ(2U, args.pointerCount);
7768 ASSERT_EQ(0, args.pointerProperties[0].id);
7769 ASSERT_EQ(1, args.pointerProperties[1].id);
7770 ASSERT_NO_FATAL_FAILURE(
7771 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7772 ASSERT_NO_FATAL_FAILURE(
7773 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7774
7775 // FINGER 1 MOVE
7776 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7777 processSync(mapper);
7778
7779 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7780 // from move
7781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7783 ASSERT_NO_FATAL_FAILURE(
7784 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7785 ASSERT_NO_FATAL_FAILURE(
7786 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7787
7788 // FINGER 0 MOVE
7789 processSlot(mapper, 0);
7790 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7791 processSync(mapper);
7792
7793 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7796 ASSERT_NO_FATAL_FAILURE(
7797 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7798 ASSERT_NO_FATAL_FAILURE(
7799 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7800
7801 // BUTTON DOWN
7802 processKey(mapper, BTN_LEFT, 1);
7803 processSync(mapper);
7804
7805 // touchinputmapper design sends a move before button press
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7810
7811 // BUTTON UP
7812 processKey(mapper, BTN_LEFT, 0);
7813 processSync(mapper);
7814
7815 // touchinputmapper design sends a move after button release
7816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7817 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7820
7821 // FINGER 0 UP
7822 processId(mapper, -1);
7823 processSync(mapper);
7824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7825 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7826
7827 // FINGER 1 MOVE
7828 processSlot(mapper, 1);
7829 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7830 processSync(mapper);
7831
7832 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7835 ASSERT_EQ(1U, args.pointerCount);
7836 ASSERT_EQ(1, args.pointerProperties[0].id);
7837 ASSERT_NO_FATAL_FAILURE(
7838 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7839
7840 // FINGER 1 UP
7841 processId(mapper, -1);
7842 processKey(mapper, BTN_TOUCH, 0);
7843 processSync(mapper);
7844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7845 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7846
7847 // non captured touchpad should be a mouse source
7848 mFakePolicy->setPointerCapture(false);
7849 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7851 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7852}
7853
7854TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7855 std::shared_ptr<FakePointerController> fakePointerController =
7856 std::make_shared<FakePointerController>();
7857 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7858 fakePointerController->setPosition(0, 0);
7859 fakePointerController->setButtonState(0);
7860
7861 // prepare device and capture
7862 prepareDisplay(DISPLAY_ORIENTATION_0);
7863 prepareAxes(POSITION | ID | SLOT);
7864 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7865 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7866 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7867 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7868 // run uncaptured pointer tests - pushes out generic events
7869 // FINGER 0 DOWN
7870 processId(mapper, 3);
7871 processPosition(mapper, 100, 100);
7872 processKey(mapper, BTN_TOUCH, 1);
7873 processSync(mapper);
7874
7875 // start at (100,100), cursor should be at (0,0) * scale
7876 NotifyMotionArgs args;
7877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7878 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7879 ASSERT_NO_FATAL_FAILURE(
7880 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7881
7882 // FINGER 0 MOVE
7883 processPosition(mapper, 200, 200);
7884 processSync(mapper);
7885
7886 // compute scaling to help with touch position checking
7887 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7888 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7889 float scale =
7890 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7891
7892 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7894 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7896 0, 0, 0, 0, 0, 0, 0));
7897}
7898
7899TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7900 std::shared_ptr<FakePointerController> fakePointerController =
7901 std::make_shared<FakePointerController>();
7902
7903 prepareDisplay(DISPLAY_ORIENTATION_0);
7904 prepareAxes(POSITION | ID | SLOT);
7905 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7906 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7907 mFakePolicy->setPointerCapture(false);
7908 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7909
7910 // uncaptured touchpad should be a pointer device
7911 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7912
7913 // captured touchpad should be a touchpad device
7914 mFakePolicy->setPointerCapture(true);
7915 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7916 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7917}
7918
Michael Wrightd02c5b62014-02-10 15:10:22 -08007919} // namespace android