blob: 99eaac6a09b30f0f8197a0255c7e48925baf1007 [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 Pradhan7e186182020-11-10 13:56:45 -08001795TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1796 NotifyPointerCaptureChangedArgs args;
1797
1798 mFakePolicy->setPointerCapture(true);
1799 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1800 mReader->loopOnce();
1801 mFakeListener->assertNotifyCaptureWasCalled(&args);
1802 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1803
1804 mFakePolicy->setPointerCapture(false);
1805 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1806 mReader->loopOnce();
1807 mFakeListener->assertNotifyCaptureWasCalled(&args);
1808 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1809
1810 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1811 // does not change.
1812 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1813 mReader->loopOnce();
1814 mFakeListener->assertNotifyCaptureWasCalled(&args);
1815 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1816}
1817
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001818// --- InputReaderIntegrationTest ---
1819
1820// These tests create and interact with the InputReader only through its interface.
1821// The InputReader is started during SetUp(), which starts its processing in its own
1822// thread. The tests use linux uinput to emulate input devices.
1823// NOTE: Interacting with the physical device while these tests are running may cause
1824// the tests to fail.
1825class InputReaderIntegrationTest : public testing::Test {
1826protected:
1827 sp<TestInputListener> mTestListener;
1828 sp<FakeInputReaderPolicy> mFakePolicy;
1829 sp<InputReaderInterface> mReader;
1830
Chris Yea52ade12020-08-27 16:49:20 -07001831 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001832 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001833 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1834 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001835
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001836 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001837 ASSERT_EQ(mReader->start(), OK);
1838
1839 // Since this test is run on a real device, all the input devices connected
1840 // to the test device will show up in mReader. We wait for those input devices to
1841 // show up before beginning the tests.
1842 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1843 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1844 }
1845
Chris Yea52ade12020-08-27 16:49:20 -07001846 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001847 ASSERT_EQ(mReader->stop(), OK);
1848 mTestListener.clear();
1849 mFakePolicy.clear();
1850 }
1851};
1852
1853TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1854 // An invalid input device that is only used for this test.
1855 class InvalidUinputDevice : public UinputDevice {
1856 public:
1857 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1858
1859 private:
1860 void configureDevice(int fd, uinput_user_dev* device) override {}
1861 };
1862
1863 const size_t numDevices = mFakePolicy->getInputDevices().size();
1864
1865 // UinputDevice does not set any event or key bits, so InputReader should not
1866 // consider it as a valid device.
1867 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1868 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1869 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1870 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1871
1872 invalidDevice.reset();
1873 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1874 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1875 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1876}
1877
1878TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1879 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1880
1881 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1882 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1883 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1884 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1885
1886 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07001887 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
1888 const auto& it =
1889 std::find_if(inputDevices.begin(), inputDevices.end(),
1890 [&keyboard](const InputDeviceInfo& info) {
1891 return info.getIdentifier().name == keyboard->getName();
1892 });
1893
1894 ASSERT_NE(it, inputDevices.end());
1895 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
1896 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
1897 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001898
1899 keyboard.reset();
1900 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1901 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1902 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1903}
1904
1905TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1906 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1907 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1908
1909 NotifyConfigurationChangedArgs configChangedArgs;
1910 ASSERT_NO_FATAL_FAILURE(
1911 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001912 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001913 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1914
1915 NotifyKeyArgs keyArgs;
1916 keyboard->pressAndReleaseHomeKey();
1917 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1918 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001919 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001920 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001921 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1922 prevTimestamp = keyArgs.eventTime;
1923
1924 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1925 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001926 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001927 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1928}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001929
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001930/**
1931 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1932 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1933 * are passed to the listener.
1934 */
1935static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1936TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1937 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1938 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1939 NotifyKeyArgs keyArgs;
1940
1941 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1942 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1943 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1944 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1945
1946 controller->pressAndReleaseKey(BTN_GEAR_UP);
1947 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1948 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1949 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1950}
1951
Arthur Hungaab25622020-01-16 11:22:11 +08001952// --- TouchProcessTest ---
1953class TouchIntegrationTest : public InputReaderIntegrationTest {
1954protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001955 const std::string UNIQUE_ID = "local:0";
1956
Chris Yea52ade12020-08-27 16:49:20 -07001957 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08001958 InputReaderIntegrationTest::SetUp();
1959 // At least add an internal display.
1960 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1961 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001962 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001963
1964 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1965 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1966 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1967 }
1968
1969 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1970 int32_t orientation, const std::string& uniqueId,
1971 std::optional<uint8_t> physicalPort,
1972 ViewportType viewportType) {
1973 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1974 physicalPort, viewportType);
1975 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1976 }
1977
1978 std::unique_ptr<UinputTouchScreen> mDevice;
1979};
1980
1981TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1982 NotifyMotionArgs args;
1983 const Point centerPoint = mDevice->getCenterPoint();
1984
1985 // ACTION_DOWN
1986 mDevice->sendDown(centerPoint);
1987 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1989
1990 // ACTION_MOVE
1991 mDevice->sendMove(centerPoint + Point(1, 1));
1992 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1994
1995 // ACTION_UP
1996 mDevice->sendUp();
1997 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1998 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1999}
2000
2001TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2002 NotifyMotionArgs args;
2003 const Point centerPoint = mDevice->getCenterPoint();
2004
2005 // ACTION_DOWN
2006 mDevice->sendDown(centerPoint);
2007 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2008 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2009
2010 // ACTION_POINTER_DOWN (Second slot)
2011 const Point secondPoint = centerPoint + Point(100, 100);
2012 mDevice->sendSlot(SECOND_SLOT);
2013 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2014 mDevice->sendDown(secondPoint + Point(1, 1));
2015 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2016 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2017 args.action);
2018
2019 // ACTION_MOVE (Second slot)
2020 mDevice->sendMove(secondPoint);
2021 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2023
2024 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002025 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002026 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002027 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002028 args.action);
2029
2030 // ACTION_UP
2031 mDevice->sendSlot(FIRST_SLOT);
2032 mDevice->sendUp();
2033 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2034 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2035}
2036
2037TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2038 NotifyMotionArgs args;
2039 const Point centerPoint = mDevice->getCenterPoint();
2040
2041 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002042 mDevice->sendSlot(FIRST_SLOT);
2043 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002044 mDevice->sendDown(centerPoint);
2045 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2046 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2047
arthurhungcc7f9802020-04-30 17:55:40 +08002048 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002049 const Point secondPoint = centerPoint + Point(100, 100);
2050 mDevice->sendSlot(SECOND_SLOT);
2051 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2052 mDevice->sendDown(secondPoint);
2053 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2054 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2055 args.action);
2056
arthurhungcc7f9802020-04-30 17:55:40 +08002057 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002058 mDevice->sendMove(secondPoint + Point(1, 1));
2059 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2061
arthurhungcc7f9802020-04-30 17:55:40 +08002062 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2063 // a palm event.
2064 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002065 mDevice->sendToolType(MT_TOOL_PALM);
2066 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002067 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2068 args.action);
2069 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002070
arthurhungcc7f9802020-04-30 17:55:40 +08002071 // Send up to second slot, expect first slot send moving.
2072 mDevice->sendPointerUp();
2073 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002075
arthurhungcc7f9802020-04-30 17:55:40 +08002076 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002077 mDevice->sendSlot(FIRST_SLOT);
2078 mDevice->sendUp();
2079
arthurhungcc7f9802020-04-30 17:55:40 +08002080 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2081 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002082}
2083
Michael Wrightd02c5b62014-02-10 15:10:22 -08002084// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002085class InputDeviceTest : public testing::Test {
2086protected:
2087 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002088 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002089 static const int32_t DEVICE_ID;
2090 static const int32_t DEVICE_GENERATION;
2091 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002092 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002093 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002094
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002095 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002096 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002097 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002098 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002099 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100
Chris Yea52ade12020-08-27 16:49:20 -07002101 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002102 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002103 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002104 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002105 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2106 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002107 InputDeviceIdentifier identifier;
2108 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002109 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002110 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002111 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002112 mReader->pushNextDevice(mDevice);
2113 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2114 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002115 }
2116
Chris Yea52ade12020-08-27 16:49:20 -07002117 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002118 mFakeListener.clear();
2119 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002120 }
2121};
2122
2123const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002124const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002125const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002126const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2127const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002128const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2129 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002130const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002131
2132TEST_F(InputDeviceTest, ImmutableProperties) {
2133 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002134 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002135 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002136}
2137
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002138TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2139 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002140}
2141
Michael Wrightd02c5b62014-02-10 15:10:22 -08002142TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2143 // Configuration.
2144 InputReaderConfiguration config;
2145 mDevice->configure(ARBITRARY_TIME, &config, 0);
2146
2147 // Reset.
2148 mDevice->reset(ARBITRARY_TIME);
2149
2150 NotifyDeviceResetArgs resetArgs;
2151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2152 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2153 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2154
2155 // Metadata.
2156 ASSERT_TRUE(mDevice->isIgnored());
2157 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2158
2159 InputDeviceInfo info;
2160 mDevice->getDeviceInfo(&info);
2161 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002162 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002163 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2164 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2165
2166 // State queries.
2167 ASSERT_EQ(0, mDevice->getMetaState());
2168
2169 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2170 << "Ignored device should return unknown key code state.";
2171 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2172 << "Ignored device should return unknown scan code state.";
2173 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2174 << "Ignored device should return unknown switch state.";
2175
2176 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2177 uint8_t flags[2] = { 0, 1 };
2178 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2179 << "Ignored device should never mark any key codes.";
2180 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2181 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2182}
2183
2184TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2185 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002186 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002187
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002188 FakeInputMapper& mapper1 =
2189 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002190 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2191 mapper1.setMetaState(AMETA_ALT_ON);
2192 mapper1.addSupportedKeyCode(AKEYCODE_A);
2193 mapper1.addSupportedKeyCode(AKEYCODE_B);
2194 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2195 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2196 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2197 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2198 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002199
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002200 FakeInputMapper& mapper2 =
2201 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002202 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002203
2204 InputReaderConfiguration config;
2205 mDevice->configure(ARBITRARY_TIME, &config, 0);
2206
2207 String8 propertyValue;
2208 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2209 << "Device should have read configuration during configuration phase.";
2210 ASSERT_STREQ("value", propertyValue.string());
2211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002212 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2213 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002214
2215 // Reset
2216 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002217 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2218 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002219
2220 NotifyDeviceResetArgs resetArgs;
2221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2222 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2223 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2224
2225 // Metadata.
2226 ASSERT_FALSE(mDevice->isIgnored());
2227 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2228
2229 InputDeviceInfo info;
2230 mDevice->getDeviceInfo(&info);
2231 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002232 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2234 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2235
2236 // State queries.
2237 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2238 << "Should query mappers and combine meta states.";
2239
2240 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2241 << "Should return unknown key code state when source not supported.";
2242 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2243 << "Should return unknown scan code state when source not supported.";
2244 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2245 << "Should return unknown switch state when source not supported.";
2246
2247 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2248 << "Should query mapper when source is supported.";
2249 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2250 << "Should query mapper when source is supported.";
2251 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2252 << "Should query mapper when source is supported.";
2253
2254 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2255 uint8_t flags[4] = { 0, 0, 0, 1 };
2256 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2257 << "Should do nothing when source is unsupported.";
2258 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2259 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2260 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2261 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2262
2263 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2264 << "Should query mapper when source is supported.";
2265 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2266 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2267 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2268 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2269
2270 // Event handling.
2271 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002272 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002273 mDevice->process(&event, 1);
2274
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002275 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2276 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277}
2278
Arthur Hung2c9a3342019-07-23 14:18:59 +08002279// A single input device is associated with a specific display. Check that:
2280// 1. Device is disabled if the viewport corresponding to the associated display is not found
2281// 2. Device is disabled when setEnabled API is called
2282TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002283 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002284
2285 // First Configuration.
2286 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2287
2288 // Device should be enabled by default.
2289 ASSERT_TRUE(mDevice->isEnabled());
2290
2291 // Prepare associated info.
2292 constexpr uint8_t hdmi = 1;
2293 const std::string UNIQUE_ID = "local:1";
2294
2295 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2296 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2297 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2298 // Device should be disabled because it is associated with a specific display via
2299 // input port <-> display port association, but the corresponding display is not found
2300 ASSERT_FALSE(mDevice->isEnabled());
2301
2302 // Prepare displays.
2303 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002304 DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002305 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2306 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2307 ASSERT_TRUE(mDevice->isEnabled());
2308
2309 // Device should be disabled after set disable.
2310 mFakePolicy->addDisabledDevice(mDevice->getId());
2311 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2312 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2313 ASSERT_FALSE(mDevice->isEnabled());
2314
2315 // Device should still be disabled even found the associated display.
2316 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2317 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2318 ASSERT_FALSE(mDevice->isEnabled());
2319}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002320
2321// --- InputMapperTest ---
2322
2323class InputMapperTest : public testing::Test {
2324protected:
2325 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002326 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002327 static const int32_t DEVICE_ID;
2328 static const int32_t DEVICE_GENERATION;
2329 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002330 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002331 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002333 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002334 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002335 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002336 std::unique_ptr<InstrumentedInputReader> mReader;
2337 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338
Chris Ye1b0c7342020-07-28 21:57:03 -07002339 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002340 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002342 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002343 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2344 mFakeListener);
2345 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002346 }
2347
Chris Yea52ade12020-08-27 16:49:20 -07002348 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002349
Chris Yea52ade12020-08-27 16:49:20 -07002350 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002351 mFakeListener.clear();
2352 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353 }
2354
2355 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002356 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002357 }
2358
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002359 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002360 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002361 mReader->requestRefreshConfiguration(changes);
2362 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002363 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002364 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2365 }
2366
arthurhungdcef2dc2020-08-11 14:47:50 +08002367 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2368 const std::string& location, int32_t eventHubId,
2369 Flags<InputDeviceClass> classes) {
2370 InputDeviceIdentifier identifier;
2371 identifier.name = name;
2372 identifier.location = location;
2373 std::shared_ptr<InputDevice> device =
2374 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2375 identifier);
2376 mReader->pushNextDevice(device);
2377 mFakeEventHub->addDevice(eventHubId, name, classes);
2378 mReader->loopOnce();
2379 return device;
2380 }
2381
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002382 template <class T, typename... Args>
2383 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002384 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002385 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002387 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002388 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 }
2390
2391 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002392 int32_t orientation, const std::string& uniqueId,
2393 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002394 mFakePolicy->addDisplayViewport(
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002395 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002396 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2397 }
2398
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002399 void clearViewports() {
2400 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 }
2402
arthurhungdcef2dc2020-08-11 14:47:50 +08002403 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404 RawEvent event;
2405 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002406 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407 event.type = type;
2408 event.code = code;
2409 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002410 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002411 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412 }
2413
2414 static void assertMotionRange(const InputDeviceInfo& info,
2415 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2416 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002417 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2419 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2420 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2421 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2422 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2423 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2424 }
2425
2426 static void assertPointerCoords(const PointerCoords& coords,
2427 float x, float y, float pressure, float size,
2428 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2429 float orientation, float distance) {
2430 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2431 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2432 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2433 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2434 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2435 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2436 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2437 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2438 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2439 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2440 }
2441
Michael Wright17db18e2020-06-26 20:51:44 +01002442 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002444 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445 ASSERT_NEAR(x, actualX, 1);
2446 ASSERT_NEAR(y, actualY, 1);
2447 }
2448};
2449
2450const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002451const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002452const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002453const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2454const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002455const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2456 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002457const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458
2459// --- SwitchInputMapperTest ---
2460
2461class SwitchInputMapperTest : public InputMapperTest {
2462protected:
2463};
2464
2465TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002466 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002468 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469}
2470
2471TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002472 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002474 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002475 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002477 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002478 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002479}
2480
2481TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002482 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002484 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2485 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2486 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2487 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002488
2489 NotifySwitchArgs args;
2490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2491 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002492 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2493 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494 args.switchMask);
2495 ASSERT_EQ(uint32_t(0), args.policyFlags);
2496}
2497
2498
2499// --- KeyboardInputMapperTest ---
2500
2501class KeyboardInputMapperTest : public InputMapperTest {
2502protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002503 const std::string UNIQUE_ID = "local:0";
2504
2505 void prepareDisplay(int32_t orientation);
2506
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002507 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002508 int32_t originalKeyCode, int32_t rotatedKeyCode,
2509 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002510};
2511
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002512/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2513 * orientation.
2514 */
2515void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002516 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2517 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002518}
2519
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002520void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002521 int32_t originalScanCode, int32_t originalKeyCode,
2522 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523 NotifyKeyArgs args;
2524
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002525 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2527 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2528 ASSERT_EQ(originalScanCode, args.scanCode);
2529 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002530 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002532 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2535 ASSERT_EQ(originalScanCode, args.scanCode);
2536 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002537 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538}
2539
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002541 KeyboardInputMapper& mapper =
2542 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2543 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002545 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546}
2547
2548TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2549 const int32_t USAGE_A = 0x070004;
2550 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2552 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002553 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2554 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2555 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002556
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002557 KeyboardInputMapper& mapper =
2558 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2559 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002560 // Initial metastate to AMETA_NONE.
2561 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2562 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563
2564 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002565 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566 NotifyKeyArgs args;
2567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2568 ASSERT_EQ(DEVICE_ID, args.deviceId);
2569 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2570 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2571 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2572 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2573 ASSERT_EQ(KEY_HOME, args.scanCode);
2574 ASSERT_EQ(AMETA_NONE, args.metaState);
2575 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2576 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2577 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2578
2579 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002580 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2582 ASSERT_EQ(DEVICE_ID, args.deviceId);
2583 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2584 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2585 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2586 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2587 ASSERT_EQ(KEY_HOME, args.scanCode);
2588 ASSERT_EQ(AMETA_NONE, args.metaState);
2589 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2590 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2591 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2592
2593 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002594 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2595 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2597 ASSERT_EQ(DEVICE_ID, args.deviceId);
2598 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2599 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2600 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2601 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2602 ASSERT_EQ(0, args.scanCode);
2603 ASSERT_EQ(AMETA_NONE, args.metaState);
2604 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2605 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2606 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2607
2608 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002609 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2610 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2612 ASSERT_EQ(DEVICE_ID, args.deviceId);
2613 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2614 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2615 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2616 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2617 ASSERT_EQ(0, args.scanCode);
2618 ASSERT_EQ(AMETA_NONE, args.metaState);
2619 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2620 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2621 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2622
2623 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002624 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2625 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2627 ASSERT_EQ(DEVICE_ID, args.deviceId);
2628 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2629 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2630 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2631 ASSERT_EQ(0, args.keyCode);
2632 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2633 ASSERT_EQ(AMETA_NONE, args.metaState);
2634 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2635 ASSERT_EQ(0U, args.policyFlags);
2636 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2637
2638 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002639 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2640 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2642 ASSERT_EQ(DEVICE_ID, args.deviceId);
2643 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2644 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2645 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2646 ASSERT_EQ(0, args.keyCode);
2647 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2648 ASSERT_EQ(AMETA_NONE, args.metaState);
2649 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2650 ASSERT_EQ(0U, args.policyFlags);
2651 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2652}
2653
2654TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2656 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002657 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2658 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2659 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002661 KeyboardInputMapper& mapper =
2662 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2663 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664
arthurhungc903df12020-08-11 15:08:42 +08002665 // Initial metastate to AMETA_NONE.
2666 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2667 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668
2669 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002670 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671 NotifyKeyArgs args;
2672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2673 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002674 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002675 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676
2677 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002678 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2680 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002681 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682
2683 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002684 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2686 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002687 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688
2689 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002690 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2692 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002693 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002694 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695}
2696
2697TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002698 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2699 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2700 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2701 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002703 KeyboardInputMapper& mapper =
2704 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2705 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002707 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2709 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2710 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2711 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2712 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2713 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2714 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2715 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2716}
2717
2718TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002719 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2720 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2722 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002723
Michael Wrightd02c5b62014-02-10 15:10:22 -08002724 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002725 KeyboardInputMapper& mapper =
2726 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2727 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002729 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002730 ASSERT_NO_FATAL_FAILURE(
2731 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2732 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2733 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2734 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2735 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2736 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2737 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002739 clearViewports();
2740 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002741 ASSERT_NO_FATAL_FAILURE(
2742 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2743 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2744 AKEYCODE_DPAD_UP, DISPLAY_ID));
2745 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2746 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2747 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2748 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002750 clearViewports();
2751 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002752 ASSERT_NO_FATAL_FAILURE(
2753 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2754 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2755 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2756 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2757 AKEYCODE_DPAD_UP, DISPLAY_ID));
2758 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2759 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002761 clearViewports();
2762 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002763 ASSERT_NO_FATAL_FAILURE(
2764 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2765 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2766 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2767 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2768 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2769 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2770 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771
2772 // Special case: if orientation changes while key is down, we still emit the same keycode
2773 // in the key up as we did in the key down.
2774 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002775 clearViewports();
2776 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002777 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2779 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2780 ASSERT_EQ(KEY_UP, args.scanCode);
2781 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2782
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002783 clearViewports();
2784 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002785 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2787 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2788 ASSERT_EQ(KEY_UP, args.scanCode);
2789 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2790}
2791
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002792TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2793 // If the keyboard is not orientation aware,
2794 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002795 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002796
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002797 KeyboardInputMapper& mapper =
2798 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2799 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002800 NotifyKeyArgs args;
2801
2802 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002803 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002805 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2807 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2808
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002809 prepareDisplay(DISPLAY_ORIENTATION_0);
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(ADISPLAY_ID_NONE, args.displayId);
2815}
2816
2817TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2818 // If the keyboard is orientation aware,
2819 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002820 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002821
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002822 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002823 KeyboardInputMapper& mapper =
2824 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2825 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002826 NotifyKeyArgs args;
2827
2828 // Display id should be ADISPLAY_ID_NONE without any display configuration.
2829 // ^--- already checked by the previous test
2830
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002831 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002832 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002833 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002835 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2837 ASSERT_EQ(DISPLAY_ID, args.displayId);
2838
2839 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002840 clearViewports();
2841 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002842 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002843 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002845 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01002846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2847 ASSERT_EQ(newDisplayId, args.displayId);
2848}
2849
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 KeyboardInputMapper& mapper =
2852 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2853 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002855 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002856 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002858 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002859 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860}
2861
2862TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002863 KeyboardInputMapper& mapper =
2864 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2865 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002867 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002868 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002870 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002871 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872}
2873
2874TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002875 KeyboardInputMapper& mapper =
2876 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2877 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002879 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880
2881 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2882 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002883 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002884 ASSERT_TRUE(flags[0]);
2885 ASSERT_FALSE(flags[1]);
2886}
2887
2888TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002889 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2890 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2891 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2892 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2893 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2894 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002895
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002896 KeyboardInputMapper& mapper =
2897 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2898 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07002899 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08002900 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2901 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902
2903 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002904 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2905 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2906 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907
2908 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002909 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2910 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002911 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2912 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2913 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002914 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
2916 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002917 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2918 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002919 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2920 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2921 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002922 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923
2924 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002925 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2926 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002927 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2928 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2929 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002930 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931
2932 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002933 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2934 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002935 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2936 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2937 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002938 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939
2940 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002941 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2942 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002943 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2944 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2945 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002946 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947
2948 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002949 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2950 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002951 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2952 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2953 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002954 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955}
2956
Chris Yea52ade12020-08-27 16:49:20 -07002957TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
2958 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
2959 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
2960 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
2961 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
2962
2963 KeyboardInputMapper& mapper =
2964 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2965 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
2966
2967 // Initial metastate should be AMETA_NONE as no meta keys added.
2968 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2969 // Meta state should be AMETA_NONE after reset
2970 mapper.reset(ARBITRARY_TIME);
2971 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2972 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
2973 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
2974 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2975
2976 NotifyKeyArgs args;
2977 // Press button "A"
2978 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
2979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2980 ASSERT_EQ(AMETA_NONE, args.metaState);
2981 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2982 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2983 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2984
2985 // Button up.
2986 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
2987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2988 ASSERT_EQ(AMETA_NONE, args.metaState);
2989 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2990 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2991 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
2992}
2993
Arthur Hung2c9a3342019-07-23 14:18:59 +08002994TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2995 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2997 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2998 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2999 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003000
3001 // keyboard 2.
3002 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003003 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003004 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003005 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003006 std::shared_ptr<InputDevice> device2 =
3007 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3008 Flags<InputDeviceClass>(0));
3009
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003010 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3011 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3012 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3013 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003015 KeyboardInputMapper& mapper =
3016 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3017 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003019 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003020 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003021 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003022 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3023 device2->reset(ARBITRARY_TIME);
3024
3025 // Prepared displays and associated info.
3026 constexpr uint8_t hdmi1 = 0;
3027 constexpr uint8_t hdmi2 = 1;
3028 const std::string SECONDARY_UNIQUE_ID = "local:1";
3029
3030 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3031 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3032
3033 // No associated display viewport found, should disable the device.
3034 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3035 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3036 ASSERT_FALSE(device2->isEnabled());
3037
3038 // Prepare second display.
3039 constexpr int32_t newDisplayId = 2;
3040 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003041 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003042 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003043 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003044 // Default device will reconfigure above, need additional reconfiguration for another device.
3045 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3046 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3047
3048 // Device should be enabled after the associated display is found.
3049 ASSERT_TRUE(mDevice->isEnabled());
3050 ASSERT_TRUE(device2->isEnabled());
3051
3052 // Test pad key events
3053 ASSERT_NO_FATAL_FAILURE(
3054 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3055 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3056 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3057 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3058 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3059 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3060 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3061
3062 ASSERT_NO_FATAL_FAILURE(
3063 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3064 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3065 AKEYCODE_DPAD_RIGHT, newDisplayId));
3066 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3067 AKEYCODE_DPAD_DOWN, newDisplayId));
3068 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3069 AKEYCODE_DPAD_LEFT, newDisplayId));
3070}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
arthurhungc903df12020-08-11 15:08:42 +08003072TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3073 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3074 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3075 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3076 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3077 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3078 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3079
3080 KeyboardInputMapper& mapper =
3081 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3082 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3083 // Initial metastate to AMETA_NONE.
3084 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3085 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3086
3087 // Initialization should have turned all of the lights off.
3088 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3089 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3090 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3091
3092 // Toggle caps lock on.
3093 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3094 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3095 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3096 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3097
3098 // Toggle num lock on.
3099 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3100 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3101 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3102 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3103
3104 // Toggle scroll lock on.
3105 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3106 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3107 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3108 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3109
3110 mFakeEventHub->removeDevice(EVENTHUB_ID);
3111 mReader->loopOnce();
3112
3113 // keyboard 2 should default toggle keys.
3114 const std::string USB2 = "USB2";
3115 const std::string DEVICE_NAME2 = "KEYBOARD2";
3116 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3117 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3118 std::shared_ptr<InputDevice> device2 =
3119 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3120 Flags<InputDeviceClass>(0));
3121 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3122 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3123 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3124 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3125 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3126 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3127
arthurhung6fe95782020-10-05 22:41:16 +08003128 KeyboardInputMapper& mapper2 =
3129 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3130 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003131 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3132 device2->reset(ARBITRARY_TIME);
3133
3134 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3135 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3136 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003137 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3138 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003139}
3140
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003141// --- KeyboardInputMapperTest_ExternalDevice ---
3142
3143class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3144protected:
Chris Yea52ade12020-08-27 16:49:20 -07003145 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003146};
3147
3148TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003149 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3150 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003151
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003152 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3153 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3154 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3155 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003157 KeyboardInputMapper& mapper =
3158 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3159 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003160
3161 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3162 NotifyKeyArgs args;
3163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3164 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3165
3166 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3168 ASSERT_EQ(uint32_t(0), args.policyFlags);
3169
3170 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3172 ASSERT_EQ(uint32_t(0), args.policyFlags);
3173
3174 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3176 ASSERT_EQ(uint32_t(0), args.policyFlags);
3177
3178 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3180 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3181
3182 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3184 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3185}
3186
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003187TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003188 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003189
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003190 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3191 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003193
Powei Fengd041c5d2019-05-03 17:11:33 -07003194 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003195 KeyboardInputMapper& mapper =
3196 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3197 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003198
3199 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3200 NotifyKeyArgs args;
3201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3202 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3203
3204 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3206 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3207
3208 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3210 ASSERT_EQ(uint32_t(0), args.policyFlags);
3211
3212 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3214 ASSERT_EQ(uint32_t(0), args.policyFlags);
3215
3216 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3218 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3219
3220 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3222 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3223}
3224
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225// --- CursorInputMapperTest ---
3226
3227class CursorInputMapperTest : public InputMapperTest {
3228protected:
3229 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3230
Michael Wright17db18e2020-06-26 20:51:44 +01003231 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232
Chris Yea52ade12020-08-27 16:49:20 -07003233 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234 InputMapperTest::SetUp();
3235
Michael Wright17db18e2020-06-26 20:51:44 +01003236 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003237 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 }
3239
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003240 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3241 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003242
3243 void prepareDisplay(int32_t orientation) {
3244 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003245 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003246 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3247 orientation, uniqueId, NO_PORT, viewportType);
3248 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249};
3250
3251const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3252
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003253void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3254 int32_t originalY, int32_t rotatedX,
3255 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256 NotifyMotionArgs args;
3257
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003258 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3259 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3260 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3264 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3265 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3266 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3267}
3268
3269TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003271 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274}
3275
3276TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003280 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281}
3282
3283TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003285 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286
3287 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289
3290 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003291 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3292 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3294 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3295
3296 // When the bounds are set, then there should be a valid motion range.
3297 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3298
3299 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003300 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301
3302 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3303 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3304 1, 800 - 1, 0.0f, 0.0f));
3305 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3306 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3307 2, 480 - 1, 0.0f, 0.0f));
3308 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3309 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3310 0.0f, 1.0f, 0.0f, 0.0f));
3311}
3312
3313TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003315 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316
3317 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003318 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319
3320 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3321 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3322 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3323 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3324 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3325 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3326 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3327 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3328 0.0f, 1.0f, 0.0f, 0.0f));
3329}
3330
3331TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334
arthurhungdcef2dc2020-08-11 14:47:50 +08003335 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336
3337 NotifyMotionArgs args;
3338
3339 // Button press.
3340 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003341 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3342 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3344 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3345 ASSERT_EQ(DEVICE_ID, args.deviceId);
3346 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3347 ASSERT_EQ(uint32_t(0), args.policyFlags);
3348 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3349 ASSERT_EQ(0, args.flags);
3350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3351 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3352 ASSERT_EQ(0, args.edgeFlags);
3353 ASSERT_EQ(uint32_t(1), args.pointerCount);
3354 ASSERT_EQ(0, args.pointerProperties[0].id);
3355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3357 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3358 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3359 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3360 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3361
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3363 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3364 ASSERT_EQ(DEVICE_ID, args.deviceId);
3365 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3366 ASSERT_EQ(uint32_t(0), args.policyFlags);
3367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3368 ASSERT_EQ(0, args.flags);
3369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3370 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3371 ASSERT_EQ(0, args.edgeFlags);
3372 ASSERT_EQ(uint32_t(1), args.pointerCount);
3373 ASSERT_EQ(0, args.pointerProperties[0].id);
3374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3376 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3377 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3378 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3379 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3380
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003382 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3383 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3385 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3386 ASSERT_EQ(DEVICE_ID, args.deviceId);
3387 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3388 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3390 ASSERT_EQ(0, args.flags);
3391 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3392 ASSERT_EQ(0, args.buttonState);
3393 ASSERT_EQ(0, args.edgeFlags);
3394 ASSERT_EQ(uint32_t(1), args.pointerCount);
3395 ASSERT_EQ(0, args.pointerProperties[0].id);
3396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3398 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3399 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3400 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3401 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3402
3403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3404 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3405 ASSERT_EQ(DEVICE_ID, args.deviceId);
3406 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3407 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3409 ASSERT_EQ(0, args.flags);
3410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3411 ASSERT_EQ(0, args.buttonState);
3412 ASSERT_EQ(0, args.edgeFlags);
3413 ASSERT_EQ(uint32_t(1), args.pointerCount);
3414 ASSERT_EQ(0, args.pointerProperties[0].id);
3415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3417 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3418 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3419 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3420 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3421}
3422
3423TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
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 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003430 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 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_MOVE, args.action);
3434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3435 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3436
3437 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003438 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3439 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3443 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3444}
3445
3446TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003448 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449
3450 NotifyMotionArgs args;
3451
3452 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003453 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3454 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3456 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3458 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3459
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3461 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3463 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3464
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003466 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3467 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003469 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3471 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3472
3473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3476 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3477}
3478
3479TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003481 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482
3483 NotifyMotionArgs args;
3484
3485 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003486 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3487 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3488 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3489 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3491 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3493 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3494 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3495
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3499 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3500 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3501
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003503 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3504 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3505 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3509 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3510 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3511
3512 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003513 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3514 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3518 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3519
3520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3523 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3524}
3525
3526TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003528 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003530 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3539}
3540
3541TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542 addConfigurationProperty("cursor.mode", "navigation");
3543 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003544 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003546 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3548 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3555
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003556 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3558 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3559 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3564 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3565
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003566 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3568 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3569 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3570 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3571 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3572 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3573 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3574 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3575
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003576 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3578 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3579 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3580 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3581 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3582 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3583 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3584 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3585}
3586
3587TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003589 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003590
3591 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3592 mFakePointerController->setPosition(100, 200);
3593 mFakePointerController->setButtonState(0);
3594
3595 NotifyMotionArgs motionArgs;
3596 NotifyKeyArgs keyArgs;
3597
3598 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003599 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3600 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3602 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3603 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3604 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3606 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3607
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3610 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3611 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3613 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3614
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003615 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3616 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619 ASSERT_EQ(0, motionArgs.buttonState);
3620 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3622 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3623
3624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003625 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626 ASSERT_EQ(0, motionArgs.buttonState);
3627 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3629 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3630
3631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003633 ASSERT_EQ(0, motionArgs.buttonState);
3634 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3636 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3637
3638 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003639 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3640 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3641 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3643 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3644 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3645 motionArgs.buttonState);
3646 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3647 mFakePointerController->getButtonState());
3648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3649 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3650
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3652 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3653 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3654 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3655 mFakePointerController->getButtonState());
3656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3657 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3658
3659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3661 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3662 motionArgs.buttonState);
3663 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3664 mFakePointerController->getButtonState());
3665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3666 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3667
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003668 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3669 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003672 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3673 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3675 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3676
3677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003679 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3680 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3682 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3683
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003684 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3685 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3688 ASSERT_EQ(0, motionArgs.buttonState);
3689 ASSERT_EQ(0, mFakePointerController->getButtonState());
3690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3691 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 -08003692 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3693 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003694
3695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696 ASSERT_EQ(0, motionArgs.buttonState);
3697 ASSERT_EQ(0, mFakePointerController->getButtonState());
3698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3700 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 -08003701
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3703 ASSERT_EQ(0, motionArgs.buttonState);
3704 ASSERT_EQ(0, mFakePointerController->getButtonState());
3705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3707 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3708
3709 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003710 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3711 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3713 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3714 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003715
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3719 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3721 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3722
3723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3724 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3725 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3726 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3728 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3729
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003730 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3731 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003733 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734 ASSERT_EQ(0, motionArgs.buttonState);
3735 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3737 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3738
3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003741 ASSERT_EQ(0, motionArgs.buttonState);
3742 ASSERT_EQ(0, mFakePointerController->getButtonState());
3743
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3745 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3747 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3748 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3749
3750 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003751 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3752 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3754 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3755 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003756
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003758 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3760 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003761 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
3764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3765 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3766 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3767 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3769 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3770
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003771 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3772 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003774 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775 ASSERT_EQ(0, motionArgs.buttonState);
3776 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3778 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003779
3780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3782 ASSERT_EQ(0, motionArgs.buttonState);
3783 ASSERT_EQ(0, mFakePointerController->getButtonState());
3784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3785 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3786
Michael Wrightd02c5b62014-02-10 15:10:22 -08003787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3788 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3789 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3790
3791 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003792 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3793 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3795 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3796 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003797
Michael Wrightd02c5b62014-02-10 15:10:22 -08003798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003799 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3801 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003802 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
3805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3807 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3808 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3810 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3811
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003812 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3813 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003815 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816 ASSERT_EQ(0, motionArgs.buttonState);
3817 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3819 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 -08003820
3821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3823 ASSERT_EQ(0, motionArgs.buttonState);
3824 ASSERT_EQ(0, mFakePointerController->getButtonState());
3825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3826 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3827
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3829 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3830 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3831
3832 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003833 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3834 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3836 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3837 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003838
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003840 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3842 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003843 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
3846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3847 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3848 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3849 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3851 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3852
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003853 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3854 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003856 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857 ASSERT_EQ(0, motionArgs.buttonState);
3858 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3860 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 -08003861
3862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3863 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3864 ASSERT_EQ(0, motionArgs.buttonState);
3865 ASSERT_EQ(0, mFakePointerController->getButtonState());
3866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3867 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3868
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3870 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3871 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3872}
3873
3874TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877
3878 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3879 mFakePointerController->setPosition(100, 200);
3880 mFakePointerController->setButtonState(0);
3881
3882 NotifyMotionArgs args;
3883
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003884 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3885 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3886 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003888 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3889 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3891 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 +01003892 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003893}
3894
3895TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003896 addConfigurationProperty("cursor.mode", "pointer");
3897 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003898 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003899
3900 NotifyDeviceResetArgs resetArgs;
3901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3902 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3903 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3904
3905 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3906 mFakePointerController->setPosition(100, 200);
3907 mFakePointerController->setButtonState(0);
3908
3909 NotifyMotionArgs args;
3910
3911 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003912 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3913 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3914 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3916 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3919 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 +01003920 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003921
3922 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003923 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3924 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3926 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3927 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3929 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3931 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3932 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3934 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3935
3936 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003937 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3938 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3940 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3943 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3945 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3946 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3948 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3949
3950 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003951 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3952 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3953 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3955 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3958 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 +01003959 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003960
3961 // Disable pointer capture and check that the device generation got bumped
3962 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08003963 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003964 mFakePolicy->setPointerCapture(false);
3965 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08003966 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003967
3968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3969 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3970 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3971
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003972 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3973 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3974 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3976 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3979 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 +01003980 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981}
3982
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003983TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003984 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003985
Garfield Tan888a6a42020-01-09 11:39:16 -08003986 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003987 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08003988 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3989 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003990 SECOND_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08003991 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3992 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3993
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003994 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3995 mFakePointerController->setPosition(100, 200);
3996 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08003997
3998 NotifyMotionArgs args;
3999 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4000 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4001 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4003 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4004 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4006 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 +01004007 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004008 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4009}
4010
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011// --- TouchInputMapperTest ---
4012
4013class TouchInputMapperTest : public InputMapperTest {
4014protected:
4015 static const int32_t RAW_X_MIN;
4016 static const int32_t RAW_X_MAX;
4017 static const int32_t RAW_Y_MIN;
4018 static const int32_t RAW_Y_MAX;
4019 static const int32_t RAW_TOUCH_MIN;
4020 static const int32_t RAW_TOUCH_MAX;
4021 static const int32_t RAW_TOOL_MIN;
4022 static const int32_t RAW_TOOL_MAX;
4023 static const int32_t RAW_PRESSURE_MIN;
4024 static const int32_t RAW_PRESSURE_MAX;
4025 static const int32_t RAW_ORIENTATION_MIN;
4026 static const int32_t RAW_ORIENTATION_MAX;
4027 static const int32_t RAW_DISTANCE_MIN;
4028 static const int32_t RAW_DISTANCE_MAX;
4029 static const int32_t RAW_TILT_MIN;
4030 static const int32_t RAW_TILT_MAX;
4031 static const int32_t RAW_ID_MIN;
4032 static const int32_t RAW_ID_MAX;
4033 static const int32_t RAW_SLOT_MIN;
4034 static const int32_t RAW_SLOT_MAX;
4035 static const float X_PRECISION;
4036 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004037 static const float X_PRECISION_VIRTUAL;
4038 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039
4040 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004041 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042
4043 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4044
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004045 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004046 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004047
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 enum Axes {
4049 POSITION = 1 << 0,
4050 TOUCH = 1 << 1,
4051 TOOL = 1 << 2,
4052 PRESSURE = 1 << 3,
4053 ORIENTATION = 1 << 4,
4054 MINOR = 1 << 5,
4055 ID = 1 << 6,
4056 DISTANCE = 1 << 7,
4057 TILT = 1 << 8,
4058 SLOT = 1 << 9,
4059 TOOL_TYPE = 1 << 10,
4060 };
4061
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004062 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4063 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004064 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004066 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 int32_t toRawX(float displayX);
4068 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004069 float toCookedX(float rawX, float rawY);
4070 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004072 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004074 float toDisplayY(int32_t rawY, int32_t displayHeight);
4075
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076};
4077
4078const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4079const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4080const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4081const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4082const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4083const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4084const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4085const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004086const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4087const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4089const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4090const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4091const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4092const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4093const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4094const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4095const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4096const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4097const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4098const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4099const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004100const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4101 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4102const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4103 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004104const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4105 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106
4107const float TouchInputMapperTest::GEOMETRIC_SCALE =
4108 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4109 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4110
4111const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4112 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4113 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4114};
4115
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004116void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004117 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4118 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004119}
4120
4121void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4122 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4123 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124}
4125
Santos Cordonfa5cf462017-04-05 10:37:00 -07004126void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004127 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4128 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4129 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004130}
4131
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004133 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4134 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137}
4138
Jason Gerecke489fda82012-09-07 17:19:40 -07004139void TouchInputMapperTest::prepareLocationCalibration() {
4140 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4141}
4142
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143int32_t TouchInputMapperTest::toRawX(float displayX) {
4144 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4145}
4146
4147int32_t TouchInputMapperTest::toRawY(float displayY) {
4148 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4149}
4150
Jason Gerecke489fda82012-09-07 17:19:40 -07004151float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4152 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4153 return rawX;
4154}
4155
4156float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4157 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4158 return rawY;
4159}
4160
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004162 return toDisplayX(rawX, DISPLAY_WIDTH);
4163}
4164
4165float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4166 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167}
4168
4169float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004170 return toDisplayY(rawY, DISPLAY_HEIGHT);
4171}
4172
4173float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4174 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175}
4176
4177
4178// --- SingleTouchInputMapperTest ---
4179
4180class SingleTouchInputMapperTest : public TouchInputMapperTest {
4181protected:
4182 void prepareButtons();
4183 void prepareAxes(int axes);
4184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004185 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4186 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4187 void processUp(SingleTouchInputMapper& mappery);
4188 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4189 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4190 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4191 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4192 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4193 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194};
4195
4196void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004197 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198}
4199
4200void SingleTouchInputMapperTest::prepareAxes(int axes) {
4201 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004202 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204 }
4205 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004206 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4207 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 }
4209 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4211 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 }
4213 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4215 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 }
4217 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004218 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 }
4221}
4222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004224 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4225 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4226 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227}
4228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004229void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004230 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4231 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232}
4233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004235 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236}
4237
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004238void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004239 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240}
4241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004242void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4243 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004244 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245}
4246
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004247void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004248 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249}
4250
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004251void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4252 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004253 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4254 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255}
4256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004257void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4258 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004259 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260}
4261
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004262void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004263 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264}
4265
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 prepareButtons();
4268 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004269 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004271 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272}
4273
4274TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004275 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4276 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 prepareButtons();
4278 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004279 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004281 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282}
4283
4284TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 prepareButtons();
4286 prepareAxes(POSITION);
4287 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004288 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004290 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291}
4292
4293TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 prepareButtons();
4295 prepareAxes(POSITION);
4296 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004297 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300}
4301
4302TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 addConfigurationProperty("touch.deviceType", "touchScreen");
4304 prepareDisplay(DISPLAY_ORIENTATION_0);
4305 prepareButtons();
4306 prepareAxes(POSITION);
4307 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004308 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
4310 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004311 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312
4313 // Virtual key is down.
4314 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4315 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4316 processDown(mapper, x, y);
4317 processSync(mapper);
4318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4319
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004320 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
4322 // Virtual key is up.
4323 processUp(mapper);
4324 processSync(mapper);
4325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004327 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328}
4329
4330TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 addConfigurationProperty("touch.deviceType", "touchScreen");
4332 prepareDisplay(DISPLAY_ORIENTATION_0);
4333 prepareButtons();
4334 prepareAxes(POSITION);
4335 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004336 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337
4338 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004339 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340
4341 // Virtual key is down.
4342 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4343 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4344 processDown(mapper, x, y);
4345 processSync(mapper);
4346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4347
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004348 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349
4350 // Virtual key is up.
4351 processUp(mapper);
4352 processSync(mapper);
4353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4354
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004355 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356}
4357
4358TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 addConfigurationProperty("touch.deviceType", "touchScreen");
4360 prepareDisplay(DISPLAY_ORIENTATION_0);
4361 prepareButtons();
4362 prepareAxes(POSITION);
4363 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004364 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365
4366 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4367 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004368 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 ASSERT_TRUE(flags[0]);
4370 ASSERT_FALSE(flags[1]);
4371}
4372
4373TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 addConfigurationProperty("touch.deviceType", "touchScreen");
4375 prepareDisplay(DISPLAY_ORIENTATION_0);
4376 prepareButtons();
4377 prepareAxes(POSITION);
4378 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004379 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380
arthurhungdcef2dc2020-08-11 14:47:50 +08004381 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
4383 NotifyKeyArgs args;
4384
4385 // Press virtual key.
4386 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4387 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4388 processDown(mapper, x, y);
4389 processSync(mapper);
4390
4391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4392 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4393 ASSERT_EQ(DEVICE_ID, args.deviceId);
4394 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4395 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4396 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4397 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4398 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4399 ASSERT_EQ(KEY_HOME, args.scanCode);
4400 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4401 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4402
4403 // Release virtual key.
4404 processUp(mapper);
4405 processSync(mapper);
4406
4407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4408 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4409 ASSERT_EQ(DEVICE_ID, args.deviceId);
4410 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4411 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4412 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4413 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4414 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4415 ASSERT_EQ(KEY_HOME, args.scanCode);
4416 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4417 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4418
4419 // Should not have sent any motions.
4420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4421}
4422
4423TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 addConfigurationProperty("touch.deviceType", "touchScreen");
4425 prepareDisplay(DISPLAY_ORIENTATION_0);
4426 prepareButtons();
4427 prepareAxes(POSITION);
4428 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004429 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430
arthurhungdcef2dc2020-08-11 14:47:50 +08004431 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432
4433 NotifyKeyArgs keyArgs;
4434
4435 // Press virtual key.
4436 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4437 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4438 processDown(mapper, x, y);
4439 processSync(mapper);
4440
4441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4442 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4443 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4444 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4445 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4446 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4447 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4448 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4449 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4450 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4451 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4452
4453 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4454 // into the display area.
4455 y -= 100;
4456 processMove(mapper, x, y);
4457 processSync(mapper);
4458
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4460 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4461 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4462 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4463 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4464 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4465 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4466 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4467 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4468 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4470 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4471
4472 NotifyMotionArgs motionArgs;
4473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4475 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4476 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4477 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4479 ASSERT_EQ(0, motionArgs.flags);
4480 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4481 ASSERT_EQ(0, motionArgs.buttonState);
4482 ASSERT_EQ(0, motionArgs.edgeFlags);
4483 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4484 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4487 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4488 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4489 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4490 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4491
4492 // Keep moving out of bounds. Should generate a pointer move.
4493 y -= 50;
4494 processMove(mapper, x, y);
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_MOVE, 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 // Release out of bounds. Should generate a pointer up.
4517 processUp(mapper);
4518 processSync(mapper);
4519
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4522 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4523 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4524 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4525 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4526 ASSERT_EQ(0, motionArgs.flags);
4527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4528 ASSERT_EQ(0, motionArgs.buttonState);
4529 ASSERT_EQ(0, motionArgs.edgeFlags);
4530 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4531 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4534 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4535 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4536 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4537 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4538
4539 // Should not have sent any more keys or motions.
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4542}
4543
4544TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 addConfigurationProperty("touch.deviceType", "touchScreen");
4546 prepareDisplay(DISPLAY_ORIENTATION_0);
4547 prepareButtons();
4548 prepareAxes(POSITION);
4549 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004550 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551
arthurhungdcef2dc2020-08-11 14:47:50 +08004552 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553
4554 NotifyMotionArgs motionArgs;
4555
4556 // Initially go down out of bounds.
4557 int32_t x = -10;
4558 int32_t y = -10;
4559 processDown(mapper, x, y);
4560 processSync(mapper);
4561
4562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4563
4564 // Move into the display area. Should generate a pointer down.
4565 x = 50;
4566 y = 75;
4567 processMove(mapper, x, y);
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_DOWN, motionArgs.action);
4576 ASSERT_EQ(0, motionArgs.flags);
4577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4578 ASSERT_EQ(0, motionArgs.buttonState);
4579 ASSERT_EQ(0, motionArgs.edgeFlags);
4580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4584 toDisplayX(x), 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 // Release. Should generate a pointer up.
4590 processUp(mapper);
4591 processSync(mapper);
4592
4593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4594 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4595 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4596 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4597 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4598 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4599 ASSERT_EQ(0, motionArgs.flags);
4600 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4601 ASSERT_EQ(0, motionArgs.buttonState);
4602 ASSERT_EQ(0, motionArgs.edgeFlags);
4603 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4604 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4607 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4608 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4609 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4610 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4611
4612 // Should not have sent any more keys or motions.
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4615}
4616
Santos Cordonfa5cf462017-04-05 10:37:00 -07004617TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004618 addConfigurationProperty("touch.deviceType", "touchScreen");
4619 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4620
4621 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4622 prepareButtons();
4623 prepareAxes(POSITION);
4624 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004625 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004626
arthurhungdcef2dc2020-08-11 14:47:50 +08004627 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004628
4629 NotifyMotionArgs motionArgs;
4630
4631 // Down.
4632 int32_t x = 100;
4633 int32_t y = 125;
4634 processDown(mapper, x, y);
4635 processSync(mapper);
4636
4637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4640 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4641 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4642 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4643 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4644 ASSERT_EQ(0, motionArgs.flags);
4645 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4646 ASSERT_EQ(0, motionArgs.buttonState);
4647 ASSERT_EQ(0, motionArgs.edgeFlags);
4648 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4649 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4652 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4653 1, 0, 0, 0, 0, 0, 0, 0));
4654 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4655 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4656 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4657
4658 // Move.
4659 x += 50;
4660 y += 75;
4661 processMove(mapper, x, y);
4662 processSync(mapper);
4663
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4665 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4666 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4667 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4668 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4669 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4671 ASSERT_EQ(0, motionArgs.flags);
4672 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4673 ASSERT_EQ(0, motionArgs.buttonState);
4674 ASSERT_EQ(0, motionArgs.edgeFlags);
4675 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4679 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4680 1, 0, 0, 0, 0, 0, 0, 0));
4681 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4682 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4684
4685 // Up.
4686 processUp(mapper);
4687 processSync(mapper);
4688
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4690 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4691 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4692 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4693 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4694 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4695 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4696 ASSERT_EQ(0, motionArgs.flags);
4697 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4698 ASSERT_EQ(0, motionArgs.buttonState);
4699 ASSERT_EQ(0, motionArgs.edgeFlags);
4700 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4701 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4704 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4705 1, 0, 0, 0, 0, 0, 0, 0));
4706 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4707 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4709
4710 // Should not have sent any more keys or motions.
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4713}
4714
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 addConfigurationProperty("touch.deviceType", "touchScreen");
4717 prepareDisplay(DISPLAY_ORIENTATION_0);
4718 prepareButtons();
4719 prepareAxes(POSITION);
4720 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004721 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722
arthurhungdcef2dc2020-08-11 14:47:50 +08004723 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724
4725 NotifyMotionArgs motionArgs;
4726
4727 // Down.
4728 int32_t x = 100;
4729 int32_t y = 125;
4730 processDown(mapper, x, y);
4731 processSync(mapper);
4732
4733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4734 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4735 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4736 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4737 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4739 ASSERT_EQ(0, motionArgs.flags);
4740 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4741 ASSERT_EQ(0, motionArgs.buttonState);
4742 ASSERT_EQ(0, motionArgs.edgeFlags);
4743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4744 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4747 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4748 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4749 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4751
4752 // Move.
4753 x += 50;
4754 y += 75;
4755 processMove(mapper, x, y);
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_MOVE, 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 // Up.
4778 processUp(mapper);
4779 processSync(mapper);
4780
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4783 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4784 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4785 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4786 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4787 ASSERT_EQ(0, motionArgs.flags);
4788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4789 ASSERT_EQ(0, motionArgs.buttonState);
4790 ASSERT_EQ(0, motionArgs.edgeFlags);
4791 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4792 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4795 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4796 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4797 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4799
4800 // Should not have sent any more keys or motions.
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4803}
4804
4805TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 addConfigurationProperty("touch.deviceType", "touchScreen");
4807 prepareButtons();
4808 prepareAxes(POSITION);
4809 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004810 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811
4812 NotifyMotionArgs args;
4813
4814 // Rotation 90.
4815 prepareDisplay(DISPLAY_ORIENTATION_90);
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
4828TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 addConfigurationProperty("touch.deviceType", "touchScreen");
4830 prepareButtons();
4831 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004832 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833
4834 NotifyMotionArgs args;
4835
4836 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004837 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838 prepareDisplay(DISPLAY_ORIENTATION_0);
4839 processDown(mapper, toRawX(50), toRawY(75));
4840 processSync(mapper);
4841
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4843 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4844 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4845
4846 processUp(mapper);
4847 processSync(mapper);
4848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4849
4850 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004851 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852 prepareDisplay(DISPLAY_ORIENTATION_90);
4853 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4854 processSync(mapper);
4855
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4857 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4858 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4859
4860 processUp(mapper);
4861 processSync(mapper);
4862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4863
4864 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004865 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 prepareDisplay(DISPLAY_ORIENTATION_180);
4867 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4868 processSync(mapper);
4869
4870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4871 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4872 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4873
4874 processUp(mapper);
4875 processSync(mapper);
4876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4877
4878 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004879 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 prepareDisplay(DISPLAY_ORIENTATION_270);
4881 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4882 processSync(mapper);
4883
4884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4885 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4886 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4887
4888 processUp(mapper);
4889 processSync(mapper);
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4891}
4892
4893TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 addConfigurationProperty("touch.deviceType", "touchScreen");
4895 prepareDisplay(DISPLAY_ORIENTATION_0);
4896 prepareButtons();
4897 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004898 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899
4900 // These calculations are based on the input device calibration documentation.
4901 int32_t rawX = 100;
4902 int32_t rawY = 200;
4903 int32_t rawPressure = 10;
4904 int32_t rawToolMajor = 12;
4905 int32_t rawDistance = 2;
4906 int32_t rawTiltX = 30;
4907 int32_t rawTiltY = 110;
4908
4909 float x = toDisplayX(rawX);
4910 float y = toDisplayY(rawY);
4911 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4912 float size = float(rawToolMajor) / RAW_TOOL_MAX;
4913 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4914 float distance = float(rawDistance);
4915
4916 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4917 float tiltScale = M_PI / 180;
4918 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4919 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4920 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4921 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4922
4923 processDown(mapper, rawX, rawY);
4924 processPressure(mapper, rawPressure);
4925 processToolMajor(mapper, rawToolMajor);
4926 processDistance(mapper, rawDistance);
4927 processTilt(mapper, rawTiltX, rawTiltY);
4928 processSync(mapper);
4929
4930 NotifyMotionArgs args;
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4933 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4934 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4935}
4936
Jason Gerecke489fda82012-09-07 17:19:40 -07004937TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07004938 addConfigurationProperty("touch.deviceType", "touchScreen");
4939 prepareDisplay(DISPLAY_ORIENTATION_0);
4940 prepareLocationCalibration();
4941 prepareButtons();
4942 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004943 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07004944
4945 int32_t rawX = 100;
4946 int32_t rawY = 200;
4947
4948 float x = toDisplayX(toCookedX(rawX, rawY));
4949 float y = toDisplayY(toCookedY(rawX, rawY));
4950
4951 processDown(mapper, rawX, rawY);
4952 processSync(mapper);
4953
4954 NotifyMotionArgs args;
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4957 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4958}
4959
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 addConfigurationProperty("touch.deviceType", "touchScreen");
4962 prepareDisplay(DISPLAY_ORIENTATION_0);
4963 prepareButtons();
4964 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004965 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966
4967 NotifyMotionArgs motionArgs;
4968 NotifyKeyArgs keyArgs;
4969
4970 processDown(mapper, 100, 200);
4971 processSync(mapper);
4972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4973 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4974 ASSERT_EQ(0, motionArgs.buttonState);
4975
4976 // press BTN_LEFT, release BTN_LEFT
4977 processKey(mapper, BTN_LEFT, 1);
4978 processSync(mapper);
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4981 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, 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_PRIMARY, motionArgs.buttonState);
4986
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987 processKey(mapper, BTN_LEFT, 0);
4988 processSync(mapper);
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004990 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004992
4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004995 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996
4997 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4998 processKey(mapper, BTN_RIGHT, 1);
4999 processKey(mapper, BTN_MIDDLE, 1);
5000 processSync(mapper);
5001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5003 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5004 motionArgs.buttonState);
5005
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5007 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5008 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5009
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5012 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5013 motionArgs.buttonState);
5014
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 processKey(mapper, BTN_RIGHT, 0);
5016 processSync(mapper);
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005018 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005020
5021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005023 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024
5025 processKey(mapper, BTN_MIDDLE, 0);
5026 processSync(mapper);
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005030
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005033 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034
5035 // press BTN_BACK, release BTN_BACK
5036 processKey(mapper, BTN_BACK, 1);
5037 processSync(mapper);
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5039 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5040 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005041
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005044 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5045
5046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5048 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049
5050 processKey(mapper, BTN_BACK, 0);
5051 processSync(mapper);
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005055
5056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005058 ASSERT_EQ(0, motionArgs.buttonState);
5059
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5061 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5062 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5063
5064 // press BTN_SIDE, release BTN_SIDE
5065 processKey(mapper, BTN_SIDE, 1);
5066 processSync(mapper);
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5068 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5069 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005070
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005073 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5074
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5077 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078
5079 processKey(mapper, BTN_SIDE, 0);
5080 processSync(mapper);
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005084
5085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005087 ASSERT_EQ(0, motionArgs.buttonState);
5088
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5091 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5092
5093 // press BTN_FORWARD, release BTN_FORWARD
5094 processKey(mapper, BTN_FORWARD, 1);
5095 processSync(mapper);
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5097 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5098 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005099
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005102 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5103
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5106 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107
5108 processKey(mapper, BTN_FORWARD, 0);
5109 processSync(mapper);
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005113
5114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005116 ASSERT_EQ(0, motionArgs.buttonState);
5117
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5119 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5120 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5121
5122 // press BTN_EXTRA, release BTN_EXTRA
5123 processKey(mapper, BTN_EXTRA, 1);
5124 processSync(mapper);
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5126 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5127 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005128
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005131 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5132
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5135 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136
5137 processKey(mapper, BTN_EXTRA, 0);
5138 processSync(mapper);
5139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005142
5143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005145 ASSERT_EQ(0, motionArgs.buttonState);
5146
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5148 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5149 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5150
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5152
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 // press BTN_STYLUS, release BTN_STYLUS
5154 processKey(mapper, BTN_STYLUS, 1);
5155 processSync(mapper);
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005158 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5159
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5162 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163
5164 processKey(mapper, BTN_STYLUS, 0);
5165 processSync(mapper);
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005167 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005169
5170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005172 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173
5174 // press BTN_STYLUS2, release BTN_STYLUS2
5175 processKey(mapper, BTN_STYLUS2, 1);
5176 processSync(mapper);
5177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005179 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5180
5181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5183 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184
5185 processKey(mapper, BTN_STYLUS2, 0);
5186 processSync(mapper);
5187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005190
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005193 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194
5195 // release touch
5196 processUp(mapper);
5197 processSync(mapper);
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5199 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5200 ASSERT_EQ(0, motionArgs.buttonState);
5201}
5202
5203TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204 addConfigurationProperty("touch.deviceType", "touchScreen");
5205 prepareDisplay(DISPLAY_ORIENTATION_0);
5206 prepareButtons();
5207 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005208 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209
5210 NotifyMotionArgs motionArgs;
5211
5212 // default tool type is finger
5213 processDown(mapper, 100, 200);
5214 processSync(mapper);
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5218
5219 // eraser
5220 processKey(mapper, BTN_TOOL_RUBBER, 1);
5221 processSync(mapper);
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5225
5226 // stylus
5227 processKey(mapper, BTN_TOOL_RUBBER, 0);
5228 processKey(mapper, BTN_TOOL_PEN, 1);
5229 processSync(mapper);
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5233
5234 // brush
5235 processKey(mapper, BTN_TOOL_PEN, 0);
5236 processKey(mapper, BTN_TOOL_BRUSH, 1);
5237 processSync(mapper);
5238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5241
5242 // pencil
5243 processKey(mapper, BTN_TOOL_BRUSH, 0);
5244 processKey(mapper, BTN_TOOL_PENCIL, 1);
5245 processSync(mapper);
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5249
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005250 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005251 processKey(mapper, BTN_TOOL_PENCIL, 0);
5252 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5253 processSync(mapper);
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5257
5258 // mouse
5259 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5260 processKey(mapper, BTN_TOOL_MOUSE, 1);
5261 processSync(mapper);
5262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5265
5266 // lens
5267 processKey(mapper, BTN_TOOL_MOUSE, 0);
5268 processKey(mapper, BTN_TOOL_LENS, 1);
5269 processSync(mapper);
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5273
5274 // double-tap
5275 processKey(mapper, BTN_TOOL_LENS, 0);
5276 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5277 processSync(mapper);
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5281
5282 // triple-tap
5283 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5284 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
5289
5290 // quad-tap
5291 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5292 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5293 processSync(mapper);
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5297
5298 // finger
5299 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5300 processKey(mapper, BTN_TOOL_FINGER, 1);
5301 processSync(mapper);
5302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5305
5306 // stylus trumps finger
5307 processKey(mapper, BTN_TOOL_PEN, 1);
5308 processSync(mapper);
5309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5312
5313 // eraser trumps stylus
5314 processKey(mapper, BTN_TOOL_RUBBER, 1);
5315 processSync(mapper);
5316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5319
5320 // mouse trumps eraser
5321 processKey(mapper, BTN_TOOL_MOUSE, 1);
5322 processSync(mapper);
5323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5326
5327 // back to default tool type
5328 processKey(mapper, BTN_TOOL_MOUSE, 0);
5329 processKey(mapper, BTN_TOOL_RUBBER, 0);
5330 processKey(mapper, BTN_TOOL_PEN, 0);
5331 processKey(mapper, BTN_TOOL_FINGER, 0);
5332 processSync(mapper);
5333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5336}
5337
5338TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 addConfigurationProperty("touch.deviceType", "touchScreen");
5340 prepareDisplay(DISPLAY_ORIENTATION_0);
5341 prepareButtons();
5342 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005343 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345
5346 NotifyMotionArgs motionArgs;
5347
5348 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5349 processKey(mapper, BTN_TOOL_FINGER, 1);
5350 processMove(mapper, 100, 200);
5351 processSync(mapper);
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5353 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5355 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5356
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5360 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5361
5362 // move a little
5363 processMove(mapper, 150, 250);
5364 processSync(mapper);
5365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5368 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5369
5370 // down when BTN_TOUCH is pressed, pressure defaults to 1
5371 processKey(mapper, BTN_TOUCH, 1);
5372 processSync(mapper);
5373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5379 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5381 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5382
5383 // up when BTN_TOUCH is released, hover restored
5384 processKey(mapper, BTN_TOUCH, 0);
5385 processSync(mapper);
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5387 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5389 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5390
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5394 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5395
5396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5399 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5400
5401 // exit hover when pointer goes away
5402 processKey(mapper, BTN_TOOL_FINGER, 0);
5403 processSync(mapper);
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5407 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5408}
5409
5410TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 addConfigurationProperty("touch.deviceType", "touchScreen");
5412 prepareDisplay(DISPLAY_ORIENTATION_0);
5413 prepareButtons();
5414 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005415 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416
5417 NotifyMotionArgs motionArgs;
5418
5419 // initially hovering because pressure is 0
5420 processDown(mapper, 100, 200);
5421 processPressure(mapper, 0);
5422 processSync(mapper);
5423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5424 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5426 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5427
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5429 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5431 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5432
5433 // move a little
5434 processMove(mapper, 150, 250);
5435 processSync(mapper);
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5439 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5440
5441 // down when pressure is non-zero
5442 processPressure(mapper, RAW_PRESSURE_MAX);
5443 processSync(mapper);
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5452 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5453
5454 // up when pressure becomes 0, hover restored
5455 processPressure(mapper, 0);
5456 processSync(mapper);
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5458 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5460 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5461
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5465 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5466
5467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5470 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5471
5472 // exit hover when pointer goes away
5473 processUp(mapper);
5474 processSync(mapper);
5475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5478 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5479}
5480
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481// --- MultiTouchInputMapperTest ---
5482
5483class MultiTouchInputMapperTest : public TouchInputMapperTest {
5484protected:
5485 void prepareAxes(int axes);
5486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005487 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5488 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5489 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5490 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5491 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5492 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5493 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5494 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5495 void processId(MultiTouchInputMapper& mapper, int32_t id);
5496 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5497 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5498 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5499 void processMTSync(MultiTouchInputMapper& mapper);
5500 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501};
5502
5503void MultiTouchInputMapperTest::prepareAxes(int axes) {
5504 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005505 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005507 }
5508 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5510 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005512 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5513 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514 }
5515 }
5516 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005517 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5518 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005520 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5521 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522 }
5523 }
5524 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005525 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5526 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 }
5528 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005529 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5530 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531 }
5532 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005533 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5534 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005535 }
5536 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005537 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5538 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 }
5540 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005541 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5542 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543 }
5544 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005545 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546 }
5547}
5548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005549void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5550 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005551 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5552 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553}
5554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005555void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5556 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005557 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558}
5559
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005560void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5561 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005562 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563}
5564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005565void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005566 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567}
5568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005569void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005570 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571}
5572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005573void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5574 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005575 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576}
5577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005578void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005579 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005580}
5581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005582void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005583 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584}
5585
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005586void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005587 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588}
5589
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005590void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005591 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592}
5593
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005594void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005595 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596}
5597
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005598void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5599 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005600 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601}
5602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005603void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005604 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605}
5606
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005607void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005608 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609}
5610
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 addConfigurationProperty("touch.deviceType", "touchScreen");
5613 prepareDisplay(DISPLAY_ORIENTATION_0);
5614 prepareAxes(POSITION);
5615 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005616 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617
arthurhungdcef2dc2020-08-11 14:47:50 +08005618 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619
5620 NotifyMotionArgs motionArgs;
5621
5622 // Two fingers down at once.
5623 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5624 processPosition(mapper, x1, y1);
5625 processMTSync(mapper);
5626 processPosition(mapper, x2, y2);
5627 processMTSync(mapper);
5628 processSync(mapper);
5629
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5631 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5632 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5633 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5634 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5635 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5636 ASSERT_EQ(0, motionArgs.flags);
5637 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5638 ASSERT_EQ(0, motionArgs.buttonState);
5639 ASSERT_EQ(0, motionArgs.edgeFlags);
5640 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5641 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5644 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5645 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5646 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5648
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5650 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5651 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5652 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5653 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5655 motionArgs.action);
5656 ASSERT_EQ(0, motionArgs.flags);
5657 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5658 ASSERT_EQ(0, motionArgs.buttonState);
5659 ASSERT_EQ(0, motionArgs.edgeFlags);
5660 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5661 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5663 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5666 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5668 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5669 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5670 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5671 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5672
5673 // Move.
5674 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5675 processPosition(mapper, x1, y1);
5676 processMTSync(mapper);
5677 processPosition(mapper, x2, y2);
5678 processMTSync(mapper);
5679 processSync(mapper);
5680
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5682 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5683 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5684 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5685 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5687 ASSERT_EQ(0, motionArgs.flags);
5688 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5689 ASSERT_EQ(0, motionArgs.buttonState);
5690 ASSERT_EQ(0, motionArgs.edgeFlags);
5691 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5694 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5697 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5699 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5700 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5701 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5702 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5703
5704 // First finger up.
5705 x2 += 15; y2 -= 20;
5706 processPosition(mapper, x2, y2);
5707 processMTSync(mapper);
5708 processSync(mapper);
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5712 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5713 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5714 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5716 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(2), motionArgs.pointerCount);
5722 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5724 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5727 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5729 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5730 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5731 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5732 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5733
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5736 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5737 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5738 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5740 ASSERT_EQ(0, motionArgs.flags);
5741 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5742 ASSERT_EQ(0, motionArgs.buttonState);
5743 ASSERT_EQ(0, motionArgs.edgeFlags);
5744 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5745 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5748 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5749 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5750 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5751 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5752
5753 // Move.
5754 x2 += 20; y2 -= 25;
5755 processPosition(mapper, x2, y2);
5756 processMTSync(mapper);
5757 processSync(mapper);
5758
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5761 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5762 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5763 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5765 ASSERT_EQ(0, motionArgs.flags);
5766 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5767 ASSERT_EQ(0, motionArgs.buttonState);
5768 ASSERT_EQ(0, motionArgs.edgeFlags);
5769 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5770 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5773 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5774 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5775 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5776 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5777
5778 // New finger down.
5779 int32_t x3 = 700, y3 = 300;
5780 processPosition(mapper, x2, y2);
5781 processMTSync(mapper);
5782 processPosition(mapper, x3, y3);
5783 processMTSync(mapper);
5784 processSync(mapper);
5785
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5788 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5789 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5790 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5791 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5792 motionArgs.action);
5793 ASSERT_EQ(0, motionArgs.flags);
5794 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5795 ASSERT_EQ(0, motionArgs.buttonState);
5796 ASSERT_EQ(0, motionArgs.edgeFlags);
5797 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5800 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5803 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5805 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5806 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5807 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5808 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5809
5810 // Second finger up.
5811 x3 += 30; y3 -= 20;
5812 processPosition(mapper, x3, y3);
5813 processMTSync(mapper);
5814 processSync(mapper);
5815
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5817 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5818 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5819 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5820 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5822 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(2), motionArgs.pointerCount);
5828 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5830 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5833 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5835 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5836 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5837 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
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_MOVE, 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 // Last finger up.
5860 processMTSync(mapper);
5861 processSync(mapper);
5862
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5864 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5865 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5866 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5867 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5868 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5869 ASSERT_EQ(0, motionArgs.flags);
5870 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5871 ASSERT_EQ(0, motionArgs.buttonState);
5872 ASSERT_EQ(0, motionArgs.edgeFlags);
5873 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5874 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5877 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5878 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5879 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5880 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5881
5882 // Should not have sent any more keys or motions.
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5885}
5886
5887TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 addConfigurationProperty("touch.deviceType", "touchScreen");
5889 prepareDisplay(DISPLAY_ORIENTATION_0);
5890 prepareAxes(POSITION | ID);
5891 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005892 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893
arthurhungdcef2dc2020-08-11 14:47:50 +08005894 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895
5896 NotifyMotionArgs motionArgs;
5897
5898 // Two fingers down at once.
5899 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5900 processPosition(mapper, x1, y1);
5901 processId(mapper, 1);
5902 processMTSync(mapper);
5903 processPosition(mapper, x2, y2);
5904 processId(mapper, 2);
5905 processMTSync(mapper);
5906 processSync(mapper);
5907
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5909 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5910 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5911 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5914 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5915
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5917 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5918 motionArgs.action);
5919 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5920 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5921 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5922 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5925 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5927 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5928
5929 // Move.
5930 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5931 processPosition(mapper, x1, y1);
5932 processId(mapper, 1);
5933 processMTSync(mapper);
5934 processPosition(mapper, x2, y2);
5935 processId(mapper, 2);
5936 processMTSync(mapper);
5937 processSync(mapper);
5938
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5941 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5942 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5944 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5947 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5949 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5950
5951 // First finger up.
5952 x2 += 15; y2 -= 20;
5953 processPosition(mapper, x2, y2);
5954 processId(mapper, 2);
5955 processMTSync(mapper);
5956 processSync(mapper);
5957
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5960 motionArgs.action);
5961 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5962 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5964 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5967 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5969 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5970
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5973 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5974 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5977 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5978
5979 // Move.
5980 x2 += 20; y2 -= 25;
5981 processPosition(mapper, x2, y2);
5982 processId(mapper, 2);
5983 processMTSync(mapper);
5984 processSync(mapper);
5985
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5988 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5989 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5992 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5993
5994 // New finger down.
5995 int32_t x3 = 700, y3 = 300;
5996 processPosition(mapper, x2, y2);
5997 processId(mapper, 2);
5998 processMTSync(mapper);
5999 processPosition(mapper, x3, y3);
6000 processId(mapper, 3);
6001 processMTSync(mapper);
6002 processSync(mapper);
6003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6005 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6006 motionArgs.action);
6007 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6008 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6010 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6013 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6015 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6016
6017 // Second finger up.
6018 x3 += 30; y3 -= 20;
6019 processPosition(mapper, x3, y3);
6020 processId(mapper, 3);
6021 processMTSync(mapper);
6022 processSync(mapper);
6023
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6025 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6026 motionArgs.action);
6027 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6028 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6030 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6033 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6035 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6036
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6039 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6040 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6043 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6044
6045 // Last finger up.
6046 processMTSync(mapper);
6047 processSync(mapper);
6048
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6050 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6051 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6052 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6055 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6056
6057 // Should not have sent any more keys or motions.
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6060}
6061
6062TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063 addConfigurationProperty("touch.deviceType", "touchScreen");
6064 prepareDisplay(DISPLAY_ORIENTATION_0);
6065 prepareAxes(POSITION | ID | SLOT);
6066 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006067 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068
arthurhungdcef2dc2020-08-11 14:47:50 +08006069 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070
6071 NotifyMotionArgs motionArgs;
6072
6073 // Two fingers down at once.
6074 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6075 processPosition(mapper, x1, y1);
6076 processId(mapper, 1);
6077 processSlot(mapper, 1);
6078 processPosition(mapper, x2, y2);
6079 processId(mapper, 2);
6080 processSync(mapper);
6081
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6084 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6085 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6088 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6089
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6091 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6092 motionArgs.action);
6093 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6094 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6096 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6099 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6101 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6102
6103 // Move.
6104 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6105 processSlot(mapper, 0);
6106 processPosition(mapper, x1, y1);
6107 processSlot(mapper, 1);
6108 processPosition(mapper, x2, y2);
6109 processSync(mapper);
6110
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6113 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6116 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6119 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6121 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6122
6123 // First finger up.
6124 x2 += 15; y2 -= 20;
6125 processSlot(mapper, 0);
6126 processId(mapper, -1);
6127 processSlot(mapper, 1);
6128 processPosition(mapper, x2, y2);
6129 processSync(mapper);
6130
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6132 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6133 motionArgs.action);
6134 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6135 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6137 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6140 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6142 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6143
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6146 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6147 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6150 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6151
6152 // Move.
6153 x2 += 20; y2 -= 25;
6154 processPosition(mapper, x2, y2);
6155 processSync(mapper);
6156
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6159 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6160 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6163 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6164
6165 // New finger down.
6166 int32_t x3 = 700, y3 = 300;
6167 processPosition(mapper, x2, y2);
6168 processSlot(mapper, 0);
6169 processId(mapper, 3);
6170 processPosition(mapper, x3, y3);
6171 processSync(mapper);
6172
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6175 motionArgs.action);
6176 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6177 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6179 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6182 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6184 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6185
6186 // Second finger up.
6187 x3 += 30; y3 -= 20;
6188 processSlot(mapper, 1);
6189 processId(mapper, -1);
6190 processSlot(mapper, 0);
6191 processPosition(mapper, x3, y3);
6192 processSync(mapper);
6193
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6195 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6196 motionArgs.action);
6197 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6198 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6200 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6203 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6205 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6209 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6210 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6213 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6214
6215 // Last finger up.
6216 processId(mapper, -1);
6217 processSync(mapper);
6218
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6221 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6222 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6225 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6226
6227 // Should not have sent any more keys or motions.
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6230}
6231
6232TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 addConfigurationProperty("touch.deviceType", "touchScreen");
6234 prepareDisplay(DISPLAY_ORIENTATION_0);
6235 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006236 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237
6238 // These calculations are based on the input device calibration documentation.
6239 int32_t rawX = 100;
6240 int32_t rawY = 200;
6241 int32_t rawTouchMajor = 7;
6242 int32_t rawTouchMinor = 6;
6243 int32_t rawToolMajor = 9;
6244 int32_t rawToolMinor = 8;
6245 int32_t rawPressure = 11;
6246 int32_t rawDistance = 0;
6247 int32_t rawOrientation = 3;
6248 int32_t id = 5;
6249
6250 float x = toDisplayX(rawX);
6251 float y = toDisplayY(rawY);
6252 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6253 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6254 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6255 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6256 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6257 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6258 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6259 float distance = float(rawDistance);
6260
6261 processPosition(mapper, rawX, rawY);
6262 processTouchMajor(mapper, rawTouchMajor);
6263 processTouchMinor(mapper, rawTouchMinor);
6264 processToolMajor(mapper, rawToolMajor);
6265 processToolMinor(mapper, rawToolMinor);
6266 processPressure(mapper, rawPressure);
6267 processOrientation(mapper, rawOrientation);
6268 processDistance(mapper, rawDistance);
6269 processId(mapper, id);
6270 processMTSync(mapper);
6271 processSync(mapper);
6272
6273 NotifyMotionArgs args;
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6275 ASSERT_EQ(0, args.pointerProperties[0].id);
6276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6277 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6278 orientation, distance));
6279}
6280
6281TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 addConfigurationProperty("touch.deviceType", "touchScreen");
6283 prepareDisplay(DISPLAY_ORIENTATION_0);
6284 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6285 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006286 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287
6288 // These calculations are based on the input device calibration documentation.
6289 int32_t rawX = 100;
6290 int32_t rawY = 200;
6291 int32_t rawTouchMajor = 140;
6292 int32_t rawTouchMinor = 120;
6293 int32_t rawToolMajor = 180;
6294 int32_t rawToolMinor = 160;
6295
6296 float x = toDisplayX(rawX);
6297 float y = toDisplayY(rawY);
6298 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6299 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6300 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6301 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6302 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6303
6304 processPosition(mapper, rawX, rawY);
6305 processTouchMajor(mapper, rawTouchMajor);
6306 processTouchMinor(mapper, rawTouchMinor);
6307 processToolMajor(mapper, rawToolMajor);
6308 processToolMinor(mapper, rawToolMinor);
6309 processMTSync(mapper);
6310 processSync(mapper);
6311
6312 NotifyMotionArgs args;
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6315 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6316}
6317
6318TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 addConfigurationProperty("touch.deviceType", "touchScreen");
6320 prepareDisplay(DISPLAY_ORIENTATION_0);
6321 prepareAxes(POSITION | TOUCH | TOOL);
6322 addConfigurationProperty("touch.size.calibration", "diameter");
6323 addConfigurationProperty("touch.size.scale", "10");
6324 addConfigurationProperty("touch.size.bias", "160");
6325 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006326 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006327
6328 // These calculations are based on the input device calibration documentation.
6329 // Note: We only provide a single common touch/tool value because the device is assumed
6330 // not to emit separate values for each pointer (isSummed = 1).
6331 int32_t rawX = 100;
6332 int32_t rawY = 200;
6333 int32_t rawX2 = 150;
6334 int32_t rawY2 = 250;
6335 int32_t rawTouchMajor = 5;
6336 int32_t rawToolMajor = 8;
6337
6338 float x = toDisplayX(rawX);
6339 float y = toDisplayY(rawY);
6340 float x2 = toDisplayX(rawX2);
6341 float y2 = toDisplayY(rawY2);
6342 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6343 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6344 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6345
6346 processPosition(mapper, rawX, rawY);
6347 processTouchMajor(mapper, rawTouchMajor);
6348 processToolMajor(mapper, rawToolMajor);
6349 processMTSync(mapper);
6350 processPosition(mapper, rawX2, rawY2);
6351 processTouchMajor(mapper, rawTouchMajor);
6352 processToolMajor(mapper, rawToolMajor);
6353 processMTSync(mapper);
6354 processSync(mapper);
6355
6356 NotifyMotionArgs args;
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6359
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6362 args.action);
6363 ASSERT_EQ(size_t(2), args.pointerCount);
6364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6365 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6367 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6368}
6369
6370TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 addConfigurationProperty("touch.deviceType", "touchScreen");
6372 prepareDisplay(DISPLAY_ORIENTATION_0);
6373 prepareAxes(POSITION | TOUCH | TOOL);
6374 addConfigurationProperty("touch.size.calibration", "area");
6375 addConfigurationProperty("touch.size.scale", "43");
6376 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006377 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378
6379 // These calculations are based on the input device calibration documentation.
6380 int32_t rawX = 100;
6381 int32_t rawY = 200;
6382 int32_t rawTouchMajor = 5;
6383 int32_t rawToolMajor = 8;
6384
6385 float x = toDisplayX(rawX);
6386 float y = toDisplayY(rawY);
6387 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6388 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6389 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6390
6391 processPosition(mapper, rawX, rawY);
6392 processTouchMajor(mapper, rawTouchMajor);
6393 processToolMajor(mapper, rawToolMajor);
6394 processMTSync(mapper);
6395 processSync(mapper);
6396
6397 NotifyMotionArgs args;
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6400 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6401}
6402
6403TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 addConfigurationProperty("touch.deviceType", "touchScreen");
6405 prepareDisplay(DISPLAY_ORIENTATION_0);
6406 prepareAxes(POSITION | PRESSURE);
6407 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6408 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006409 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410
Michael Wrightaa449c92017-12-13 21:21:43 +00006411 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006412 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006413 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6414 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6415 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6416
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 // These calculations are based on the input device calibration documentation.
6418 int32_t rawX = 100;
6419 int32_t rawY = 200;
6420 int32_t rawPressure = 60;
6421
6422 float x = toDisplayX(rawX);
6423 float y = toDisplayY(rawY);
6424 float pressure = float(rawPressure) * 0.01f;
6425
6426 processPosition(mapper, rawX, rawY);
6427 processPressure(mapper, rawPressure);
6428 processMTSync(mapper);
6429 processSync(mapper);
6430
6431 NotifyMotionArgs args;
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6434 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6435}
6436
6437TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438 addConfigurationProperty("touch.deviceType", "touchScreen");
6439 prepareDisplay(DISPLAY_ORIENTATION_0);
6440 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006441 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442
6443 NotifyMotionArgs motionArgs;
6444 NotifyKeyArgs keyArgs;
6445
6446 processId(mapper, 1);
6447 processPosition(mapper, 100, 200);
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6451 ASSERT_EQ(0, motionArgs.buttonState);
6452
6453 // press BTN_LEFT, release BTN_LEFT
6454 processKey(mapper, BTN_LEFT, 1);
6455 processSync(mapper);
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6458 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, 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_PRIMARY, motionArgs.buttonState);
6463
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464 processKey(mapper, BTN_LEFT, 0);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006467 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006468 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006469
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006472 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473
6474 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6475 processKey(mapper, BTN_RIGHT, 1);
6476 processKey(mapper, BTN_MIDDLE, 1);
6477 processSync(mapper);
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6480 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6481 motionArgs.buttonState);
6482
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6485 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6486
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6489 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6490 motionArgs.buttonState);
6491
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492 processKey(mapper, BTN_RIGHT, 0);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006497
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006500 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501
6502 processKey(mapper, BTN_MIDDLE, 0);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006507
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006510 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511
6512 // press BTN_BACK, release BTN_BACK
6513 processKey(mapper, BTN_BACK, 1);
6514 processSync(mapper);
6515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6516 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6517 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006518
Michael Wrightd02c5b62014-02-10 15:10:22 -08006519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006521 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6522
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6525 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006526
6527 processKey(mapper, BTN_BACK, 0);
6528 processSync(mapper);
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006531 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006532
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006535 ASSERT_EQ(0, motionArgs.buttonState);
6536
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6538 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6539 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6540
6541 // press BTN_SIDE, release BTN_SIDE
6542 processKey(mapper, BTN_SIDE, 1);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6545 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6546 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006547
Michael Wrightd02c5b62014-02-10 15:10:22 -08006548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006550 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6551
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6554 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006555
6556 processKey(mapper, BTN_SIDE, 0);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006560 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006561
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006564 ASSERT_EQ(0, motionArgs.buttonState);
6565
Michael Wrightd02c5b62014-02-10 15:10:22 -08006566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6567 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6568 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6569
6570 // press BTN_FORWARD, release BTN_FORWARD
6571 processKey(mapper, BTN_FORWARD, 1);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6574 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6575 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006576
Michael Wrightd02c5b62014-02-10 15:10:22 -08006577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006579 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6580
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6583 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006584
6585 processKey(mapper, BTN_FORWARD, 0);
6586 processSync(mapper);
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006589 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006590
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006593 ASSERT_EQ(0, motionArgs.buttonState);
6594
Michael Wrightd02c5b62014-02-10 15:10:22 -08006595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6596 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6597 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6598
6599 // press BTN_EXTRA, release BTN_EXTRA
6600 processKey(mapper, BTN_EXTRA, 1);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6603 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6604 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006605
Michael Wrightd02c5b62014-02-10 15:10:22 -08006606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006608 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6609
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6612 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006613
6614 processKey(mapper, BTN_EXTRA, 0);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006618 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006619
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006622 ASSERT_EQ(0, motionArgs.buttonState);
6623
Michael Wrightd02c5b62014-02-10 15:10:22 -08006624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6625 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6626 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6627
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6629
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630 // press BTN_STYLUS, release BTN_STYLUS
6631 processKey(mapper, BTN_STYLUS, 1);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006635 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6636
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6639 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640
6641 processKey(mapper, BTN_STYLUS, 0);
6642 processSync(mapper);
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006645 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006646
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006649 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006650
6651 // press BTN_STYLUS2, release BTN_STYLUS2
6652 processKey(mapper, BTN_STYLUS2, 1);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006656 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6657
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6660 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006661
6662 processKey(mapper, BTN_STYLUS2, 0);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006666 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006667
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006670 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006671
6672 // release touch
6673 processId(mapper, -1);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6677 ASSERT_EQ(0, motionArgs.buttonState);
6678}
6679
6680TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006681 addConfigurationProperty("touch.deviceType", "touchScreen");
6682 prepareDisplay(DISPLAY_ORIENTATION_0);
6683 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006684 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006685
6686 NotifyMotionArgs motionArgs;
6687
6688 // default tool type is finger
6689 processId(mapper, 1);
6690 processPosition(mapper, 100, 200);
6691 processSync(mapper);
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6695
6696 // eraser
6697 processKey(mapper, BTN_TOOL_RUBBER, 1);
6698 processSync(mapper);
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6702
6703 // stylus
6704 processKey(mapper, BTN_TOOL_RUBBER, 0);
6705 processKey(mapper, BTN_TOOL_PEN, 1);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6710
6711 // brush
6712 processKey(mapper, BTN_TOOL_PEN, 0);
6713 processKey(mapper, BTN_TOOL_BRUSH, 1);
6714 processSync(mapper);
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6718
6719 // pencil
6720 processKey(mapper, BTN_TOOL_BRUSH, 0);
6721 processKey(mapper, BTN_TOOL_PENCIL, 1);
6722 processSync(mapper);
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6726
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006727 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006728 processKey(mapper, BTN_TOOL_PENCIL, 0);
6729 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6730 processSync(mapper);
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6734
6735 // mouse
6736 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6737 processKey(mapper, BTN_TOOL_MOUSE, 1);
6738 processSync(mapper);
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6742
6743 // lens
6744 processKey(mapper, BTN_TOOL_MOUSE, 0);
6745 processKey(mapper, BTN_TOOL_LENS, 1);
6746 processSync(mapper);
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6750
6751 // double-tap
6752 processKey(mapper, BTN_TOOL_LENS, 0);
6753 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6754 processSync(mapper);
6755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6758
6759 // triple-tap
6760 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6761 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6766
6767 // quad-tap
6768 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6769 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6774
6775 // finger
6776 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6777 processKey(mapper, BTN_TOOL_FINGER, 1);
6778 processSync(mapper);
6779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6782
6783 // stylus trumps finger
6784 processKey(mapper, BTN_TOOL_PEN, 1);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6789
6790 // eraser trumps stylus
6791 processKey(mapper, BTN_TOOL_RUBBER, 1);
6792 processSync(mapper);
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6796
6797 // mouse trumps eraser
6798 processKey(mapper, BTN_TOOL_MOUSE, 1);
6799 processSync(mapper);
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6803
6804 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6805 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6806 processSync(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6810
6811 // MT tool type trumps BTN tool types: MT_TOOL_PEN
6812 processToolType(mapper, MT_TOOL_PEN);
6813 processSync(mapper);
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6817
6818 // back to default tool type
6819 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6820 processKey(mapper, BTN_TOOL_MOUSE, 0);
6821 processKey(mapper, BTN_TOOL_RUBBER, 0);
6822 processKey(mapper, BTN_TOOL_PEN, 0);
6823 processKey(mapper, BTN_TOOL_FINGER, 0);
6824 processSync(mapper);
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6828}
6829
6830TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006831 addConfigurationProperty("touch.deviceType", "touchScreen");
6832 prepareDisplay(DISPLAY_ORIENTATION_0);
6833 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006834 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006835 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006836
6837 NotifyMotionArgs motionArgs;
6838
6839 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6840 processId(mapper, 1);
6841 processPosition(mapper, 100, 200);
6842 processSync(mapper);
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6844 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6846 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6847
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6849 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6851 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6852
6853 // move a little
6854 processPosition(mapper, 150, 250);
6855 processSync(mapper);
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6857 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6859 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6860
6861 // down when BTN_TOUCH is pressed, pressure defaults to 1
6862 processKey(mapper, BTN_TOUCH, 1);
6863 processSync(mapper);
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6865 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6872 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6873
6874 // up when BTN_TOUCH is released, hover restored
6875 processKey(mapper, BTN_TOUCH, 0);
6876 processSync(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6878 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6880 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6881
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6885 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6886
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6888 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6890 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6891
6892 // exit hover when pointer goes away
6893 processId(mapper, -1);
6894 processSync(mapper);
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6896 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6898 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6899}
6900
6901TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006902 addConfigurationProperty("touch.deviceType", "touchScreen");
6903 prepareDisplay(DISPLAY_ORIENTATION_0);
6904 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006905 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006906
6907 NotifyMotionArgs motionArgs;
6908
6909 // initially hovering because pressure is 0
6910 processId(mapper, 1);
6911 processPosition(mapper, 100, 200);
6912 processPressure(mapper, 0);
6913 processSync(mapper);
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6915 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6917 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6918
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6920 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6922 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6923
6924 // move a little
6925 processPosition(mapper, 150, 250);
6926 processSync(mapper);
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6928 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6930 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6931
6932 // down when pressure becomes non-zero
6933 processPressure(mapper, RAW_PRESSURE_MAX);
6934 processSync(mapper);
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6941 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6943 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6944
6945 // up when pressure becomes 0, hover restored
6946 processPressure(mapper, 0);
6947 processSync(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6949 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6951 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6952
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6954 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6956 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6957
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6961 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6962
6963 // exit hover when pointer goes away
6964 processId(mapper, -1);
6965 processSync(mapper);
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6967 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6969 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6970}
6971
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006972/**
6973 * Set the input device port <--> display port associations, and check that the
6974 * events are routed to the display that matches the display port.
6975 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6976 */
6977TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006978 const std::string usb2 = "USB2";
6979 const uint8_t hdmi1 = 0;
6980 const uint8_t hdmi2 = 1;
6981 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006982 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006983
6984 addConfigurationProperty("touch.deviceType", "touchScreen");
6985 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006986 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006987
6988 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6989 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6990
6991 // We are intentionally not adding the viewport for display 1 yet. Since the port association
6992 // for this input device is specified, and the matching viewport is not present,
6993 // the input device should be disabled (at the mapper level).
6994
6995 // Add viewport for display 2 on hdmi2
6996 prepareSecondaryDisplay(type, hdmi2);
6997 // Send a touch event
6998 processPosition(mapper, 100, 100);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7001
7002 // Add viewport for display 1 on hdmi1
7003 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7004 // Send a touch event again
7005 processPosition(mapper, 100, 100);
7006 processSync(mapper);
7007
7008 NotifyMotionArgs args;
7009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7010 ASSERT_EQ(DISPLAY_ID, args.displayId);
7011}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007012
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007013TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007014 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007015 std::shared_ptr<FakePointerController> fakePointerController =
7016 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007017 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007018 fakePointerController->setPosition(100, 200);
7019 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007020 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7021
Garfield Tan888a6a42020-01-09 11:39:16 -08007022 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007023 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007024
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007025 prepareDisplay(DISPLAY_ORIENTATION_0);
7026 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007027 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007028
7029 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007030 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007031
7032 NotifyMotionArgs motionArgs;
7033 processPosition(mapper, 100, 100);
7034 processSync(mapper);
7035
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7037 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7038 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7039}
7040
Arthur Hung7c645402019-01-25 17:45:42 +08007041TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7042 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007043 prepareAxes(POSITION | ID | SLOT);
7044 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007045 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007046
7047 // Create the second touch screen device, and enable multi fingers.
7048 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007049 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007050 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007051 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007052 std::shared_ptr<InputDevice> device2 =
7053 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7054 Flags<InputDeviceClass>(0));
7055
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007056 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7057 0 /*flat*/, 0 /*fuzz*/);
7058 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7059 0 /*flat*/, 0 /*fuzz*/);
7060 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7061 0 /*flat*/, 0 /*fuzz*/);
7062 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7063 0 /*flat*/, 0 /*fuzz*/);
7064 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7065 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7066 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007067
7068 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007069 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007070 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7071 device2->reset(ARBITRARY_TIME);
7072
7073 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007074 std::shared_ptr<FakePointerController> fakePointerController =
7075 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007076 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7077 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7078
7079 // Setup policy for associated displays and show touches.
7080 const uint8_t hdmi1 = 0;
7081 const uint8_t hdmi2 = 1;
7082 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7083 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7084 mFakePolicy->setShowTouches(true);
7085
7086 // Create displays.
7087 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007088 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007089
7090 // Default device will reconfigure above, need additional reconfiguration for another device.
7091 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007092 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007093
7094 // Two fingers down at default display.
7095 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7096 processPosition(mapper, x1, y1);
7097 processId(mapper, 1);
7098 processSlot(mapper, 1);
7099 processPosition(mapper, x2, y2);
7100 processId(mapper, 2);
7101 processSync(mapper);
7102
7103 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7104 fakePointerController->getSpots().find(DISPLAY_ID);
7105 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7106 ASSERT_EQ(size_t(2), iter->second.size());
7107
7108 // Two fingers down at second display.
7109 processPosition(mapper2, x1, y1);
7110 processId(mapper2, 1);
7111 processSlot(mapper2, 1);
7112 processPosition(mapper2, x2, y2);
7113 processId(mapper2, 2);
7114 processSync(mapper2);
7115
7116 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7117 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7118 ASSERT_EQ(size_t(2), iter->second.size());
7119}
7120
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007121TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007122 prepareAxes(POSITION);
7123 addConfigurationProperty("touch.deviceType", "touchScreen");
7124 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007125 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007126
7127 NotifyMotionArgs motionArgs;
7128 // Unrotated video frame
7129 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7130 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007131 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007132 processPosition(mapper, 100, 200);
7133 processSync(mapper);
7134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7135 ASSERT_EQ(frames, motionArgs.videoFrames);
7136
7137 // Subsequent touch events should not have any videoframes
7138 // This is implemented separately in FakeEventHub,
7139 // but that should match the behaviour of TouchVideoDevice.
7140 processPosition(mapper, 200, 200);
7141 processSync(mapper);
7142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7143 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7144}
7145
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007146TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007147 prepareAxes(POSITION);
7148 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007149 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007150 // Unrotated video frame
7151 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7152 NotifyMotionArgs motionArgs;
7153
7154 // Test all 4 orientations
7155 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7156 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7157 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7158 clearViewports();
7159 prepareDisplay(orientation);
7160 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007161 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007162 processPosition(mapper, 100, 200);
7163 processSync(mapper);
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7165 frames[0].rotate(orientation);
7166 ASSERT_EQ(frames, motionArgs.videoFrames);
7167 }
7168}
7169
7170TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007171 prepareAxes(POSITION);
7172 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007173 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007174 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7175 // so mix these.
7176 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7177 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7178 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7179 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7180 NotifyMotionArgs motionArgs;
7181
7182 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007183 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007184 processPosition(mapper, 100, 200);
7185 processSync(mapper);
7186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7187 std::for_each(frames.begin(), frames.end(),
7188 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7189 ASSERT_EQ(frames, motionArgs.videoFrames);
7190}
7191
Arthur Hung9da14732019-09-02 16:16:58 +08007192/**
7193 * If we had defined port associations, but the viewport is not ready, the touch device would be
7194 * expected to be disabled, and it should be enabled after the viewport has found.
7195 */
7196TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007197 constexpr uint8_t hdmi2 = 1;
7198 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007199 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007200
7201 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7202
7203 addConfigurationProperty("touch.deviceType", "touchScreen");
7204 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007206
7207 ASSERT_EQ(mDevice->isEnabled(), false);
7208
7209 // Add display on hdmi2, the device should be enabled and can receive touch event.
7210 prepareSecondaryDisplay(type, hdmi2);
7211 ASSERT_EQ(mDevice->isEnabled(), true);
7212
7213 // Send a touch event.
7214 processPosition(mapper, 100, 100);
7215 processSync(mapper);
7216
7217 NotifyMotionArgs args;
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7219 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7220}
7221
Arthur Hung6cd19a42019-08-30 19:04:12 +08007222
Arthur Hung6cd19a42019-08-30 19:04:12 +08007223
Arthur Hung421eb1c2020-01-16 00:09:42 +08007224TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007225 addConfigurationProperty("touch.deviceType", "touchScreen");
7226 prepareDisplay(DISPLAY_ORIENTATION_0);
7227 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007228 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007229
7230 NotifyMotionArgs motionArgs;
7231
7232 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7233 // finger down
7234 processId(mapper, 1);
7235 processPosition(mapper, x1, y1);
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 // finger move
7242 processId(mapper, 1);
7243 processPosition(mapper, x2, y2);
7244 processSync(mapper);
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7248
7249 // finger up.
7250 processId(mapper, -1);
7251 processSync(mapper);
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7255
7256 // new finger down
7257 processId(mapper, 1);
7258 processPosition(mapper, x3, y3);
7259 processSync(mapper);
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7263}
7264
7265/**
arthurhungcc7f9802020-04-30 17:55:40 +08007266 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7267 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007268 */
arthurhungcc7f9802020-04-30 17:55:40 +08007269TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007270 addConfigurationProperty("touch.deviceType", "touchScreen");
7271 prepareDisplay(DISPLAY_ORIENTATION_0);
7272 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007273 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007274
7275 NotifyMotionArgs motionArgs;
7276
7277 // default tool type is finger
7278 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007279 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007280 processPosition(mapper, x1, y1);
7281 processSync(mapper);
7282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7283 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7285
7286 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7287 processToolType(mapper, MT_TOOL_PALM);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7291
7292 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007293 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007294 processPosition(mapper, x2, y2);
7295 processSync(mapper);
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7297
7298 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007299 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007300 processSync(mapper);
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7302
7303 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007304 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007305 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007306 processPosition(mapper, x3, y3);
7307 processSync(mapper);
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7309 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7311}
7312
arthurhungbf89a482020-04-17 17:37:55 +08007313/**
arthurhungcc7f9802020-04-30 17:55:40 +08007314 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7315 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007316 */
arthurhungcc7f9802020-04-30 17:55:40 +08007317TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007318 addConfigurationProperty("touch.deviceType", "touchScreen");
7319 prepareDisplay(DISPLAY_ORIENTATION_0);
7320 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7321 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7322
7323 NotifyMotionArgs motionArgs;
7324
7325 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007326 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7327 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007328 processPosition(mapper, x1, y1);
7329 processSync(mapper);
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7331 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7333
7334 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007335 processSlot(mapper, SECOND_SLOT);
7336 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007337 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007338 processSync(mapper);
7339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7340 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7341 motionArgs.action);
7342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7343
7344 // If the tool type of the first finger changes to MT_TOOL_PALM,
7345 // we expect to receive ACTION_POINTER_UP with cancel flag.
7346 processSlot(mapper, FIRST_SLOT);
7347 processId(mapper, FIRST_TRACKING_ID);
7348 processToolType(mapper, MT_TOOL_PALM);
7349 processSync(mapper);
7350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7351 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7352 motionArgs.action);
7353 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7354
7355 // The following MOVE events of second finger should be processed.
7356 processSlot(mapper, SECOND_SLOT);
7357 processId(mapper, SECOND_TRACKING_ID);
7358 processPosition(mapper, x2 + 1, y2 + 1);
7359 processSync(mapper);
7360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7362 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7363
7364 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7365 // it. Second finger receive move.
7366 processSlot(mapper, FIRST_SLOT);
7367 processId(mapper, INVALID_TRACKING_ID);
7368 processSync(mapper);
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7371 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7372
7373 // Second finger keeps moving.
7374 processSlot(mapper, SECOND_SLOT);
7375 processId(mapper, SECOND_TRACKING_ID);
7376 processPosition(mapper, x2 + 2, y2 + 2);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7380 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7381
7382 // Second finger up.
7383 processId(mapper, INVALID_TRACKING_ID);
7384 processSync(mapper);
7385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7386 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7387 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7388}
7389
7390/**
7391 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7392 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7393 */
7394TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7395 addConfigurationProperty("touch.deviceType", "touchScreen");
7396 prepareDisplay(DISPLAY_ORIENTATION_0);
7397 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7398 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7399
7400 NotifyMotionArgs motionArgs;
7401
7402 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7403 // First finger down.
7404 processId(mapper, FIRST_TRACKING_ID);
7405 processPosition(mapper, x1, y1);
7406 processSync(mapper);
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7408 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7410
7411 // Second finger down.
7412 processSlot(mapper, SECOND_SLOT);
7413 processId(mapper, SECOND_TRACKING_ID);
7414 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007415 processSync(mapper);
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7417 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7418 motionArgs.action);
7419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7420
arthurhungcc7f9802020-04-30 17:55:40 +08007421 // If the tool type of the first finger changes to MT_TOOL_PALM,
7422 // we expect to receive ACTION_POINTER_UP with cancel flag.
7423 processSlot(mapper, FIRST_SLOT);
7424 processId(mapper, FIRST_TRACKING_ID);
7425 processToolType(mapper, MT_TOOL_PALM);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7428 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7429 motionArgs.action);
7430 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7431
7432 // Second finger keeps moving.
7433 processSlot(mapper, SECOND_SLOT);
7434 processId(mapper, SECOND_TRACKING_ID);
7435 processPosition(mapper, x2 + 1, y2 + 1);
7436 processSync(mapper);
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7439
7440 // second finger becomes palm, receive cancel due to only 1 finger is active.
7441 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007442 processToolType(mapper, MT_TOOL_PALM);
7443 processSync(mapper);
7444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7445 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7446
arthurhungcc7f9802020-04-30 17:55:40 +08007447 // third finger down.
7448 processSlot(mapper, THIRD_SLOT);
7449 processId(mapper, THIRD_TRACKING_ID);
7450 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007451 processPosition(mapper, x3, y3);
7452 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007456 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7457
7458 // third finger move
7459 processId(mapper, THIRD_TRACKING_ID);
7460 processPosition(mapper, x3 + 1, y3 + 1);
7461 processSync(mapper);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7464
7465 // first finger up, third finger receive move.
7466 processSlot(mapper, FIRST_SLOT);
7467 processId(mapper, INVALID_TRACKING_ID);
7468 processSync(mapper);
7469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7471 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7472
7473 // second finger up, third finger receive move.
7474 processSlot(mapper, SECOND_SLOT);
7475 processId(mapper, INVALID_TRACKING_ID);
7476 processSync(mapper);
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7479 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7480
7481 // third finger up.
7482 processSlot(mapper, THIRD_SLOT);
7483 processId(mapper, INVALID_TRACKING_ID);
7484 processSync(mapper);
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7487 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7488}
7489
7490/**
7491 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7492 * and the active finger could still be allowed to receive the events
7493 */
7494TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7495 addConfigurationProperty("touch.deviceType", "touchScreen");
7496 prepareDisplay(DISPLAY_ORIENTATION_0);
7497 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7498 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7499
7500 NotifyMotionArgs motionArgs;
7501
7502 // default tool type is finger
7503 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7504 processId(mapper, FIRST_TRACKING_ID);
7505 processPosition(mapper, x1, y1);
7506 processSync(mapper);
7507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7508 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7510
7511 // Second finger down.
7512 processSlot(mapper, SECOND_SLOT);
7513 processId(mapper, SECOND_TRACKING_ID);
7514 processPosition(mapper, x2, y2);
7515 processSync(mapper);
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7517 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7518 motionArgs.action);
7519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7520
7521 // If the tool type of the second finger changes to MT_TOOL_PALM,
7522 // we expect to receive ACTION_POINTER_UP with cancel flag.
7523 processId(mapper, SECOND_TRACKING_ID);
7524 processToolType(mapper, MT_TOOL_PALM);
7525 processSync(mapper);
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7527 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7528 motionArgs.action);
7529 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7530
7531 // The following MOVE event should be processed.
7532 processSlot(mapper, FIRST_SLOT);
7533 processId(mapper, FIRST_TRACKING_ID);
7534 processPosition(mapper, x1 + 1, y1 + 1);
7535 processSync(mapper);
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7538 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7539
7540 // second finger up.
7541 processSlot(mapper, SECOND_SLOT);
7542 processId(mapper, INVALID_TRACKING_ID);
7543 processSync(mapper);
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7546
7547 // first finger keep moving
7548 processSlot(mapper, FIRST_SLOT);
7549 processId(mapper, FIRST_TRACKING_ID);
7550 processPosition(mapper, x1 + 2, y1 + 2);
7551 processSync(mapper);
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7554
7555 // first finger up.
7556 processId(mapper, INVALID_TRACKING_ID);
7557 processSync(mapper);
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7560 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007561}
7562
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007563// --- MultiTouchInputMapperTest_ExternalDevice ---
7564
7565class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7566protected:
Chris Yea52ade12020-08-27 16:49:20 -07007567 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007568};
7569
7570/**
7571 * Expect fallback to internal viewport if device is external and external viewport is not present.
7572 */
7573TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7574 prepareAxes(POSITION);
7575 addConfigurationProperty("touch.deviceType", "touchScreen");
7576 prepareDisplay(DISPLAY_ORIENTATION_0);
7577 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7578
7579 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7580
7581 NotifyMotionArgs motionArgs;
7582
7583 // Expect the event to be sent to the internal viewport,
7584 // because an external viewport is not present.
7585 processPosition(mapper, 100, 100);
7586 processSync(mapper);
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7588 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7589
7590 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007591 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007592 processPosition(mapper, 100, 100);
7593 processSync(mapper);
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7595 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7596}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007597
7598/**
7599 * Test touch should not work if outside of surface.
7600 */
7601class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7602protected:
7603 void halfDisplayToCenterHorizontal(int32_t orientation) {
7604 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007605 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007606
7607 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7608 internalViewport->orientation = orientation;
7609 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7610 internalViewport->logicalLeft = 0;
7611 internalViewport->logicalTop = 0;
7612 internalViewport->logicalRight = DISPLAY_HEIGHT;
7613 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7614
7615 internalViewport->physicalLeft = 0;
7616 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7617 internalViewport->physicalRight = DISPLAY_HEIGHT;
7618 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7619
7620 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7621 internalViewport->deviceHeight = DISPLAY_WIDTH;
7622 } else {
7623 internalViewport->logicalLeft = 0;
7624 internalViewport->logicalTop = 0;
7625 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7626 internalViewport->logicalBottom = DISPLAY_HEIGHT;
7627
7628 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7629 internalViewport->physicalTop = 0;
7630 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7631 internalViewport->physicalBottom = DISPLAY_HEIGHT;
7632
7633 internalViewport->deviceWidth = DISPLAY_WIDTH;
7634 internalViewport->deviceHeight = DISPLAY_HEIGHT;
7635 }
7636
7637 mFakePolicy->updateViewport(internalViewport.value());
7638 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7639 }
7640
7641 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7642 int32_t xOutside, int32_t yOutside, int32_t xExpected,
7643 int32_t yExpected) {
7644 // touch on outside area should not work.
7645 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7646 processSync(mapper);
7647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7648
7649 // touch on inside area should receive the event.
7650 NotifyMotionArgs args;
7651 processPosition(mapper, toRawX(xInside), toRawY(yInside));
7652 processSync(mapper);
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7654 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7655 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7656
7657 // Reset.
7658 mapper.reset(ARBITRARY_TIME);
7659 }
7660};
7661
7662TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7663 addConfigurationProperty("touch.deviceType", "touchScreen");
7664 prepareDisplay(DISPLAY_ORIENTATION_0);
7665 prepareAxes(POSITION);
7666 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7667
7668 // Touch on center of normal display should work.
7669 const int32_t x = DISPLAY_WIDTH / 4;
7670 const int32_t y = DISPLAY_HEIGHT / 2;
7671 processPosition(mapper, toRawX(x), toRawY(y));
7672 processSync(mapper);
7673 NotifyMotionArgs args;
7674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7676 0.0f, 0.0f, 0.0f, 0.0f));
7677 // Reset.
7678 mapper.reset(ARBITRARY_TIME);
7679
7680 // Let physical display be different to device, and make surface and physical could be 1:1.
7681 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7682
7683 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7684 const int32_t yExpected = y;
7685 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7686}
7687
7688TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7689 addConfigurationProperty("touch.deviceType", "touchScreen");
7690 prepareDisplay(DISPLAY_ORIENTATION_0);
7691 prepareAxes(POSITION);
7692 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7693
7694 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7695 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7696
7697 const int32_t x = DISPLAY_WIDTH / 4;
7698 const int32_t y = DISPLAY_HEIGHT / 2;
7699
7700 // expect x/y = swap x/y then reverse y.
7701 const int32_t xExpected = y;
7702 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7703 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7704}
7705
7706TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7707 addConfigurationProperty("touch.deviceType", "touchScreen");
7708 prepareDisplay(DISPLAY_ORIENTATION_0);
7709 prepareAxes(POSITION);
7710 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7711
7712 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7713 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7714
7715 const int32_t x = DISPLAY_WIDTH / 4;
7716 const int32_t y = DISPLAY_HEIGHT / 2;
7717
7718 // expect x/y = swap x/y then reverse x.
7719 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7720 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7721 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7722}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007723
7724TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
7725 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
7726 std::shared_ptr<FakePointerController> fakePointerController =
7727 std::make_shared<FakePointerController>();
7728 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7729 fakePointerController->setPosition(0, 0);
7730 fakePointerController->setButtonState(0);
7731
7732 // prepare device and capture
7733 prepareDisplay(DISPLAY_ORIENTATION_0);
7734 prepareAxes(POSITION | ID | SLOT);
7735 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7736 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7737 mFakePolicy->setPointerCapture(true);
7738 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7739 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7740
7741 // captured touchpad should be a touchpad source
7742 NotifyDeviceResetArgs resetArgs;
7743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7744 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7745
Chris Yef74dc422020-09-02 22:41:50 -07007746 InputDeviceInfo deviceInfo;
7747 mDevice->getDeviceInfo(&deviceInfo);
7748
7749 const InputDeviceInfo::MotionRange* relRangeX =
7750 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
7751 ASSERT_NE(relRangeX, nullptr);
7752 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
7753 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
7754 const InputDeviceInfo::MotionRange* relRangeY =
7755 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
7756 ASSERT_NE(relRangeY, nullptr);
7757 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
7758 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
7759
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007760 // run captured pointer tests - note that this is unscaled, so input listener events should be
7761 // identical to what the hardware sends (accounting for any
7762 // calibration).
7763 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07007764 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007765 processId(mapper, 1);
7766 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
7767 processKey(mapper, BTN_TOUCH, 1);
7768 processSync(mapper);
7769
7770 // expect coord[0] to contain initial location of touch 0
7771 NotifyMotionArgs args;
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7773 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7774 ASSERT_EQ(1U, args.pointerCount);
7775 ASSERT_EQ(0, args.pointerProperties[0].id);
7776 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
7777 ASSERT_NO_FATAL_FAILURE(
7778 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7779
7780 // FINGER 1 DOWN
7781 processSlot(mapper, 1);
7782 processId(mapper, 2);
7783 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
7784 processSync(mapper);
7785
7786 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07007788 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7789 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08007790 ASSERT_EQ(2U, args.pointerCount);
7791 ASSERT_EQ(0, args.pointerProperties[0].id);
7792 ASSERT_EQ(1, args.pointerProperties[1].id);
7793 ASSERT_NO_FATAL_FAILURE(
7794 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7795 ASSERT_NO_FATAL_FAILURE(
7796 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
7797
7798 // FINGER 1 MOVE
7799 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
7800 processSync(mapper);
7801
7802 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
7803 // from move
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7806 ASSERT_NO_FATAL_FAILURE(
7807 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
7808 ASSERT_NO_FATAL_FAILURE(
7809 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7810
7811 // FINGER 0 MOVE
7812 processSlot(mapper, 0);
7813 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
7814 processSync(mapper);
7815
7816 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7819 ASSERT_NO_FATAL_FAILURE(
7820 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
7821 ASSERT_NO_FATAL_FAILURE(
7822 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
7823
7824 // BUTTON DOWN
7825 processKey(mapper, BTN_LEFT, 1);
7826 processSync(mapper);
7827
7828 // touchinputmapper design sends a move before button press
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7832 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
7833
7834 // BUTTON UP
7835 processKey(mapper, BTN_LEFT, 0);
7836 processSync(mapper);
7837
7838 // touchinputmapper design sends a move after button release
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7840 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
7841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7843
7844 // FINGER 0 UP
7845 processId(mapper, -1);
7846 processSync(mapper);
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
7849
7850 // FINGER 1 MOVE
7851 processSlot(mapper, 1);
7852 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
7853 processSync(mapper);
7854
7855 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
7856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
7858 ASSERT_EQ(1U, args.pointerCount);
7859 ASSERT_EQ(1, args.pointerProperties[0].id);
7860 ASSERT_NO_FATAL_FAILURE(
7861 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
7862
7863 // FINGER 1 UP
7864 processId(mapper, -1);
7865 processKey(mapper, BTN_TOUCH, 0);
7866 processSync(mapper);
7867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7868 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
7869
7870 // non captured touchpad should be a mouse source
7871 mFakePolicy->setPointerCapture(false);
7872 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
7874 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7875}
7876
7877TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
7878 std::shared_ptr<FakePointerController> fakePointerController =
7879 std::make_shared<FakePointerController>();
7880 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
7881 fakePointerController->setPosition(0, 0);
7882 fakePointerController->setButtonState(0);
7883
7884 // prepare device and capture
7885 prepareDisplay(DISPLAY_ORIENTATION_0);
7886 prepareAxes(POSITION | ID | SLOT);
7887 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7888 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
7889 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7890 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7891 // run uncaptured pointer tests - pushes out generic events
7892 // FINGER 0 DOWN
7893 processId(mapper, 3);
7894 processPosition(mapper, 100, 100);
7895 processKey(mapper, BTN_TOUCH, 1);
7896 processSync(mapper);
7897
7898 // start at (100,100), cursor should be at (0,0) * scale
7899 NotifyMotionArgs args;
7900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7902 ASSERT_NO_FATAL_FAILURE(
7903 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
7904
7905 // FINGER 0 MOVE
7906 processPosition(mapper, 200, 200);
7907 processSync(mapper);
7908
7909 // compute scaling to help with touch position checking
7910 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
7911 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
7912 float scale =
7913 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
7914
7915 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7917 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
7918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
7919 0, 0, 0, 0, 0, 0, 0));
7920}
7921
7922TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
7923 std::shared_ptr<FakePointerController> fakePointerController =
7924 std::make_shared<FakePointerController>();
7925
7926 prepareDisplay(DISPLAY_ORIENTATION_0);
7927 prepareAxes(POSITION | ID | SLOT);
7928 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
7929 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7930 mFakePolicy->setPointerCapture(false);
7931 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7932
7933 // uncaptured touchpad should be a pointer device
7934 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
7935
7936 // captured touchpad should be a touchpad device
7937 mFakePolicy->setPointerCapture(true);
7938 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
7939 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
7940}
7941
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942} // namespace android