blob: 36da8ddc8e4632fdd9b26ff2474403d301309890 [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>
Chris Yef59a2f42020-10-16 12:55:26 -070025#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070026#include <SingleTouchInputMapper.h>
27#include <SwitchInputMapper.h>
28#include <TestInputListener.h>
29#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080030#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000031#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070032#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080034#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <math.h>
36
Michael Wright17db18e2020-06-26 20:51:44 +010037#include <memory>
Michael Wrightdde67b82020-10-27 16:09:22 +000038#include "input/DisplayViewport.h"
39#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010040
Michael Wrightd02c5b62014-02-10 15:10:22 -080041namespace android {
42
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070043using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070044using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070045
46// Timeout for waiting for an expected event
47static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
48
Michael Wrightd02c5b62014-02-10 15:10:22 -080049// An arbitrary time value.
50static const nsecs_t ARBITRARY_TIME = 1234;
51
52// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080053static constexpr int32_t DISPLAY_ID = 0;
54static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
55static constexpr int32_t DISPLAY_WIDTH = 480;
56static constexpr int32_t DISPLAY_HEIGHT = 800;
57static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
58static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
59static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070060static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070061static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t FIRST_SLOT = 0;
64static constexpr int32_t SECOND_SLOT = 1;
65static constexpr int32_t THIRD_SLOT = 2;
66static constexpr int32_t INVALID_TRACKING_ID = -1;
67static constexpr int32_t FIRST_TRACKING_ID = 0;
68static constexpr int32_t SECOND_TRACKING_ID = 1;
69static constexpr int32_t THIRD_TRACKING_ID = 2;
70
Michael Wrightd02c5b62014-02-10 15:10:22 -080071// Error tolerance for floating point assertions.
72static const float EPSILON = 0.001f;
73
74template<typename T>
75static inline T min(T a, T b) {
76 return a < b ? a : b;
77}
78
79static inline float avg(float x, float y) {
80 return (x + y) / 2;
81}
82
83
84// --- FakePointerController ---
85
86class FakePointerController : public PointerControllerInterface {
87 bool mHaveBounds;
88 float mMinX, mMinY, mMaxX, mMaxY;
89 float mX, mY;
90 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +080091 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -080092
Michael Wrightd02c5b62014-02-10 15:10:22 -080093public:
94 FakePointerController() :
95 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +080096 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 }
98
Michael Wright17db18e2020-06-26 20:51:44 +010099 virtual ~FakePointerController() {}
100
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101 void setBounds(float minX, float minY, float maxX, float maxY) {
102 mHaveBounds = true;
103 mMinX = minX;
104 mMinY = minY;
105 mMaxX = maxX;
106 mMaxY = maxY;
107 }
108
Chris Yea52ade12020-08-27 16:49:20 -0700109 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110 mX = x;
111 mY = y;
112 }
113
Chris Yea52ade12020-08-27 16:49:20 -0700114 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Chris Yea52ade12020-08-27 16:49:20 -0700116 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
Chris Yea52ade12020-08-27 16:49:20 -0700118 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119 *outX = mX;
120 *outY = mY;
121 }
122
Chris Yea52ade12020-08-27 16:49:20 -0700123 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800124
Chris Yea52ade12020-08-27 16:49:20 -0700125 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800126 mDisplayId = viewport.displayId;
127 }
128
Arthur Hung7c645402019-01-25 17:45:42 +0800129 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
130 return mSpotsByDisplay;
131 }
132
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133private:
Chris Yea52ade12020-08-27 16:49:20 -0700134 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 *outMinX = mMinX;
136 *outMinY = mMinY;
137 *outMaxX = mMaxX;
138 *outMaxY = mMaxY;
139 return mHaveBounds;
140 }
141
Chris Yea52ade12020-08-27 16:49:20 -0700142 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 mX += deltaX;
144 if (mX < mMinX) mX = mMinX;
145 if (mX > mMaxX) mX = mMaxX;
146 mY += deltaY;
147 if (mY < mMinY) mY = mMinY;
148 if (mY > mMaxY) mY = mMaxY;
149 }
150
Chris Yea52ade12020-08-27 16:49:20 -0700151 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152
Chris Yea52ade12020-08-27 16:49:20 -0700153 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154
Chris Yea52ade12020-08-27 16:49:20 -0700155 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156
Chris Yea52ade12020-08-27 16:49:20 -0700157 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
158 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800159 std::vector<int32_t> newSpots;
160 // Add spots for fingers that are down.
161 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
162 uint32_t id = idBits.clearFirstMarkedBit();
163 newSpots.push_back(id);
164 }
165
166 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167 }
168
Chris Yea52ade12020-08-27 16:49:20 -0700169 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800170
171 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172};
173
174
175// --- FakeInputReaderPolicy ---
176
177class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700178 std::mutex mLock;
179 std::condition_variable mDevicesChangedCondition;
180
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100182 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700183 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
184 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100185 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700186 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
188protected:
Chris Yea52ade12020-08-27 16:49:20 -0700189 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191public:
192 FakeInputReaderPolicy() {
193 }
194
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800196 waitForInputDevices([](bool devicesChanged) {
197 if (!devicesChanged) {
198 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
199 }
200 });
201 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700202
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800203 void assertInputDevicesNotChanged() {
204 waitForInputDevices([](bool devicesChanged) {
205 if (devicesChanged) {
206 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
207 }
208 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 }
210
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700211 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100212 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100213 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700214 }
215
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700216 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
217 return mConfig.getDisplayViewportByUniqueId(uniqueId);
218 }
219 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
220 return mConfig.getDisplayViewportByType(type);
221 }
222
223 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
224 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700225 }
226
227 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000228 bool isActive, const std::string& uniqueId,
229 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
230 const DisplayViewport viewport =
231 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
232 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700233 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100234 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800235 }
236
Arthur Hung6cd19a42019-08-30 19:04:12 +0800237 bool updateViewport(const DisplayViewport& viewport) {
238 size_t count = mViewports.size();
239 for (size_t i = 0; i < count; i++) {
240 const DisplayViewport& currentViewport = mViewports[i];
241 if (currentViewport.displayId == viewport.displayId) {
242 mViewports[i] = viewport;
243 mConfig.setDisplayViewports(mViewports);
244 return true;
245 }
246 }
247 // no viewport found.
248 return false;
249 }
250
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100251 void addExcludedDeviceName(const std::string& deviceName) {
252 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253 }
254
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700255 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
256 mConfig.portAssociations.insert({inputPort, displayPort});
257 }
258
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000259 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700260
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000261 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700262
Michael Wright17db18e2020-06-26 20:51:44 +0100263 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
264 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265 }
266
267 const InputReaderConfiguration* getReaderConfiguration() const {
268 return &mConfig;
269 }
270
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800271 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 return mInputDevices;
273 }
274
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100275 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700276 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700277 return transform;
278 }
279
280 void setTouchAffineTransformation(const TouchAffineTransformation t) {
281 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800282 }
283
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800284 void setPointerCapture(bool enabled) {
285 mConfig.pointerCapture = enabled;
286 }
287
Arthur Hung7c645402019-01-25 17:45:42 +0800288 void setShowTouches(bool enabled) {
289 mConfig.showTouches = enabled;
290 }
291
Garfield Tan888a6a42020-01-09 11:39:16 -0800292 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
293 mConfig.defaultPointerDisplayId = pointerDisplayId;
294 }
295
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800296 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
297
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700299 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000300 int32_t orientation, bool isActive,
301 const std::string& uniqueId,
302 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700303 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
304 || orientation == DISPLAY_ORIENTATION_270);
305 DisplayViewport v;
306 v.displayId = displayId;
307 v.orientation = orientation;
308 v.logicalLeft = 0;
309 v.logicalTop = 0;
310 v.logicalRight = isRotated ? height : width;
311 v.logicalBottom = isRotated ? width : height;
312 v.physicalLeft = 0;
313 v.physicalTop = 0;
314 v.physicalRight = isRotated ? height : width;
315 v.physicalBottom = isRotated ? width : height;
316 v.deviceWidth = isRotated ? height : width;
317 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000318 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700319 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700320 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100321 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700322 return v;
323 }
324
Chris Yea52ade12020-08-27 16:49:20 -0700325 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 *outConfig = mConfig;
327 }
328
Chris Yea52ade12020-08-27 16:49:20 -0700329 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100330 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 }
332
Chris Yea52ade12020-08-27 16:49:20 -0700333 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700334 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700336 mInputDevicesChanged = true;
337 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 }
339
Chris Yea52ade12020-08-27 16:49:20 -0700340 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
341 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700342 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 }
344
Chris Yea52ade12020-08-27 16:49:20 -0700345 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800346
347 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
348 std::unique_lock<std::mutex> lock(mLock);
349 base::ScopedLockAssertion assumeLocked(mLock);
350
351 const bool devicesChanged =
352 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
353 return mInputDevicesChanged;
354 });
355 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
356 mInputDevicesChanged = false;
357 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358};
359
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360// --- FakeEventHub ---
361
362class FakeEventHub : public EventHubInterface {
363 struct KeyInfo {
364 int32_t keyCode;
365 uint32_t flags;
366 };
367
Chris Yef59a2f42020-10-16 12:55:26 -0700368 struct SensorInfo {
369 InputDeviceSensorType sensorType;
370 int32_t sensorDataIndex;
371 };
372
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373 struct Device {
374 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700375 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 PropertyMap configuration;
377 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
378 KeyedVector<int, bool> relativeAxes;
379 KeyedVector<int32_t, int32_t> keyCodeStates;
380 KeyedVector<int32_t, int32_t> scanCodeStates;
381 KeyedVector<int32_t, int32_t> switchStates;
382 KeyedVector<int32_t, int32_t> absoluteAxisValue;
383 KeyedVector<int32_t, KeyInfo> keysByScanCode;
384 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
385 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700386 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
387 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800388 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700389 bool enabled;
390
391 status_t enable() {
392 enabled = true;
393 return OK;
394 }
395
396 status_t disable() {
397 enabled = false;
398 return OK;
399 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400
Chris Ye1b0c7342020-07-28 21:57:03 -0700401 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800402 };
403
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700404 std::mutex mLock;
405 std::condition_variable mEventsCondition;
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100408 std::vector<std::string> mExcludedDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700409 List<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600410 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000411 std::vector<int32_t> mVibrators = {0, 1};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700413public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414 virtual ~FakeEventHub() {
415 for (size_t i = 0; i < mDevices.size(); i++) {
416 delete mDevices.valueAt(i);
417 }
418 }
419
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420 FakeEventHub() { }
421
Chris Ye1b0c7342020-07-28 21:57:03 -0700422 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 Device* device = new Device(classes);
424 device->identifier.name = name;
425 mDevices.add(deviceId, device);
426
427 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
428 }
429
430 void removeDevice(int32_t deviceId) {
431 delete mDevices.valueFor(deviceId);
432 mDevices.removeItem(deviceId);
433
434 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
435 }
436
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700437 bool isDeviceEnabled(int32_t deviceId) {
438 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700439 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700440 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
441 return false;
442 }
443 return device->enabled;
444 }
445
446 status_t enableDevice(int32_t deviceId) {
447 status_t result;
448 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700449 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700450 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
451 return BAD_VALUE;
452 }
453 if (device->enabled) {
454 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
455 return OK;
456 }
457 result = device->enable();
458 return result;
459 }
460
461 status_t disableDevice(int32_t deviceId) {
462 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700463 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700464 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
465 return BAD_VALUE;
466 }
467 if (!device->enabled) {
468 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
469 return OK;
470 }
471 return device->disable();
472 }
473
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474 void finishDeviceScan() {
475 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
476 }
477
478 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
479 Device* device = getDevice(deviceId);
480 device->configuration.addProperty(key, value);
481 }
482
483 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
484 Device* device = getDevice(deviceId);
485 device->configuration.addAll(configuration);
486 }
487
488 void addAbsoluteAxis(int32_t deviceId, int axis,
489 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
490 Device* device = getDevice(deviceId);
491
492 RawAbsoluteAxisInfo info;
493 info.valid = true;
494 info.minValue = minValue;
495 info.maxValue = maxValue;
496 info.flat = flat;
497 info.fuzz = fuzz;
498 info.resolution = resolution;
499 device->absoluteAxes.add(axis, info);
500 }
501
502 void addRelativeAxis(int32_t deviceId, int32_t axis) {
503 Device* device = getDevice(deviceId);
504 device->relativeAxes.add(axis, true);
505 }
506
507 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
508 Device* device = getDevice(deviceId);
509 device->keyCodeStates.replaceValueFor(keyCode, state);
510 }
511
512 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
513 Device* device = getDevice(deviceId);
514 device->scanCodeStates.replaceValueFor(scanCode, state);
515 }
516
517 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
518 Device* device = getDevice(deviceId);
519 device->switchStates.replaceValueFor(switchCode, state);
520 }
521
522 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
523 Device* device = getDevice(deviceId);
524 device->absoluteAxisValue.replaceValueFor(axis, value);
525 }
526
527 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
528 int32_t keyCode, uint32_t flags) {
529 Device* device = getDevice(deviceId);
530 KeyInfo info;
531 info.keyCode = keyCode;
532 info.flags = flags;
533 if (scanCode) {
534 device->keysByScanCode.add(scanCode, info);
535 }
536 if (usageCode) {
537 device->keysByUsageCode.add(usageCode, info);
538 }
539 }
540
541 void addLed(int32_t deviceId, int32_t led, bool initialState) {
542 Device* device = getDevice(deviceId);
543 device->leds.add(led, initialState);
544 }
545
Chris Yef59a2f42020-10-16 12:55:26 -0700546 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
547 int32_t sensorDataIndex) {
548 Device* device = getDevice(deviceId);
549 SensorInfo info;
550 info.sensorType = sensorType;
551 info.sensorDataIndex = sensorDataIndex;
552 device->sensorsByAbsCode.emplace(absCode, info);
553 }
554
555 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
556 Device* device = getDevice(deviceId);
557 typename BitArray<MSC_MAX>::Buffer buffer;
558 buffer[mscEvent / 32] = 1 << mscEvent % 32;
559 device->mscBitmask.loadFromBuffer(buffer);
560 }
561
Michael Wrightd02c5b62014-02-10 15:10:22 -0800562 bool getLedState(int32_t deviceId, int32_t led) {
563 Device* device = getDevice(deviceId);
564 return device->leds.valueFor(led);
565 }
566
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100567 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 return mExcludedDevices;
569 }
570
571 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
572 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800573 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 }
575
576 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
577 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700578 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 RawEvent event;
580 event.when = when;
581 event.deviceId = deviceId;
582 event.type = type;
583 event.code = code;
584 event.value = value;
585 mEvents.push_back(event);
586
587 if (type == EV_ABS) {
588 setAbsoluteAxisValue(deviceId, code, value);
589 }
590 }
591
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600592 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
593 std::vector<TouchVideoFrame>> videoFrames) {
594 mVideoFrames = std::move(videoFrames);
595 }
596
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700598 std::unique_lock<std::mutex> lock(mLock);
599 base::ScopedLockAssertion assumeLocked(mLock);
600 const bool queueIsEmpty =
601 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
602 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
603 if (!queueIsEmpty) {
604 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
605 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 }
607
608private:
609 Device* getDevice(int32_t deviceId) const {
610 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100611 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Chris Yea52ade12020-08-27 16:49:20 -0700614 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700616 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618
Chris Yea52ade12020-08-27 16:49:20 -0700619 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620 Device* device = getDevice(deviceId);
621 return device ? device->identifier : InputDeviceIdentifier();
622 }
623
Chris Yea52ade12020-08-27 16:49:20 -0700624 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625
Chris Yea52ade12020-08-27 16:49:20 -0700626 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 Device* device = getDevice(deviceId);
628 if (device) {
629 *outConfiguration = device->configuration;
630 }
631 }
632
Chris Yea52ade12020-08-27 16:49:20 -0700633 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
634 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800636 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 ssize_t index = device->absoluteAxes.indexOfKey(axis);
638 if (index >= 0) {
639 *outAxisInfo = device->absoluteAxes.valueAt(index);
640 return OK;
641 }
642 }
643 outAxisInfo->clear();
644 return -1;
645 }
646
Chris Yea52ade12020-08-27 16:49:20 -0700647 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 Device* device = getDevice(deviceId);
649 if (device) {
650 return device->relativeAxes.indexOfKey(axis) >= 0;
651 }
652 return false;
653 }
654
Chris Yea52ade12020-08-27 16:49:20 -0700655 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656
Chris Yef59a2f42020-10-16 12:55:26 -0700657 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
658 Device* device = getDevice(deviceId);
659 if (device) {
660 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
661 }
662 return false;
663 }
664
Chris Yea52ade12020-08-27 16:49:20 -0700665 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
666 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 Device* device = getDevice(deviceId);
668 if (device) {
669 const KeyInfo* key = getKey(device, scanCode, usageCode);
670 if (key) {
671 if (outKeycode) {
672 *outKeycode = key->keyCode;
673 }
674 if (outFlags) {
675 *outFlags = key->flags;
676 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700677 if (outMetaState) {
678 *outMetaState = metaState;
679 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 return OK;
681 }
682 }
683 return NAME_NOT_FOUND;
684 }
685
686 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
687 if (usageCode) {
688 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
689 if (index >= 0) {
690 return &device->keysByUsageCode.valueAt(index);
691 }
692 }
693 if (scanCode) {
694 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
695 if (index >= 0) {
696 return &device->keysByScanCode.valueAt(index);
697 }
698 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700699 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 }
701
Chris Yea52ade12020-08-27 16:49:20 -0700702 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703
Chris Yef59a2f42020-10-16 12:55:26 -0700704 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
705 int32_t absCode) {
706 Device* device = getDevice(deviceId);
707 if (!device) {
708 return Errorf("Sensor device not found.");
709 }
710 auto it = device->sensorsByAbsCode.find(absCode);
711 if (it == device->sensorsByAbsCode.end()) {
712 return Errorf("Sensor map not found.");
713 }
714 const SensorInfo& info = it->second;
715 return std::make_pair(info.sensorType, info.sensorDataIndex);
716 }
717
Chris Yea52ade12020-08-27 16:49:20 -0700718 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 mExcludedDevices = devices;
720 }
721
Chris Yea52ade12020-08-27 16:49:20 -0700722 size_t getEvents(int, RawEvent* buffer, size_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700723 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 if (mEvents.empty()) {
725 return 0;
726 }
727
728 *buffer = *mEvents.begin();
729 mEvents.erase(mEvents.begin());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700730 mEventsCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 return 1;
732 }
733
Chris Yea52ade12020-08-27 16:49:20 -0700734 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600735 auto it = mVideoFrames.find(deviceId);
736 if (it != mVideoFrames.end()) {
737 std::vector<TouchVideoFrame> frames = std::move(it->second);
738 mVideoFrames.erase(deviceId);
739 return frames;
740 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800741 return {};
742 }
743
Chris Yea52ade12020-08-27 16:49:20 -0700744 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 Device* device = getDevice(deviceId);
746 if (device) {
747 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
748 if (index >= 0) {
749 return device->scanCodeStates.valueAt(index);
750 }
751 }
752 return AKEY_STATE_UNKNOWN;
753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 Device* device = getDevice(deviceId);
757 if (device) {
758 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
759 if (index >= 0) {
760 return device->keyCodeStates.valueAt(index);
761 }
762 }
763 return AKEY_STATE_UNKNOWN;
764 }
765
Chris Yea52ade12020-08-27 16:49:20 -0700766 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800767 Device* device = getDevice(deviceId);
768 if (device) {
769 ssize_t index = device->switchStates.indexOfKey(sw);
770 if (index >= 0) {
771 return device->switchStates.valueAt(index);
772 }
773 }
774 return AKEY_STATE_UNKNOWN;
775 }
776
Chris Yea52ade12020-08-27 16:49:20 -0700777 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
778 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779 Device* device = getDevice(deviceId);
780 if (device) {
781 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
782 if (index >= 0) {
783 *outValue = device->absoluteAxisValue.valueAt(index);
784 return OK;
785 }
786 }
787 *outValue = 0;
788 return -1;
789 }
790
Chris Yea52ade12020-08-27 16:49:20 -0700791 // Return true if the device has non-empty key layout.
792 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
793 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 bool result = false;
795 Device* device = getDevice(deviceId);
796 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700797 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 for (size_t i = 0; i < numCodes; i++) {
799 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
800 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
801 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 }
803 }
804 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
805 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
806 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 }
808 }
809 }
810 }
811 return result;
812 }
813
Chris Yea52ade12020-08-27 16:49:20 -0700814 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800815 Device* device = getDevice(deviceId);
816 if (device) {
817 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
818 return index >= 0;
819 }
820 return false;
821 }
822
Chris Yea52ade12020-08-27 16:49:20 -0700823 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824 Device* device = getDevice(deviceId);
825 return device && device->leds.indexOfKey(led) >= 0;
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 Device* device = getDevice(deviceId);
830 if (device) {
831 ssize_t index = device->leds.indexOfKey(led);
832 if (index >= 0) {
833 device->leds.replaceValueAt(led, on);
834 } else {
835 ADD_FAILURE()
836 << "Attempted to set the state of an LED that the EventHub declared "
837 "was not present. led=" << led;
838 }
839 }
840 }
841
Chris Yea52ade12020-08-27 16:49:20 -0700842 void getVirtualKeyDefinitions(
843 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 outVirtualKeys.clear();
845
846 Device* device = getDevice(deviceId);
847 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800848 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 }
850 }
851
Chris Yea52ade12020-08-27 16:49:20 -0700852 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700853 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 }
855
Chris Yea52ade12020-08-27 16:49:20 -0700856 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857 return false;
858 }
859
Chris Yea52ade12020-08-27 16:49:20 -0700860 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
Chris Yea52ade12020-08-27 16:49:20 -0700862 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863
Chris Ye87143712020-11-10 05:05:58 +0000864 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
865
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100866 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867 return false;
868 }
869
Chris Yea52ade12020-08-27 16:49:20 -0700870 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871
Chris Yea52ade12020-08-27 16:49:20 -0700872 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873
Chris Yea52ade12020-08-27 16:49:20 -0700874 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875
Chris Yea52ade12020-08-27 16:49:20 -0700876 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877};
878
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879// --- FakeInputMapper ---
880
881class FakeInputMapper : public InputMapper {
882 uint32_t mSources;
883 int32_t mKeyboardType;
884 int32_t mMetaState;
885 KeyedVector<int32_t, int32_t> mKeyCodeStates;
886 KeyedVector<int32_t, int32_t> mScanCodeStates;
887 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800888 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700890 std::mutex mLock;
891 std::condition_variable mStateChangedCondition;
892 bool mConfigureWasCalled GUARDED_BY(mLock);
893 bool mResetWasCalled GUARDED_BY(mLock);
894 bool mProcessWasCalled GUARDED_BY(mLock);
895 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896
Arthur Hungc23540e2018-11-29 20:42:11 +0800897 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800899 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
900 : InputMapper(deviceContext),
901 mSources(sources),
902 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800904 mConfigureWasCalled(false),
905 mResetWasCalled(false),
906 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907
Chris Yea52ade12020-08-27 16:49:20 -0700908 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
910 void setKeyboardType(int32_t keyboardType) {
911 mKeyboardType = keyboardType;
912 }
913
914 void setMetaState(int32_t metaState) {
915 mMetaState = metaState;
916 }
917
918 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700919 std::unique_lock<std::mutex> lock(mLock);
920 base::ScopedLockAssertion assumeLocked(mLock);
921 const bool configureCalled =
922 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
923 return mConfigureWasCalled;
924 });
925 if (!configureCalled) {
926 FAIL() << "Expected configure() to have been called.";
927 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 mConfigureWasCalled = false;
929 }
930
931 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700932 std::unique_lock<std::mutex> lock(mLock);
933 base::ScopedLockAssertion assumeLocked(mLock);
934 const bool resetCalled =
935 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
936 return mResetWasCalled;
937 });
938 if (!resetCalled) {
939 FAIL() << "Expected reset() to have been called.";
940 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 mResetWasCalled = false;
942 }
943
Yi Kong9b14ac62018-07-17 13:48:38 -0700944 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700945 std::unique_lock<std::mutex> lock(mLock);
946 base::ScopedLockAssertion assumeLocked(mLock);
947 const bool processCalled =
948 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
949 return mProcessWasCalled;
950 });
951 if (!processCalled) {
952 FAIL() << "Expected process() to have been called.";
953 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954 if (outLastEvent) {
955 *outLastEvent = mLastEvent;
956 }
957 mProcessWasCalled = false;
958 }
959
960 void setKeyCodeState(int32_t keyCode, int32_t state) {
961 mKeyCodeStates.replaceValueFor(keyCode, state);
962 }
963
964 void setScanCodeState(int32_t scanCode, int32_t state) {
965 mScanCodeStates.replaceValueFor(scanCode, state);
966 }
967
968 void setSwitchState(int32_t switchCode, int32_t state) {
969 mSwitchStates.replaceValueFor(switchCode, state);
970 }
971
972 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800973 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 }
975
976private:
Chris Yea52ade12020-08-27 16:49:20 -0700977 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978
Chris Yea52ade12020-08-27 16:49:20 -0700979 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 InputMapper::populateDeviceInfo(deviceInfo);
981
982 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
983 deviceInfo->setKeyboardType(mKeyboardType);
984 }
985 }
986
Chris Yea52ade12020-08-27 16:49:20 -0700987 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700988 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800990
991 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800992 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800993 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
994 mViewport = config->getDisplayViewportByPort(*displayPort);
995 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996
997 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998 }
999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001001 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001003 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004 }
1005
Chris Yea52ade12020-08-27 16:49:20 -07001006 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001007 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008 mLastEvent = *rawEvent;
1009 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001010 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 }
1012
Chris Yea52ade12020-08-27 16:49:20 -07001013 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1015 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1016 }
1017
Chris Yea52ade12020-08-27 16:49:20 -07001018 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1020 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1021 }
1022
Chris Yea52ade12020-08-27 16:49:20 -07001023 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1025 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1026 }
1027
Chris Yea52ade12020-08-27 16:49:20 -07001028 // Return true if the device has non-empty key layout.
1029 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1030 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 for (size_t i = 0; i < numCodes; i++) {
1032 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1033 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1034 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001035 }
1036 }
1037 }
Chris Yea52ade12020-08-27 16:49:20 -07001038 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 return result;
1040 }
1041
1042 virtual int32_t getMetaState() {
1043 return mMetaState;
1044 }
1045
1046 virtual void fadePointer() {
1047 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001048
1049 virtual std::optional<int32_t> getAssociatedDisplay() {
1050 if (mViewport) {
1051 return std::make_optional(mViewport->displayId);
1052 }
1053 return std::nullopt;
1054 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055};
1056
1057
1058// --- InstrumentedInputReader ---
1059
1060class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001061 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062
1063public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001064 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1065 const sp<InputReaderPolicyInterface>& policy,
1066 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001067 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001069 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001073 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001074 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075 InputDeviceIdentifier identifier;
1076 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001077 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001079 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 }
1081
Prabir Pradhan28efc192019-11-05 01:10:04 +00001082 // Make the protected loopOnce method accessible to tests.
1083 using InputReader::loopOnce;
1084
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001086 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1087 const InputDeviceIdentifier& identifier)
1088 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001089 if (!mNextDevices.empty()) {
1090 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1091 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092 return device;
1093 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001094 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 }
1096
arthurhungdcef2dc2020-08-11 14:47:50 +08001097 // --- FakeInputReaderContext ---
1098 class FakeInputReaderContext : public ContextImpl {
1099 int32_t mGlobalMetaState;
1100 bool mUpdateGlobalMetaStateWasCalled;
1101 int32_t mGeneration;
1102
1103 public:
1104 FakeInputReaderContext(InputReader* reader)
1105 : ContextImpl(reader),
1106 mGlobalMetaState(0),
1107 mUpdateGlobalMetaStateWasCalled(false),
1108 mGeneration(1) {}
1109
1110 virtual ~FakeInputReaderContext() {}
1111
1112 void assertUpdateGlobalMetaStateWasCalled() {
1113 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1114 << "Expected updateGlobalMetaState() to have been called.";
1115 mUpdateGlobalMetaStateWasCalled = false;
1116 }
1117
1118 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1119
1120 uint32_t getGeneration() { return mGeneration; }
1121
1122 void updateGlobalMetaState() override {
1123 mUpdateGlobalMetaStateWasCalled = true;
1124 ContextImpl::updateGlobalMetaState();
1125 }
1126
1127 int32_t getGlobalMetaState() override {
1128 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1129 }
1130
1131 int32_t bumpGeneration() override {
1132 mGeneration = ContextImpl::bumpGeneration();
1133 return mGeneration;
1134 }
1135 } mFakeContext;
1136
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001138
1139public:
1140 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141};
1142
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001143// --- InputReaderPolicyTest ---
1144class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001145protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001146 sp<FakeInputReaderPolicy> mFakePolicy;
1147
Chris Yea52ade12020-08-27 16:49:20 -07001148 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1149 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001150};
1151
1152/**
1153 * Check that empty set of viewports is an acceptable configuration.
1154 * Also try to get internal viewport two different ways - by type and by uniqueId.
1155 *
1156 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1157 * Such configuration is not currently allowed.
1158 */
1159TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001160 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001161
1162 // We didn't add any viewports yet, so there shouldn't be any.
1163 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001164 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001165 ASSERT_FALSE(internalViewport);
1166
1167 // Add an internal viewport, then clear it
1168 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001169 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001170 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001171
1172 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001173 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001174 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001175 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001176
1177 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001178 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001179 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001180 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001181
1182 mFakePolicy->clearViewports();
1183 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001184 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001185 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001186 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001187 ASSERT_FALSE(internalViewport);
1188}
1189
1190TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1191 const std::string internalUniqueId = "local:0";
1192 const std::string externalUniqueId = "local:1";
1193 const std::string virtualUniqueId1 = "virtual:2";
1194 const std::string virtualUniqueId2 = "virtual:3";
1195 constexpr int32_t virtualDisplayId1 = 2;
1196 constexpr int32_t virtualDisplayId2 = 3;
1197
1198 // Add an internal viewport
1199 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001200 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1201 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001202 // Add an external viewport
1203 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001204 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1205 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001206 // Add an virtual viewport
1207 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001208 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1209 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001210 // Add another virtual viewport
1211 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001212 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1213 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001214
1215 // Check matching by type for internal
1216 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001217 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001218 ASSERT_TRUE(internalViewport);
1219 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1220
1221 // Check matching by type for external
1222 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001223 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001224 ASSERT_TRUE(externalViewport);
1225 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1226
1227 // Check matching by uniqueId for virtual viewport #1
1228 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001229 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001230 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001231 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001232 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1233 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1234
1235 // Check matching by uniqueId for virtual viewport #2
1236 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001237 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001238 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001239 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001240 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1241 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1242}
1243
1244
1245/**
1246 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1247 * that lookup works by checking display id.
1248 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1249 */
1250TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1251 const std::string uniqueId1 = "uniqueId1";
1252 const std::string uniqueId2 = "uniqueId2";
1253 constexpr int32_t displayId1 = 2;
1254 constexpr int32_t displayId2 = 3;
1255
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001256 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1257 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001258 for (const ViewportType& type : types) {
1259 mFakePolicy->clearViewports();
1260 // Add a viewport
1261 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001262 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1263 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001264 // Add another viewport
1265 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001266 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1267 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001268
1269 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001270 std::optional<DisplayViewport> viewport1 =
1271 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001272 ASSERT_TRUE(viewport1);
1273 ASSERT_EQ(displayId1, viewport1->displayId);
1274 ASSERT_EQ(type, viewport1->type);
1275
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001276 std::optional<DisplayViewport> viewport2 =
1277 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001278 ASSERT_TRUE(viewport2);
1279 ASSERT_EQ(displayId2, viewport2->displayId);
1280 ASSERT_EQ(type, viewport2->type);
1281
1282 // When there are multiple viewports of the same kind, and uniqueId is not specified
1283 // in the call to getDisplayViewport, then that situation is not supported.
1284 // The viewports can be stored in any order, so we cannot rely on the order, since that
1285 // is just implementation detail.
1286 // However, we can check that it still returns *a* viewport, we just cannot assert
1287 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001288 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001289 ASSERT_TRUE(someViewport);
1290 }
1291}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001292
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001293/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001294 * When we have multiple internal displays make sure we always return the default display when
1295 * querying by type.
1296 */
1297TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1298 const std::string uniqueId1 = "uniqueId1";
1299 const std::string uniqueId2 = "uniqueId2";
1300 constexpr int32_t nonDefaultDisplayId = 2;
1301 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1302 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1303
1304 // Add the default display first and ensure it gets returned.
1305 mFakePolicy->clearViewports();
1306 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001307 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001308 ViewportType::INTERNAL);
1309 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001310 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001311 ViewportType::INTERNAL);
1312
1313 std::optional<DisplayViewport> viewport =
1314 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1315 ASSERT_TRUE(viewport);
1316 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1317 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1318
1319 // Add the default display second to make sure order doesn't matter.
1320 mFakePolicy->clearViewports();
1321 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001322 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001323 ViewportType::INTERNAL);
1324 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001325 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001326 ViewportType::INTERNAL);
1327
1328 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1329 ASSERT_TRUE(viewport);
1330 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1331 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1332}
1333
1334/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001335 * Check getDisplayViewportByPort
1336 */
1337TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001338 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001339 const std::string uniqueId1 = "uniqueId1";
1340 const std::string uniqueId2 = "uniqueId2";
1341 constexpr int32_t displayId1 = 1;
1342 constexpr int32_t displayId2 = 2;
1343 const uint8_t hdmi1 = 0;
1344 const uint8_t hdmi2 = 1;
1345 const uint8_t hdmi3 = 2;
1346
1347 mFakePolicy->clearViewports();
1348 // Add a viewport that's associated with some display port that's not of interest.
1349 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001350 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1351 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001352 // Add another viewport, connected to HDMI1 port
1353 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001354 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1355 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001356
1357 // Check that correct display viewport was returned by comparing the display ports.
1358 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1359 ASSERT_TRUE(hdmi1Viewport);
1360 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1361 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1362
1363 // Check that we can still get the same viewport using the uniqueId
1364 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1365 ASSERT_TRUE(hdmi1Viewport);
1366 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1367 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1368 ASSERT_EQ(type, hdmi1Viewport->type);
1369
1370 // Check that we cannot find a port with "HDMI2", because we never added one
1371 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1372 ASSERT_FALSE(hdmi2Viewport);
1373}
1374
Michael Wrightd02c5b62014-02-10 15:10:22 -08001375// --- InputReaderTest ---
1376
1377class InputReaderTest : public testing::Test {
1378protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001379 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001380 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001381 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001382 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001383
Chris Yea52ade12020-08-27 16:49:20 -07001384 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001385 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001387 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001388
Prabir Pradhan28efc192019-11-05 01:10:04 +00001389 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1390 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001391 }
1392
Chris Yea52ade12020-08-27 16:49:20 -07001393 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001394 mFakeListener.clear();
1395 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001396 }
1397
Chris Ye1b0c7342020-07-28 21:57:03 -07001398 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001399 const PropertyMap* configuration) {
1400 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401
1402 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001403 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001404 }
1405 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001406 mReader->loopOnce();
1407 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1409 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410 }
1411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001412 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001413 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001414 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001415 }
1416
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001417 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001418 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001419 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001420 }
1421
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001422 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001423 const std::string& name,
1424 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001425 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001426 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1427 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001428 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001429 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001430 return mapper;
1431 }
1432};
1433
Chris Ye98d3f532020-10-01 21:48:59 -07001434TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001435 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1436 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1437 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001438
Chris Ye98d3f532020-10-01 21:48:59 -07001439 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001440 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001441 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001442 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1444 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1445 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001446}
1447
1448TEST_F(InputReaderTest, PolicyGetInputDevices) {
1449 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1450 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1451 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001452
1453 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001454 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001455 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001456 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001457 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1459 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1460 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1461}
1462
Chris Yee7310032020-09-22 15:36:28 -07001463TEST_F(InputReaderTest, GetMergedInputDevices) {
1464 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1465 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1466 // Add two subdevices to device
1467 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1468 // Must add at least one mapper or the device will be ignored!
1469 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1470 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1471
1472 // Push same device instance for next device to be added, so they'll have same identifier.
1473 mReader->pushNextDevice(device);
1474 mReader->pushNextDevice(device);
1475 ASSERT_NO_FATAL_FAILURE(
1476 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1477 ASSERT_NO_FATAL_FAILURE(
1478 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1479
1480 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001481 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001482}
1483
Chris Yee14523a2020-12-19 13:46:00 -08001484TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1485 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1486 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1487 // Add two subdevices to device
1488 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1489 // Must add at least one mapper or the device will be ignored!
1490 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1491 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1492
1493 // Push same device instance for next device to be added, so they'll have same identifier.
1494 mReader->pushNextDevice(device);
1495 mReader->pushNextDevice(device);
1496 // Sensor device is initially disabled
1497 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1498 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1499 nullptr));
1500 // Device is disabled because the only sub device is a sensor device and disabled initially.
1501 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1502 ASSERT_FALSE(device->isEnabled());
1503 ASSERT_NO_FATAL_FAILURE(
1504 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1505 // The merged device is enabled if any sub device is enabled
1506 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1507 ASSERT_TRUE(device->isEnabled());
1508}
1509
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001510TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001511 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001512 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001513 constexpr int32_t eventHubId = 1;
1514 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001515 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001516 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001517 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001518 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001519
Yi Kong9b14ac62018-07-17 13:48:38 -07001520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001521
1522 NotifyDeviceResetArgs resetArgs;
1523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001524 ASSERT_EQ(deviceId, resetArgs.deviceId);
1525
1526 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001527 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001528 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001529
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001531 ASSERT_EQ(deviceId, resetArgs.deviceId);
1532 ASSERT_EQ(device->isEnabled(), false);
1533
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001534 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001535 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001538 ASSERT_EQ(device->isEnabled(), false);
1539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001540 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001541 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001543 ASSERT_EQ(deviceId, resetArgs.deviceId);
1544 ASSERT_EQ(device->isEnabled(), true);
1545}
1546
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001548 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001549 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001550 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001551 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001553 AINPUT_SOURCE_KEYBOARD, nullptr);
1554 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001555
1556 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1557 AINPUT_SOURCE_ANY, AKEYCODE_A))
1558 << "Should return unknown when the device id is >= 0 but unknown.";
1559
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001560 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1561 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1562 << "Should return unknown when the device id is valid but the sources are not "
1563 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001565 ASSERT_EQ(AKEY_STATE_DOWN,
1566 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1567 AKEYCODE_A))
1568 << "Should return value provided by mapper when device id is valid and the device "
1569 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570
1571 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1572 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1573 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1574
1575 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1576 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1577 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1578}
1579
1580TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001581 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001582 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001583 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001584 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001585 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001586 AINPUT_SOURCE_KEYBOARD, nullptr);
1587 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588
1589 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1590 AINPUT_SOURCE_ANY, KEY_A))
1591 << "Should return unknown when the device id is >= 0 but unknown.";
1592
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001593 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1594 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1595 << "Should return unknown when the device id is valid but the sources are not "
1596 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001597
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001598 ASSERT_EQ(AKEY_STATE_DOWN,
1599 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1600 KEY_A))
1601 << "Should return value provided by mapper when device id is valid and the device "
1602 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001603
1604 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1605 AINPUT_SOURCE_TRACKBALL, KEY_A))
1606 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1607
1608 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1609 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1610 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1611}
1612
1613TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001615 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001616 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001617 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001618 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001619 AINPUT_SOURCE_KEYBOARD, nullptr);
1620 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621
1622 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1623 AINPUT_SOURCE_ANY, SW_LID))
1624 << "Should return unknown when the device id is >= 0 but unknown.";
1625
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1627 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1628 << "Should return unknown when the device id is valid but the sources are not "
1629 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001631 ASSERT_EQ(AKEY_STATE_DOWN,
1632 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1633 SW_LID))
1634 << "Should return value provided by mapper when device id is valid and the device "
1635 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001636
1637 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1638 AINPUT_SOURCE_TRACKBALL, SW_LID))
1639 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1640
1641 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1642 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1643 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1644}
1645
1646TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001647 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001648 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001649 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001650 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001651 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001652 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001654 mapper.addSupportedKeyCode(AKEYCODE_A);
1655 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001656
1657 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1658 uint8_t flags[4] = { 0, 0, 0, 1 };
1659
1660 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1661 << "Should return false when device id is >= 0 but unknown.";
1662 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1663
1664 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001665 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1666 << "Should return false when device id is valid but the sources are not supported by "
1667 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001668 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1669
1670 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001671 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1672 keyCodes, flags))
1673 << "Should return value provided by mapper when device id is valid and the device "
1674 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1676
1677 flags[3] = 1;
1678 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1679 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1680 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1681
1682 flags[3] = 1;
1683 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1684 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1685 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1686}
1687
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001688TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001689 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001690 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001691
1692 NotifyConfigurationChangedArgs args;
1693
1694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1695 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1696}
1697
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001698TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001700 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001701 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001702 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001703 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001704 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001705
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001707 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001708 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1709
1710 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001711 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714 ASSERT_EQ(EV_KEY, event.type);
1715 ASSERT_EQ(KEY_A, event.code);
1716 ASSERT_EQ(1, event.value);
1717}
1718
Garfield Tan1c7bc862020-01-28 13:24:04 -08001719TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001721 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 constexpr int32_t eventHubId = 1;
1723 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001724 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001725 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001726 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001728
1729 NotifyDeviceResetArgs resetArgs;
1730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001731 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001732
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001733 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001734 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001736 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001737 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001739 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001740 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001742 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001743 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001744
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001745 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001746 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001748 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001749 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001750}
1751
Garfield Tan1c7bc862020-01-28 13:24:04 -08001752TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1753 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001754 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001755 constexpr int32_t eventHubId = 1;
1756 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1757 // Must add at least one mapper or the device will be ignored!
1758 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001759 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001760 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1761
1762 NotifyDeviceResetArgs resetArgs;
1763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1764 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1765}
1766
Arthur Hungc23540e2018-11-29 20:42:11 +08001767TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001768 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001769 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001770 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001771 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001772 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1773 FakeInputMapper& mapper =
1774 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001775 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001776
1777 const uint8_t hdmi1 = 1;
1778
1779 // Associated touch screen with second display.
1780 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1781
1782 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001783 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001784 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001785 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001786 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001787 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001788 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001789 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001790 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001791 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001792
1793 // Add the device, and make sure all of the callbacks are triggered.
1794 // The device is added after the input port associations are processed since
1795 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001796 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001799 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001800
Arthur Hung2c9a3342019-07-23 14:18:59 +08001801 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001802 ASSERT_EQ(deviceId, device->getId());
1803 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1804 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001805
1806 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001808 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001809 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001810}
1811
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001812TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1813 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1814 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1815 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1816 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1817 // Must add at least one mapper or the device will be ignored!
1818 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1819 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1820 mReader->pushNextDevice(device);
1821 mReader->pushNextDevice(device);
1822 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1823 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1824
1825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1826
1827 NotifyDeviceResetArgs resetArgs;
1828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1829 ASSERT_EQ(deviceId, resetArgs.deviceId);
1830 ASSERT_TRUE(device->isEnabled());
1831 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1832 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1833
1834 disableDevice(deviceId);
1835 mReader->loopOnce();
1836
1837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1838 ASSERT_EQ(deviceId, resetArgs.deviceId);
1839 ASSERT_FALSE(device->isEnabled());
1840 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1841 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1842
1843 enableDevice(deviceId);
1844 mReader->loopOnce();
1845
1846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1847 ASSERT_EQ(deviceId, resetArgs.deviceId);
1848 ASSERT_TRUE(device->isEnabled());
1849 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1850 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1851}
1852
1853TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1854 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1855 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1856 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1857 // Add two subdevices to device
1858 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1859 FakeInputMapper& mapperDevice1 =
1860 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1861 FakeInputMapper& mapperDevice2 =
1862 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1863 mReader->pushNextDevice(device);
1864 mReader->pushNextDevice(device);
1865 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1866 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1867
1868 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1869 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1870
1871 ASSERT_EQ(AKEY_STATE_DOWN,
1872 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1873 ASSERT_EQ(AKEY_STATE_DOWN,
1874 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1875 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1876 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1877}
1878
Prabir Pradhan7e186182020-11-10 13:56:45 -08001879TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1880 NotifyPointerCaptureChangedArgs args;
1881
1882 mFakePolicy->setPointerCapture(true);
1883 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1884 mReader->loopOnce();
1885 mFakeListener->assertNotifyCaptureWasCalled(&args);
1886 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1887
1888 mFakePolicy->setPointerCapture(false);
1889 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1890 mReader->loopOnce();
1891 mFakeListener->assertNotifyCaptureWasCalled(&args);
1892 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1893
1894 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1895 // does not change.
1896 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1897 mReader->loopOnce();
1898 mFakeListener->assertNotifyCaptureWasCalled(&args);
1899 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1900}
1901
Chris Ye87143712020-11-10 05:05:58 +00001902class FakeVibratorInputMapper : public FakeInputMapper {
1903public:
1904 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1905 : FakeInputMapper(deviceContext, sources) {}
1906
1907 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1908};
1909
1910TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1911 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1912 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1913 constexpr int32_t eventHubId = 1;
1914 const char* DEVICE_LOCATION = "BLUETOOTH";
1915 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1916 FakeVibratorInputMapper& mapper =
1917 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1918 mReader->pushNextDevice(device);
1919
1920 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1921 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1922
1923 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1924 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1925}
1926
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001927// --- InputReaderIntegrationTest ---
1928
1929// These tests create and interact with the InputReader only through its interface.
1930// The InputReader is started during SetUp(), which starts its processing in its own
1931// thread. The tests use linux uinput to emulate input devices.
1932// NOTE: Interacting with the physical device while these tests are running may cause
1933// the tests to fail.
1934class InputReaderIntegrationTest : public testing::Test {
1935protected:
1936 sp<TestInputListener> mTestListener;
1937 sp<FakeInputReaderPolicy> mFakePolicy;
1938 sp<InputReaderInterface> mReader;
1939
Chris Yea52ade12020-08-27 16:49:20 -07001940 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001941 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07001942 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1943 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001944
Prabir Pradhan9244aea2020-02-05 20:31:40 -08001945 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001946 ASSERT_EQ(mReader->start(), OK);
1947
1948 // Since this test is run on a real device, all the input devices connected
1949 // to the test device will show up in mReader. We wait for those input devices to
1950 // show up before beginning the tests.
1951 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1952 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1953 }
1954
Chris Yea52ade12020-08-27 16:49:20 -07001955 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001956 ASSERT_EQ(mReader->stop(), OK);
1957 mTestListener.clear();
1958 mFakePolicy.clear();
1959 }
1960};
1961
1962TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1963 // An invalid input device that is only used for this test.
1964 class InvalidUinputDevice : public UinputDevice {
1965 public:
1966 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1967
1968 private:
1969 void configureDevice(int fd, uinput_user_dev* device) override {}
1970 };
1971
1972 const size_t numDevices = mFakePolicy->getInputDevices().size();
1973
1974 // UinputDevice does not set any event or key bits, so InputReader should not
1975 // consider it as a valid device.
1976 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1977 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1978 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1979 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1980
1981 invalidDevice.reset();
1982 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1983 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1984 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1985}
1986
1987TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1988 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1989
1990 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1991 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1992 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1993 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1994
1995 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07001996 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
1997 const auto& it =
1998 std::find_if(inputDevices.begin(), inputDevices.end(),
1999 [&keyboard](const InputDeviceInfo& info) {
2000 return info.getIdentifier().name == keyboard->getName();
2001 });
2002
2003 ASSERT_NE(it, inputDevices.end());
2004 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2005 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2006 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002007
2008 keyboard.reset();
2009 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2010 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2011 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2012}
2013
2014TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2015 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2016 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2017
2018 NotifyConfigurationChangedArgs configChangedArgs;
2019 ASSERT_NO_FATAL_FAILURE(
2020 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002021 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002022 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2023
2024 NotifyKeyArgs keyArgs;
2025 keyboard->pressAndReleaseHomeKey();
2026 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2027 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002028 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002029 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002030 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2031 prevTimestamp = keyArgs.eventTime;
2032
2033 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2034 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002035 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002036 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2037}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002038
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002039/**
2040 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2041 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2042 * are passed to the listener.
2043 */
2044static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2045TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2046 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2047 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2048 NotifyKeyArgs keyArgs;
2049
2050 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2051 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2052 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2053 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2054
2055 controller->pressAndReleaseKey(BTN_GEAR_UP);
2056 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2057 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2058 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2059}
2060
Arthur Hungaab25622020-01-16 11:22:11 +08002061// --- TouchProcessTest ---
2062class TouchIntegrationTest : public InputReaderIntegrationTest {
2063protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002064 const std::string UNIQUE_ID = "local:0";
2065
Chris Yea52ade12020-08-27 16:49:20 -07002066 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002067 InputReaderIntegrationTest::SetUp();
2068 // At least add an internal display.
2069 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2070 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002071 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002072
2073 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2074 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2076 }
2077
2078 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2079 int32_t orientation, const std::string& uniqueId,
2080 std::optional<uint8_t> physicalPort,
2081 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002082 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2083 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002084 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2085 }
2086
2087 std::unique_ptr<UinputTouchScreen> mDevice;
2088};
2089
2090TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2091 NotifyMotionArgs args;
2092 const Point centerPoint = mDevice->getCenterPoint();
2093
2094 // ACTION_DOWN
2095 mDevice->sendDown(centerPoint);
2096 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2098
2099 // ACTION_MOVE
2100 mDevice->sendMove(centerPoint + Point(1, 1));
2101 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2103
2104 // ACTION_UP
2105 mDevice->sendUp();
2106 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2107 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2108}
2109
2110TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2111 NotifyMotionArgs args;
2112 const Point centerPoint = mDevice->getCenterPoint();
2113
2114 // ACTION_DOWN
2115 mDevice->sendDown(centerPoint);
2116 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2117 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2118
2119 // ACTION_POINTER_DOWN (Second slot)
2120 const Point secondPoint = centerPoint + Point(100, 100);
2121 mDevice->sendSlot(SECOND_SLOT);
2122 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2123 mDevice->sendDown(secondPoint + Point(1, 1));
2124 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2125 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2126 args.action);
2127
2128 // ACTION_MOVE (Second slot)
2129 mDevice->sendMove(secondPoint);
2130 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2132
2133 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002134 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002135 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002136 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002137 args.action);
2138
2139 // ACTION_UP
2140 mDevice->sendSlot(FIRST_SLOT);
2141 mDevice->sendUp();
2142 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2143 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2144}
2145
2146TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2147 NotifyMotionArgs args;
2148 const Point centerPoint = mDevice->getCenterPoint();
2149
2150 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002151 mDevice->sendSlot(FIRST_SLOT);
2152 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002153 mDevice->sendDown(centerPoint);
2154 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2155 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2156
arthurhungcc7f9802020-04-30 17:55:40 +08002157 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002158 const Point secondPoint = centerPoint + Point(100, 100);
2159 mDevice->sendSlot(SECOND_SLOT);
2160 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2161 mDevice->sendDown(secondPoint);
2162 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2163 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2164 args.action);
2165
arthurhungcc7f9802020-04-30 17:55:40 +08002166 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002167 mDevice->sendMove(secondPoint + Point(1, 1));
2168 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2170
arthurhungcc7f9802020-04-30 17:55:40 +08002171 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2172 // a palm event.
2173 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002174 mDevice->sendToolType(MT_TOOL_PALM);
2175 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002176 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2177 args.action);
2178 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002179
arthurhungcc7f9802020-04-30 17:55:40 +08002180 // Send up to second slot, expect first slot send moving.
2181 mDevice->sendPointerUp();
2182 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002184
arthurhungcc7f9802020-04-30 17:55:40 +08002185 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002186 mDevice->sendSlot(FIRST_SLOT);
2187 mDevice->sendUp();
2188
arthurhungcc7f9802020-04-30 17:55:40 +08002189 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2190 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002191}
2192
Michael Wrightd02c5b62014-02-10 15:10:22 -08002193// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002194class InputDeviceTest : public testing::Test {
2195protected:
2196 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002197 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002198 static const int32_t DEVICE_ID;
2199 static const int32_t DEVICE_GENERATION;
2200 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002201 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002202 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002203
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002204 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002205 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002206 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002207 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002208 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002209
Chris Yea52ade12020-08-27 16:49:20 -07002210 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002211 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002212 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002213 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002214 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2215 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002216 InputDeviceIdentifier identifier;
2217 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002218 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002219 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002220 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002221 mReader->pushNextDevice(mDevice);
2222 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2223 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002224 }
2225
Chris Yea52ade12020-08-27 16:49:20 -07002226 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002227 mFakeListener.clear();
2228 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002229 }
2230};
2231
2232const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002233const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002234const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2236const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002237const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2238 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002239const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240
2241TEST_F(InputDeviceTest, ImmutableProperties) {
2242 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002243 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002244 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002245}
2246
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002247TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2248 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002249}
2250
Michael Wrightd02c5b62014-02-10 15:10:22 -08002251TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2252 // Configuration.
2253 InputReaderConfiguration config;
2254 mDevice->configure(ARBITRARY_TIME, &config, 0);
2255
2256 // Reset.
2257 mDevice->reset(ARBITRARY_TIME);
2258
2259 NotifyDeviceResetArgs resetArgs;
2260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2261 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2262 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2263
2264 // Metadata.
2265 ASSERT_TRUE(mDevice->isIgnored());
2266 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2267
2268 InputDeviceInfo info;
2269 mDevice->getDeviceInfo(&info);
2270 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002271 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002272 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2273 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2274
2275 // State queries.
2276 ASSERT_EQ(0, mDevice->getMetaState());
2277
2278 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2279 << "Ignored device should return unknown key code state.";
2280 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2281 << "Ignored device should return unknown scan code state.";
2282 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2283 << "Ignored device should return unknown switch state.";
2284
2285 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2286 uint8_t flags[2] = { 0, 1 };
2287 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2288 << "Ignored device should never mark any key codes.";
2289 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2290 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2291}
2292
2293TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2294 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002295 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002296
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002297 FakeInputMapper& mapper1 =
2298 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002299 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2300 mapper1.setMetaState(AMETA_ALT_ON);
2301 mapper1.addSupportedKeyCode(AKEYCODE_A);
2302 mapper1.addSupportedKeyCode(AKEYCODE_B);
2303 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2304 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2305 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2306 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2307 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002308
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002309 FakeInputMapper& mapper2 =
2310 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002311 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312
2313 InputReaderConfiguration config;
2314 mDevice->configure(ARBITRARY_TIME, &config, 0);
2315
2316 String8 propertyValue;
2317 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2318 << "Device should have read configuration during configuration phase.";
2319 ASSERT_STREQ("value", propertyValue.string());
2320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002321 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2322 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323
2324 // Reset
2325 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002326 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2327 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002328
2329 NotifyDeviceResetArgs resetArgs;
2330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2331 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2332 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2333
2334 // Metadata.
2335 ASSERT_FALSE(mDevice->isIgnored());
2336 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2337
2338 InputDeviceInfo info;
2339 mDevice->getDeviceInfo(&info);
2340 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002341 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002342 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2343 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2344
2345 // State queries.
2346 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2347 << "Should query mappers and combine meta states.";
2348
2349 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2350 << "Should return unknown key code state when source not supported.";
2351 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2352 << "Should return unknown scan code state when source not supported.";
2353 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2354 << "Should return unknown switch state when source not supported.";
2355
2356 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2357 << "Should query mapper when source is supported.";
2358 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2359 << "Should query mapper when source is supported.";
2360 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2361 << "Should query mapper when source is supported.";
2362
2363 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2364 uint8_t flags[4] = { 0, 0, 0, 1 };
2365 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2366 << "Should do nothing when source is unsupported.";
2367 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2368 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2369 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2370 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2371
2372 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2373 << "Should query mapper when source is supported.";
2374 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2375 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2376 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2377 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2378
2379 // Event handling.
2380 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002381 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 mDevice->process(&event, 1);
2383
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002384 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2385 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386}
2387
Arthur Hung2c9a3342019-07-23 14:18:59 +08002388// A single input device is associated with a specific display. Check that:
2389// 1. Device is disabled if the viewport corresponding to the associated display is not found
2390// 2. Device is disabled when setEnabled API is called
2391TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002392 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002393
2394 // First Configuration.
2395 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2396
2397 // Device should be enabled by default.
2398 ASSERT_TRUE(mDevice->isEnabled());
2399
2400 // Prepare associated info.
2401 constexpr uint8_t hdmi = 1;
2402 const std::string UNIQUE_ID = "local:1";
2403
2404 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2405 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2406 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2407 // Device should be disabled because it is associated with a specific display via
2408 // input port <-> display port association, but the corresponding display is not found
2409 ASSERT_FALSE(mDevice->isEnabled());
2410
2411 // Prepare displays.
2412 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002413 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2414 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002415 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2416 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2417 ASSERT_TRUE(mDevice->isEnabled());
2418
2419 // Device should be disabled after set disable.
2420 mFakePolicy->addDisabledDevice(mDevice->getId());
2421 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2422 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2423 ASSERT_FALSE(mDevice->isEnabled());
2424
2425 // Device should still be disabled even found the associated display.
2426 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2427 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2428 ASSERT_FALSE(mDevice->isEnabled());
2429}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430
2431// --- InputMapperTest ---
2432
2433class InputMapperTest : public testing::Test {
2434protected:
2435 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002436 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437 static const int32_t DEVICE_ID;
2438 static const int32_t DEVICE_GENERATION;
2439 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002440 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002441 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002443 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002445 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002446 std::unique_ptr<InstrumentedInputReader> mReader;
2447 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448
Chris Ye1b0c7342020-07-28 21:57:03 -07002449 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002450 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002452 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002453 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2454 mFakeListener);
2455 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 }
2457
Chris Yea52ade12020-08-27 16:49:20 -07002458 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002459
Chris Yea52ade12020-08-27 16:49:20 -07002460 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461 mFakeListener.clear();
2462 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 }
2464
2465 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002466 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467 }
2468
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002469 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002470 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002471 mReader->requestRefreshConfiguration(changes);
2472 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002473 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002474 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2475 }
2476
arthurhungdcef2dc2020-08-11 14:47:50 +08002477 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2478 const std::string& location, int32_t eventHubId,
2479 Flags<InputDeviceClass> classes) {
2480 InputDeviceIdentifier identifier;
2481 identifier.name = name;
2482 identifier.location = location;
2483 std::shared_ptr<InputDevice> device =
2484 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2485 identifier);
2486 mReader->pushNextDevice(device);
2487 mFakeEventHub->addDevice(eventHubId, name, classes);
2488 mReader->loopOnce();
2489 return device;
2490 }
2491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002492 template <class T, typename... Args>
2493 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002494 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002495 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002497 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002498 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002499 }
2500
2501 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002502 int32_t orientation, const std::string& uniqueId,
2503 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002504 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2505 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002506 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2507 }
2508
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002509 void clearViewports() {
2510 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511 }
2512
arthurhungdcef2dc2020-08-11 14:47:50 +08002513 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514 RawEvent event;
2515 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002516 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002517 event.type = type;
2518 event.code = code;
2519 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002520 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002521 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522 }
2523
2524 static void assertMotionRange(const InputDeviceInfo& info,
2525 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2526 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002527 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2529 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2530 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2531 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2532 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2533 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2534 }
2535
2536 static void assertPointerCoords(const PointerCoords& coords,
2537 float x, float y, float pressure, float size,
2538 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2539 float orientation, float distance) {
2540 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2541 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2542 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2543 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2544 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2545 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2546 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2547 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2548 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2549 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2550 }
2551
Michael Wright17db18e2020-06-26 20:51:44 +01002552 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002554 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555 ASSERT_NEAR(x, actualX, 1);
2556 ASSERT_NEAR(y, actualY, 1);
2557 }
2558};
2559
2560const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002561const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002562const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2564const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002565const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2566 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002567const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568
2569// --- SwitchInputMapperTest ---
2570
2571class SwitchInputMapperTest : public InputMapperTest {
2572protected:
2573};
2574
2575TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002576 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579}
2580
2581TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002582 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002584 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002585 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002586
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002587 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002588 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002589}
2590
2591TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002592 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002594 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2595 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2596 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2597 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598
2599 NotifySwitchArgs args;
2600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2601 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002602 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2603 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604 args.switchMask);
2605 ASSERT_EQ(uint32_t(0), args.policyFlags);
2606}
2607
Chris Ye87143712020-11-10 05:05:58 +00002608// --- VibratorInputMapperTest ---
2609class VibratorInputMapperTest : public InputMapperTest {
2610protected:
2611 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2612};
2613
2614TEST_F(VibratorInputMapperTest, GetSources) {
2615 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2616
2617 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2618}
2619
2620TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2621 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2622
2623 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2624}
2625
2626TEST_F(VibratorInputMapperTest, Vibrate) {
2627 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
2628 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2629
2630 VibrationElement pattern(2);
2631 VibrationSequence sequence(2);
2632 pattern.duration = std::chrono::milliseconds(200);
2633 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2634 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2635 sequence.addElement(pattern);
2636 pattern.duration = std::chrono::milliseconds(500);
2637 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2638 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2639 sequence.addElement(pattern);
2640
2641 std::vector<int64_t> timings = {0, 1};
2642 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2643
2644 ASSERT_FALSE(mapper.isVibrating());
2645 mapper.vibrate(sequence, -1 /* repeat */, 0 /* token */);
2646 ASSERT_TRUE(mapper.isVibrating());
2647}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648
Chris Yef59a2f42020-10-16 12:55:26 -07002649// --- SensorInputMapperTest ---
2650
2651class SensorInputMapperTest : public InputMapperTest {
2652protected:
2653 static const int32_t ACCEL_RAW_MIN;
2654 static const int32_t ACCEL_RAW_MAX;
2655 static const int32_t ACCEL_RAW_FUZZ;
2656 static const int32_t ACCEL_RAW_FLAT;
2657 static const int32_t ACCEL_RAW_RESOLUTION;
2658
2659 static const int32_t GYRO_RAW_MIN;
2660 static const int32_t GYRO_RAW_MAX;
2661 static const int32_t GYRO_RAW_FUZZ;
2662 static const int32_t GYRO_RAW_FLAT;
2663 static const int32_t GYRO_RAW_RESOLUTION;
2664
2665 static const float GRAVITY_MS2_UNIT;
2666 static const float DEGREE_RADIAN_UNIT;
2667
2668 void prepareAccelAxes();
2669 void prepareGyroAxes();
2670 void setAccelProperties();
2671 void setGyroProperties();
2672 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2673};
2674
2675const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2676const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2677const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2678const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2679const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2680
2681const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2682const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2683const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2684const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2685const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2686
2687const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2688const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2689
2690void SensorInputMapperTest::prepareAccelAxes() {
2691 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2692 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2693 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2694 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2695 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2696 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2697}
2698
2699void SensorInputMapperTest::prepareGyroAxes() {
2700 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2701 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2702 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2703 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2704 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2705 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2706}
2707
2708void SensorInputMapperTest::setAccelProperties() {
2709 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2710 /* sensorDataIndex */ 0);
2711 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2712 /* sensorDataIndex */ 1);
2713 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2714 /* sensorDataIndex */ 2);
2715 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2716 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2717 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2718 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2719 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2720}
2721
2722void SensorInputMapperTest::setGyroProperties() {
2723 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2724 /* sensorDataIndex */ 0);
2725 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2726 /* sensorDataIndex */ 1);
2727 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2728 /* sensorDataIndex */ 2);
2729 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2730 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2731 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2732 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2733 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2734}
2735
2736TEST_F(SensorInputMapperTest, GetSources) {
2737 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2738
2739 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2740}
2741
2742TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2743 setAccelProperties();
2744 prepareAccelAxes();
2745 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2746
2747 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2748 std::chrono::microseconds(10000),
2749 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002750 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002751 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, 20000);
2752 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, -20000);
2753 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Z, 40000);
2754 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2755 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2756
2757 NotifySensorArgs args;
2758 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2759 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2760 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2761
2762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2763 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2764 ASSERT_EQ(args.deviceId, DEVICE_ID);
2765 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2766 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2767 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2768 ASSERT_EQ(args.values, values);
2769 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2770}
2771
2772TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2773 setGyroProperties();
2774 prepareGyroAxes();
2775 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2776
2777 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2778 std::chrono::microseconds(10000),
2779 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002780 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002781 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RX, 20000);
2782 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RY, -20000);
2783 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RZ, 40000);
2784 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2785 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2786
2787 NotifySensorArgs args;
2788 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2789 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2790 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2791
2792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2793 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2794 ASSERT_EQ(args.deviceId, DEVICE_ID);
2795 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2796 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2797 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2798 ASSERT_EQ(args.values, values);
2799 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2800}
2801
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802// --- KeyboardInputMapperTest ---
2803
2804class KeyboardInputMapperTest : public InputMapperTest {
2805protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002806 const std::string UNIQUE_ID = "local:0";
2807
2808 void prepareDisplay(int32_t orientation);
2809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002810 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002811 int32_t originalKeyCode, int32_t rotatedKeyCode,
2812 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813};
2814
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002815/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2816 * orientation.
2817 */
2818void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002819 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2820 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002821}
2822
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002823void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002824 int32_t originalScanCode, int32_t originalKeyCode,
2825 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826 NotifyKeyArgs args;
2827
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002828 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2830 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2831 ASSERT_EQ(originalScanCode, args.scanCode);
2832 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002833 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002835 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2837 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2838 ASSERT_EQ(originalScanCode, args.scanCode);
2839 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002840 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841}
2842
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002844 KeyboardInputMapper& mapper =
2845 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2846 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002848 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849}
2850
2851TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2852 const int32_t USAGE_A = 0x070004;
2853 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002854 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2855 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002856 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2857 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2858 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002860 KeyboardInputMapper& mapper =
2861 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2862 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08002863 // Initial metastate to AMETA_NONE.
2864 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2865 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866
2867 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002868 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869 NotifyKeyArgs args;
2870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2871 ASSERT_EQ(DEVICE_ID, args.deviceId);
2872 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2873 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2874 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2875 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2876 ASSERT_EQ(KEY_HOME, args.scanCode);
2877 ASSERT_EQ(AMETA_NONE, args.metaState);
2878 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2879 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2880 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2881
2882 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002883 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2885 ASSERT_EQ(DEVICE_ID, args.deviceId);
2886 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2887 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2888 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2889 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2890 ASSERT_EQ(KEY_HOME, args.scanCode);
2891 ASSERT_EQ(AMETA_NONE, args.metaState);
2892 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2893 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2894 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2895
2896 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002897 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2898 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2900 ASSERT_EQ(DEVICE_ID, args.deviceId);
2901 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2902 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2903 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2904 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2905 ASSERT_EQ(0, args.scanCode);
2906 ASSERT_EQ(AMETA_NONE, args.metaState);
2907 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2908 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2909 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2910
2911 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002912 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2913 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2915 ASSERT_EQ(DEVICE_ID, args.deviceId);
2916 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2917 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2918 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2919 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2920 ASSERT_EQ(0, args.scanCode);
2921 ASSERT_EQ(AMETA_NONE, args.metaState);
2922 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2923 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2924 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2925
2926 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002927 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2928 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2930 ASSERT_EQ(DEVICE_ID, args.deviceId);
2931 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2932 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2933 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2934 ASSERT_EQ(0, args.keyCode);
2935 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2936 ASSERT_EQ(AMETA_NONE, args.metaState);
2937 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2938 ASSERT_EQ(0U, args.policyFlags);
2939 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2940
2941 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002942 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2943 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2945 ASSERT_EQ(DEVICE_ID, args.deviceId);
2946 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2947 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2948 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2949 ASSERT_EQ(0, args.keyCode);
2950 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2951 ASSERT_EQ(AMETA_NONE, args.metaState);
2952 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2953 ASSERT_EQ(0U, args.policyFlags);
2954 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2955}
2956
2957TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002958 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2959 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002960 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2961 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2962 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002964 KeyboardInputMapper& mapper =
2965 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2966 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967
arthurhungc903df12020-08-11 15:08:42 +08002968 // Initial metastate to AMETA_NONE.
2969 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2970 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971
2972 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002973 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 NotifyKeyArgs args;
2975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002978 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979
2980 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002981 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985
2986 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002987 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002990 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991
2992 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002993 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002996 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002997 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998}
2999
3000TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003006 KeyboardInputMapper& mapper =
3007 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3008 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003010 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3012 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3013 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3014 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3015 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3016 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3017 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3018 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3019}
3020
3021TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003022 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3024 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003028 KeyboardInputMapper& mapper =
3029 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3030 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003032 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003033 ASSERT_NO_FATAL_FAILURE(
3034 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3035 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3036 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3037 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3038 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3039 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3040 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003042 clearViewports();
3043 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003044 ASSERT_NO_FATAL_FAILURE(
3045 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3046 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3047 AKEYCODE_DPAD_UP, DISPLAY_ID));
3048 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3049 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3050 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3051 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003053 clearViewports();
3054 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003055 ASSERT_NO_FATAL_FAILURE(
3056 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3057 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3058 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3059 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3060 AKEYCODE_DPAD_UP, DISPLAY_ID));
3061 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3062 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003064 clearViewports();
3065 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003066 ASSERT_NO_FATAL_FAILURE(
3067 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3068 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3069 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3070 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3071 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3072 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3073 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
3075 // Special case: if orientation changes while key is down, we still emit the same keycode
3076 // in the key up as we did in the key down.
3077 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003078 clearViewports();
3079 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003080 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3082 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3083 ASSERT_EQ(KEY_UP, args.scanCode);
3084 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3085
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003086 clearViewports();
3087 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003088 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3091 ASSERT_EQ(KEY_UP, args.scanCode);
3092 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3093}
3094
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003095TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3096 // If the keyboard is not orientation aware,
3097 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 KeyboardInputMapper& mapper =
3101 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3102 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003103 NotifyKeyArgs args;
3104
3105 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003106 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003108 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3110 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3111
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003112 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003113 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003115 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3117 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3118}
3119
3120TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3121 // If the keyboard is orientation aware,
3122 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003124
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003125 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 KeyboardInputMapper& mapper =
3127 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3128 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003129 NotifyKeyArgs args;
3130
3131 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3132 // ^--- already checked by the previous test
3133
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003134 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003135 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003136 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003138 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(DISPLAY_ID, args.displayId);
3141
3142 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003143 clearViewports();
3144 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003145 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003146 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003148 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3150 ASSERT_EQ(newDisplayId, args.displayId);
3151}
3152
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003154 KeyboardInputMapper& mapper =
3155 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3156 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003158 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003159 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003161 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003162 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163}
3164
3165TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003166 KeyboardInputMapper& mapper =
3167 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3168 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003170 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003171 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003173 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003174 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175}
3176
3177TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003178 KeyboardInputMapper& mapper =
3179 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3180 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003182 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183
3184 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3185 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003186 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187 ASSERT_TRUE(flags[0]);
3188 ASSERT_FALSE(flags[1]);
3189}
3190
3191TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003192 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3193 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3194 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3195 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3196 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3197 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003199 KeyboardInputMapper& mapper =
3200 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3201 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003202 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003203 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3204 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205
3206 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003207 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3208 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3209 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
3211 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003212 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3213 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003214 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3215 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3216 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218
3219 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003220 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3221 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003222 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3223 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3224 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003225 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226
3227 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003228 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3229 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003230 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3231 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3232 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003233 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234
3235 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003236 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3237 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003238 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3239 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3240 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003241 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242
3243 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003244 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3245 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003246 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3247 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3248 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250
3251 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003252 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3253 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003254 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3255 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3256 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258}
3259
Chris Yea52ade12020-08-27 16:49:20 -07003260TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3261 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3262 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3263 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3264 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3265
3266 KeyboardInputMapper& mapper =
3267 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3268 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3269
3270 // Initial metastate should be AMETA_NONE as no meta keys added.
3271 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3272 // Meta state should be AMETA_NONE after reset
3273 mapper.reset(ARBITRARY_TIME);
3274 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3275 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3276 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3277 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3278
3279 NotifyKeyArgs args;
3280 // Press button "A"
3281 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
3282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3283 ASSERT_EQ(AMETA_NONE, args.metaState);
3284 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3285 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3286 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3287
3288 // Button up.
3289 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
3290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3291 ASSERT_EQ(AMETA_NONE, args.metaState);
3292 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3293 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3294 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3295}
3296
Arthur Hung2c9a3342019-07-23 14:18:59 +08003297TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3298 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003299 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3300 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3301 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3302 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003303
3304 // keyboard 2.
3305 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003306 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003307 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003308 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003309 std::shared_ptr<InputDevice> device2 =
3310 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3311 Flags<InputDeviceClass>(0));
3312
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003313 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3314 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3315 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3316 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003317
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003318 KeyboardInputMapper& mapper =
3319 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3320 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003323 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003324 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003325 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3326 device2->reset(ARBITRARY_TIME);
3327
3328 // Prepared displays and associated info.
3329 constexpr uint8_t hdmi1 = 0;
3330 constexpr uint8_t hdmi2 = 1;
3331 const std::string SECONDARY_UNIQUE_ID = "local:1";
3332
3333 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3334 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3335
3336 // No associated display viewport found, should disable the device.
3337 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3338 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3339 ASSERT_FALSE(device2->isEnabled());
3340
3341 // Prepare second display.
3342 constexpr int32_t newDisplayId = 2;
3343 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003344 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003345 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003346 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003347 // Default device will reconfigure above, need additional reconfiguration for another device.
3348 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3349 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3350
3351 // Device should be enabled after the associated display is found.
3352 ASSERT_TRUE(mDevice->isEnabled());
3353 ASSERT_TRUE(device2->isEnabled());
3354
3355 // Test pad key events
3356 ASSERT_NO_FATAL_FAILURE(
3357 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3358 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3359 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3360 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3361 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3362 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3363 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3364
3365 ASSERT_NO_FATAL_FAILURE(
3366 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3367 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3368 AKEYCODE_DPAD_RIGHT, newDisplayId));
3369 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3370 AKEYCODE_DPAD_DOWN, newDisplayId));
3371 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3372 AKEYCODE_DPAD_LEFT, newDisplayId));
3373}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374
arthurhungc903df12020-08-11 15:08:42 +08003375TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3376 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3377 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3378 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3379 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3380 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3381 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3382
3383 KeyboardInputMapper& mapper =
3384 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3385 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3386 // Initial metastate to AMETA_NONE.
3387 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3388 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3389
3390 // Initialization should have turned all of the lights off.
3391 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3392 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3393 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3394
3395 // Toggle caps lock on.
3396 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3397 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3398 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3399 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3400
3401 // Toggle num lock on.
3402 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3403 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3404 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3405 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3406
3407 // Toggle scroll lock on.
3408 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3409 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3410 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3411 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3412
3413 mFakeEventHub->removeDevice(EVENTHUB_ID);
3414 mReader->loopOnce();
3415
3416 // keyboard 2 should default toggle keys.
3417 const std::string USB2 = "USB2";
3418 const std::string DEVICE_NAME2 = "KEYBOARD2";
3419 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3420 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3421 std::shared_ptr<InputDevice> device2 =
3422 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3423 Flags<InputDeviceClass>(0));
3424 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3425 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3426 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3427 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3428 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3429 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3430
arthurhung6fe95782020-10-05 22:41:16 +08003431 KeyboardInputMapper& mapper2 =
3432 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3433 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003434 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3435 device2->reset(ARBITRARY_TIME);
3436
3437 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3438 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3439 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003440 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3441 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003442}
3443
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003444// --- KeyboardInputMapperTest_ExternalDevice ---
3445
3446class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3447protected:
Chris Yea52ade12020-08-27 16:49:20 -07003448 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003449};
3450
3451TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003452 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3453 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003454
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003455 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3456 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3457 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3458 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 KeyboardInputMapper& mapper =
3461 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3462 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003463
3464 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3465 NotifyKeyArgs args;
3466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3467 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3468
3469 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3471 ASSERT_EQ(uint32_t(0), args.policyFlags);
3472
3473 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3475 ASSERT_EQ(uint32_t(0), args.policyFlags);
3476
3477 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3479 ASSERT_EQ(uint32_t(0), args.policyFlags);
3480
3481 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3483 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3484
3485 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3487 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3488}
3489
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003490TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003491 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003492
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003496
Powei Fengd041c5d2019-05-03 17:11:33 -07003497 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003498 KeyboardInputMapper& mapper =
3499 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3500 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003501
3502 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3503 NotifyKeyArgs args;
3504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3505 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3506
3507 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3509 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3510
3511 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3513 ASSERT_EQ(uint32_t(0), args.policyFlags);
3514
3515 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3517 ASSERT_EQ(uint32_t(0), args.policyFlags);
3518
3519 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3521 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3522
3523 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3525 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3526}
3527
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528// --- CursorInputMapperTest ---
3529
3530class CursorInputMapperTest : public InputMapperTest {
3531protected:
3532 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3533
Michael Wright17db18e2020-06-26 20:51:44 +01003534 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
Chris Yea52ade12020-08-27 16:49:20 -07003536 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537 InputMapperTest::SetUp();
3538
Michael Wright17db18e2020-06-26 20:51:44 +01003539 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003540 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541 }
3542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003543 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3544 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003545
3546 void prepareDisplay(int32_t orientation) {
3547 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003548 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003549 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3550 orientation, uniqueId, NO_PORT, viewportType);
3551 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552};
3553
3554const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003556void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3557 int32_t originalY, int32_t rotatedX,
3558 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 NotifyMotionArgs args;
3560
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003561 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3562 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3563 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3567 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3568 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3569 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3570}
3571
3572TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003574 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003576 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577}
3578
3579TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003581 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003583 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584}
3585
3586TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003588 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
3590 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003591 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003592
3593 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003594 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3595 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3597 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3598
3599 // When the bounds are set, then there should be a valid motion range.
3600 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3601
3602 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003603 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604
3605 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3606 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3607 1, 800 - 1, 0.0f, 0.0f));
3608 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3609 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3610 2, 480 - 1, 0.0f, 0.0f));
3611 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3612 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3613 0.0f, 1.0f, 0.0f, 0.0f));
3614}
3615
3616TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003618 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619
3620 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003621 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622
3623 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3624 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3625 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3626 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3627 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3628 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3629 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3630 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3631 0.0f, 1.0f, 0.0f, 0.0f));
3632}
3633
3634TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003636 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003637
arthurhungdcef2dc2020-08-11 14:47:50 +08003638 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639
3640 NotifyMotionArgs args;
3641
3642 // Button press.
3643 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003644 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3645 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3647 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3648 ASSERT_EQ(DEVICE_ID, args.deviceId);
3649 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3650 ASSERT_EQ(uint32_t(0), args.policyFlags);
3651 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3652 ASSERT_EQ(0, args.flags);
3653 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3654 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3655 ASSERT_EQ(0, args.edgeFlags);
3656 ASSERT_EQ(uint32_t(1), args.pointerCount);
3657 ASSERT_EQ(0, args.pointerProperties[0].id);
3658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3660 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3661 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3662 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3663 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3664
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3666 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3667 ASSERT_EQ(DEVICE_ID, args.deviceId);
3668 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3669 ASSERT_EQ(uint32_t(0), args.policyFlags);
3670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3671 ASSERT_EQ(0, args.flags);
3672 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3673 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3674 ASSERT_EQ(0, args.edgeFlags);
3675 ASSERT_EQ(uint32_t(1), args.pointerCount);
3676 ASSERT_EQ(0, args.pointerProperties[0].id);
3677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3679 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3680 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3681 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3682 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3683
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003685 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3686 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3688 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3689 ASSERT_EQ(DEVICE_ID, args.deviceId);
3690 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3691 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3693 ASSERT_EQ(0, args.flags);
3694 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3695 ASSERT_EQ(0, args.buttonState);
3696 ASSERT_EQ(0, args.edgeFlags);
3697 ASSERT_EQ(uint32_t(1), args.pointerCount);
3698 ASSERT_EQ(0, args.pointerProperties[0].id);
3699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3701 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3702 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3703 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3704 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3705
3706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3707 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3708 ASSERT_EQ(DEVICE_ID, args.deviceId);
3709 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3710 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3712 ASSERT_EQ(0, args.flags);
3713 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3714 ASSERT_EQ(0, args.buttonState);
3715 ASSERT_EQ(0, args.edgeFlags);
3716 ASSERT_EQ(uint32_t(1), args.pointerCount);
3717 ASSERT_EQ(0, args.pointerProperties[0].id);
3718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3720 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3721 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3722 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3723 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3724}
3725
3726TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729
3730 NotifyMotionArgs args;
3731
3732 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003733 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3734 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3738 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3739
3740 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003741 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3742 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3746 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3747}
3748
3749TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752
3753 NotifyMotionArgs args;
3754
3755 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003756 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3757 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3759 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3761 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3762
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3766 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3767
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003769 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3770 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003772 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3774 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3775
3776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3779 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3780}
3781
3782TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003784 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785
3786 NotifyMotionArgs args;
3787
3788 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003789 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3790 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3791 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3792 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3794 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3796 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3797 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3798
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3802 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3803 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3804
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003806 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3807 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3808 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3812 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3813 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3814
3815 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003816 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3817 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003819 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3821 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3822
3823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3826 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3827}
3828
3829TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003831 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003833 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3835 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3836 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3837 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3838 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3839 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3840 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3841 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3842}
3843
3844TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845 addConfigurationProperty("cursor.mode", "navigation");
3846 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003847 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003849 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3851 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3852 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3853 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3854 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3855 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3856 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3857 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3858
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003859 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
3861 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
3862 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
3863 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
3864 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
3865 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
3866 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
3867 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
3868
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003869 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
3871 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
3872 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
3873 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
3874 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
3875 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
3876 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
3877 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
3878
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003879 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
3881 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
3882 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
3883 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
3884 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
3885 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
3886 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
3887 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
3888}
3889
3890TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003892 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893
3894 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3895 mFakePointerController->setPosition(100, 200);
3896 mFakePointerController->setButtonState(0);
3897
3898 NotifyMotionArgs motionArgs;
3899 NotifyKeyArgs keyArgs;
3900
3901 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003902 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3903 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3905 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3906 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3907 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3909 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3910
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3913 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3914 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3916 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3917
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003918 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3919 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922 ASSERT_EQ(0, motionArgs.buttonState);
3923 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3925 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3926
3927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003928 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 ASSERT_EQ(0, motionArgs.buttonState);
3930 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3932 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3933
3934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003936 ASSERT_EQ(0, motionArgs.buttonState);
3937 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3939 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3940
3941 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003942 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3943 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3944 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3947 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3948 motionArgs.buttonState);
3949 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3950 mFakePointerController->getButtonState());
3951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3952 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3953
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3956 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3957 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3958 mFakePointerController->getButtonState());
3959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3960 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3961
3962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3964 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3965 motionArgs.buttonState);
3966 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3967 mFakePointerController->getButtonState());
3968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3969 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3970
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003971 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3972 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3976 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3978 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3979
3980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003982 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3983 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3985 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3986
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003987 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3988 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003990 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3991 ASSERT_EQ(0, motionArgs.buttonState);
3992 ASSERT_EQ(0, mFakePointerController->getButtonState());
3993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3994 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 -08003995 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3996 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003997
3998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_EQ(0, motionArgs.buttonState);
4000 ASSERT_EQ(0, mFakePointerController->getButtonState());
4001 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4003 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 -08004004
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4006 ASSERT_EQ(0, motionArgs.buttonState);
4007 ASSERT_EQ(0, mFakePointerController->getButtonState());
4008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4010 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4011
4012 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004013 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
4014 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4016 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4017 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004018
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004020 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4022 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4024 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4025
4026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4028 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4029 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4031 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4032
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004033 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
4034 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004036 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037 ASSERT_EQ(0, motionArgs.buttonState);
4038 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4040 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4041
4042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004044 ASSERT_EQ(0, motionArgs.buttonState);
4045 ASSERT_EQ(0, mFakePointerController->getButtonState());
4046
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4048 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4050 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4051 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4052
4053 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004054 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
4055 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4057 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4058 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004059
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004061 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4063 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4065 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4066
4067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4069 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4070 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4072 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4073
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004074 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
4075 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 ASSERT_EQ(0, motionArgs.buttonState);
4079 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4081 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 -08004082
4083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4084 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4085 ASSERT_EQ(0, motionArgs.buttonState);
4086 ASSERT_EQ(0, mFakePointerController->getButtonState());
4087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4088 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4089
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4091 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4092 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4093
4094 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004095 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
4096 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4098 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4099 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004100
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004102 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4104 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4106 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4107
4108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4109 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4110 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4111 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4113 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4114
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004115 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
4116 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119 ASSERT_EQ(0, motionArgs.buttonState);
4120 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4122 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 -08004123
4124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4125 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4126 ASSERT_EQ(0, motionArgs.buttonState);
4127 ASSERT_EQ(0, mFakePointerController->getButtonState());
4128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4129 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4130
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4132 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4133 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4134
4135 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004136 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
4137 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4139 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4140 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004141
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004143 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4145 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4147 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4148
4149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4151 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4152 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4154 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4155
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004156 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
4157 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 ASSERT_EQ(0, motionArgs.buttonState);
4161 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4163 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 -08004164
4165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4166 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4167 ASSERT_EQ(0, motionArgs.buttonState);
4168 ASSERT_EQ(0, mFakePointerController->getButtonState());
4169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4170 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4171
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4173 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4174 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4175}
4176
4177TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004179 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180
4181 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4182 mFakePointerController->setPosition(100, 200);
4183 mFakePointerController->setButtonState(0);
4184
4185 NotifyMotionArgs args;
4186
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004187 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4188 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4189 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004191 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4192 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4194 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 +01004195 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004196}
4197
4198TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004199 addConfigurationProperty("cursor.mode", "pointer");
4200 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004201 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004202
4203 NotifyDeviceResetArgs resetArgs;
4204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4205 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4206 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4207
4208 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4209 mFakePointerController->setPosition(100, 200);
4210 mFakePointerController->setButtonState(0);
4211
4212 NotifyMotionArgs args;
4213
4214 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004215 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4216 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4217 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4219 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4222 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 +01004223 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004224
4225 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004226 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
4227 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4229 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4230 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4232 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4234 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4237 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4238
4239 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004240 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
4241 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4243 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4246 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4248 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4249 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4251 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4252
4253 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004254 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
4255 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
4256 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4258 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4261 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 +01004262 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004263
4264 // Disable pointer capture and check that the device generation got bumped
4265 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004266 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004267 mFakePolicy->setPointerCapture(false);
4268 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004269 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004270
4271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4272 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4273 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4274
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004275 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4276 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4277 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4279 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4282 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 +01004283 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284}
4285
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004286TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004287 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004288
Garfield Tan888a6a42020-01-09 11:39:16 -08004289 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004290 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004291 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4292 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004293 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4294 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004295 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4296 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4297
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004298 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4299 mFakePointerController->setPosition(100, 200);
4300 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004301
4302 NotifyMotionArgs args;
4303 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4304 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4305 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4307 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4308 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4310 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 +01004311 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004312 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4313}
4314
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315// --- TouchInputMapperTest ---
4316
4317class TouchInputMapperTest : public InputMapperTest {
4318protected:
4319 static const int32_t RAW_X_MIN;
4320 static const int32_t RAW_X_MAX;
4321 static const int32_t RAW_Y_MIN;
4322 static const int32_t RAW_Y_MAX;
4323 static const int32_t RAW_TOUCH_MIN;
4324 static const int32_t RAW_TOUCH_MAX;
4325 static const int32_t RAW_TOOL_MIN;
4326 static const int32_t RAW_TOOL_MAX;
4327 static const int32_t RAW_PRESSURE_MIN;
4328 static const int32_t RAW_PRESSURE_MAX;
4329 static const int32_t RAW_ORIENTATION_MIN;
4330 static const int32_t RAW_ORIENTATION_MAX;
4331 static const int32_t RAW_DISTANCE_MIN;
4332 static const int32_t RAW_DISTANCE_MAX;
4333 static const int32_t RAW_TILT_MIN;
4334 static const int32_t RAW_TILT_MAX;
4335 static const int32_t RAW_ID_MIN;
4336 static const int32_t RAW_ID_MAX;
4337 static const int32_t RAW_SLOT_MIN;
4338 static const int32_t RAW_SLOT_MAX;
4339 static const float X_PRECISION;
4340 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004341 static const float X_PRECISION_VIRTUAL;
4342 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343
4344 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004345 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346
4347 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4348
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004349 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004350 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004351
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 enum Axes {
4353 POSITION = 1 << 0,
4354 TOUCH = 1 << 1,
4355 TOOL = 1 << 2,
4356 PRESSURE = 1 << 3,
4357 ORIENTATION = 1 << 4,
4358 MINOR = 1 << 5,
4359 ID = 1 << 6,
4360 DISTANCE = 1 << 7,
4361 TILT = 1 << 8,
4362 SLOT = 1 << 9,
4363 TOOL_TYPE = 1 << 10,
4364 };
4365
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004366 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4367 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004368 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004370 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 int32_t toRawX(float displayX);
4372 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004373 float toCookedX(float rawX, float rawY);
4374 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004376 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004378 float toDisplayY(int32_t rawY, int32_t displayHeight);
4379
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380};
4381
4382const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4383const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4384const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4385const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4386const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4387const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4388const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4389const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004390const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4391const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4393const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4394const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4395const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4396const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4397const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4398const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4399const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4400const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4401const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4402const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4403const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004404const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4405 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4406const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4407 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004408const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4409 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410
4411const float TouchInputMapperTest::GEOMETRIC_SCALE =
4412 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4413 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4414
4415const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4416 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4417 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4418};
4419
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004420void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004421 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4422 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004423}
4424
4425void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4426 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4427 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428}
4429
Santos Cordonfa5cf462017-04-05 10:37:00 -07004430void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004431 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4432 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4433 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004434}
4435
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004437 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4438 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4439 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4440 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441}
4442
Jason Gerecke489fda82012-09-07 17:19:40 -07004443void TouchInputMapperTest::prepareLocationCalibration() {
4444 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4445}
4446
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447int32_t TouchInputMapperTest::toRawX(float displayX) {
4448 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4449}
4450
4451int32_t TouchInputMapperTest::toRawY(float displayY) {
4452 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4453}
4454
Jason Gerecke489fda82012-09-07 17:19:40 -07004455float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4456 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4457 return rawX;
4458}
4459
4460float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4461 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4462 return rawY;
4463}
4464
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004466 return toDisplayX(rawX, DISPLAY_WIDTH);
4467}
4468
4469float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4470 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471}
4472
4473float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004474 return toDisplayY(rawY, DISPLAY_HEIGHT);
4475}
4476
4477float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4478 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479}
4480
4481
4482// --- SingleTouchInputMapperTest ---
4483
4484class SingleTouchInputMapperTest : public TouchInputMapperTest {
4485protected:
4486 void prepareButtons();
4487 void prepareAxes(int axes);
4488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004489 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4490 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4491 void processUp(SingleTouchInputMapper& mappery);
4492 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4493 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4494 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4495 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4496 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4497 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498};
4499
4500void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004501 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502}
4503
4504void SingleTouchInputMapperTest::prepareAxes(int axes) {
4505 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4507 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 }
4509 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4511 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 }
4513 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004514 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4515 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 }
4517 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004518 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4519 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 }
4521 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004522 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4523 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 }
4525}
4526
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004527void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004528 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4529 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4530 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531}
4532
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004533void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004534 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4535 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536}
4537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004538void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004539 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540}
4541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004542void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004543 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544}
4545
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004546void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4547 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004548 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549}
4550
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004551void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004552 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553}
4554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004555void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4556 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004557 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4558 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559}
4560
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004561void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4562 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004563 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564}
4565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004566void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004567 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568}
4569
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571 prepareButtons();
4572 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004573 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004575 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576}
4577
4578TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004579 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4580 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 prepareButtons();
4582 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004583 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004585 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586}
4587
4588TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 prepareButtons();
4590 prepareAxes(POSITION);
4591 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004592 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004594 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595}
4596
4597TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 prepareButtons();
4599 prepareAxes(POSITION);
4600 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004601 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004603 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604}
4605
4606TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607 addConfigurationProperty("touch.deviceType", "touchScreen");
4608 prepareDisplay(DISPLAY_ORIENTATION_0);
4609 prepareButtons();
4610 prepareAxes(POSITION);
4611 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004612 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613
4614 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004615 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616
4617 // Virtual key is down.
4618 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4619 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4620 processDown(mapper, x, y);
4621 processSync(mapper);
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4623
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004624 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625
4626 // Virtual key is up.
4627 processUp(mapper);
4628 processSync(mapper);
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4630
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004631 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632}
4633
4634TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 addConfigurationProperty("touch.deviceType", "touchScreen");
4636 prepareDisplay(DISPLAY_ORIENTATION_0);
4637 prepareButtons();
4638 prepareAxes(POSITION);
4639 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004640 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641
4642 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004643 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644
4645 // Virtual key is down.
4646 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4647 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4648 processDown(mapper, x, y);
4649 processSync(mapper);
4650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4651
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004652 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653
4654 // Virtual key is up.
4655 processUp(mapper);
4656 processSync(mapper);
4657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4658
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004659 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660}
4661
4662TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 addConfigurationProperty("touch.deviceType", "touchScreen");
4664 prepareDisplay(DISPLAY_ORIENTATION_0);
4665 prepareButtons();
4666 prepareAxes(POSITION);
4667 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004668 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669
4670 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4671 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004672 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_TRUE(flags[0]);
4674 ASSERT_FALSE(flags[1]);
4675}
4676
4677TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 addConfigurationProperty("touch.deviceType", "touchScreen");
4679 prepareDisplay(DISPLAY_ORIENTATION_0);
4680 prepareButtons();
4681 prepareAxes(POSITION);
4682 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004683 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684
arthurhungdcef2dc2020-08-11 14:47:50 +08004685 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686
4687 NotifyKeyArgs args;
4688
4689 // Press virtual key.
4690 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4691 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4692 processDown(mapper, x, y);
4693 processSync(mapper);
4694
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4696 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4697 ASSERT_EQ(DEVICE_ID, args.deviceId);
4698 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4699 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4700 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4701 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4702 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4703 ASSERT_EQ(KEY_HOME, args.scanCode);
4704 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4705 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4706
4707 // Release virtual key.
4708 processUp(mapper);
4709 processSync(mapper);
4710
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4712 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4713 ASSERT_EQ(DEVICE_ID, args.deviceId);
4714 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4715 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4716 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4717 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4718 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4719 ASSERT_EQ(KEY_HOME, args.scanCode);
4720 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4721 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4722
4723 // Should not have sent any motions.
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4725}
4726
4727TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 addConfigurationProperty("touch.deviceType", "touchScreen");
4729 prepareDisplay(DISPLAY_ORIENTATION_0);
4730 prepareButtons();
4731 prepareAxes(POSITION);
4732 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004733 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734
arthurhungdcef2dc2020-08-11 14:47:50 +08004735 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736
4737 NotifyKeyArgs keyArgs;
4738
4739 // Press virtual key.
4740 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4741 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4742 processDown(mapper, x, y);
4743 processSync(mapper);
4744
4745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4746 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4747 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4748 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4749 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4750 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4751 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4752 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4753 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4754 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4755 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4756
4757 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4758 // into the display area.
4759 y -= 100;
4760 processMove(mapper, x, y);
4761 processSync(mapper);
4762
4763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4764 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4765 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4766 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4767 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4768 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4769 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4770 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4771 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4772 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4774 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4775
4776 NotifyMotionArgs motionArgs;
4777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4778 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4779 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4780 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4781 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4782 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4783 ASSERT_EQ(0, motionArgs.flags);
4784 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4785 ASSERT_EQ(0, motionArgs.buttonState);
4786 ASSERT_EQ(0, motionArgs.edgeFlags);
4787 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4788 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4791 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4792 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4793 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4794 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4795
4796 // Keep moving out of bounds. Should generate a pointer move.
4797 y -= 50;
4798 processMove(mapper, x, y);
4799 processSync(mapper);
4800
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4803 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4804 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4805 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4807 ASSERT_EQ(0, motionArgs.flags);
4808 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4809 ASSERT_EQ(0, motionArgs.buttonState);
4810 ASSERT_EQ(0, motionArgs.edgeFlags);
4811 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4812 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4815 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4816 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4817 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4818 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4819
4820 // Release out of bounds. Should generate a pointer up.
4821 processUp(mapper);
4822 processSync(mapper);
4823
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4826 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4827 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4828 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4829 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4830 ASSERT_EQ(0, motionArgs.flags);
4831 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4832 ASSERT_EQ(0, motionArgs.buttonState);
4833 ASSERT_EQ(0, motionArgs.edgeFlags);
4834 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4835 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4838 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4839 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4840 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4841 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4842
4843 // Should not have sent any more keys or motions.
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4846}
4847
4848TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849 addConfigurationProperty("touch.deviceType", "touchScreen");
4850 prepareDisplay(DISPLAY_ORIENTATION_0);
4851 prepareButtons();
4852 prepareAxes(POSITION);
4853 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004854 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855
arthurhungdcef2dc2020-08-11 14:47:50 +08004856 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857
4858 NotifyMotionArgs motionArgs;
4859
4860 // Initially go down out of bounds.
4861 int32_t x = -10;
4862 int32_t y = -10;
4863 processDown(mapper, x, y);
4864 processSync(mapper);
4865
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4867
4868 // Move into the display area. Should generate a pointer down.
4869 x = 50;
4870 y = 75;
4871 processMove(mapper, x, y);
4872 processSync(mapper);
4873
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4875 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4876 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4877 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4878 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4880 ASSERT_EQ(0, motionArgs.flags);
4881 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4882 ASSERT_EQ(0, motionArgs.buttonState);
4883 ASSERT_EQ(0, motionArgs.edgeFlags);
4884 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4885 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4888 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4889 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4890 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4891 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4892
4893 // Release. Should generate a pointer up.
4894 processUp(mapper);
4895 processSync(mapper);
4896
4897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4898 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4899 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4900 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4901 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4902 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4903 ASSERT_EQ(0, motionArgs.flags);
4904 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4905 ASSERT_EQ(0, motionArgs.buttonState);
4906 ASSERT_EQ(0, motionArgs.edgeFlags);
4907 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4908 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4911 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4912 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4913 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4914 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4915
4916 // Should not have sent any more keys or motions.
4917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4919}
4920
Santos Cordonfa5cf462017-04-05 10:37:00 -07004921TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004922 addConfigurationProperty("touch.deviceType", "touchScreen");
4923 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4924
4925 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4926 prepareButtons();
4927 prepareAxes(POSITION);
4928 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004929 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004930
arthurhungdcef2dc2020-08-11 14:47:50 +08004931 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004932
4933 NotifyMotionArgs motionArgs;
4934
4935 // Down.
4936 int32_t x = 100;
4937 int32_t y = 125;
4938 processDown(mapper, x, y);
4939 processSync(mapper);
4940
4941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4942 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4943 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4944 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4945 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4946 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4948 ASSERT_EQ(0, motionArgs.flags);
4949 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4950 ASSERT_EQ(0, motionArgs.buttonState);
4951 ASSERT_EQ(0, motionArgs.edgeFlags);
4952 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4953 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4956 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4957 1, 0, 0, 0, 0, 0, 0, 0));
4958 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4959 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4960 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4961
4962 // Move.
4963 x += 50;
4964 y += 75;
4965 processMove(mapper, x, y);
4966 processSync(mapper);
4967
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4970 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4971 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4972 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4973 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4975 ASSERT_EQ(0, motionArgs.flags);
4976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4977 ASSERT_EQ(0, motionArgs.buttonState);
4978 ASSERT_EQ(0, motionArgs.edgeFlags);
4979 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4980 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4981 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4983 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4984 1, 0, 0, 0, 0, 0, 0, 0));
4985 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4986 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4987 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4988
4989 // Up.
4990 processUp(mapper);
4991 processSync(mapper);
4992
4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4995 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4996 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4997 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4998 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4999 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5000 ASSERT_EQ(0, motionArgs.flags);
5001 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5002 ASSERT_EQ(0, motionArgs.buttonState);
5003 ASSERT_EQ(0, motionArgs.edgeFlags);
5004 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5005 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5006 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5008 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5009 1, 0, 0, 0, 0, 0, 0, 0));
5010 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5011 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5012 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5013
5014 // Should not have sent any more keys or motions.
5015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5017}
5018
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 addConfigurationProperty("touch.deviceType", "touchScreen");
5021 prepareDisplay(DISPLAY_ORIENTATION_0);
5022 prepareButtons();
5023 prepareAxes(POSITION);
5024 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005025 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026
arthurhungdcef2dc2020-08-11 14:47:50 +08005027 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028
5029 NotifyMotionArgs motionArgs;
5030
5031 // Down.
5032 int32_t x = 100;
5033 int32_t y = 125;
5034 processDown(mapper, x, y);
5035 processSync(mapper);
5036
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5038 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5039 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5040 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5041 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5043 ASSERT_EQ(0, motionArgs.flags);
5044 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5045 ASSERT_EQ(0, motionArgs.buttonState);
5046 ASSERT_EQ(0, motionArgs.edgeFlags);
5047 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5048 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5051 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5052 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5053 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5054 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5055
5056 // Move.
5057 x += 50;
5058 y += 75;
5059 processMove(mapper, x, y);
5060 processSync(mapper);
5061
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5063 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5064 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5065 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5066 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5068 ASSERT_EQ(0, motionArgs.flags);
5069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5070 ASSERT_EQ(0, motionArgs.buttonState);
5071 ASSERT_EQ(0, motionArgs.edgeFlags);
5072 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5073 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5076 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5077 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5078 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5080
5081 // Up.
5082 processUp(mapper);
5083 processSync(mapper);
5084
5085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5086 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5087 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5088 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5089 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5090 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5091 ASSERT_EQ(0, motionArgs.flags);
5092 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5093 ASSERT_EQ(0, motionArgs.buttonState);
5094 ASSERT_EQ(0, motionArgs.edgeFlags);
5095 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5096 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5099 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5100 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5101 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5102 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5103
5104 // Should not have sent any more keys or motions.
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5107}
5108
5109TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 addConfigurationProperty("touch.deviceType", "touchScreen");
5111 prepareButtons();
5112 prepareAxes(POSITION);
5113 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005114 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115
5116 NotifyMotionArgs args;
5117
5118 // Rotation 90.
5119 prepareDisplay(DISPLAY_ORIENTATION_90);
5120 processDown(mapper, toRawX(50), toRawY(75));
5121 processSync(mapper);
5122
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5124 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5125 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5126
5127 processUp(mapper);
5128 processSync(mapper);
5129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5130}
5131
5132TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133 addConfigurationProperty("touch.deviceType", "touchScreen");
5134 prepareButtons();
5135 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005136 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137
5138 NotifyMotionArgs args;
5139
5140 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005141 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 prepareDisplay(DISPLAY_ORIENTATION_0);
5143 processDown(mapper, toRawX(50), toRawY(75));
5144 processSync(mapper);
5145
5146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5147 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5148 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5149
5150 processUp(mapper);
5151 processSync(mapper);
5152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5153
5154 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005155 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156 prepareDisplay(DISPLAY_ORIENTATION_90);
5157 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5158 processSync(mapper);
5159
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5161 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5162 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5163
5164 processUp(mapper);
5165 processSync(mapper);
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5167
5168 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005169 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170 prepareDisplay(DISPLAY_ORIENTATION_180);
5171 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5172 processSync(mapper);
5173
5174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5175 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5176 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5177
5178 processUp(mapper);
5179 processSync(mapper);
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5181
5182 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005183 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184 prepareDisplay(DISPLAY_ORIENTATION_270);
5185 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5186 processSync(mapper);
5187
5188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5189 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5190 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5191
5192 processUp(mapper);
5193 processSync(mapper);
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5195}
5196
5197TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198 addConfigurationProperty("touch.deviceType", "touchScreen");
5199 prepareDisplay(DISPLAY_ORIENTATION_0);
5200 prepareButtons();
5201 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005202 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203
5204 // These calculations are based on the input device calibration documentation.
5205 int32_t rawX = 100;
5206 int32_t rawY = 200;
5207 int32_t rawPressure = 10;
5208 int32_t rawToolMajor = 12;
5209 int32_t rawDistance = 2;
5210 int32_t rawTiltX = 30;
5211 int32_t rawTiltY = 110;
5212
5213 float x = toDisplayX(rawX);
5214 float y = toDisplayY(rawY);
5215 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5216 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5217 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5218 float distance = float(rawDistance);
5219
5220 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5221 float tiltScale = M_PI / 180;
5222 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5223 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5224 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5225 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5226
5227 processDown(mapper, rawX, rawY);
5228 processPressure(mapper, rawPressure);
5229 processToolMajor(mapper, rawToolMajor);
5230 processDistance(mapper, rawDistance);
5231 processTilt(mapper, rawTiltX, rawTiltY);
5232 processSync(mapper);
5233
5234 NotifyMotionArgs args;
5235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5237 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5238 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5239}
5240
Jason Gerecke489fda82012-09-07 17:19:40 -07005241TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005242 addConfigurationProperty("touch.deviceType", "touchScreen");
5243 prepareDisplay(DISPLAY_ORIENTATION_0);
5244 prepareLocationCalibration();
5245 prepareButtons();
5246 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005247 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005248
5249 int32_t rawX = 100;
5250 int32_t rawY = 200;
5251
5252 float x = toDisplayX(toCookedX(rawX, rawY));
5253 float y = toDisplayY(toCookedY(rawX, rawY));
5254
5255 processDown(mapper, rawX, rawY);
5256 processSync(mapper);
5257
5258 NotifyMotionArgs args;
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5261 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5262}
5263
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005265 addConfigurationProperty("touch.deviceType", "touchScreen");
5266 prepareDisplay(DISPLAY_ORIENTATION_0);
5267 prepareButtons();
5268 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005269 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270
5271 NotifyMotionArgs motionArgs;
5272 NotifyKeyArgs keyArgs;
5273
5274 processDown(mapper, 100, 200);
5275 processSync(mapper);
5276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5278 ASSERT_EQ(0, motionArgs.buttonState);
5279
5280 // press BTN_LEFT, release BTN_LEFT
5281 processKey(mapper, BTN_LEFT, 1);
5282 processSync(mapper);
5283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5285 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5286
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5289 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5290
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291 processKey(mapper, BTN_LEFT, 0);
5292 processSync(mapper);
5293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005296
5297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005299 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300
5301 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5302 processKey(mapper, BTN_RIGHT, 1);
5303 processKey(mapper, BTN_MIDDLE, 1);
5304 processSync(mapper);
5305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5307 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5308 motionArgs.buttonState);
5309
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5311 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5312 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5313
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5316 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5317 motionArgs.buttonState);
5318
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 processKey(mapper, BTN_RIGHT, 0);
5320 processSync(mapper);
5321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005324
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005327 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328
5329 processKey(mapper, BTN_MIDDLE, 0);
5330 processSync(mapper);
5331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005334
5335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005337 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338
5339 // press BTN_BACK, release BTN_BACK
5340 processKey(mapper, BTN_BACK, 1);
5341 processSync(mapper);
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5343 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5344 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005345
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005348 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5349
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5352 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353
5354 processKey(mapper, BTN_BACK, 0);
5355 processSync(mapper);
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005359
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005362 ASSERT_EQ(0, motionArgs.buttonState);
5363
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5365 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5366 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5367
5368 // press BTN_SIDE, release BTN_SIDE
5369 processKey(mapper, BTN_SIDE, 1);
5370 processSync(mapper);
5371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5372 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5373 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005374
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005377 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5378
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5381 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382
5383 processKey(mapper, BTN_SIDE, 0);
5384 processSync(mapper);
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005386 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005388
5389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005391 ASSERT_EQ(0, motionArgs.buttonState);
5392
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5395 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5396
5397 // press BTN_FORWARD, release BTN_FORWARD
5398 processKey(mapper, BTN_FORWARD, 1);
5399 processSync(mapper);
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5402 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005403
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005406 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5407
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5410 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411
5412 processKey(mapper, BTN_FORWARD, 0);
5413 processSync(mapper);
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005417
5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005420 ASSERT_EQ(0, motionArgs.buttonState);
5421
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5423 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5424 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5425
5426 // press BTN_EXTRA, release BTN_EXTRA
5427 processKey(mapper, BTN_EXTRA, 1);
5428 processSync(mapper);
5429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5431 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005432
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005435 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5436
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5439 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440
5441 processKey(mapper, BTN_EXTRA, 0);
5442 processSync(mapper);
5443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005444 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005446
5447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005449 ASSERT_EQ(0, motionArgs.buttonState);
5450
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5452 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5453 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5454
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5456
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457 // press BTN_STYLUS, release BTN_STYLUS
5458 processKey(mapper, BTN_STYLUS, 1);
5459 processSync(mapper);
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005462 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5463
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5466 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467
5468 processKey(mapper, BTN_STYLUS, 0);
5469 processSync(mapper);
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005473
5474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005476 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477
5478 // press BTN_STYLUS2, release BTN_STYLUS2
5479 processKey(mapper, BTN_STYLUS2, 1);
5480 processSync(mapper);
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005483 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5484
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5487 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488
5489 processKey(mapper, BTN_STYLUS2, 0);
5490 processSync(mapper);
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005492 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005494
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005497 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498
5499 // release touch
5500 processUp(mapper);
5501 processSync(mapper);
5502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5503 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5504 ASSERT_EQ(0, motionArgs.buttonState);
5505}
5506
5507TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 addConfigurationProperty("touch.deviceType", "touchScreen");
5509 prepareDisplay(DISPLAY_ORIENTATION_0);
5510 prepareButtons();
5511 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005512 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513
5514 NotifyMotionArgs motionArgs;
5515
5516 // default tool type is finger
5517 processDown(mapper, 100, 200);
5518 processSync(mapper);
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5522
5523 // eraser
5524 processKey(mapper, BTN_TOOL_RUBBER, 1);
5525 processSync(mapper);
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5529
5530 // stylus
5531 processKey(mapper, BTN_TOOL_RUBBER, 0);
5532 processKey(mapper, BTN_TOOL_PEN, 1);
5533 processSync(mapper);
5534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5537
5538 // brush
5539 processKey(mapper, BTN_TOOL_PEN, 0);
5540 processKey(mapper, BTN_TOOL_BRUSH, 1);
5541 processSync(mapper);
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5545
5546 // pencil
5547 processKey(mapper, BTN_TOOL_BRUSH, 0);
5548 processKey(mapper, BTN_TOOL_PENCIL, 1);
5549 processSync(mapper);
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5553
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005554 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555 processKey(mapper, BTN_TOOL_PENCIL, 0);
5556 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5557 processSync(mapper);
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5561
5562 // mouse
5563 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5564 processKey(mapper, BTN_TOOL_MOUSE, 1);
5565 processSync(mapper);
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5569
5570 // lens
5571 processKey(mapper, BTN_TOOL_MOUSE, 0);
5572 processKey(mapper, BTN_TOOL_LENS, 1);
5573 processSync(mapper);
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5577
5578 // double-tap
5579 processKey(mapper, BTN_TOOL_LENS, 0);
5580 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5581 processSync(mapper);
5582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5585
5586 // triple-tap
5587 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5588 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5589 processSync(mapper);
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5593
5594 // quad-tap
5595 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5596 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5597 processSync(mapper);
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5601
5602 // finger
5603 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5604 processKey(mapper, BTN_TOOL_FINGER, 1);
5605 processSync(mapper);
5606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5609
5610 // stylus trumps finger
5611 processKey(mapper, BTN_TOOL_PEN, 1);
5612 processSync(mapper);
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5616
5617 // eraser trumps stylus
5618 processKey(mapper, BTN_TOOL_RUBBER, 1);
5619 processSync(mapper);
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5623
5624 // mouse trumps eraser
5625 processKey(mapper, BTN_TOOL_MOUSE, 1);
5626 processSync(mapper);
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5630
5631 // back to default tool type
5632 processKey(mapper, BTN_TOOL_MOUSE, 0);
5633 processKey(mapper, BTN_TOOL_RUBBER, 0);
5634 processKey(mapper, BTN_TOOL_PEN, 0);
5635 processKey(mapper, BTN_TOOL_FINGER, 0);
5636 processSync(mapper);
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5640}
5641
5642TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005643 addConfigurationProperty("touch.deviceType", "touchScreen");
5644 prepareDisplay(DISPLAY_ORIENTATION_0);
5645 prepareButtons();
5646 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005647 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005648 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649
5650 NotifyMotionArgs motionArgs;
5651
5652 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5653 processKey(mapper, BTN_TOOL_FINGER, 1);
5654 processMove(mapper, 100, 200);
5655 processSync(mapper);
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5657 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5659 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5660
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5664 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5665
5666 // move a little
5667 processMove(mapper, 150, 250);
5668 processSync(mapper);
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5670 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5672 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5673
5674 // down when BTN_TOUCH is pressed, pressure defaults to 1
5675 processKey(mapper, BTN_TOUCH, 1);
5676 processSync(mapper);
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5680 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5681
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5683 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5685 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5686
5687 // up when BTN_TOUCH is released, hover restored
5688 processKey(mapper, BTN_TOUCH, 0);
5689 processSync(mapper);
5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5691 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5693 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5696 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5698 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5699
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5703 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5704
5705 // exit hover when pointer goes away
5706 processKey(mapper, BTN_TOOL_FINGER, 0);
5707 processSync(mapper);
5708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5709 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5711 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5712}
5713
5714TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715 addConfigurationProperty("touch.deviceType", "touchScreen");
5716 prepareDisplay(DISPLAY_ORIENTATION_0);
5717 prepareButtons();
5718 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005719 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720
5721 NotifyMotionArgs motionArgs;
5722
5723 // initially hovering because pressure is 0
5724 processDown(mapper, 100, 200);
5725 processPressure(mapper, 0);
5726 processSync(mapper);
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5728 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5730 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5731
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5735 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5736
5737 // move a little
5738 processMove(mapper, 150, 250);
5739 processSync(mapper);
5740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5743 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5744
5745 // down when pressure is non-zero
5746 processPressure(mapper, RAW_PRESSURE_MAX);
5747 processSync(mapper);
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5749 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5751 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5752
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5754 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5756 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5757
5758 // up when pressure becomes 0, hover restored
5759 processPressure(mapper, 0);
5760 processSync(mapper);
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5762 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5764 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5765
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5767 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5769 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5770
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5772 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5774 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5775
5776 // exit hover when pointer goes away
5777 processUp(mapper);
5778 processSync(mapper);
5779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5782 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5783}
5784
Michael Wrightd02c5b62014-02-10 15:10:22 -08005785// --- MultiTouchInputMapperTest ---
5786
5787class MultiTouchInputMapperTest : public TouchInputMapperTest {
5788protected:
5789 void prepareAxes(int axes);
5790
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005791 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5792 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5793 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5794 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5795 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5796 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5797 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5798 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5799 void processId(MultiTouchInputMapper& mapper, int32_t id);
5800 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5801 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5802 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5803 void processMTSync(MultiTouchInputMapper& mapper);
5804 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805};
5806
5807void MultiTouchInputMapperTest::prepareAxes(int axes) {
5808 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005809 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5810 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811 }
5812 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005813 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5814 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005816 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5817 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818 }
5819 }
5820 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005821 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5822 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005824 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5825 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826 }
5827 }
5828 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005829 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5830 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831 }
5832 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005833 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5834 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 }
5836 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005837 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5838 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839 }
5840 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005841 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5842 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843 }
5844 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005845 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5846 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 }
5848 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005849 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850 }
5851}
5852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005853void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5854 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005855 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5856 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857}
5858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005859void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5860 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005861 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862}
5863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005864void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5865 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005866 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867}
5868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005869void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005870 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871}
5872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005873void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005874 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875}
5876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005877void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5878 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005879 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005880}
5881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005882void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005883 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884}
5885
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005886void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005887 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888}
5889
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005890void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005891 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892}
5893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005894void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005895 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896}
5897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005898void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005899 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900}
5901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005902void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5903 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005904 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905}
5906
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005907void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005908 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909}
5910
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005911void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08005912 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913}
5914
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 addConfigurationProperty("touch.deviceType", "touchScreen");
5917 prepareDisplay(DISPLAY_ORIENTATION_0);
5918 prepareAxes(POSITION);
5919 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921
arthurhungdcef2dc2020-08-11 14:47:50 +08005922 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923
5924 NotifyMotionArgs motionArgs;
5925
5926 // Two fingers down at once.
5927 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5928 processPosition(mapper, x1, y1);
5929 processMTSync(mapper);
5930 processPosition(mapper, x2, y2);
5931 processMTSync(mapper);
5932 processSync(mapper);
5933
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5936 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5937 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5938 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5940 ASSERT_EQ(0, motionArgs.flags);
5941 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5942 ASSERT_EQ(0, motionArgs.buttonState);
5943 ASSERT_EQ(0, motionArgs.edgeFlags);
5944 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5945 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5948 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5949 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5950 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5951 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5952
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5954 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5955 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5956 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5957 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5959 motionArgs.action);
5960 ASSERT_EQ(0, motionArgs.flags);
5961 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5962 ASSERT_EQ(0, motionArgs.buttonState);
5963 ASSERT_EQ(0, motionArgs.edgeFlags);
5964 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5965 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5967 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5970 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5972 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5973 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5974 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5975 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5976
5977 // Move.
5978 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5979 processPosition(mapper, x1, y1);
5980 processMTSync(mapper);
5981 processPosition(mapper, x2, y2);
5982 processMTSync(mapper);
5983 processSync(mapper);
5984
5985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5986 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5987 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5988 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5989 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5991 ASSERT_EQ(0, motionArgs.flags);
5992 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5993 ASSERT_EQ(0, motionArgs.buttonState);
5994 ASSERT_EQ(0, motionArgs.edgeFlags);
5995 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5996 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5998 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6001 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6003 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6004 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6005 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6006 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6007
6008 // First finger up.
6009 x2 += 15; y2 -= 20;
6010 processPosition(mapper, x2, y2);
6011 processMTSync(mapper);
6012 processSync(mapper);
6013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6015 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6016 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6017 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6018 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6019 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6020 motionArgs.action);
6021 ASSERT_EQ(0, motionArgs.flags);
6022 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6023 ASSERT_EQ(0, motionArgs.buttonState);
6024 ASSERT_EQ(0, motionArgs.edgeFlags);
6025 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6026 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6028 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6031 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6033 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6034 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6035 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6036 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6037
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6040 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6041 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6042 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6044 ASSERT_EQ(0, motionArgs.flags);
6045 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6046 ASSERT_EQ(0, motionArgs.buttonState);
6047 ASSERT_EQ(0, motionArgs.edgeFlags);
6048 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6049 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6052 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6053 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6054 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6055 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6056
6057 // Move.
6058 x2 += 20; y2 -= 25;
6059 processPosition(mapper, x2, y2);
6060 processMTSync(mapper);
6061 processSync(mapper);
6062
6063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6064 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6065 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6066 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6067 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6069 ASSERT_EQ(0, motionArgs.flags);
6070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6071 ASSERT_EQ(0, motionArgs.buttonState);
6072 ASSERT_EQ(0, motionArgs.edgeFlags);
6073 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6074 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6077 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6078 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6079 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6081
6082 // New finger down.
6083 int32_t x3 = 700, y3 = 300;
6084 processPosition(mapper, x2, y2);
6085 processMTSync(mapper);
6086 processPosition(mapper, x3, y3);
6087 processMTSync(mapper);
6088 processSync(mapper);
6089
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6091 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6092 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6093 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6094 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6095 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6096 motionArgs.action);
6097 ASSERT_EQ(0, motionArgs.flags);
6098 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6099 ASSERT_EQ(0, motionArgs.buttonState);
6100 ASSERT_EQ(0, motionArgs.edgeFlags);
6101 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6102 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6104 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6107 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6109 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6110 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6111 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6112 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6113
6114 // Second finger up.
6115 x3 += 30; y3 -= 20;
6116 processPosition(mapper, x3, y3);
6117 processMTSync(mapper);
6118 processSync(mapper);
6119
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6121 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6122 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6123 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6124 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6125 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6126 motionArgs.action);
6127 ASSERT_EQ(0, motionArgs.flags);
6128 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6129 ASSERT_EQ(0, motionArgs.buttonState);
6130 ASSERT_EQ(0, motionArgs.edgeFlags);
6131 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6132 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6134 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6137 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6139 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6140 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6141 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6142 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6143
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6146 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6147 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6148 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6150 ASSERT_EQ(0, motionArgs.flags);
6151 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6152 ASSERT_EQ(0, motionArgs.buttonState);
6153 ASSERT_EQ(0, motionArgs.edgeFlags);
6154 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6155 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6158 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6159 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6160 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6161 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6162
6163 // Last finger up.
6164 processMTSync(mapper);
6165 processSync(mapper);
6166
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6168 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6169 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6170 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6171 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6173 ASSERT_EQ(0, motionArgs.flags);
6174 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6175 ASSERT_EQ(0, motionArgs.buttonState);
6176 ASSERT_EQ(0, motionArgs.edgeFlags);
6177 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6178 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6181 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6182 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6183 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6185
6186 // Should not have sent any more keys or motions.
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6189}
6190
6191TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192 addConfigurationProperty("touch.deviceType", "touchScreen");
6193 prepareDisplay(DISPLAY_ORIENTATION_0);
6194 prepareAxes(POSITION | ID);
6195 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006196 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197
arthurhungdcef2dc2020-08-11 14:47:50 +08006198 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199
6200 NotifyMotionArgs motionArgs;
6201
6202 // Two fingers down at once.
6203 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6204 processPosition(mapper, x1, y1);
6205 processId(mapper, 1);
6206 processMTSync(mapper);
6207 processPosition(mapper, x2, y2);
6208 processId(mapper, 2);
6209 processMTSync(mapper);
6210 processSync(mapper);
6211
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6214 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6215 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6218 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6219
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6222 motionArgs.action);
6223 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6224 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6226 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6229 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6231 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6232
6233 // Move.
6234 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6235 processPosition(mapper, x1, y1);
6236 processId(mapper, 1);
6237 processMTSync(mapper);
6238 processPosition(mapper, x2, y2);
6239 processId(mapper, 2);
6240 processMTSync(mapper);
6241 processSync(mapper);
6242
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6245 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6246 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6248 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6251 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6253 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6254
6255 // First finger up.
6256 x2 += 15; y2 -= 20;
6257 processPosition(mapper, x2, y2);
6258 processId(mapper, 2);
6259 processMTSync(mapper);
6260 processSync(mapper);
6261
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6264 motionArgs.action);
6265 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6266 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6268 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6271 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6273 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6274
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6277 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6278 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6281 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6282
6283 // Move.
6284 x2 += 20; y2 -= 25;
6285 processPosition(mapper, x2, y2);
6286 processId(mapper, 2);
6287 processMTSync(mapper);
6288 processSync(mapper);
6289
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6292 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6293 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6296 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6297
6298 // New finger down.
6299 int32_t x3 = 700, y3 = 300;
6300 processPosition(mapper, x2, y2);
6301 processId(mapper, 2);
6302 processMTSync(mapper);
6303 processPosition(mapper, x3, y3);
6304 processId(mapper, 3);
6305 processMTSync(mapper);
6306 processSync(mapper);
6307
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6310 motionArgs.action);
6311 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6312 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6314 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6317 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6319 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6320
6321 // Second finger up.
6322 x3 += 30; y3 -= 20;
6323 processPosition(mapper, x3, y3);
6324 processId(mapper, 3);
6325 processMTSync(mapper);
6326 processSync(mapper);
6327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6330 motionArgs.action);
6331 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6332 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6334 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6337 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6339 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6340
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6343 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6344 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6347 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6348
6349 // Last finger up.
6350 processMTSync(mapper);
6351 processSync(mapper);
6352
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6355 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6356 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6359 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6360
6361 // Should not have sent any more keys or motions.
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6364}
6365
6366TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 addConfigurationProperty("touch.deviceType", "touchScreen");
6368 prepareDisplay(DISPLAY_ORIENTATION_0);
6369 prepareAxes(POSITION | ID | SLOT);
6370 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006371 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372
arthurhungdcef2dc2020-08-11 14:47:50 +08006373 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374
6375 NotifyMotionArgs motionArgs;
6376
6377 // Two fingers down at once.
6378 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6379 processPosition(mapper, x1, y1);
6380 processId(mapper, 1);
6381 processSlot(mapper, 1);
6382 processPosition(mapper, x2, y2);
6383 processId(mapper, 2);
6384 processSync(mapper);
6385
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6388 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6389 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6392 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6393
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6396 motionArgs.action);
6397 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6398 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6400 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6403 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6405 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6406
6407 // Move.
6408 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6409 processSlot(mapper, 0);
6410 processPosition(mapper, x1, y1);
6411 processSlot(mapper, 1);
6412 processPosition(mapper, x2, y2);
6413 processSync(mapper);
6414
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6417 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6418 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6420 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6423 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6425 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6426
6427 // First finger up.
6428 x2 += 15; y2 -= 20;
6429 processSlot(mapper, 0);
6430 processId(mapper, -1);
6431 processSlot(mapper, 1);
6432 processPosition(mapper, x2, y2);
6433 processSync(mapper);
6434
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6436 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6437 motionArgs.action);
6438 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6441 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6444 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6446 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6447
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6450 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6451 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6454 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6455
6456 // Move.
6457 x2 += 20; y2 -= 25;
6458 processPosition(mapper, x2, y2);
6459 processSync(mapper);
6460
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6463 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6464 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6467 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6468
6469 // New finger down.
6470 int32_t x3 = 700, y3 = 300;
6471 processPosition(mapper, x2, y2);
6472 processSlot(mapper, 0);
6473 processId(mapper, 3);
6474 processPosition(mapper, x3, y3);
6475 processSync(mapper);
6476
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6479 motionArgs.action);
6480 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6483 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6488 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6489
6490 // Second finger up.
6491 x3 += 30; y3 -= 20;
6492 processSlot(mapper, 1);
6493 processId(mapper, -1);
6494 processSlot(mapper, 0);
6495 processPosition(mapper, x3, y3);
6496 processSync(mapper);
6497
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6500 motionArgs.action);
6501 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6502 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6504 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6507 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6509 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6510
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6513 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6517 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6518
6519 // Last finger up.
6520 processId(mapper, -1);
6521 processSync(mapper);
6522
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6525 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6526 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6529 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6530
6531 // Should not have sent any more keys or motions.
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6534}
6535
6536TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537 addConfigurationProperty("touch.deviceType", "touchScreen");
6538 prepareDisplay(DISPLAY_ORIENTATION_0);
6539 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006541
6542 // These calculations are based on the input device calibration documentation.
6543 int32_t rawX = 100;
6544 int32_t rawY = 200;
6545 int32_t rawTouchMajor = 7;
6546 int32_t rawTouchMinor = 6;
6547 int32_t rawToolMajor = 9;
6548 int32_t rawToolMinor = 8;
6549 int32_t rawPressure = 11;
6550 int32_t rawDistance = 0;
6551 int32_t rawOrientation = 3;
6552 int32_t id = 5;
6553
6554 float x = toDisplayX(rawX);
6555 float y = toDisplayY(rawY);
6556 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6557 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6558 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6559 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6560 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6561 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6562 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6563 float distance = float(rawDistance);
6564
6565 processPosition(mapper, rawX, rawY);
6566 processTouchMajor(mapper, rawTouchMajor);
6567 processTouchMinor(mapper, rawTouchMinor);
6568 processToolMajor(mapper, rawToolMajor);
6569 processToolMinor(mapper, rawToolMinor);
6570 processPressure(mapper, rawPressure);
6571 processOrientation(mapper, rawOrientation);
6572 processDistance(mapper, rawDistance);
6573 processId(mapper, id);
6574 processMTSync(mapper);
6575 processSync(mapper);
6576
6577 NotifyMotionArgs args;
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6579 ASSERT_EQ(0, args.pointerProperties[0].id);
6580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6581 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6582 orientation, distance));
6583}
6584
6585TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006586 addConfigurationProperty("touch.deviceType", "touchScreen");
6587 prepareDisplay(DISPLAY_ORIENTATION_0);
6588 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6589 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006591
6592 // These calculations are based on the input device calibration documentation.
6593 int32_t rawX = 100;
6594 int32_t rawY = 200;
6595 int32_t rawTouchMajor = 140;
6596 int32_t rawTouchMinor = 120;
6597 int32_t rawToolMajor = 180;
6598 int32_t rawToolMinor = 160;
6599
6600 float x = toDisplayX(rawX);
6601 float y = toDisplayY(rawY);
6602 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6603 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6604 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6605 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6606 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6607
6608 processPosition(mapper, rawX, rawY);
6609 processTouchMajor(mapper, rawTouchMajor);
6610 processTouchMinor(mapper, rawTouchMinor);
6611 processToolMajor(mapper, rawToolMajor);
6612 processToolMinor(mapper, rawToolMinor);
6613 processMTSync(mapper);
6614 processSync(mapper);
6615
6616 NotifyMotionArgs args;
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6619 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6620}
6621
6622TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006623 addConfigurationProperty("touch.deviceType", "touchScreen");
6624 prepareDisplay(DISPLAY_ORIENTATION_0);
6625 prepareAxes(POSITION | TOUCH | TOOL);
6626 addConfigurationProperty("touch.size.calibration", "diameter");
6627 addConfigurationProperty("touch.size.scale", "10");
6628 addConfigurationProperty("touch.size.bias", "160");
6629 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006630 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006631
6632 // These calculations are based on the input device calibration documentation.
6633 // Note: We only provide a single common touch/tool value because the device is assumed
6634 // not to emit separate values for each pointer (isSummed = 1).
6635 int32_t rawX = 100;
6636 int32_t rawY = 200;
6637 int32_t rawX2 = 150;
6638 int32_t rawY2 = 250;
6639 int32_t rawTouchMajor = 5;
6640 int32_t rawToolMajor = 8;
6641
6642 float x = toDisplayX(rawX);
6643 float y = toDisplayY(rawY);
6644 float x2 = toDisplayX(rawX2);
6645 float y2 = toDisplayY(rawY2);
6646 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6647 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6648 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6649
6650 processPosition(mapper, rawX, rawY);
6651 processTouchMajor(mapper, rawTouchMajor);
6652 processToolMajor(mapper, rawToolMajor);
6653 processMTSync(mapper);
6654 processPosition(mapper, rawX2, rawY2);
6655 processTouchMajor(mapper, rawTouchMajor);
6656 processToolMajor(mapper, rawToolMajor);
6657 processMTSync(mapper);
6658 processSync(mapper);
6659
6660 NotifyMotionArgs args;
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6663
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6666 args.action);
6667 ASSERT_EQ(size_t(2), args.pointerCount);
6668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6669 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6671 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6672}
6673
6674TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006675 addConfigurationProperty("touch.deviceType", "touchScreen");
6676 prepareDisplay(DISPLAY_ORIENTATION_0);
6677 prepareAxes(POSITION | TOUCH | TOOL);
6678 addConfigurationProperty("touch.size.calibration", "area");
6679 addConfigurationProperty("touch.size.scale", "43");
6680 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006681 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006682
6683 // These calculations are based on the input device calibration documentation.
6684 int32_t rawX = 100;
6685 int32_t rawY = 200;
6686 int32_t rawTouchMajor = 5;
6687 int32_t rawToolMajor = 8;
6688
6689 float x = toDisplayX(rawX);
6690 float y = toDisplayY(rawY);
6691 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6692 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6693 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6694
6695 processPosition(mapper, rawX, rawY);
6696 processTouchMajor(mapper, rawTouchMajor);
6697 processToolMajor(mapper, rawToolMajor);
6698 processMTSync(mapper);
6699 processSync(mapper);
6700
6701 NotifyMotionArgs args;
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6704 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6705}
6706
6707TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006708 addConfigurationProperty("touch.deviceType", "touchScreen");
6709 prepareDisplay(DISPLAY_ORIENTATION_0);
6710 prepareAxes(POSITION | PRESSURE);
6711 addConfigurationProperty("touch.pressure.calibration", "amplitude");
6712 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006713 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006714
Michael Wrightaa449c92017-12-13 21:21:43 +00006715 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006716 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00006717 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6718 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6719 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6720
Michael Wrightd02c5b62014-02-10 15:10:22 -08006721 // These calculations are based on the input device calibration documentation.
6722 int32_t rawX = 100;
6723 int32_t rawY = 200;
6724 int32_t rawPressure = 60;
6725
6726 float x = toDisplayX(rawX);
6727 float y = toDisplayY(rawY);
6728 float pressure = float(rawPressure) * 0.01f;
6729
6730 processPosition(mapper, rawX, rawY);
6731 processPressure(mapper, rawPressure);
6732 processMTSync(mapper);
6733 processSync(mapper);
6734
6735 NotifyMotionArgs args;
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6738 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6739}
6740
6741TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006742 addConfigurationProperty("touch.deviceType", "touchScreen");
6743 prepareDisplay(DISPLAY_ORIENTATION_0);
6744 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006745 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006746
6747 NotifyMotionArgs motionArgs;
6748 NotifyKeyArgs keyArgs;
6749
6750 processId(mapper, 1);
6751 processPosition(mapper, 100, 200);
6752 processSync(mapper);
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6754 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6755 ASSERT_EQ(0, motionArgs.buttonState);
6756
6757 // press BTN_LEFT, release BTN_LEFT
6758 processKey(mapper, BTN_LEFT, 1);
6759 processSync(mapper);
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6762 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6763
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6766 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6767
Michael Wrightd02c5b62014-02-10 15:10:22 -08006768 processKey(mapper, BTN_LEFT, 0);
6769 processSync(mapper);
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006772 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006773
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006776 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006777
6778 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6779 processKey(mapper, BTN_RIGHT, 1);
6780 processKey(mapper, BTN_MIDDLE, 1);
6781 processSync(mapper);
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6784 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6785 motionArgs.buttonState);
6786
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6788 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6789 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6790
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6793 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6794 motionArgs.buttonState);
6795
Michael Wrightd02c5b62014-02-10 15:10:22 -08006796 processKey(mapper, BTN_RIGHT, 0);
6797 processSync(mapper);
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006801
6802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006804 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006805
6806 processKey(mapper, BTN_MIDDLE, 0);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006810 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006811
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006814 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815
6816 // press BTN_BACK, release BTN_BACK
6817 processKey(mapper, BTN_BACK, 1);
6818 processSync(mapper);
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6820 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6821 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006822
Michael Wrightd02c5b62014-02-10 15:10:22 -08006823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006825 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6826
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6828 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6829 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006830
6831 processKey(mapper, BTN_BACK, 0);
6832 processSync(mapper);
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006834 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006835 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006836
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006839 ASSERT_EQ(0, motionArgs.buttonState);
6840
Michael Wrightd02c5b62014-02-10 15:10:22 -08006841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6842 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6843 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6844
6845 // press BTN_SIDE, release BTN_SIDE
6846 processKey(mapper, BTN_SIDE, 1);
6847 processSync(mapper);
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6849 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6850 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006851
Michael Wrightd02c5b62014-02-10 15:10:22 -08006852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006854 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6855
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6857 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6858 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006859
6860 processKey(mapper, BTN_SIDE, 0);
6861 processSync(mapper);
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006863 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006864 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006865
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006868 ASSERT_EQ(0, motionArgs.buttonState);
6869
Michael Wrightd02c5b62014-02-10 15:10:22 -08006870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6871 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6872 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6873
6874 // press BTN_FORWARD, release BTN_FORWARD
6875 processKey(mapper, BTN_FORWARD, 1);
6876 processSync(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6878 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6879 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006880
Michael Wrightd02c5b62014-02-10 15:10:22 -08006881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006883 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6884
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6887 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006888
6889 processKey(mapper, BTN_FORWARD, 0);
6890 processSync(mapper);
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006892 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006894
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006897 ASSERT_EQ(0, motionArgs.buttonState);
6898
Michael Wrightd02c5b62014-02-10 15:10:22 -08006899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6900 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6901 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6902
6903 // press BTN_EXTRA, release BTN_EXTRA
6904 processKey(mapper, BTN_EXTRA, 1);
6905 processSync(mapper);
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6907 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6908 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006909
Michael Wrightd02c5b62014-02-10 15:10:22 -08006910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006912 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6913
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6915 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6916 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006917
6918 processKey(mapper, BTN_EXTRA, 0);
6919 processSync(mapper);
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006922 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006923
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006926 ASSERT_EQ(0, motionArgs.buttonState);
6927
Michael Wrightd02c5b62014-02-10 15:10:22 -08006928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6929 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6930 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6931
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6933
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934 // press BTN_STYLUS, release BTN_STYLUS
6935 processKey(mapper, BTN_STYLUS, 1);
6936 processSync(mapper);
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006939 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6940
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6943 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006944
6945 processKey(mapper, BTN_STYLUS, 0);
6946 processSync(mapper);
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006948 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006950
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006953 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006954
6955 // press BTN_STYLUS2, release BTN_STYLUS2
6956 processKey(mapper, BTN_STYLUS2, 1);
6957 processSync(mapper);
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006960 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6961
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6964 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006965
6966 processKey(mapper, BTN_STYLUS2, 0);
6967 processSync(mapper);
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006969 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006970 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006971
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006974 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975
6976 // release touch
6977 processId(mapper, -1);
6978 processSync(mapper);
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6980 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6981 ASSERT_EQ(0, motionArgs.buttonState);
6982}
6983
6984TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006985 addConfigurationProperty("touch.deviceType", "touchScreen");
6986 prepareDisplay(DISPLAY_ORIENTATION_0);
6987 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006988 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989
6990 NotifyMotionArgs motionArgs;
6991
6992 // default tool type is finger
6993 processId(mapper, 1);
6994 processPosition(mapper, 100, 200);
6995 processSync(mapper);
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6997 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6999
7000 // eraser
7001 processKey(mapper, BTN_TOOL_RUBBER, 1);
7002 processSync(mapper);
7003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7006
7007 // stylus
7008 processKey(mapper, BTN_TOOL_RUBBER, 0);
7009 processKey(mapper, BTN_TOOL_PEN, 1);
7010 processSync(mapper);
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7014
7015 // brush
7016 processKey(mapper, BTN_TOOL_PEN, 0);
7017 processKey(mapper, BTN_TOOL_BRUSH, 1);
7018 processSync(mapper);
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7022
7023 // pencil
7024 processKey(mapper, BTN_TOOL_BRUSH, 0);
7025 processKey(mapper, BTN_TOOL_PENCIL, 1);
7026 processSync(mapper);
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7030
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007031 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007032 processKey(mapper, BTN_TOOL_PENCIL, 0);
7033 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7034 processSync(mapper);
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7038
7039 // mouse
7040 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7041 processKey(mapper, BTN_TOOL_MOUSE, 1);
7042 processSync(mapper);
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7046
7047 // lens
7048 processKey(mapper, BTN_TOOL_MOUSE, 0);
7049 processKey(mapper, BTN_TOOL_LENS, 1);
7050 processSync(mapper);
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7054
7055 // double-tap
7056 processKey(mapper, BTN_TOOL_LENS, 0);
7057 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7058 processSync(mapper);
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7062
7063 // triple-tap
7064 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7065 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7070
7071 // quad-tap
7072 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7073 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7074 processSync(mapper);
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7078
7079 // finger
7080 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7081 processKey(mapper, BTN_TOOL_FINGER, 1);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7086
7087 // stylus trumps finger
7088 processKey(mapper, BTN_TOOL_PEN, 1);
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7093
7094 // eraser trumps stylus
7095 processKey(mapper, BTN_TOOL_RUBBER, 1);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7100
7101 // mouse trumps eraser
7102 processKey(mapper, BTN_TOOL_MOUSE, 1);
7103 processSync(mapper);
7104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7107
7108 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7109 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7110 processSync(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7114
7115 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7116 processToolType(mapper, MT_TOOL_PEN);
7117 processSync(mapper);
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7121
7122 // back to default tool type
7123 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7124 processKey(mapper, BTN_TOOL_MOUSE, 0);
7125 processKey(mapper, BTN_TOOL_RUBBER, 0);
7126 processKey(mapper, BTN_TOOL_PEN, 0);
7127 processKey(mapper, BTN_TOOL_FINGER, 0);
7128 processSync(mapper);
7129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7132}
7133
7134TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 addConfigurationProperty("touch.deviceType", "touchScreen");
7136 prepareDisplay(DISPLAY_ORIENTATION_0);
7137 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007138 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007139 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140
7141 NotifyMotionArgs motionArgs;
7142
7143 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7144 processId(mapper, 1);
7145 processPosition(mapper, 100, 200);
7146 processSync(mapper);
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7148 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7150 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7151
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7153 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7155 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7156
7157 // move a little
7158 processPosition(mapper, 150, 250);
7159 processSync(mapper);
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7161 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7163 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7164
7165 // down when BTN_TOUCH is pressed, pressure defaults to 1
7166 processKey(mapper, BTN_TOUCH, 1);
7167 processSync(mapper);
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7169 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7171 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7172
7173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7174 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7176 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7177
7178 // up when BTN_TOUCH is released, hover restored
7179 processKey(mapper, BTN_TOUCH, 0);
7180 processSync(mapper);
7181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7182 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7184 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7185
7186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7187 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7189 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7190
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7194 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7195
7196 // exit hover when pointer goes away
7197 processId(mapper, -1);
7198 processSync(mapper);
7199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7200 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7202 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7203}
7204
7205TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206 addConfigurationProperty("touch.deviceType", "touchScreen");
7207 prepareDisplay(DISPLAY_ORIENTATION_0);
7208 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007209 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210
7211 NotifyMotionArgs motionArgs;
7212
7213 // initially hovering because pressure is 0
7214 processId(mapper, 1);
7215 processPosition(mapper, 100, 200);
7216 processPressure(mapper, 0);
7217 processSync(mapper);
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7219 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7221 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7222
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7226 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7227
7228 // move a little
7229 processPosition(mapper, 150, 250);
7230 processSync(mapper);
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7232 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7234 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7235
7236 // down when pressure becomes non-zero
7237 processPressure(mapper, RAW_PRESSURE_MAX);
7238 processSync(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7242 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7243
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7245 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7247 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7248
7249 // up when pressure becomes 0, hover restored
7250 processPressure(mapper, 0);
7251 processSync(mapper);
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7255 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7256
7257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7258 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7260 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7261
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7265 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7266
7267 // exit hover when pointer goes away
7268 processId(mapper, -1);
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7273 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7274}
7275
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007276/**
7277 * Set the input device port <--> display port associations, and check that the
7278 * events are routed to the display that matches the display port.
7279 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7280 */
7281TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007282 const std::string usb2 = "USB2";
7283 const uint8_t hdmi1 = 0;
7284 const uint8_t hdmi2 = 1;
7285 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007286 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007287
7288 addConfigurationProperty("touch.deviceType", "touchScreen");
7289 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007290 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007291
7292 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7293 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7294
7295 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7296 // for this input device is specified, and the matching viewport is not present,
7297 // the input device should be disabled (at the mapper level).
7298
7299 // Add viewport for display 2 on hdmi2
7300 prepareSecondaryDisplay(type, hdmi2);
7301 // Send a touch event
7302 processPosition(mapper, 100, 100);
7303 processSync(mapper);
7304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7305
7306 // Add viewport for display 1 on hdmi1
7307 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7308 // Send a touch event again
7309 processPosition(mapper, 100, 100);
7310 processSync(mapper);
7311
7312 NotifyMotionArgs args;
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7314 ASSERT_EQ(DISPLAY_ID, args.displayId);
7315}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007316
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007317TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007318 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007319 std::shared_ptr<FakePointerController> fakePointerController =
7320 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007321 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007322 fakePointerController->setPosition(100, 200);
7323 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007324 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7325
Garfield Tan888a6a42020-01-09 11:39:16 -08007326 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007327 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007328
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007329 prepareDisplay(DISPLAY_ORIENTATION_0);
7330 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007331 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007332
7333 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007334 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007335
7336 NotifyMotionArgs motionArgs;
7337 processPosition(mapper, 100, 100);
7338 processSync(mapper);
7339
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7341 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7342 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7343}
7344
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007345/**
7346 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7347 * events should not be delivered to the listener.
7348 */
7349TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7350 addConfigurationProperty("touch.deviceType", "touchScreen");
7351 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7352 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7353 ViewportType::INTERNAL);
7354 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7355 prepareAxes(POSITION);
7356 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7357
7358 NotifyMotionArgs motionArgs;
7359 processPosition(mapper, 100, 100);
7360 processSync(mapper);
7361
7362 mFakeListener->assertNotifyMotionWasNotCalled();
7363}
7364
Garfield Tanc734e4f2021-01-15 20:01:39 -08007365TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7366 addConfigurationProperty("touch.deviceType", "touchScreen");
7367 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7368 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7369 ViewportType::INTERNAL);
7370 std::optional<DisplayViewport> optionalDisplayViewport =
7371 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7372 ASSERT_TRUE(optionalDisplayViewport.has_value());
7373 DisplayViewport displayViewport = *optionalDisplayViewport;
7374
7375 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7376 prepareAxes(POSITION);
7377 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7378
7379 // Finger down
7380 int32_t x = 100, y = 100;
7381 processPosition(mapper, x, y);
7382 processSync(mapper);
7383
7384 NotifyMotionArgs motionArgs;
7385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7386 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7387
7388 // Deactivate display viewport
7389 displayViewport.isActive = false;
7390 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7391 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7392
7393 // Finger move
7394 x += 10, y += 10;
7395 processPosition(mapper, x, y);
7396 processSync(mapper);
7397
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7399 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7400
7401 // Reactivate display viewport
7402 displayViewport.isActive = true;
7403 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7404 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7405
7406 // Finger move again
7407 x += 10, y += 10;
7408 processPosition(mapper, x, y);
7409 processSync(mapper);
7410
7411 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7412 // no pointer on the touch device.
7413 mFakeListener->assertNotifyMotionWasNotCalled();
7414}
7415
Arthur Hung7c645402019-01-25 17:45:42 +08007416TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7417 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007418 prepareAxes(POSITION | ID | SLOT);
7419 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007420 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007421
7422 // Create the second touch screen device, and enable multi fingers.
7423 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007424 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007425 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007426 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007427 std::shared_ptr<InputDevice> device2 =
7428 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7429 Flags<InputDeviceClass>(0));
7430
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007431 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7432 0 /*flat*/, 0 /*fuzz*/);
7433 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7434 0 /*flat*/, 0 /*fuzz*/);
7435 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7436 0 /*flat*/, 0 /*fuzz*/);
7437 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7438 0 /*flat*/, 0 /*fuzz*/);
7439 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7440 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7441 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007442
7443 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007444 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007445 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7446 device2->reset(ARBITRARY_TIME);
7447
7448 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007449 std::shared_ptr<FakePointerController> fakePointerController =
7450 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007451 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7452 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7453
7454 // Setup policy for associated displays and show touches.
7455 const uint8_t hdmi1 = 0;
7456 const uint8_t hdmi2 = 1;
7457 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7458 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7459 mFakePolicy->setShowTouches(true);
7460
7461 // Create displays.
7462 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007463 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007464
7465 // Default device will reconfigure above, need additional reconfiguration for another device.
7466 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007467 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007468
7469 // Two fingers down at default display.
7470 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7471 processPosition(mapper, x1, y1);
7472 processId(mapper, 1);
7473 processSlot(mapper, 1);
7474 processPosition(mapper, x2, y2);
7475 processId(mapper, 2);
7476 processSync(mapper);
7477
7478 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7479 fakePointerController->getSpots().find(DISPLAY_ID);
7480 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7481 ASSERT_EQ(size_t(2), iter->second.size());
7482
7483 // Two fingers down at second display.
7484 processPosition(mapper2, x1, y1);
7485 processId(mapper2, 1);
7486 processSlot(mapper2, 1);
7487 processPosition(mapper2, x2, y2);
7488 processId(mapper2, 2);
7489 processSync(mapper2);
7490
7491 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7492 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7493 ASSERT_EQ(size_t(2), iter->second.size());
7494}
7495
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007496TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007497 prepareAxes(POSITION);
7498 addConfigurationProperty("touch.deviceType", "touchScreen");
7499 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007500 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007501
7502 NotifyMotionArgs motionArgs;
7503 // Unrotated video frame
7504 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7505 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007506 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007507 processPosition(mapper, 100, 200);
7508 processSync(mapper);
7509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7510 ASSERT_EQ(frames, motionArgs.videoFrames);
7511
7512 // Subsequent touch events should not have any videoframes
7513 // This is implemented separately in FakeEventHub,
7514 // but that should match the behaviour of TouchVideoDevice.
7515 processPosition(mapper, 200, 200);
7516 processSync(mapper);
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7518 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7519}
7520
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007521TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007522 prepareAxes(POSITION);
7523 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007524 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007525 // Unrotated video frame
7526 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7527 NotifyMotionArgs motionArgs;
7528
7529 // Test all 4 orientations
7530 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7531 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7532 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7533 clearViewports();
7534 prepareDisplay(orientation);
7535 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007536 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007537 processPosition(mapper, 100, 200);
7538 processSync(mapper);
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7540 frames[0].rotate(orientation);
7541 ASSERT_EQ(frames, motionArgs.videoFrames);
7542 }
7543}
7544
7545TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007546 prepareAxes(POSITION);
7547 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007548 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007549 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7550 // so mix these.
7551 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7552 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7553 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7554 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7555 NotifyMotionArgs motionArgs;
7556
7557 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007558 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007559 processPosition(mapper, 100, 200);
7560 processSync(mapper);
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 std::for_each(frames.begin(), frames.end(),
7563 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7564 ASSERT_EQ(frames, motionArgs.videoFrames);
7565}
7566
Arthur Hung9da14732019-09-02 16:16:58 +08007567/**
7568 * If we had defined port associations, but the viewport is not ready, the touch device would be
7569 * expected to be disabled, and it should be enabled after the viewport has found.
7570 */
7571TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007572 constexpr uint8_t hdmi2 = 1;
7573 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007574 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007575
7576 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7577
7578 addConfigurationProperty("touch.deviceType", "touchScreen");
7579 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007580 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007581
7582 ASSERT_EQ(mDevice->isEnabled(), false);
7583
7584 // Add display on hdmi2, the device should be enabled and can receive touch event.
7585 prepareSecondaryDisplay(type, hdmi2);
7586 ASSERT_EQ(mDevice->isEnabled(), true);
7587
7588 // Send a touch event.
7589 processPosition(mapper, 100, 100);
7590 processSync(mapper);
7591
7592 NotifyMotionArgs args;
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7594 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7595}
7596
Arthur Hung6cd19a42019-08-30 19:04:12 +08007597
Arthur Hung6cd19a42019-08-30 19:04:12 +08007598
Arthur Hung421eb1c2020-01-16 00:09:42 +08007599TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007600 addConfigurationProperty("touch.deviceType", "touchScreen");
7601 prepareDisplay(DISPLAY_ORIENTATION_0);
7602 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007603 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007604
7605 NotifyMotionArgs motionArgs;
7606
7607 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7608 // finger down
7609 processId(mapper, 1);
7610 processPosition(mapper, x1, y1);
7611 processSync(mapper);
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7613 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7615
7616 // finger move
7617 processId(mapper, 1);
7618 processPosition(mapper, x2, y2);
7619 processSync(mapper);
7620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7623
7624 // finger up.
7625 processId(mapper, -1);
7626 processSync(mapper);
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7628 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7630
7631 // new finger down
7632 processId(mapper, 1);
7633 processPosition(mapper, x3, y3);
7634 processSync(mapper);
7635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7636 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7638}
7639
7640/**
arthurhungcc7f9802020-04-30 17:55:40 +08007641 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7642 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007643 */
arthurhungcc7f9802020-04-30 17:55:40 +08007644TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007645 addConfigurationProperty("touch.deviceType", "touchScreen");
7646 prepareDisplay(DISPLAY_ORIENTATION_0);
7647 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007648 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007649
7650 NotifyMotionArgs motionArgs;
7651
7652 // default tool type is finger
7653 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007654 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007655 processPosition(mapper, x1, y1);
7656 processSync(mapper);
7657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7658 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7660
7661 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7662 processToolType(mapper, MT_TOOL_PALM);
7663 processSync(mapper);
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7665 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7666
7667 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007668 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007669 processPosition(mapper, x2, y2);
7670 processSync(mapper);
7671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7672
7673 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08007674 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007675 processSync(mapper);
7676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7677
7678 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08007679 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007680 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007681 processPosition(mapper, x3, y3);
7682 processSync(mapper);
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7686}
7687
arthurhungbf89a482020-04-17 17:37:55 +08007688/**
arthurhungcc7f9802020-04-30 17:55:40 +08007689 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7690 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08007691 */
arthurhungcc7f9802020-04-30 17:55:40 +08007692TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08007693 addConfigurationProperty("touch.deviceType", "touchScreen");
7694 prepareDisplay(DISPLAY_ORIENTATION_0);
7695 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7696 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7697
7698 NotifyMotionArgs motionArgs;
7699
7700 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08007701 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7702 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007703 processPosition(mapper, x1, y1);
7704 processSync(mapper);
7705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7706 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7707 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7708
7709 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08007710 processSlot(mapper, SECOND_SLOT);
7711 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007712 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08007713 processSync(mapper);
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7715 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7716 motionArgs.action);
7717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7718
7719 // If the tool type of the first finger changes to MT_TOOL_PALM,
7720 // we expect to receive ACTION_POINTER_UP with cancel flag.
7721 processSlot(mapper, FIRST_SLOT);
7722 processId(mapper, FIRST_TRACKING_ID);
7723 processToolType(mapper, MT_TOOL_PALM);
7724 processSync(mapper);
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7726 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7727 motionArgs.action);
7728 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7729
7730 // The following MOVE events of second finger should be processed.
7731 processSlot(mapper, SECOND_SLOT);
7732 processId(mapper, SECOND_TRACKING_ID);
7733 processPosition(mapper, x2 + 1, y2 + 1);
7734 processSync(mapper);
7735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7737 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7738
7739 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
7740 // it. Second finger receive move.
7741 processSlot(mapper, FIRST_SLOT);
7742 processId(mapper, INVALID_TRACKING_ID);
7743 processSync(mapper);
7744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7746 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7747
7748 // Second finger keeps moving.
7749 processSlot(mapper, SECOND_SLOT);
7750 processId(mapper, SECOND_TRACKING_ID);
7751 processPosition(mapper, x2 + 2, y2 + 2);
7752 processSync(mapper);
7753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7755 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7756
7757 // Second finger up.
7758 processId(mapper, INVALID_TRACKING_ID);
7759 processSync(mapper);
7760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7761 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7762 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7763}
7764
7765/**
7766 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
7767 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
7768 */
7769TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
7770 addConfigurationProperty("touch.deviceType", "touchScreen");
7771 prepareDisplay(DISPLAY_ORIENTATION_0);
7772 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7773 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7774
7775 NotifyMotionArgs motionArgs;
7776
7777 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7778 // First finger down.
7779 processId(mapper, FIRST_TRACKING_ID);
7780 processPosition(mapper, x1, y1);
7781 processSync(mapper);
7782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7783 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7785
7786 // Second finger down.
7787 processSlot(mapper, SECOND_SLOT);
7788 processId(mapper, SECOND_TRACKING_ID);
7789 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08007790 processSync(mapper);
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7792 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7793 motionArgs.action);
7794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7795
arthurhungcc7f9802020-04-30 17:55:40 +08007796 // If the tool type of the first finger changes to MT_TOOL_PALM,
7797 // we expect to receive ACTION_POINTER_UP with cancel flag.
7798 processSlot(mapper, FIRST_SLOT);
7799 processId(mapper, FIRST_TRACKING_ID);
7800 processToolType(mapper, MT_TOOL_PALM);
7801 processSync(mapper);
7802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7803 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7804 motionArgs.action);
7805 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7806
7807 // Second finger keeps moving.
7808 processSlot(mapper, SECOND_SLOT);
7809 processId(mapper, SECOND_TRACKING_ID);
7810 processPosition(mapper, x2 + 1, y2 + 1);
7811 processSync(mapper);
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7814
7815 // second finger becomes palm, receive cancel due to only 1 finger is active.
7816 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08007817 processToolType(mapper, MT_TOOL_PALM);
7818 processSync(mapper);
7819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7820 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7821
arthurhungcc7f9802020-04-30 17:55:40 +08007822 // third finger down.
7823 processSlot(mapper, THIRD_SLOT);
7824 processId(mapper, THIRD_TRACKING_ID);
7825 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08007826 processPosition(mapper, x3, y3);
7827 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08007828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7829 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08007831 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7832
7833 // third finger move
7834 processId(mapper, THIRD_TRACKING_ID);
7835 processPosition(mapper, x3 + 1, y3 + 1);
7836 processSync(mapper);
7837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7839
7840 // first finger up, third finger receive move.
7841 processSlot(mapper, FIRST_SLOT);
7842 processId(mapper, INVALID_TRACKING_ID);
7843 processSync(mapper);
7844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7846 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7847
7848 // second finger up, third finger receive move.
7849 processSlot(mapper, SECOND_SLOT);
7850 processId(mapper, INVALID_TRACKING_ID);
7851 processSync(mapper);
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7854 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7855
7856 // third finger up.
7857 processSlot(mapper, THIRD_SLOT);
7858 processId(mapper, INVALID_TRACKING_ID);
7859 processSync(mapper);
7860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7861 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7862 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7863}
7864
7865/**
7866 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
7867 * and the active finger could still be allowed to receive the events
7868 */
7869TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
7870 addConfigurationProperty("touch.deviceType", "touchScreen");
7871 prepareDisplay(DISPLAY_ORIENTATION_0);
7872 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7873 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7874
7875 NotifyMotionArgs motionArgs;
7876
7877 // default tool type is finger
7878 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
7879 processId(mapper, FIRST_TRACKING_ID);
7880 processPosition(mapper, x1, y1);
7881 processSync(mapper);
7882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7883 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7885
7886 // Second finger down.
7887 processSlot(mapper, SECOND_SLOT);
7888 processId(mapper, SECOND_TRACKING_ID);
7889 processPosition(mapper, x2, y2);
7890 processSync(mapper);
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7892 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7893 motionArgs.action);
7894 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7895
7896 // If the tool type of the second finger changes to MT_TOOL_PALM,
7897 // we expect to receive ACTION_POINTER_UP with cancel flag.
7898 processId(mapper, SECOND_TRACKING_ID);
7899 processToolType(mapper, MT_TOOL_PALM);
7900 processSync(mapper);
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7902 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7903 motionArgs.action);
7904 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
7905
7906 // The following MOVE event should be processed.
7907 processSlot(mapper, FIRST_SLOT);
7908 processId(mapper, FIRST_TRACKING_ID);
7909 processPosition(mapper, x1 + 1, y1 + 1);
7910 processSync(mapper);
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7913 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
7914
7915 // second finger up.
7916 processSlot(mapper, SECOND_SLOT);
7917 processId(mapper, INVALID_TRACKING_ID);
7918 processSync(mapper);
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7921
7922 // first finger keep moving
7923 processSlot(mapper, FIRST_SLOT);
7924 processId(mapper, FIRST_TRACKING_ID);
7925 processPosition(mapper, x1 + 2, y1 + 2);
7926 processSync(mapper);
7927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7929
7930 // first finger up.
7931 processId(mapper, INVALID_TRACKING_ID);
7932 processSync(mapper);
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7935 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08007936}
7937
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007938// --- MultiTouchInputMapperTest_ExternalDevice ---
7939
7940class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7941protected:
Chris Yea52ade12020-08-27 16:49:20 -07007942 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007943};
7944
7945/**
7946 * Expect fallback to internal viewport if device is external and external viewport is not present.
7947 */
7948TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7949 prepareAxes(POSITION);
7950 addConfigurationProperty("touch.deviceType", "touchScreen");
7951 prepareDisplay(DISPLAY_ORIENTATION_0);
7952 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7953
7954 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7955
7956 NotifyMotionArgs motionArgs;
7957
7958 // Expect the event to be sent to the internal viewport,
7959 // because an external viewport is not present.
7960 processPosition(mapper, 100, 100);
7961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7963 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7964
7965 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007966 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007967 processPosition(mapper, 100, 100);
7968 processSync(mapper);
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7970 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7971}
Arthur Hung4197f6b2020-03-16 15:39:59 +08007972
7973/**
7974 * Test touch should not work if outside of surface.
7975 */
7976class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7977protected:
7978 void halfDisplayToCenterHorizontal(int32_t orientation) {
7979 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007980 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08007981
7982 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7983 internalViewport->orientation = orientation;
7984 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7985 internalViewport->logicalLeft = 0;
7986 internalViewport->logicalTop = 0;
7987 internalViewport->logicalRight = DISPLAY_HEIGHT;
7988 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7989
7990 internalViewport->physicalLeft = 0;
7991 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7992 internalViewport->physicalRight = DISPLAY_HEIGHT;
7993 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7994
7995 internalViewport->deviceWidth = DISPLAY_HEIGHT;
7996 internalViewport->deviceHeight = DISPLAY_WIDTH;
7997 } else {
7998 internalViewport->logicalLeft = 0;
7999 internalViewport->logicalTop = 0;
8000 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8001 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8002
8003 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8004 internalViewport->physicalTop = 0;
8005 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8006 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8007
8008 internalViewport->deviceWidth = DISPLAY_WIDTH;
8009 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8010 }
8011
8012 mFakePolicy->updateViewport(internalViewport.value());
8013 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8014 }
8015
arthurhung5d547942020-12-14 17:04:45 +08008016 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8017 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008018 int32_t yExpected) {
8019 // touch on outside area should not work.
8020 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8021 processSync(mapper);
8022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8023
8024 // touch on inside area should receive the event.
8025 NotifyMotionArgs args;
8026 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8027 processSync(mapper);
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8029 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8030 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8031
8032 // Reset.
8033 mapper.reset(ARBITRARY_TIME);
8034 }
8035};
8036
arthurhung5d547942020-12-14 17:04:45 +08008037TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008038 addConfigurationProperty("touch.deviceType", "touchScreen");
8039 prepareDisplay(DISPLAY_ORIENTATION_0);
8040 prepareAxes(POSITION);
8041 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8042
8043 // Touch on center of normal display should work.
8044 const int32_t x = DISPLAY_WIDTH / 4;
8045 const int32_t y = DISPLAY_HEIGHT / 2;
8046 processPosition(mapper, toRawX(x), toRawY(y));
8047 processSync(mapper);
8048 NotifyMotionArgs args;
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8051 0.0f, 0.0f, 0.0f, 0.0f));
8052 // Reset.
8053 mapper.reset(ARBITRARY_TIME);
8054
8055 // Let physical display be different to device, and make surface and physical could be 1:1.
8056 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8057
8058 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8059 const int32_t yExpected = y;
8060 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8061}
8062
arthurhung5d547942020-12-14 17:04:45 +08008063TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008064 addConfigurationProperty("touch.deviceType", "touchScreen");
8065 prepareDisplay(DISPLAY_ORIENTATION_0);
8066 prepareAxes(POSITION);
8067 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8068
8069 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8070 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8071
8072 const int32_t x = DISPLAY_WIDTH / 4;
8073 const int32_t y = DISPLAY_HEIGHT / 2;
8074
8075 // expect x/y = swap x/y then reverse y.
8076 const int32_t xExpected = y;
8077 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8078 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8079}
8080
arthurhung5d547942020-12-14 17:04:45 +08008081TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008082 addConfigurationProperty("touch.deviceType", "touchScreen");
8083 prepareDisplay(DISPLAY_ORIENTATION_0);
8084 prepareAxes(POSITION);
8085 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8086
8087 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8088 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8089
8090 const int32_t x = DISPLAY_WIDTH / 4;
8091 const int32_t y = DISPLAY_HEIGHT / 2;
8092
8093 // expect x/y = swap x/y then reverse x.
8094 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8095 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8096 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8097}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008098
arthurhunga36b28e2020-12-29 20:28:15 +08008099TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8100 addConfigurationProperty("touch.deviceType", "touchScreen");
8101 prepareDisplay(DISPLAY_ORIENTATION_0);
8102 prepareAxes(POSITION);
8103 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8104
8105 const int32_t x = 0;
8106 const int32_t y = 0;
8107
8108 const int32_t xExpected = x;
8109 const int32_t yExpected = y;
8110 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8111
8112 clearViewports();
8113 prepareDisplay(DISPLAY_ORIENTATION_90);
8114 // expect x/y = swap x/y then reverse y.
8115 const int32_t xExpected90 = y;
8116 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8117 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8118
8119 clearViewports();
8120 prepareDisplay(DISPLAY_ORIENTATION_270);
8121 // expect x/y = swap x/y then reverse x.
8122 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8123 const int32_t yExpected270 = x;
8124 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8125}
8126
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008127TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8128 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8129 std::shared_ptr<FakePointerController> fakePointerController =
8130 std::make_shared<FakePointerController>();
8131 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8132 fakePointerController->setPosition(0, 0);
8133 fakePointerController->setButtonState(0);
8134
8135 // prepare device and capture
8136 prepareDisplay(DISPLAY_ORIENTATION_0);
8137 prepareAxes(POSITION | ID | SLOT);
8138 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8139 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8140 mFakePolicy->setPointerCapture(true);
8141 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8142 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8143
8144 // captured touchpad should be a touchpad source
8145 NotifyDeviceResetArgs resetArgs;
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8147 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8148
Chris Yef74dc422020-09-02 22:41:50 -07008149 InputDeviceInfo deviceInfo;
8150 mDevice->getDeviceInfo(&deviceInfo);
8151
8152 const InputDeviceInfo::MotionRange* relRangeX =
8153 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8154 ASSERT_NE(relRangeX, nullptr);
8155 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8156 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8157 const InputDeviceInfo::MotionRange* relRangeY =
8158 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8159 ASSERT_NE(relRangeY, nullptr);
8160 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8161 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8162
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008163 // run captured pointer tests - note that this is unscaled, so input listener events should be
8164 // identical to what the hardware sends (accounting for any
8165 // calibration).
8166 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008167 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008168 processId(mapper, 1);
8169 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8170 processKey(mapper, BTN_TOUCH, 1);
8171 processSync(mapper);
8172
8173 // expect coord[0] to contain initial location of touch 0
8174 NotifyMotionArgs args;
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8177 ASSERT_EQ(1U, args.pointerCount);
8178 ASSERT_EQ(0, args.pointerProperties[0].id);
8179 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8180 ASSERT_NO_FATAL_FAILURE(
8181 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8182
8183 // FINGER 1 DOWN
8184 processSlot(mapper, 1);
8185 processId(mapper, 2);
8186 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8187 processSync(mapper);
8188
8189 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008191 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8192 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008193 ASSERT_EQ(2U, args.pointerCount);
8194 ASSERT_EQ(0, args.pointerProperties[0].id);
8195 ASSERT_EQ(1, args.pointerProperties[1].id);
8196 ASSERT_NO_FATAL_FAILURE(
8197 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8198 ASSERT_NO_FATAL_FAILURE(
8199 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8200
8201 // FINGER 1 MOVE
8202 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8203 processSync(mapper);
8204
8205 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8206 // from move
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8209 ASSERT_NO_FATAL_FAILURE(
8210 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8211 ASSERT_NO_FATAL_FAILURE(
8212 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8213
8214 // FINGER 0 MOVE
8215 processSlot(mapper, 0);
8216 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8217 processSync(mapper);
8218
8219 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8222 ASSERT_NO_FATAL_FAILURE(
8223 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8224 ASSERT_NO_FATAL_FAILURE(
8225 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8226
8227 // BUTTON DOWN
8228 processKey(mapper, BTN_LEFT, 1);
8229 processSync(mapper);
8230
8231 // touchinputmapper design sends a move before button press
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8236
8237 // BUTTON UP
8238 processKey(mapper, BTN_LEFT, 0);
8239 processSync(mapper);
8240
8241 // touchinputmapper design sends a move after button release
8242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8246
8247 // FINGER 0 UP
8248 processId(mapper, -1);
8249 processSync(mapper);
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8251 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8252
8253 // FINGER 1 MOVE
8254 processSlot(mapper, 1);
8255 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8256 processSync(mapper);
8257
8258 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8261 ASSERT_EQ(1U, args.pointerCount);
8262 ASSERT_EQ(1, args.pointerProperties[0].id);
8263 ASSERT_NO_FATAL_FAILURE(
8264 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8265
8266 // FINGER 1 UP
8267 processId(mapper, -1);
8268 processKey(mapper, BTN_TOUCH, 0);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8271 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8272
8273 // non captured touchpad should be a mouse source
8274 mFakePolicy->setPointerCapture(false);
8275 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8277 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8278}
8279
8280TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8281 std::shared_ptr<FakePointerController> fakePointerController =
8282 std::make_shared<FakePointerController>();
8283 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8284 fakePointerController->setPosition(0, 0);
8285 fakePointerController->setButtonState(0);
8286
8287 // prepare device and capture
8288 prepareDisplay(DISPLAY_ORIENTATION_0);
8289 prepareAxes(POSITION | ID | SLOT);
8290 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8291 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8292 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8293 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8294 // run uncaptured pointer tests - pushes out generic events
8295 // FINGER 0 DOWN
8296 processId(mapper, 3);
8297 processPosition(mapper, 100, 100);
8298 processKey(mapper, BTN_TOUCH, 1);
8299 processSync(mapper);
8300
8301 // start at (100,100), cursor should be at (0,0) * scale
8302 NotifyMotionArgs args;
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8305 ASSERT_NO_FATAL_FAILURE(
8306 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8307
8308 // FINGER 0 MOVE
8309 processPosition(mapper, 200, 200);
8310 processSync(mapper);
8311
8312 // compute scaling to help with touch position checking
8313 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8314 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8315 float scale =
8316 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8317
8318 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8322 0, 0, 0, 0, 0, 0, 0));
8323}
8324
8325TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8326 std::shared_ptr<FakePointerController> fakePointerController =
8327 std::make_shared<FakePointerController>();
8328
8329 prepareDisplay(DISPLAY_ORIENTATION_0);
8330 prepareAxes(POSITION | ID | SLOT);
8331 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8332 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8333 mFakePolicy->setPointerCapture(false);
8334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8335
8336 // uncaptured touchpad should be a pointer device
8337 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8338
8339 // captured touchpad should be a touchpad device
8340 mFakePolicy->setPointerCapture(true);
8341 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8342 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8343}
8344
Michael Wrightd02c5b62014-02-10 15:10:22 -08008345} // namespace android