blob: 2836516a416c0a2b19f7788a0ff6386da5111d48 [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
Kim Low03ea0352020-11-06 12:45:07 -080017#include <BatteryInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070018#include <CursorInputMapper.h>
19#include <InputDevice.h>
20#include <InputMapper.h>
21#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080022#include <InputReaderBase.h>
23#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
Chris Ye3fdbfef2021-01-06 18:45:18 -080025#include <LightInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070026#include <MultiTouchInputMapper.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <SingleTouchInputMapper.h>
29#include <SwitchInputMapper.h>
30#include <TestInputListener.h>
31#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080032#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000033#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070034#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <gtest/gtest.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080036#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <math.h>
38
Michael Wright17db18e2020-06-26 20:51:44 +010039#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080040#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070046using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070047using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048
49// Timeout for waiting for an expected event
50static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
51
Michael Wrightd02c5b62014-02-10 15:10:22 -080052// An arbitrary time value.
53static const nsecs_t ARBITRARY_TIME = 1234;
54
55// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080056static constexpr int32_t DISPLAY_ID = 0;
57static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
58static constexpr int32_t DISPLAY_WIDTH = 480;
59static constexpr int32_t DISPLAY_HEIGHT = 800;
60static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
61static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
62static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070063static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070064static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080065
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t FIRST_SLOT = 0;
67static constexpr int32_t SECOND_SLOT = 1;
68static constexpr int32_t THIRD_SLOT = 2;
69static constexpr int32_t INVALID_TRACKING_ID = -1;
70static constexpr int32_t FIRST_TRACKING_ID = 0;
71static constexpr int32_t SECOND_TRACKING_ID = 1;
72static constexpr int32_t THIRD_TRACKING_ID = 2;
Kim Low03ea0352020-11-06 12:45:07 -080073static constexpr int32_t BATTERY_STATUS = 4;
74static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080075static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
76static constexpr int32_t LIGHT_COLOR = 0x7F448866;
77static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080078
Michael Wrightd02c5b62014-02-10 15:10:22 -080079// Error tolerance for floating point assertions.
80static const float EPSILON = 0.001f;
81
82template<typename T>
83static inline T min(T a, T b) {
84 return a < b ? a : b;
85}
86
87static inline float avg(float x, float y) {
88 return (x + y) / 2;
89}
90
Chris Ye3fdbfef2021-01-06 18:45:18 -080091// Mapping for light color name and the light color
92const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
93 {"green", LightColor::GREEN},
94 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -080095
96// --- FakePointerController ---
97
98class FakePointerController : public PointerControllerInterface {
99 bool mHaveBounds;
100 float mMinX, mMinY, mMaxX, mMaxY;
101 float mX, mY;
102 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800103 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105public:
106 FakePointerController() :
107 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800108 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109 }
110
Michael Wright17db18e2020-06-26 20:51:44 +0100111 virtual ~FakePointerController() {}
112
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113 void setBounds(float minX, float minY, float maxX, float maxY) {
114 mHaveBounds = true;
115 mMinX = minX;
116 mMinY = minY;
117 mMaxX = maxX;
118 mMaxY = maxY;
119 }
120
Chris Yea52ade12020-08-27 16:49:20 -0700121 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122 mX = x;
123 mY = y;
124 }
125
Chris Yea52ade12020-08-27 16:49:20 -0700126 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127
Chris Yea52ade12020-08-27 16:49:20 -0700128 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800129
Chris Yea52ade12020-08-27 16:49:20 -0700130 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131 *outX = mX;
132 *outY = mY;
133 }
134
Chris Yea52ade12020-08-27 16:49:20 -0700135 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800136
Chris Yea52ade12020-08-27 16:49:20 -0700137 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800138 mDisplayId = viewport.displayId;
139 }
140
Arthur Hung7c645402019-01-25 17:45:42 +0800141 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
142 return mSpotsByDisplay;
143 }
144
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145private:
Chris Yea52ade12020-08-27 16:49:20 -0700146 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147 *outMinX = mMinX;
148 *outMinY = mMinY;
149 *outMaxX = mMaxX;
150 *outMaxY = mMaxY;
151 return mHaveBounds;
152 }
153
Chris Yea52ade12020-08-27 16:49:20 -0700154 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155 mX += deltaX;
156 if (mX < mMinX) mX = mMinX;
157 if (mX > mMaxX) mX = mMaxX;
158 mY += deltaY;
159 if (mY < mMinY) mY = mMinY;
160 if (mY > mMaxY) mY = mMaxY;
161 }
162
Chris Yea52ade12020-08-27 16:49:20 -0700163 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
Chris Yea52ade12020-08-27 16:49:20 -0700165 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
170 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800171 std::vector<int32_t> newSpots;
172 // Add spots for fingers that are down.
173 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
174 uint32_t id = idBits.clearFirstMarkedBit();
175 newSpots.push_back(id);
176 }
177
178 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 }
180
Chris Yea52ade12020-08-27 16:49:20 -0700181 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800182
183 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184};
185
186
187// --- FakeInputReaderPolicy ---
188
189class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700190 std::mutex mLock;
191 std::condition_variable mDevicesChangedCondition;
192
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100194 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
196 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100197 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700198 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199
200protected:
Chris Yea52ade12020-08-27 16:49:20 -0700201 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202
203public:
204 FakeInputReaderPolicy() {
205 }
206
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700207 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800208 waitForInputDevices([](bool devicesChanged) {
209 if (!devicesChanged) {
210 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
211 }
212 });
213 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700214
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800215 void assertInputDevicesNotChanged() {
216 waitForInputDevices([](bool devicesChanged) {
217 if (devicesChanged) {
218 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
219 }
220 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 }
222
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700223 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100224 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100225 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700226 }
227
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700228 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
229 return mConfig.getDisplayViewportByUniqueId(uniqueId);
230 }
231 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
232 return mConfig.getDisplayViewportByType(type);
233 }
234
235 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
236 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 }
238
239 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000240 bool isActive, const std::string& uniqueId,
241 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
242 const DisplayViewport viewport =
243 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
244 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700245 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100246 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247 }
248
Arthur Hung6cd19a42019-08-30 19:04:12 +0800249 bool updateViewport(const DisplayViewport& viewport) {
250 size_t count = mViewports.size();
251 for (size_t i = 0; i < count; i++) {
252 const DisplayViewport& currentViewport = mViewports[i];
253 if (currentViewport.displayId == viewport.displayId) {
254 mViewports[i] = viewport;
255 mConfig.setDisplayViewports(mViewports);
256 return true;
257 }
258 }
259 // no viewport found.
260 return false;
261 }
262
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100263 void addExcludedDeviceName(const std::string& deviceName) {
264 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265 }
266
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700267 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
268 mConfig.portAssociations.insert({inputPort, displayPort});
269 }
270
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000271 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700272
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000273 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700274
Michael Wright17db18e2020-06-26 20:51:44 +0100275 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
276 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277 }
278
279 const InputReaderConfiguration* getReaderConfiguration() const {
280 return &mConfig;
281 }
282
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800283 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 return mInputDevices;
285 }
286
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100287 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700288 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700289 return transform;
290 }
291
292 void setTouchAffineTransformation(const TouchAffineTransformation t) {
293 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800294 }
295
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800296 void setPointerCapture(bool enabled) {
297 mConfig.pointerCapture = enabled;
298 }
299
Arthur Hung7c645402019-01-25 17:45:42 +0800300 void setShowTouches(bool enabled) {
301 mConfig.showTouches = enabled;
302 }
303
Garfield Tan888a6a42020-01-09 11:39:16 -0800304 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
305 mConfig.defaultPointerDisplayId = pointerDisplayId;
306 }
307
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800308 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
309
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700311 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000312 int32_t orientation, bool isActive,
313 const std::string& uniqueId,
314 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700315 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
316 || orientation == DISPLAY_ORIENTATION_270);
317 DisplayViewport v;
318 v.displayId = displayId;
319 v.orientation = orientation;
320 v.logicalLeft = 0;
321 v.logicalTop = 0;
322 v.logicalRight = isRotated ? height : width;
323 v.logicalBottom = isRotated ? width : height;
324 v.physicalLeft = 0;
325 v.physicalTop = 0;
326 v.physicalRight = isRotated ? height : width;
327 v.physicalBottom = isRotated ? width : height;
328 v.deviceWidth = isRotated ? height : width;
329 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000330 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700331 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700332 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100333 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700334 return v;
335 }
336
Chris Yea52ade12020-08-27 16:49:20 -0700337 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 *outConfig = mConfig;
339 }
340
Chris Yea52ade12020-08-27 16:49:20 -0700341 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100342 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 }
344
Chris Yea52ade12020-08-27 16:49:20 -0700345 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700346 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700348 mInputDevicesChanged = true;
349 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 }
351
Chris Yea52ade12020-08-27 16:49:20 -0700352 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
353 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700354 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355 }
356
Chris Yea52ade12020-08-27 16:49:20 -0700357 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800358
359 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
360 std::unique_lock<std::mutex> lock(mLock);
361 base::ScopedLockAssertion assumeLocked(mLock);
362
363 const bool devicesChanged =
364 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
365 return mInputDevicesChanged;
366 });
367 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
368 mInputDevicesChanged = false;
369 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370};
371
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372// --- FakeEventHub ---
373
374class FakeEventHub : public EventHubInterface {
375 struct KeyInfo {
376 int32_t keyCode;
377 uint32_t flags;
378 };
379
Chris Yef59a2f42020-10-16 12:55:26 -0700380 struct SensorInfo {
381 InputDeviceSensorType sensorType;
382 int32_t sensorDataIndex;
383 };
384
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 struct Device {
386 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700387 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388 PropertyMap configuration;
389 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
390 KeyedVector<int, bool> relativeAxes;
391 KeyedVector<int32_t, int32_t> keyCodeStates;
392 KeyedVector<int32_t, int32_t> scanCodeStates;
393 KeyedVector<int32_t, int32_t> switchStates;
394 KeyedVector<int32_t, int32_t> absoluteAxisValue;
395 KeyedVector<int32_t, KeyInfo> keysByScanCode;
396 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
397 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700398 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
399 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800400 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700401 bool enabled;
402
403 status_t enable() {
404 enabled = true;
405 return OK;
406 }
407
408 status_t disable() {
409 enabled = false;
410 return OK;
411 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412
Chris Ye1b0c7342020-07-28 21:57:03 -0700413 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414 };
415
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700416 std::mutex mLock;
417 std::condition_variable mEventsCondition;
418
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100420 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000421 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600422 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000423 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800424 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
425 // Simulates a device light brightness, from light id to light brightness.
426 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
427 // Simulates a device light intensities, from light id to light intensities map.
428 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
429 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700431public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 virtual ~FakeEventHub() {
433 for (size_t i = 0; i < mDevices.size(); i++) {
434 delete mDevices.valueAt(i);
435 }
436 }
437
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 FakeEventHub() { }
439
Chris Ye1b0c7342020-07-28 21:57:03 -0700440 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441 Device* device = new Device(classes);
442 device->identifier.name = name;
443 mDevices.add(deviceId, device);
444
445 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
446 }
447
448 void removeDevice(int32_t deviceId) {
449 delete mDevices.valueFor(deviceId);
450 mDevices.removeItem(deviceId);
451
452 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
453 }
454
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700455 bool isDeviceEnabled(int32_t deviceId) {
456 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700457 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700458 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
459 return false;
460 }
461 return device->enabled;
462 }
463
464 status_t enableDevice(int32_t deviceId) {
465 status_t result;
466 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700467 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700468 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
469 return BAD_VALUE;
470 }
471 if (device->enabled) {
472 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
473 return OK;
474 }
475 result = device->enable();
476 return result;
477 }
478
479 status_t disableDevice(int32_t deviceId) {
480 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700481 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700482 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
483 return BAD_VALUE;
484 }
485 if (!device->enabled) {
486 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
487 return OK;
488 }
489 return device->disable();
490 }
491
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492 void finishDeviceScan() {
493 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
494 }
495
496 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
497 Device* device = getDevice(deviceId);
498 device->configuration.addProperty(key, value);
499 }
500
501 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
502 Device* device = getDevice(deviceId);
503 device->configuration.addAll(configuration);
504 }
505
506 void addAbsoluteAxis(int32_t deviceId, int axis,
507 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
508 Device* device = getDevice(deviceId);
509
510 RawAbsoluteAxisInfo info;
511 info.valid = true;
512 info.minValue = minValue;
513 info.maxValue = maxValue;
514 info.flat = flat;
515 info.fuzz = fuzz;
516 info.resolution = resolution;
517 device->absoluteAxes.add(axis, info);
518 }
519
520 void addRelativeAxis(int32_t deviceId, int32_t axis) {
521 Device* device = getDevice(deviceId);
522 device->relativeAxes.add(axis, true);
523 }
524
525 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
526 Device* device = getDevice(deviceId);
527 device->keyCodeStates.replaceValueFor(keyCode, state);
528 }
529
530 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
531 Device* device = getDevice(deviceId);
532 device->scanCodeStates.replaceValueFor(scanCode, state);
533 }
534
535 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
536 Device* device = getDevice(deviceId);
537 device->switchStates.replaceValueFor(switchCode, state);
538 }
539
540 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
541 Device* device = getDevice(deviceId);
542 device->absoluteAxisValue.replaceValueFor(axis, value);
543 }
544
545 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
546 int32_t keyCode, uint32_t flags) {
547 Device* device = getDevice(deviceId);
548 KeyInfo info;
549 info.keyCode = keyCode;
550 info.flags = flags;
551 if (scanCode) {
552 device->keysByScanCode.add(scanCode, info);
553 }
554 if (usageCode) {
555 device->keysByUsageCode.add(usageCode, info);
556 }
557 }
558
559 void addLed(int32_t deviceId, int32_t led, bool initialState) {
560 Device* device = getDevice(deviceId);
561 device->leds.add(led, initialState);
562 }
563
Chris Yef59a2f42020-10-16 12:55:26 -0700564 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
565 int32_t sensorDataIndex) {
566 Device* device = getDevice(deviceId);
567 SensorInfo info;
568 info.sensorType = sensorType;
569 info.sensorDataIndex = sensorDataIndex;
570 device->sensorsByAbsCode.emplace(absCode, info);
571 }
572
573 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
574 Device* device = getDevice(deviceId);
575 typename BitArray<MSC_MAX>::Buffer buffer;
576 buffer[mscEvent / 32] = 1 << mscEvent % 32;
577 device->mscBitmask.loadFromBuffer(buffer);
578 }
579
Chris Ye3fdbfef2021-01-06 18:45:18 -0800580 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
581 mRawLightInfos.emplace(rawId, std::move(info));
582 }
583
584 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
585 mLightBrightness.emplace(rawId, brightness);
586 }
587
588 void fakeLightIntensities(int32_t rawId,
589 const std::unordered_map<LightColor, int32_t> intensities) {
590 mLightIntensities.emplace(rawId, std::move(intensities));
591 }
592
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 bool getLedState(int32_t deviceId, int32_t led) {
594 Device* device = getDevice(deviceId);
595 return device->leds.valueFor(led);
596 }
597
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100598 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 return mExcludedDevices;
600 }
601
602 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
603 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800604 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 }
606
607 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
608 int32_t code, int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700609 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 RawEvent event;
611 event.when = when;
612 event.deviceId = deviceId;
613 event.type = type;
614 event.code = code;
615 event.value = value;
616 mEvents.push_back(event);
617
618 if (type == EV_ABS) {
619 setAbsoluteAxisValue(deviceId, code, value);
620 }
621 }
622
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600623 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
624 std::vector<TouchVideoFrame>> videoFrames) {
625 mVideoFrames = std::move(videoFrames);
626 }
627
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 std::unique_lock<std::mutex> lock(mLock);
630 base::ScopedLockAssertion assumeLocked(mLock);
631 const bool queueIsEmpty =
632 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
633 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
634 if (!queueIsEmpty) {
635 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
636 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 }
638
639private:
640 Device* getDevice(int32_t deviceId) const {
641 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100642 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 }
644
Chris Yea52ade12020-08-27 16:49:20 -0700645 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700647 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 }
649
Chris Yea52ade12020-08-27 16:49:20 -0700650 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 Device* device = getDevice(deviceId);
652 return device ? device->identifier : InputDeviceIdentifier();
653 }
654
Chris Yea52ade12020-08-27 16:49:20 -0700655 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656
Chris Yea52ade12020-08-27 16:49:20 -0700657 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 Device* device = getDevice(deviceId);
659 if (device) {
660 *outConfiguration = device->configuration;
661 }
662 }
663
Chris Yea52ade12020-08-27 16:49:20 -0700664 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
665 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800667 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 ssize_t index = device->absoluteAxes.indexOfKey(axis);
669 if (index >= 0) {
670 *outAxisInfo = device->absoluteAxes.valueAt(index);
671 return OK;
672 }
673 }
674 outAxisInfo->clear();
675 return -1;
676 }
677
Chris Yea52ade12020-08-27 16:49:20 -0700678 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 Device* device = getDevice(deviceId);
680 if (device) {
681 return device->relativeAxes.indexOfKey(axis) >= 0;
682 }
683 return false;
684 }
685
Chris Yea52ade12020-08-27 16:49:20 -0700686 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687
Chris Yef59a2f42020-10-16 12:55:26 -0700688 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
689 Device* device = getDevice(deviceId);
690 if (device) {
691 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
692 }
693 return false;
694 }
695
Chris Yea52ade12020-08-27 16:49:20 -0700696 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
697 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800698 Device* device = getDevice(deviceId);
699 if (device) {
700 const KeyInfo* key = getKey(device, scanCode, usageCode);
701 if (key) {
702 if (outKeycode) {
703 *outKeycode = key->keyCode;
704 }
705 if (outFlags) {
706 *outFlags = key->flags;
707 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700708 if (outMetaState) {
709 *outMetaState = metaState;
710 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 return OK;
712 }
713 }
714 return NAME_NOT_FOUND;
715 }
716
717 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
718 if (usageCode) {
719 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
720 if (index >= 0) {
721 return &device->keysByUsageCode.valueAt(index);
722 }
723 }
724 if (scanCode) {
725 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
726 if (index >= 0) {
727 return &device->keysByScanCode.valueAt(index);
728 }
729 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700730 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 }
732
Chris Yea52ade12020-08-27 16:49:20 -0700733 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734
Chris Yef59a2f42020-10-16 12:55:26 -0700735 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
736 int32_t absCode) {
737 Device* device = getDevice(deviceId);
738 if (!device) {
739 return Errorf("Sensor device not found.");
740 }
741 auto it = device->sensorsByAbsCode.find(absCode);
742 if (it == device->sensorsByAbsCode.end()) {
743 return Errorf("Sensor map not found.");
744 }
745 const SensorInfo& info = it->second;
746 return std::make_pair(info.sensorType, info.sensorDataIndex);
747 }
748
Chris Yea52ade12020-08-27 16:49:20 -0700749 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 mExcludedDevices = devices;
751 }
752
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000753 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
754 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000756 const size_t filledSize = std::min(mEvents.size(), bufferSize);
757 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
758
759 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700760 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000761 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 }
763
Chris Yea52ade12020-08-27 16:49:20 -0700764 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600765 auto it = mVideoFrames.find(deviceId);
766 if (it != mVideoFrames.end()) {
767 std::vector<TouchVideoFrame> frames = std::move(it->second);
768 mVideoFrames.erase(deviceId);
769 return frames;
770 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800771 return {};
772 }
773
Chris Yea52ade12020-08-27 16:49:20 -0700774 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775 Device* device = getDevice(deviceId);
776 if (device) {
777 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
778 if (index >= 0) {
779 return device->scanCodeStates.valueAt(index);
780 }
781 }
782 return AKEY_STATE_UNKNOWN;
783 }
784
Chris Yea52ade12020-08-27 16:49:20 -0700785 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800786 Device* device = getDevice(deviceId);
787 if (device) {
788 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
789 if (index >= 0) {
790 return device->keyCodeStates.valueAt(index);
791 }
792 }
793 return AKEY_STATE_UNKNOWN;
794 }
795
Chris Yea52ade12020-08-27 16:49:20 -0700796 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797 Device* device = getDevice(deviceId);
798 if (device) {
799 ssize_t index = device->switchStates.indexOfKey(sw);
800 if (index >= 0) {
801 return device->switchStates.valueAt(index);
802 }
803 }
804 return AKEY_STATE_UNKNOWN;
805 }
806
Chris Yea52ade12020-08-27 16:49:20 -0700807 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
808 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 Device* device = getDevice(deviceId);
810 if (device) {
811 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
812 if (index >= 0) {
813 *outValue = device->absoluteAxisValue.valueAt(index);
814 return OK;
815 }
816 }
817 *outValue = 0;
818 return -1;
819 }
820
Chris Yea52ade12020-08-27 16:49:20 -0700821 // Return true if the device has non-empty key layout.
822 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
823 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824 bool result = false;
825 Device* device = getDevice(deviceId);
826 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700827 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 for (size_t i = 0; i < numCodes; i++) {
829 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
830 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
831 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800832 }
833 }
834 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
835 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
836 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837 }
838 }
839 }
840 }
841 return result;
842 }
843
Chris Yea52ade12020-08-27 16:49:20 -0700844 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 Device* device = getDevice(deviceId);
846 if (device) {
847 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
848 return index >= 0;
849 }
850 return false;
851 }
852
Chris Yea52ade12020-08-27 16:49:20 -0700853 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 Device* device = getDevice(deviceId);
855 return device && device->leds.indexOfKey(led) >= 0;
856 }
857
Chris Yea52ade12020-08-27 16:49:20 -0700858 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859 Device* device = getDevice(deviceId);
860 if (device) {
861 ssize_t index = device->leds.indexOfKey(led);
862 if (index >= 0) {
863 device->leds.replaceValueAt(led, on);
864 } else {
865 ADD_FAILURE()
866 << "Attempted to set the state of an LED that the EventHub declared "
867 "was not present. led=" << led;
868 }
869 }
870 }
871
Chris Yea52ade12020-08-27 16:49:20 -0700872 void getVirtualKeyDefinitions(
873 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874 outVirtualKeys.clear();
875
876 Device* device = getDevice(deviceId);
877 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800878 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 }
880 }
881
Chris Yea52ade12020-08-27 16:49:20 -0700882 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700883 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884 }
885
Chris Yea52ade12020-08-27 16:49:20 -0700886 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887 return false;
888 }
889
Chris Yea52ade12020-08-27 16:49:20 -0700890 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891
Chris Yea52ade12020-08-27 16:49:20 -0700892 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893
Chris Ye87143712020-11-10 05:05:58 +0000894 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
895
Kim Low03ea0352020-11-06 12:45:07 -0800896 std::optional<int32_t> getBatteryCapacity(int32_t) const override { return BATTERY_CAPACITY; }
897
898 std::optional<int32_t> getBatteryStatus(int32_t) const override { return BATTERY_STATUS; }
899
Chris Ye3fdbfef2021-01-06 18:45:18 -0800900 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
901 std::vector<int32_t> ids;
902 for (const auto& [rawId, info] : mRawLightInfos) {
903 ids.push_back(rawId);
904 }
905 return ids;
906 }
907
908 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
909 auto it = mRawLightInfos.find(lightId);
910 if (it == mRawLightInfos.end()) {
911 return std::nullopt;
912 }
913 return it->second;
914 }
915
916 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
917 mLightBrightness.emplace(lightId, brightness);
918 }
919
920 void setLightIntensities(int32_t deviceId, int32_t lightId,
921 std::unordered_map<LightColor, int32_t> intensities) override {
922 mLightIntensities.emplace(lightId, intensities);
923 };
924
925 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
926 auto lightIt = mLightBrightness.find(lightId);
927 if (lightIt == mLightBrightness.end()) {
928 return std::nullopt;
929 }
930 return lightIt->second;
931 }
932
933 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
934 int32_t deviceId, int32_t lightId) override {
935 auto lightIt = mLightIntensities.find(lightId);
936 if (lightIt == mLightIntensities.end()) {
937 return std::nullopt;
938 }
939 return lightIt->second;
940 };
941
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100942 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943 return false;
944 }
945
Chris Yea52ade12020-08-27 16:49:20 -0700946 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947
Chris Yea52ade12020-08-27 16:49:20 -0700948 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949
Chris Yea52ade12020-08-27 16:49:20 -0700950 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951
Chris Yea52ade12020-08-27 16:49:20 -0700952 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953};
954
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955// --- FakeInputMapper ---
956
957class FakeInputMapper : public InputMapper {
958 uint32_t mSources;
959 int32_t mKeyboardType;
960 int32_t mMetaState;
961 KeyedVector<int32_t, int32_t> mKeyCodeStates;
962 KeyedVector<int32_t, int32_t> mScanCodeStates;
963 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800964 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700966 std::mutex mLock;
967 std::condition_variable mStateChangedCondition;
968 bool mConfigureWasCalled GUARDED_BY(mLock);
969 bool mResetWasCalled GUARDED_BY(mLock);
970 bool mProcessWasCalled GUARDED_BY(mLock);
971 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972
Arthur Hungc23540e2018-11-29 20:42:11 +0800973 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800975 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
976 : InputMapper(deviceContext),
977 mSources(sources),
978 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800980 mConfigureWasCalled(false),
981 mResetWasCalled(false),
982 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
Chris Yea52ade12020-08-27 16:49:20 -0700984 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985
986 void setKeyboardType(int32_t keyboardType) {
987 mKeyboardType = keyboardType;
988 }
989
990 void setMetaState(int32_t metaState) {
991 mMetaState = metaState;
992 }
993
994 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700995 std::unique_lock<std::mutex> lock(mLock);
996 base::ScopedLockAssertion assumeLocked(mLock);
997 const bool configureCalled =
998 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
999 return mConfigureWasCalled;
1000 });
1001 if (!configureCalled) {
1002 FAIL() << "Expected configure() to have been called.";
1003 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004 mConfigureWasCalled = false;
1005 }
1006
1007 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001008 std::unique_lock<std::mutex> lock(mLock);
1009 base::ScopedLockAssertion assumeLocked(mLock);
1010 const bool resetCalled =
1011 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1012 return mResetWasCalled;
1013 });
1014 if (!resetCalled) {
1015 FAIL() << "Expected reset() to have been called.";
1016 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017 mResetWasCalled = false;
1018 }
1019
Yi Kong9b14ac62018-07-17 13:48:38 -07001020 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001021 std::unique_lock<std::mutex> lock(mLock);
1022 base::ScopedLockAssertion assumeLocked(mLock);
1023 const bool processCalled =
1024 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1025 return mProcessWasCalled;
1026 });
1027 if (!processCalled) {
1028 FAIL() << "Expected process() to have been called.";
1029 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030 if (outLastEvent) {
1031 *outLastEvent = mLastEvent;
1032 }
1033 mProcessWasCalled = false;
1034 }
1035
1036 void setKeyCodeState(int32_t keyCode, int32_t state) {
1037 mKeyCodeStates.replaceValueFor(keyCode, state);
1038 }
1039
1040 void setScanCodeState(int32_t scanCode, int32_t state) {
1041 mScanCodeStates.replaceValueFor(scanCode, state);
1042 }
1043
1044 void setSwitchState(int32_t switchCode, int32_t state) {
1045 mSwitchStates.replaceValueFor(switchCode, state);
1046 }
1047
1048 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001049 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050 }
1051
1052private:
Chris Yea52ade12020-08-27 16:49:20 -07001053 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001054
Chris Yea52ade12020-08-27 16:49:20 -07001055 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056 InputMapper::populateDeviceInfo(deviceInfo);
1057
1058 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1059 deviceInfo->setKeyboardType(mKeyboardType);
1060 }
1061 }
1062
Chris Yea52ade12020-08-27 16:49:20 -07001063 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001064 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001066
1067 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001068 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001069 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1070 mViewport = config->getDisplayViewportByPort(*displayPort);
1071 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001072
1073 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074 }
1075
Chris Yea52ade12020-08-27 16:49:20 -07001076 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001077 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001079 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 }
1081
Chris Yea52ade12020-08-27 16:49:20 -07001082 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001083 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084 mLastEvent = *rawEvent;
1085 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001086 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087 }
1088
Chris Yea52ade12020-08-27 16:49:20 -07001089 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1091 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1092 }
1093
Chris Yea52ade12020-08-27 16:49:20 -07001094 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1096 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1097 }
1098
Chris Yea52ade12020-08-27 16:49:20 -07001099 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1101 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1102 }
1103
Chris Yea52ade12020-08-27 16:49:20 -07001104 // Return true if the device has non-empty key layout.
1105 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1106 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 for (size_t i = 0; i < numCodes; i++) {
1108 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1109 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1110 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 }
1112 }
1113 }
Chris Yea52ade12020-08-27 16:49:20 -07001114 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 return result;
1116 }
1117
1118 virtual int32_t getMetaState() {
1119 return mMetaState;
1120 }
1121
1122 virtual void fadePointer() {
1123 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001124
1125 virtual std::optional<int32_t> getAssociatedDisplay() {
1126 if (mViewport) {
1127 return std::make_optional(mViewport->displayId);
1128 }
1129 return std::nullopt;
1130 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131};
1132
1133
1134// --- InstrumentedInputReader ---
1135
1136class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001137 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138
1139public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001140 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1141 const sp<InputReaderPolicyInterface>& policy,
1142 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001143 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001145 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001147 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001149 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001150 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 InputDeviceIdentifier identifier;
1152 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001153 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001155 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 }
1157
Prabir Pradhan28efc192019-11-05 01:10:04 +00001158 // Make the protected loopOnce method accessible to tests.
1159 using InputReader::loopOnce;
1160
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001162 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1163 const InputDeviceIdentifier& identifier)
1164 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001165 if (!mNextDevices.empty()) {
1166 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1167 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 return device;
1169 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001170 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 }
1172
arthurhungdcef2dc2020-08-11 14:47:50 +08001173 // --- FakeInputReaderContext ---
1174 class FakeInputReaderContext : public ContextImpl {
1175 int32_t mGlobalMetaState;
1176 bool mUpdateGlobalMetaStateWasCalled;
1177 int32_t mGeneration;
1178
1179 public:
1180 FakeInputReaderContext(InputReader* reader)
1181 : ContextImpl(reader),
1182 mGlobalMetaState(0),
1183 mUpdateGlobalMetaStateWasCalled(false),
1184 mGeneration(1) {}
1185
1186 virtual ~FakeInputReaderContext() {}
1187
1188 void assertUpdateGlobalMetaStateWasCalled() {
1189 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1190 << "Expected updateGlobalMetaState() to have been called.";
1191 mUpdateGlobalMetaStateWasCalled = false;
1192 }
1193
1194 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1195
1196 uint32_t getGeneration() { return mGeneration; }
1197
1198 void updateGlobalMetaState() override {
1199 mUpdateGlobalMetaStateWasCalled = true;
1200 ContextImpl::updateGlobalMetaState();
1201 }
1202
1203 int32_t getGlobalMetaState() override {
1204 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1205 }
1206
1207 int32_t bumpGeneration() override {
1208 mGeneration = ContextImpl::bumpGeneration();
1209 return mGeneration;
1210 }
1211 } mFakeContext;
1212
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001214
1215public:
1216 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217};
1218
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001219// --- InputReaderPolicyTest ---
1220class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001221protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001222 sp<FakeInputReaderPolicy> mFakePolicy;
1223
Chris Yea52ade12020-08-27 16:49:20 -07001224 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1225 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001226};
1227
1228/**
1229 * Check that empty set of viewports is an acceptable configuration.
1230 * Also try to get internal viewport two different ways - by type and by uniqueId.
1231 *
1232 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1233 * Such configuration is not currently allowed.
1234 */
1235TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001236 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001237
1238 // We didn't add any viewports yet, so there shouldn't be any.
1239 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001240 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001241 ASSERT_FALSE(internalViewport);
1242
1243 // Add an internal viewport, then clear it
1244 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001245 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001246 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001247
1248 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001249 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001250 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001251 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252
1253 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001254 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001255 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001256 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001257
1258 mFakePolicy->clearViewports();
1259 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001260 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001261 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001262 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001263 ASSERT_FALSE(internalViewport);
1264}
1265
1266TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1267 const std::string internalUniqueId = "local:0";
1268 const std::string externalUniqueId = "local:1";
1269 const std::string virtualUniqueId1 = "virtual:2";
1270 const std::string virtualUniqueId2 = "virtual:3";
1271 constexpr int32_t virtualDisplayId1 = 2;
1272 constexpr int32_t virtualDisplayId2 = 3;
1273
1274 // Add an internal viewport
1275 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001276 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1277 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001278 // Add an external viewport
1279 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001280 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1281 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001282 // Add an virtual viewport
1283 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001284 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1285 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001286 // Add another virtual viewport
1287 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001288 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1289 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001290
1291 // Check matching by type for internal
1292 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001293 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001294 ASSERT_TRUE(internalViewport);
1295 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1296
1297 // Check matching by type for external
1298 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001299 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001300 ASSERT_TRUE(externalViewport);
1301 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1302
1303 // Check matching by uniqueId for virtual viewport #1
1304 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001305 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001306 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001307 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001308 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1309 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1310
1311 // Check matching by uniqueId for virtual viewport #2
1312 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001313 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001314 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001315 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001316 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1317 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1318}
1319
1320
1321/**
1322 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1323 * that lookup works by checking display id.
1324 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1325 */
1326TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1327 const std::string uniqueId1 = "uniqueId1";
1328 const std::string uniqueId2 = "uniqueId2";
1329 constexpr int32_t displayId1 = 2;
1330 constexpr int32_t displayId2 = 3;
1331
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001332 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1333 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001334 for (const ViewportType& type : types) {
1335 mFakePolicy->clearViewports();
1336 // Add a viewport
1337 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001338 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1339 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001340 // Add another viewport
1341 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001342 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1343 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001344
1345 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001346 std::optional<DisplayViewport> viewport1 =
1347 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348 ASSERT_TRUE(viewport1);
1349 ASSERT_EQ(displayId1, viewport1->displayId);
1350 ASSERT_EQ(type, viewport1->type);
1351
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001352 std::optional<DisplayViewport> viewport2 =
1353 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354 ASSERT_TRUE(viewport2);
1355 ASSERT_EQ(displayId2, viewport2->displayId);
1356 ASSERT_EQ(type, viewport2->type);
1357
1358 // When there are multiple viewports of the same kind, and uniqueId is not specified
1359 // in the call to getDisplayViewport, then that situation is not supported.
1360 // The viewports can be stored in any order, so we cannot rely on the order, since that
1361 // is just implementation detail.
1362 // However, we can check that it still returns *a* viewport, we just cannot assert
1363 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001364 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365 ASSERT_TRUE(someViewport);
1366 }
1367}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001368
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001369/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001370 * When we have multiple internal displays make sure we always return the default display when
1371 * querying by type.
1372 */
1373TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1374 const std::string uniqueId1 = "uniqueId1";
1375 const std::string uniqueId2 = "uniqueId2";
1376 constexpr int32_t nonDefaultDisplayId = 2;
1377 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1378 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1379
1380 // Add the default display first and ensure it gets returned.
1381 mFakePolicy->clearViewports();
1382 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001383 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001384 ViewportType::INTERNAL);
1385 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001386 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001387 ViewportType::INTERNAL);
1388
1389 std::optional<DisplayViewport> viewport =
1390 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1391 ASSERT_TRUE(viewport);
1392 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1393 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1394
1395 // Add the default display second to make sure order doesn't matter.
1396 mFakePolicy->clearViewports();
1397 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001398 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001399 ViewportType::INTERNAL);
1400 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001401 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001402 ViewportType::INTERNAL);
1403
1404 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1405 ASSERT_TRUE(viewport);
1406 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1407 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1408}
1409
1410/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001411 * Check getDisplayViewportByPort
1412 */
1413TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001414 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001415 const std::string uniqueId1 = "uniqueId1";
1416 const std::string uniqueId2 = "uniqueId2";
1417 constexpr int32_t displayId1 = 1;
1418 constexpr int32_t displayId2 = 2;
1419 const uint8_t hdmi1 = 0;
1420 const uint8_t hdmi2 = 1;
1421 const uint8_t hdmi3 = 2;
1422
1423 mFakePolicy->clearViewports();
1424 // Add a viewport that's associated with some display port that's not of interest.
1425 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001426 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1427 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001428 // Add another viewport, connected to HDMI1 port
1429 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001430 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1431 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001432
1433 // Check that correct display viewport was returned by comparing the display ports.
1434 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1435 ASSERT_TRUE(hdmi1Viewport);
1436 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1437 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1438
1439 // Check that we can still get the same viewport using the uniqueId
1440 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1441 ASSERT_TRUE(hdmi1Viewport);
1442 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1443 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1444 ASSERT_EQ(type, hdmi1Viewport->type);
1445
1446 // Check that we cannot find a port with "HDMI2", because we never added one
1447 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1448 ASSERT_FALSE(hdmi2Viewport);
1449}
1450
Michael Wrightd02c5b62014-02-10 15:10:22 -08001451// --- InputReaderTest ---
1452
1453class InputReaderTest : public testing::Test {
1454protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001455 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001456 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001457 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001458 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001459
Chris Yea52ade12020-08-27 16:49:20 -07001460 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001461 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001462 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001463 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001464
Prabir Pradhan28efc192019-11-05 01:10:04 +00001465 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1466 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001467 }
1468
Chris Yea52ade12020-08-27 16:49:20 -07001469 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001470 mFakeListener.clear();
1471 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001472 }
1473
Chris Ye1b0c7342020-07-28 21:57:03 -07001474 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001475 const PropertyMap* configuration) {
1476 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001477
1478 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001479 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001480 }
1481 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001482 mReader->loopOnce();
1483 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001484 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1485 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001486 }
1487
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001488 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001489 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001490 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001491 }
1492
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001493 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001494 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001495 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001496 }
1497
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001498 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001499 const std::string& name,
1500 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001501 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001502 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1503 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001504 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001505 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001506 return mapper;
1507 }
1508};
1509
Chris Ye98d3f532020-10-01 21:48:59 -07001510TEST_F(InputReaderTest, ReaderGetInputDevices) {
Chris Ye1b0c7342020-07-28 21:57:03 -07001511 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1512 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1513 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514
Chris Ye98d3f532020-10-01 21:48:59 -07001515 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001517 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001518 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1520 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1521 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
Chris Ye98d3f532020-10-01 21:48:59 -07001522}
1523
1524TEST_F(InputReaderTest, PolicyGetInputDevices) {
1525 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1526 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1527 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528
1529 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001530 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001531 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001532 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001533 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001534 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1535 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1536 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1537}
1538
Chris Yee7310032020-09-22 15:36:28 -07001539TEST_F(InputReaderTest, GetMergedInputDevices) {
1540 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1541 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1542 // Add two subdevices to device
1543 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1544 // Must add at least one mapper or the device will be ignored!
1545 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1546 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1547
1548 // Push same device instance for next device to be added, so they'll have same identifier.
1549 mReader->pushNextDevice(device);
1550 mReader->pushNextDevice(device);
1551 ASSERT_NO_FATAL_FAILURE(
1552 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1553 ASSERT_NO_FATAL_FAILURE(
1554 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1555
1556 // Two devices will be merged to one input device as they have same identifier
Chris Ye98d3f532020-10-01 21:48:59 -07001557 ASSERT_EQ(1U, mReader->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001558}
1559
Chris Yee14523a2020-12-19 13:46:00 -08001560TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1561 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1562 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1563 // Add two subdevices to device
1564 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1565 // Must add at least one mapper or the device will be ignored!
1566 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1567 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1568
1569 // Push same device instance for next device to be added, so they'll have same identifier.
1570 mReader->pushNextDevice(device);
1571 mReader->pushNextDevice(device);
1572 // Sensor device is initially disabled
1573 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1574 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1575 nullptr));
1576 // Device is disabled because the only sub device is a sensor device and disabled initially.
1577 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1578 ASSERT_FALSE(device->isEnabled());
1579 ASSERT_NO_FATAL_FAILURE(
1580 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1581 // The merged device is enabled if any sub device is enabled
1582 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1583 ASSERT_TRUE(device->isEnabled());
1584}
1585
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001586TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001588 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001589 constexpr int32_t eventHubId = 1;
1590 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001591 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001592 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001593 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001594 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001595
Yi Kong9b14ac62018-07-17 13:48:38 -07001596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001597
1598 NotifyDeviceResetArgs resetArgs;
1599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001600 ASSERT_EQ(deviceId, resetArgs.deviceId);
1601
1602 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001603 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001604 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001605
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607 ASSERT_EQ(deviceId, resetArgs.deviceId);
1608 ASSERT_EQ(device->isEnabled(), false);
1609
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001610 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001611 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001614 ASSERT_EQ(device->isEnabled(), false);
1615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001616 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001617 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001619 ASSERT_EQ(deviceId, resetArgs.deviceId);
1620 ASSERT_EQ(device->isEnabled(), true);
1621}
1622
Michael Wrightd02c5b62014-02-10 15:10:22 -08001623TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001624 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001625 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001627 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001628 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 AINPUT_SOURCE_KEYBOARD, nullptr);
1630 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631
1632 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1633 AINPUT_SOURCE_ANY, AKEYCODE_A))
1634 << "Should return unknown when the device id is >= 0 but unknown.";
1635
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001636 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1637 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1638 << "Should return unknown when the device id is valid but the sources are not "
1639 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001641 ASSERT_EQ(AKEY_STATE_DOWN,
1642 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1643 AKEYCODE_A))
1644 << "Should return value provided by mapper when device id is valid and the device "
1645 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646
1647 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1648 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1649 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1650
1651 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1652 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1653 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1654}
1655
1656TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001657 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001658 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001659 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001660 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001661 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001662 AINPUT_SOURCE_KEYBOARD, nullptr);
1663 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664
1665 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1666 AINPUT_SOURCE_ANY, KEY_A))
1667 << "Should return unknown when the device id is >= 0 but unknown.";
1668
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001669 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1670 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1671 << "Should return unknown when the device id is valid but the sources are not "
1672 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001673
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001674 ASSERT_EQ(AKEY_STATE_DOWN,
1675 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1676 KEY_A))
1677 << "Should return value provided by mapper when device id is valid and the device "
1678 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679
1680 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1681 AINPUT_SOURCE_TRACKBALL, KEY_A))
1682 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1683
1684 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1685 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1686 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1687}
1688
1689TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001691 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001692 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001693 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001694 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001695 AINPUT_SOURCE_KEYBOARD, nullptr);
1696 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001697
1698 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1699 AINPUT_SOURCE_ANY, SW_LID))
1700 << "Should return unknown when the device id is >= 0 but unknown.";
1701
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001702 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1703 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1704 << "Should return unknown when the device id is valid but the sources are not "
1705 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001706
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 ASSERT_EQ(AKEY_STATE_DOWN,
1708 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1709 SW_LID))
1710 << "Should return value provided by mapper when device id is valid and the device "
1711 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712
1713 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1714 AINPUT_SOURCE_TRACKBALL, SW_LID))
1715 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1716
1717 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1718 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1719 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1720}
1721
1722TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001723 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001724 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001725 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001726 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001728 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001729
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001730 mapper.addSupportedKeyCode(AKEYCODE_A);
1731 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001732
1733 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1734 uint8_t flags[4] = { 0, 0, 0, 1 };
1735
1736 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1737 << "Should return false when device id is >= 0 but unknown.";
1738 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1739
1740 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001741 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1742 << "Should return false when device id is valid but the sources are not supported by "
1743 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1745
1746 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001747 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1748 keyCodes, flags))
1749 << "Should return value provided by mapper when device id is valid and the device "
1750 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001751 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1752
1753 flags[3] = 1;
1754 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1755 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1756 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1757
1758 flags[3] = 1;
1759 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1760 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1761 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1762}
1763
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001764TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001765 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001766 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001767
1768 NotifyConfigurationChangedArgs args;
1769
1770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1771 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1772}
1773
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001774TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001775 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001776 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001777 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001778 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001780 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001783 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1785
1786 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001787 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001788 ASSERT_EQ(0, event.when);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001789 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001790 ASSERT_EQ(EV_KEY, event.type);
1791 ASSERT_EQ(KEY_A, event.code);
1792 ASSERT_EQ(1, event.value);
1793}
1794
Garfield Tan1c7bc862020-01-28 13:24:04 -08001795TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001796 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001797 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001798 constexpr int32_t eventHubId = 1;
1799 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001800 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001801 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001802 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001804
1805 NotifyDeviceResetArgs resetArgs;
1806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001807 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001808
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001809 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001810 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001812 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001813 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001815 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001816 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001818 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001819 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001820
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001821 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001822 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001824 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001825 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001826}
1827
Garfield Tan1c7bc862020-01-28 13:24:04 -08001828TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1829 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001830 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001831 constexpr int32_t eventHubId = 1;
1832 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1833 // Must add at least one mapper or the device will be ignored!
1834 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001835 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001836 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1837
1838 NotifyDeviceResetArgs resetArgs;
1839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1840 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1841}
1842
Arthur Hungc23540e2018-11-29 20:42:11 +08001843TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001845 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001846 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001847 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001848 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1849 FakeInputMapper& mapper =
1850 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001851 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001852
1853 const uint8_t hdmi1 = 1;
1854
1855 // Associated touch screen with second display.
1856 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1857
1858 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001859 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001860 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001861 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001862 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001863 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001864 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001865 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001866 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001867 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001868
1869 // Add the device, and make sure all of the callbacks are triggered.
1870 // The device is added after the input port associations are processed since
1871 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001875 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001876
Arthur Hung2c9a3342019-07-23 14:18:59 +08001877 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001878 ASSERT_EQ(deviceId, device->getId());
1879 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1880 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001881
1882 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001883 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001884 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001885 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001886}
1887
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001888TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1889 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1890 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1891 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1892 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1893 // Must add at least one mapper or the device will be ignored!
1894 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1895 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1896 mReader->pushNextDevice(device);
1897 mReader->pushNextDevice(device);
1898 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1899 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1900
1901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1902
1903 NotifyDeviceResetArgs resetArgs;
1904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1905 ASSERT_EQ(deviceId, resetArgs.deviceId);
1906 ASSERT_TRUE(device->isEnabled());
1907 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1908 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1909
1910 disableDevice(deviceId);
1911 mReader->loopOnce();
1912
1913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1914 ASSERT_EQ(deviceId, resetArgs.deviceId);
1915 ASSERT_FALSE(device->isEnabled());
1916 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1917 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1918
1919 enableDevice(deviceId);
1920 mReader->loopOnce();
1921
1922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1923 ASSERT_EQ(deviceId, resetArgs.deviceId);
1924 ASSERT_TRUE(device->isEnabled());
1925 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1926 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1927}
1928
1929TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1930 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1931 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1932 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1933 // Add two subdevices to device
1934 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1935 FakeInputMapper& mapperDevice1 =
1936 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1937 FakeInputMapper& mapperDevice2 =
1938 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1939 mReader->pushNextDevice(device);
1940 mReader->pushNextDevice(device);
1941 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1942 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1943
1944 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1945 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1946
1947 ASSERT_EQ(AKEY_STATE_DOWN,
1948 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1949 ASSERT_EQ(AKEY_STATE_DOWN,
1950 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1951 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1952 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1953}
1954
Prabir Pradhan7e186182020-11-10 13:56:45 -08001955TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1956 NotifyPointerCaptureChangedArgs args;
1957
1958 mFakePolicy->setPointerCapture(true);
1959 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1960 mReader->loopOnce();
1961 mFakeListener->assertNotifyCaptureWasCalled(&args);
1962 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1963
1964 mFakePolicy->setPointerCapture(false);
1965 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1966 mReader->loopOnce();
1967 mFakeListener->assertNotifyCaptureWasCalled(&args);
1968 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1969
1970 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1971 // does not change.
1972 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1973 mReader->loopOnce();
1974 mFakeListener->assertNotifyCaptureWasCalled(&args);
1975 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1976}
1977
Chris Ye87143712020-11-10 05:05:58 +00001978class FakeVibratorInputMapper : public FakeInputMapper {
1979public:
1980 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1981 : FakeInputMapper(deviceContext, sources) {}
1982
1983 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1984};
1985
1986TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1987 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1988 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
1989 constexpr int32_t eventHubId = 1;
1990 const char* DEVICE_LOCATION = "BLUETOOTH";
1991 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1992 FakeVibratorInputMapper& mapper =
1993 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1994 mReader->pushNextDevice(device);
1995
1996 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1997 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1998
1999 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2000 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2001}
2002
Kim Low03ea0352020-11-06 12:45:07 -08002003class FakeBatteryInputMapper : public FakeInputMapper {
2004public:
2005 FakeBatteryInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2006 : FakeInputMapper(deviceContext, sources) {}
2007
2008 std::optional<int32_t> getBatteryCapacity() override {
2009 return getDeviceContext().getBatteryCapacity();
2010 }
2011
2012 std::optional<int32_t> getBatteryStatus() override {
2013 return getDeviceContext().getBatteryStatus();
2014 }
2015};
2016
2017TEST_F(InputReaderTest, BatteryGetCapacity) {
2018 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2019 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2020 constexpr int32_t eventHubId = 1;
2021 const char* DEVICE_LOCATION = "BLUETOOTH";
2022 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2023 FakeBatteryInputMapper& mapper =
2024 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2025 mReader->pushNextDevice(device);
2026
2027 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2028 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2029
2030 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2031}
2032
2033TEST_F(InputReaderTest, BatteryGetStatus) {
2034 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2035 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2036 constexpr int32_t eventHubId = 1;
2037 const char* DEVICE_LOCATION = "BLUETOOTH";
2038 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2039 FakeBatteryInputMapper& mapper =
2040 device->addMapper<FakeBatteryInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2041 mReader->pushNextDevice(device);
2042
2043 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2044 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2045
2046 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2047}
2048
Chris Ye3fdbfef2021-01-06 18:45:18 -08002049class FakeLightInputMapper : public FakeInputMapper {
2050public:
2051 FakeLightInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2052 : FakeInputMapper(deviceContext, sources) {}
2053
2054 bool setLightColor(int32_t lightId, int32_t color) override {
2055 getDeviceContext().setLightBrightness(lightId, color >> 24);
2056 return true;
2057 }
2058
2059 std::optional<int32_t> getLightColor(int32_t lightId) override {
2060 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2061 if (!result.has_value()) {
2062 return std::nullopt;
2063 }
2064 return result.value() << 24;
2065 }
2066};
2067
2068TEST_F(InputReaderTest, LightGetColor) {
2069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2070 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2071 constexpr int32_t eventHubId = 1;
2072 const char* DEVICE_LOCATION = "BLUETOOTH";
2073 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2074 FakeLightInputMapper& mapper =
2075 device->addMapper<FakeLightInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2076 mReader->pushNextDevice(device);
2077 RawLightInfo info = {.id = 1,
2078 .name = "Mono",
2079 .maxBrightness = 255,
2080 .flags = InputLightClass::BRIGHTNESS,
2081 .path = ""};
2082 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2083 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2084
2085 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2086 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2087
2088 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2089 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2090}
2091
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002092// --- InputReaderIntegrationTest ---
2093
2094// These tests create and interact with the InputReader only through its interface.
2095// The InputReader is started during SetUp(), which starts its processing in its own
2096// thread. The tests use linux uinput to emulate input devices.
2097// NOTE: Interacting with the physical device while these tests are running may cause
2098// the tests to fail.
2099class InputReaderIntegrationTest : public testing::Test {
2100protected:
2101 sp<TestInputListener> mTestListener;
2102 sp<FakeInputReaderPolicy> mFakePolicy;
2103 sp<InputReaderInterface> mReader;
2104
Chris Yea52ade12020-08-27 16:49:20 -07002105 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002106 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002107 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2108 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002109
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002110 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002111 ASSERT_EQ(mReader->start(), OK);
2112
2113 // Since this test is run on a real device, all the input devices connected
2114 // to the test device will show up in mReader. We wait for those input devices to
2115 // show up before beginning the tests.
2116 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2117 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2118 }
2119
Chris Yea52ade12020-08-27 16:49:20 -07002120 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002121 ASSERT_EQ(mReader->stop(), OK);
2122 mTestListener.clear();
2123 mFakePolicy.clear();
2124 }
2125};
2126
2127TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2128 // An invalid input device that is only used for this test.
2129 class InvalidUinputDevice : public UinputDevice {
2130 public:
2131 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2132
2133 private:
2134 void configureDevice(int fd, uinput_user_dev* device) override {}
2135 };
2136
2137 const size_t numDevices = mFakePolicy->getInputDevices().size();
2138
2139 // UinputDevice does not set any event or key bits, so InputReader should not
2140 // consider it as a valid device.
2141 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2142 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2143 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2144 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2145
2146 invalidDevice.reset();
2147 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2148 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2149 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2150}
2151
2152TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2153 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2154
2155 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2156 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2157 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2158 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2159
2160 // Find the test device by its name.
Chris Ye98d3f532020-10-01 21:48:59 -07002161 const std::vector<InputDeviceInfo> inputDevices = mReader->getInputDevices();
2162 const auto& it =
2163 std::find_if(inputDevices.begin(), inputDevices.end(),
2164 [&keyboard](const InputDeviceInfo& info) {
2165 return info.getIdentifier().name == keyboard->getName();
2166 });
2167
2168 ASSERT_NE(it, inputDevices.end());
2169 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2170 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2171 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002172
2173 keyboard.reset();
2174 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2175 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2176 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2177}
2178
2179TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2180 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2181 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2182
2183 NotifyConfigurationChangedArgs configChangedArgs;
2184 ASSERT_NO_FATAL_FAILURE(
2185 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002186 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002187 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2188
2189 NotifyKeyArgs keyArgs;
2190 keyboard->pressAndReleaseHomeKey();
2191 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2192 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002193 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002194 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002195 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2196 prevTimestamp = keyArgs.eventTime;
2197
2198 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2199 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002200 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002201 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
2202}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002203
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002204/**
2205 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2206 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2207 * are passed to the listener.
2208 */
2209static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2210TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2211 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2212 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2213 NotifyKeyArgs keyArgs;
2214
2215 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2216 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2218 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2219
2220 controller->pressAndReleaseKey(BTN_GEAR_UP);
2221 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2223 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2224}
2225
Arthur Hungaab25622020-01-16 11:22:11 +08002226// --- TouchProcessTest ---
2227class TouchIntegrationTest : public InputReaderIntegrationTest {
2228protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002229 const std::string UNIQUE_ID = "local:0";
2230
Chris Yea52ade12020-08-27 16:49:20 -07002231 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002232 InputReaderIntegrationTest::SetUp();
2233 // At least add an internal display.
2234 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2235 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002236 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002237
2238 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2239 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2240 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2241 }
2242
2243 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2244 int32_t orientation, const std::string& uniqueId,
2245 std::optional<uint8_t> physicalPort,
2246 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002247 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2248 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002249 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2250 }
2251
2252 std::unique_ptr<UinputTouchScreen> mDevice;
2253};
2254
2255TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2256 NotifyMotionArgs args;
2257 const Point centerPoint = mDevice->getCenterPoint();
2258
2259 // ACTION_DOWN
2260 mDevice->sendDown(centerPoint);
2261 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2263
2264 // ACTION_MOVE
2265 mDevice->sendMove(centerPoint + Point(1, 1));
2266 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2268
2269 // ACTION_UP
2270 mDevice->sendUp();
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2272 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2273}
2274
2275TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2276 NotifyMotionArgs args;
2277 const Point centerPoint = mDevice->getCenterPoint();
2278
2279 // ACTION_DOWN
2280 mDevice->sendDown(centerPoint);
2281 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2283
2284 // ACTION_POINTER_DOWN (Second slot)
2285 const Point secondPoint = centerPoint + Point(100, 100);
2286 mDevice->sendSlot(SECOND_SLOT);
2287 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2288 mDevice->sendDown(secondPoint + Point(1, 1));
2289 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2290 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2291 args.action);
2292
2293 // ACTION_MOVE (Second slot)
2294 mDevice->sendMove(secondPoint);
2295 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2297
2298 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002299 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002300 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002301 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002302 args.action);
2303
2304 // ACTION_UP
2305 mDevice->sendSlot(FIRST_SLOT);
2306 mDevice->sendUp();
2307 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2308 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2309}
2310
2311TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2312 NotifyMotionArgs args;
2313 const Point centerPoint = mDevice->getCenterPoint();
2314
2315 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002316 mDevice->sendSlot(FIRST_SLOT);
2317 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002318 mDevice->sendDown(centerPoint);
2319 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2321
arthurhungcc7f9802020-04-30 17:55:40 +08002322 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002323 const Point secondPoint = centerPoint + Point(100, 100);
2324 mDevice->sendSlot(SECOND_SLOT);
2325 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2326 mDevice->sendDown(secondPoint);
2327 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2328 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2329 args.action);
2330
arthurhungcc7f9802020-04-30 17:55:40 +08002331 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002332 mDevice->sendMove(secondPoint + Point(1, 1));
2333 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2335
arthurhungcc7f9802020-04-30 17:55:40 +08002336 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2337 // a palm event.
2338 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002339 mDevice->sendToolType(MT_TOOL_PALM);
2340 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002341 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2342 args.action);
2343 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002344
arthurhungcc7f9802020-04-30 17:55:40 +08002345 // Send up to second slot, expect first slot send moving.
2346 mDevice->sendPointerUp();
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002349
arthurhungcc7f9802020-04-30 17:55:40 +08002350 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002351 mDevice->sendSlot(FIRST_SLOT);
2352 mDevice->sendUp();
2353
arthurhungcc7f9802020-04-30 17:55:40 +08002354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2355 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002356}
2357
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002359class InputDeviceTest : public testing::Test {
2360protected:
2361 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002362 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 static const int32_t DEVICE_ID;
2364 static const int32_t DEVICE_GENERATION;
2365 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002366 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002367 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002369 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002371 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002372 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002373 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002374
Chris Yea52ade12020-08-27 16:49:20 -07002375 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002376 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002377 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002378 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002379 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2380 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 InputDeviceIdentifier identifier;
2382 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002383 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002384 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002385 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002386 mReader->pushNextDevice(mDevice);
2387 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2388 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 }
2390
Chris Yea52ade12020-08-27 16:49:20 -07002391 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392 mFakeListener.clear();
2393 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394 }
2395};
2396
2397const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002398const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002399const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2401const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002402const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2403 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002404const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405
2406TEST_F(InputDeviceTest, ImmutableProperties) {
2407 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002408 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002409 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410}
2411
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002412TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2413 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002414}
2415
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2417 // Configuration.
2418 InputReaderConfiguration config;
2419 mDevice->configure(ARBITRARY_TIME, &config, 0);
2420
2421 // Reset.
2422 mDevice->reset(ARBITRARY_TIME);
2423
2424 NotifyDeviceResetArgs resetArgs;
2425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2426 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2427 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2428
2429 // Metadata.
2430 ASSERT_TRUE(mDevice->isIgnored());
2431 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2432
2433 InputDeviceInfo info;
2434 mDevice->getDeviceInfo(&info);
2435 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002436 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2438 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2439
2440 // State queries.
2441 ASSERT_EQ(0, mDevice->getMetaState());
2442
2443 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2444 << "Ignored device should return unknown key code state.";
2445 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2446 << "Ignored device should return unknown scan code state.";
2447 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2448 << "Ignored device should return unknown switch state.";
2449
2450 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2451 uint8_t flags[2] = { 0, 1 };
2452 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2453 << "Ignored device should never mark any key codes.";
2454 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2455 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2456}
2457
2458TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2459 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002460 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002462 FakeInputMapper& mapper1 =
2463 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002464 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2465 mapper1.setMetaState(AMETA_ALT_ON);
2466 mapper1.addSupportedKeyCode(AKEYCODE_A);
2467 mapper1.addSupportedKeyCode(AKEYCODE_B);
2468 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2469 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2470 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2471 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2472 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002474 FakeInputMapper& mapper2 =
2475 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002476 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002477
2478 InputReaderConfiguration config;
2479 mDevice->configure(ARBITRARY_TIME, &config, 0);
2480
2481 String8 propertyValue;
2482 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2483 << "Device should have read configuration during configuration phase.";
2484 ASSERT_STREQ("value", propertyValue.string());
2485
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002486 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2487 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002488
2489 // Reset
2490 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002491 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2492 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002493
2494 NotifyDeviceResetArgs resetArgs;
2495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2496 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2497 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2498
2499 // Metadata.
2500 ASSERT_FALSE(mDevice->isIgnored());
2501 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2502
2503 InputDeviceInfo info;
2504 mDevice->getDeviceInfo(&info);
2505 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002506 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2508 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2509
2510 // State queries.
2511 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2512 << "Should query mappers and combine meta states.";
2513
2514 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2515 << "Should return unknown key code state when source not supported.";
2516 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2517 << "Should return unknown scan code state when source not supported.";
2518 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2519 << "Should return unknown switch state when source not supported.";
2520
2521 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2522 << "Should query mapper when source is supported.";
2523 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2524 << "Should query mapper when source is supported.";
2525 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2526 << "Should query mapper when source is supported.";
2527
2528 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2529 uint8_t flags[4] = { 0, 0, 0, 1 };
2530 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2531 << "Should do nothing when source is unsupported.";
2532 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2533 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2534 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2535 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2536
2537 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2538 << "Should query mapper when source is supported.";
2539 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2540 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2541 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2542 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2543
2544 // Event handling.
2545 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002546 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002547 mDevice->process(&event, 1);
2548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002549 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2550 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551}
2552
Arthur Hung2c9a3342019-07-23 14:18:59 +08002553// A single input device is associated with a specific display. Check that:
2554// 1. Device is disabled if the viewport corresponding to the associated display is not found
2555// 2. Device is disabled when setEnabled API is called
2556TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002557 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002558
2559 // First Configuration.
2560 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2561
2562 // Device should be enabled by default.
2563 ASSERT_TRUE(mDevice->isEnabled());
2564
2565 // Prepare associated info.
2566 constexpr uint8_t hdmi = 1;
2567 const std::string UNIQUE_ID = "local:1";
2568
2569 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2570 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2571 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2572 // Device should be disabled because it is associated with a specific display via
2573 // input port <-> display port association, but the corresponding display is not found
2574 ASSERT_FALSE(mDevice->isEnabled());
2575
2576 // Prepare displays.
2577 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002578 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2579 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002580 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2581 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2582 ASSERT_TRUE(mDevice->isEnabled());
2583
2584 // Device should be disabled after set disable.
2585 mFakePolicy->addDisabledDevice(mDevice->getId());
2586 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2587 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2588 ASSERT_FALSE(mDevice->isEnabled());
2589
2590 // Device should still be disabled even found the associated display.
2591 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2592 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2593 ASSERT_FALSE(mDevice->isEnabled());
2594}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002595
2596// --- InputMapperTest ---
2597
2598class InputMapperTest : public testing::Test {
2599protected:
2600 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002601 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602 static const int32_t DEVICE_ID;
2603 static const int32_t DEVICE_GENERATION;
2604 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002605 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002606 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002608 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002610 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002611 std::unique_ptr<InstrumentedInputReader> mReader;
2612 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
Chris Ye1b0c7342020-07-28 21:57:03 -07002614 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002615 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002617 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002618 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2619 mFakeListener);
2620 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621 }
2622
Chris Yea52ade12020-08-27 16:49:20 -07002623 void SetUp() override { SetUp(DEVICE_CLASSES); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002624
Chris Yea52ade12020-08-27 16:49:20 -07002625 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626 mFakeListener.clear();
2627 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628 }
2629
2630 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002631 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632 }
2633
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002634 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002635 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002636 mReader->requestRefreshConfiguration(changes);
2637 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002638 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002639 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2640 }
2641
arthurhungdcef2dc2020-08-11 14:47:50 +08002642 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2643 const std::string& location, int32_t eventHubId,
2644 Flags<InputDeviceClass> classes) {
2645 InputDeviceIdentifier identifier;
2646 identifier.name = name;
2647 identifier.location = location;
2648 std::shared_ptr<InputDevice> device =
2649 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2650 identifier);
2651 mReader->pushNextDevice(device);
2652 mFakeEventHub->addDevice(eventHubId, name, classes);
2653 mReader->loopOnce();
2654 return device;
2655 }
2656
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002657 template <class T, typename... Args>
2658 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002659 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002660 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002662 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002663 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664 }
2665
2666 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002667 int32_t orientation, const std::string& uniqueId,
2668 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002669 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2670 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002671 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2672 }
2673
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002674 void clearViewports() {
2675 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676 }
2677
arthurhungdcef2dc2020-08-11 14:47:50 +08002678 void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679 RawEvent event;
2680 event.when = when;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002681 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682 event.type = type;
2683 event.code = code;
2684 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002685 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002686 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 }
2688
2689 static void assertMotionRange(const InputDeviceInfo& info,
2690 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2691 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002692 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2694 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2695 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2696 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2697 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2698 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2699 }
2700
2701 static void assertPointerCoords(const PointerCoords& coords,
2702 float x, float y, float pressure, float size,
2703 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2704 float orientation, float distance) {
2705 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2706 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2707 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2708 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2709 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2710 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2711 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2712 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2713 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2714 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2715 }
2716
Michael Wright17db18e2020-06-26 20:51:44 +01002717 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002719 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720 ASSERT_NEAR(x, actualX, 1);
2721 ASSERT_NEAR(y, actualY, 1);
2722 }
2723};
2724
2725const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002726const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002727const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2729const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002730const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2731 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002732const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002733
2734// --- SwitchInputMapperTest ---
2735
2736class SwitchInputMapperTest : public InputMapperTest {
2737protected:
2738};
2739
2740TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002741 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002743 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744}
2745
2746TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002747 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002749 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002750 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002752 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002753 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754}
2755
2756TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08002759 process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2760 process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2761 process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2762 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763
2764 NotifySwitchArgs args;
2765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2766 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002767 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2768 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769 args.switchMask);
2770 ASSERT_EQ(uint32_t(0), args.policyFlags);
2771}
2772
Chris Ye87143712020-11-10 05:05:58 +00002773// --- VibratorInputMapperTest ---
2774class VibratorInputMapperTest : public InputMapperTest {
2775protected:
2776 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2777};
2778
2779TEST_F(VibratorInputMapperTest, GetSources) {
2780 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2781
2782 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2783}
2784
2785TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2786 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2787
2788 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2789}
2790
2791TEST_F(VibratorInputMapperTest, Vibrate) {
2792 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002793 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002794 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2795
2796 VibrationElement pattern(2);
2797 VibrationSequence sequence(2);
2798 pattern.duration = std::chrono::milliseconds(200);
2799 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2800 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2801 sequence.addElement(pattern);
2802 pattern.duration = std::chrono::milliseconds(500);
2803 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2804 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2805 sequence.addElement(pattern);
2806
2807 std::vector<int64_t> timings = {0, 1};
2808 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2809
2810 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002811 // Start vibrating
2812 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002813 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002814 // Verify vibrator state listener was notified.
2815 mReader->loopOnce();
2816 NotifyVibratorStateArgs args;
2817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2818 ASSERT_EQ(DEVICE_ID, args.deviceId);
2819 ASSERT_TRUE(args.isOn);
2820 // Stop vibrating
2821 mapper.cancelVibrate(VIBRATION_TOKEN);
2822 ASSERT_FALSE(mapper.isVibrating());
2823 // Verify vibrator state listener was notified.
2824 mReader->loopOnce();
2825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2826 ASSERT_EQ(DEVICE_ID, args.deviceId);
2827 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002828}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829
Chris Yef59a2f42020-10-16 12:55:26 -07002830// --- SensorInputMapperTest ---
2831
2832class SensorInputMapperTest : public InputMapperTest {
2833protected:
2834 static const int32_t ACCEL_RAW_MIN;
2835 static const int32_t ACCEL_RAW_MAX;
2836 static const int32_t ACCEL_RAW_FUZZ;
2837 static const int32_t ACCEL_RAW_FLAT;
2838 static const int32_t ACCEL_RAW_RESOLUTION;
2839
2840 static const int32_t GYRO_RAW_MIN;
2841 static const int32_t GYRO_RAW_MAX;
2842 static const int32_t GYRO_RAW_FUZZ;
2843 static const int32_t GYRO_RAW_FLAT;
2844 static const int32_t GYRO_RAW_RESOLUTION;
2845
2846 static const float GRAVITY_MS2_UNIT;
2847 static const float DEGREE_RADIAN_UNIT;
2848
2849 void prepareAccelAxes();
2850 void prepareGyroAxes();
2851 void setAccelProperties();
2852 void setGyroProperties();
2853 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2854};
2855
2856const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2857const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2858const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2859const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2860const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2861
2862const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2863const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2864const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2865const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2866const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2867
2868const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2869const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2870
2871void SensorInputMapperTest::prepareAccelAxes() {
2872 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2873 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2874 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2875 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2876 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2877 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2878}
2879
2880void SensorInputMapperTest::prepareGyroAxes() {
2881 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2882 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2884 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2885 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2886 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2887}
2888
2889void SensorInputMapperTest::setAccelProperties() {
2890 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2891 /* sensorDataIndex */ 0);
2892 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2893 /* sensorDataIndex */ 1);
2894 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2895 /* sensorDataIndex */ 2);
2896 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2897 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2898 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2899 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2900 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2901}
2902
2903void SensorInputMapperTest::setGyroProperties() {
2904 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2905 /* sensorDataIndex */ 0);
2906 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2907 /* sensorDataIndex */ 1);
2908 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2909 /* sensorDataIndex */ 2);
2910 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2911 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2912 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2913 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2914 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2915}
2916
2917TEST_F(SensorInputMapperTest, GetSources) {
2918 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2919
2920 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2921}
2922
2923TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2924 setAccelProperties();
2925 prepareAccelAxes();
2926 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2927
2928 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2929 std::chrono::microseconds(10000),
2930 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002931 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002932 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, 20000);
2933 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, -20000);
2934 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Z, 40000);
2935 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2936 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2937
2938 NotifySensorArgs args;
2939 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2940 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2941 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2942
2943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2944 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2945 ASSERT_EQ(args.deviceId, DEVICE_ID);
2946 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2947 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2948 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2949 ASSERT_EQ(args.values, values);
2950 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2951}
2952
2953TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2954 setGyroProperties();
2955 prepareGyroAxes();
2956 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2957
2958 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2959 std::chrono::microseconds(10000),
2960 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002961 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Chris Yef59a2f42020-10-16 12:55:26 -07002962 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RX, 20000);
2963 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RY, -20000);
2964 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_RZ, 40000);
2965 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2966 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2967
2968 NotifySensorArgs args;
2969 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2970 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2971 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2972
2973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2974 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2975 ASSERT_EQ(args.deviceId, DEVICE_ID);
2976 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2977 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2978 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2979 ASSERT_EQ(args.values, values);
2980 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2981}
2982
Kim Low03ea0352020-11-06 12:45:07 -08002983// --- BatteryInputMapperTest ---
2984class BatteryInputMapperTest : public InputMapperTest {
2985protected:
2986 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY); }
2987};
2988
2989TEST_F(BatteryInputMapperTest, GetSources) {
2990 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2991
2992 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2993}
2994
2995TEST_F(BatteryInputMapperTest, GetBatteryCapacity) {
2996 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
2997
2998 ASSERT_TRUE(mapper.getBatteryCapacity());
Chris Ye3fdbfef2021-01-06 18:45:18 -08002999 ASSERT_EQ(mapper.getBatteryCapacity().value_or(-1), BATTERY_CAPACITY);
Kim Low03ea0352020-11-06 12:45:07 -08003000}
3001
3002TEST_F(BatteryInputMapperTest, GetBatteryStatus) {
3003 BatteryInputMapper& mapper = addMapperAndConfigure<BatteryInputMapper>();
3004
3005 ASSERT_TRUE(mapper.getBatteryStatus());
Chris Ye3fdbfef2021-01-06 18:45:18 -08003006 ASSERT_EQ(mapper.getBatteryStatus().value_or(-1), BATTERY_STATUS);
3007}
3008
3009// --- LightInputMapperTest ---
3010class LightInputMapperTest : public InputMapperTest {
3011protected:
3012 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT); }
3013};
3014
3015TEST_F(LightInputMapperTest, GetSources) {
3016 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3017
3018 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3019}
3020
3021TEST_F(LightInputMapperTest, SingleLight) {
3022 RawLightInfo infoSingle = {.id = 1,
3023 .name = "Mono",
3024 .maxBrightness = 255,
3025 .flags = InputLightClass::BRIGHTNESS,
3026 .path = ""};
3027 mFakeEventHub->addRawLightInfo(infoSingle.id, std::move(infoSingle));
3028
3029 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3030 InputDeviceInfo info;
3031 mapper.populateDeviceInfo(&info);
3032 const auto& ids = info.getLightIds();
3033 ASSERT_EQ(1UL, ids.size());
3034 ASSERT_EQ(InputDeviceLightType::SINGLE, info.getLightInfo(ids[0])->type);
3035
3036 ASSERT_TRUE(mapper.setLightColor(ids[0], LIGHT_BRIGHTNESS));
3037 ASSERT_EQ(mapper.getLightColor(ids[0]).value_or(-1), LIGHT_BRIGHTNESS);
3038}
3039
3040TEST_F(LightInputMapperTest, RGBLight) {
3041 RawLightInfo infoRed = {.id = 1,
3042 .name = "red",
3043 .maxBrightness = 255,
3044 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
3045 .path = ""};
3046 RawLightInfo infoGreen = {.id = 2,
3047 .name = "green",
3048 .maxBrightness = 255,
3049 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
3050 .path = ""};
3051 RawLightInfo infoBlue = {.id = 3,
3052 .name = "blue",
3053 .maxBrightness = 255,
3054 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
3055 .path = ""};
3056 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
3057 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
3058 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
3059
3060 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3061 InputDeviceInfo info;
3062 mapper.populateDeviceInfo(&info);
3063 const auto& ids = info.getLightIds();
3064 ASSERT_EQ(1UL, ids.size());
3065 ASSERT_EQ(InputDeviceLightType::RGB, info.getLightInfo(ids[0])->type);
3066
3067 ASSERT_TRUE(mapper.setLightColor(ids[0], LIGHT_COLOR));
3068 ASSERT_EQ(mapper.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
3069}
3070
3071TEST_F(LightInputMapperTest, MultiColorRGBLight) {
3072 RawLightInfo infoColor = {.id = 1,
3073 .name = "red",
3074 .maxBrightness = 255,
3075 .flags = InputLightClass::BRIGHTNESS |
3076 InputLightClass::MULTI_INTENSITY |
3077 InputLightClass::MULTI_INDEX,
3078 .path = ""};
3079
3080 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
3081
3082 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3083 InputDeviceInfo info;
3084 mapper.populateDeviceInfo(&info);
3085 const auto& ids = info.getLightIds();
3086 ASSERT_EQ(1UL, ids.size());
3087 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, info.getLightInfo(ids[0])->type);
3088
3089 ASSERT_TRUE(mapper.setLightColor(ids[0], LIGHT_COLOR));
3090 ASSERT_EQ(mapper.getLightColor(ids[0]).value_or(-1), LIGHT_COLOR);
3091}
3092
3093TEST_F(LightInputMapperTest, PlayerIdLight) {
3094 RawLightInfo info1 = {.id = 1,
3095 .name = "player1",
3096 .maxBrightness = 255,
3097 .flags = InputLightClass::BRIGHTNESS,
3098 .path = ""};
3099 RawLightInfo info2 = {.id = 2,
3100 .name = "player2",
3101 .maxBrightness = 255,
3102 .flags = InputLightClass::BRIGHTNESS,
3103 .path = ""};
3104 RawLightInfo info3 = {.id = 3,
3105 .name = "player3",
3106 .maxBrightness = 255,
3107 .flags = InputLightClass::BRIGHTNESS,
3108 .path = ""};
3109 RawLightInfo info4 = {.id = 4,
3110 .name = "player4",
3111 .maxBrightness = 255,
3112 .flags = InputLightClass::BRIGHTNESS,
3113 .path = ""};
3114 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
3115 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
3116 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
3117 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
3118
3119 LightInputMapper& mapper = addMapperAndConfigure<LightInputMapper>();
3120 InputDeviceInfo info;
3121 mapper.populateDeviceInfo(&info);
3122 const auto& ids = info.getLightIds();
3123 ASSERT_EQ(1UL, ids.size());
3124 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, info.getLightInfo(ids[0])->type);
3125
3126 ASSERT_FALSE(mapper.setLightColor(ids[0], LIGHT_COLOR));
3127 ASSERT_TRUE(mapper.setLightPlayerId(ids[0], LIGHT_PLAYER_ID));
3128 ASSERT_EQ(mapper.getLightPlayerId(ids[0]).value_or(-1), LIGHT_PLAYER_ID);
Kim Low03ea0352020-11-06 12:45:07 -08003129}
3130
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131// --- KeyboardInputMapperTest ---
3132
3133class KeyboardInputMapperTest : public InputMapperTest {
3134protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003135 const std::string UNIQUE_ID = "local:0";
3136
3137 void prepareDisplay(int32_t orientation);
3138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003139 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003140 int32_t originalKeyCode, int32_t rotatedKeyCode,
3141 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142};
3143
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003144/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3145 * orientation.
3146 */
3147void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003148 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3149 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003150}
3151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003152void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003153 int32_t originalScanCode, int32_t originalKeyCode,
3154 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 NotifyKeyArgs args;
3156
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003157 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3159 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3160 ASSERT_EQ(originalScanCode, args.scanCode);
3161 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003162 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003164 process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3166 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3167 ASSERT_EQ(originalScanCode, args.scanCode);
3168 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003169 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170}
3171
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003173 KeyboardInputMapper& mapper =
3174 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3175 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003177 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178}
3179
3180TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3181 const int32_t USAGE_A = 0x070004;
3182 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003183 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3184 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003185 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3186 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3187 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003189 KeyboardInputMapper& mapper =
3190 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3191 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003192 // Initial metastate to AMETA_NONE.
3193 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3194 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195
3196 // Key down by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003197 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 NotifyKeyArgs args;
3199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3200 ASSERT_EQ(DEVICE_ID, args.deviceId);
3201 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3202 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3204 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3205 ASSERT_EQ(KEY_HOME, args.scanCode);
3206 ASSERT_EQ(AMETA_NONE, args.metaState);
3207 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3208 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3209 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3210
3211 // Key up by scan code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003212 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3214 ASSERT_EQ(DEVICE_ID, args.deviceId);
3215 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3216 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3217 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3218 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3219 ASSERT_EQ(KEY_HOME, args.scanCode);
3220 ASSERT_EQ(AMETA_NONE, args.metaState);
3221 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3222 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3223 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3224
3225 // Key down by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003226 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3227 process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3229 ASSERT_EQ(DEVICE_ID, args.deviceId);
3230 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3231 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3232 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3233 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3234 ASSERT_EQ(0, args.scanCode);
3235 ASSERT_EQ(AMETA_NONE, args.metaState);
3236 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3237 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3238 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3239
3240 // Key up by usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003241 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3242 process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3244 ASSERT_EQ(DEVICE_ID, args.deviceId);
3245 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3246 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3247 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3248 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3249 ASSERT_EQ(0, args.scanCode);
3250 ASSERT_EQ(AMETA_NONE, args.metaState);
3251 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3252 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3253 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3254
3255 // Key down with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003256 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3257 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3259 ASSERT_EQ(DEVICE_ID, args.deviceId);
3260 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3261 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3262 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3263 ASSERT_EQ(0, args.keyCode);
3264 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3265 ASSERT_EQ(AMETA_NONE, args.metaState);
3266 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3267 ASSERT_EQ(0U, args.policyFlags);
3268 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3269
3270 // Key up with unknown scan code or usage code.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003271 process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3272 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3274 ASSERT_EQ(DEVICE_ID, args.deviceId);
3275 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3276 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3277 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3278 ASSERT_EQ(0, args.keyCode);
3279 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3280 ASSERT_EQ(AMETA_NONE, args.metaState);
3281 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3282 ASSERT_EQ(0U, args.policyFlags);
3283 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3284}
3285
3286TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003287 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3288 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003289 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3290 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3291 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 KeyboardInputMapper& mapper =
3294 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3295 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
arthurhungc903df12020-08-11 15:08:42 +08003297 // Initial metastate to AMETA_NONE.
3298 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3299 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300
3301 // Metakey down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003302 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303 NotifyKeyArgs args;
3304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3305 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003307 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308
3309 // Key down.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003310 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3312 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003313 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314
3315 // Key up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003316 process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3318 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320
3321 // Metakey up.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003322 process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3324 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003325 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003326 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327}
3328
3329TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003330 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3331 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3332 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3333 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335 KeyboardInputMapper& mapper =
3336 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3337 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003339 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3341 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3342 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3343 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3344 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3345 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3346 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3347 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3348}
3349
3350TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003351 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3352 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3353 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3354 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003357 KeyboardInputMapper& mapper =
3358 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3359 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003361 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003362 ASSERT_NO_FATAL_FAILURE(
3363 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3364 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3365 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3366 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3367 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3368 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3369 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003371 clearViewports();
3372 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003373 ASSERT_NO_FATAL_FAILURE(
3374 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3375 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3376 AKEYCODE_DPAD_UP, DISPLAY_ID));
3377 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3378 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3379 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3380 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003382 clearViewports();
3383 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003384 ASSERT_NO_FATAL_FAILURE(
3385 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3386 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3387 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3388 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3389 AKEYCODE_DPAD_UP, DISPLAY_ID));
3390 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3391 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003393 clearViewports();
3394 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003395 ASSERT_NO_FATAL_FAILURE(
3396 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3397 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3398 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3399 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3400 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3401 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3402 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403
3404 // Special case: if orientation changes while key is down, we still emit the same keycode
3405 // in the key up as we did in the key down.
3406 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003407 clearViewports();
3408 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003409 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3411 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3412 ASSERT_EQ(KEY_UP, args.scanCode);
3413 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3414
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003415 clearViewports();
3416 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003417 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3419 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3420 ASSERT_EQ(KEY_UP, args.scanCode);
3421 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3422}
3423
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003424TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3425 // If the keyboard is not orientation aware,
3426 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003428
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003429 KeyboardInputMapper& mapper =
3430 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3431 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003432 NotifyKeyArgs args;
3433
3434 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003435 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003437 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3439 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3440
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003441 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003442 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003444 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3446 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3447}
3448
3449TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3450 // If the keyboard is orientation aware,
3451 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003452 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003453
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003454 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003455 KeyboardInputMapper& mapper =
3456 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3457 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003458 NotifyKeyArgs args;
3459
3460 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3461 // ^--- already checked by the previous test
3462
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003463 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003464 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003465 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003467 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3469 ASSERT_EQ(DISPLAY_ID, args.displayId);
3470
3471 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003472 clearViewports();
3473 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003474 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003475 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003477 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3479 ASSERT_EQ(newDisplayId, args.displayId);
3480}
3481
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003483 KeyboardInputMapper& mapper =
3484 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3485 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003487 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003488 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003490 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003491 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492}
3493
3494TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 KeyboardInputMapper& mapper =
3496 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3497 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003499 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003500 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003502 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003503 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504}
3505
3506TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003507 KeyboardInputMapper& mapper =
3508 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3509 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003511 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512
3513 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3514 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003515 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516 ASSERT_TRUE(flags[0]);
3517 ASSERT_FALSE(flags[1]);
3518}
3519
3520TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003521 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3522 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3523 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3524 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3525 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3526 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003528 KeyboardInputMapper& mapper =
3529 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3530 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003531 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003532 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3533 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534
3535 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003536 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3537 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3538 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
3540 // Toggle caps lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003541 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3542 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003543 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3544 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3545 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003546 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547
3548 // Toggle num lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003549 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3550 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003551 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3552 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3553 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003554 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555
3556 // Toggle caps lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003557 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3558 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003559 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3560 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3561 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003562 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
3564 // Toggle scroll lock on.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003565 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3566 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003567 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3568 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3569 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003570 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571
3572 // Toggle num lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003573 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3574 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003575 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3576 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3577 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003578 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579
3580 // Toggle scroll lock off.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003581 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3582 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003583 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3584 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3585 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003586 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587}
3588
Chris Yea52ade12020-08-27 16:49:20 -07003589TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3590 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3591 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3592 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3593 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3594
3595 KeyboardInputMapper& mapper =
3596 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3597 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3598
3599 // Initial metastate should be AMETA_NONE as no meta keys added.
3600 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3601 // Meta state should be AMETA_NONE after reset
3602 mapper.reset(ARBITRARY_TIME);
3603 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3604 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3605 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3606 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3607
3608 NotifyKeyArgs args;
3609 // Press button "A"
3610 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_A, 1);
3611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3612 ASSERT_EQ(AMETA_NONE, args.metaState);
3613 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3614 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3615 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3616
3617 // Button up.
3618 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_A, 0);
3619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3620 ASSERT_EQ(AMETA_NONE, args.metaState);
3621 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3622 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3623 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3624}
3625
Arthur Hung2c9a3342019-07-23 14:18:59 +08003626TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3627 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003628 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3629 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3631 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003632
3633 // keyboard 2.
3634 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003635 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003636 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003637 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003638 std::shared_ptr<InputDevice> device2 =
3639 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3640 Flags<InputDeviceClass>(0));
3641
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003642 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3643 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3644 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3645 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003646
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003647 KeyboardInputMapper& mapper =
3648 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3649 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003650
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003651 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003652 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003653 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003654 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3655 device2->reset(ARBITRARY_TIME);
3656
3657 // Prepared displays and associated info.
3658 constexpr uint8_t hdmi1 = 0;
3659 constexpr uint8_t hdmi2 = 1;
3660 const std::string SECONDARY_UNIQUE_ID = "local:1";
3661
3662 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3663 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3664
3665 // No associated display viewport found, should disable the device.
3666 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3667 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3668 ASSERT_FALSE(device2->isEnabled());
3669
3670 // Prepare second display.
3671 constexpr int32_t newDisplayId = 2;
3672 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003673 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003674 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003675 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003676 // Default device will reconfigure above, need additional reconfiguration for another device.
3677 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3678 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3679
3680 // Device should be enabled after the associated display is found.
3681 ASSERT_TRUE(mDevice->isEnabled());
3682 ASSERT_TRUE(device2->isEnabled());
3683
3684 // Test pad key events
3685 ASSERT_NO_FATAL_FAILURE(
3686 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3687 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3688 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3689 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3690 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3691 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3692 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3693
3694 ASSERT_NO_FATAL_FAILURE(
3695 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3696 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3697 AKEYCODE_DPAD_RIGHT, newDisplayId));
3698 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3699 AKEYCODE_DPAD_DOWN, newDisplayId));
3700 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3701 AKEYCODE_DPAD_LEFT, newDisplayId));
3702}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703
arthurhungc903df12020-08-11 15:08:42 +08003704TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3705 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3706 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3707 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3708 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3709 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3710 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3711
3712 KeyboardInputMapper& mapper =
3713 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3714 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3715 // Initial metastate to AMETA_NONE.
3716 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3717 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3718
3719 // Initialization should have turned all of the lights off.
3720 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3721 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3722 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3723
3724 // Toggle caps lock on.
3725 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3726 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3727 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3728 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3729
3730 // Toggle num lock on.
3731 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
3732 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
3733 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3734 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3735
3736 // Toggle scroll lock on.
3737 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3738 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3739 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3740 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3741
3742 mFakeEventHub->removeDevice(EVENTHUB_ID);
3743 mReader->loopOnce();
3744
3745 // keyboard 2 should default toggle keys.
3746 const std::string USB2 = "USB2";
3747 const std::string DEVICE_NAME2 = "KEYBOARD2";
3748 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3749 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3750 std::shared_ptr<InputDevice> device2 =
3751 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3752 Flags<InputDeviceClass>(0));
3753 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3754 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3755 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3756 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3757 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3758 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3759
arthurhung6fe95782020-10-05 22:41:16 +08003760 KeyboardInputMapper& mapper2 =
3761 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3762 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003763 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3764 device2->reset(ARBITRARY_TIME);
3765
3766 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3767 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3768 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003769 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3770 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003771}
3772
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003773// --- KeyboardInputMapperTest_ExternalDevice ---
3774
3775class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3776protected:
Chris Yea52ade12020-08-27 16:49:20 -07003777 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003778};
3779
3780TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003781 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3782 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003783
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3787 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 KeyboardInputMapper& mapper =
3790 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3791 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003792
3793 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3794 NotifyKeyArgs args;
3795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3796 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3797
3798 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3800 ASSERT_EQ(uint32_t(0), args.policyFlags);
3801
3802 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3804 ASSERT_EQ(uint32_t(0), args.policyFlags);
3805
3806 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3808 ASSERT_EQ(uint32_t(0), args.policyFlags);
3809
3810 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3812 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3813
3814 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
3815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3816 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3817}
3818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003819TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003820 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003821
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003822 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3823 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3824 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003825
Powei Fengd041c5d2019-05-03 17:11:33 -07003826 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003827 KeyboardInputMapper& mapper =
3828 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3829 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003830
3831 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
3832 NotifyKeyArgs args;
3833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3834 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3835
3836 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
3837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3838 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3839
3840 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3842 ASSERT_EQ(uint32_t(0), args.policyFlags);
3843
3844 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3846 ASSERT_EQ(uint32_t(0), args.policyFlags);
3847
3848 process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3850 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3851
3852 process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3854 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3855}
3856
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857// --- CursorInputMapperTest ---
3858
3859class CursorInputMapperTest : public InputMapperTest {
3860protected:
3861 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3862
Michael Wright17db18e2020-06-26 20:51:44 +01003863 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864
Chris Yea52ade12020-08-27 16:49:20 -07003865 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866 InputMapperTest::SetUp();
3867
Michael Wright17db18e2020-06-26 20:51:44 +01003868 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003869 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 }
3871
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003872 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3873 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003874
3875 void prepareDisplay(int32_t orientation) {
3876 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003877 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003878 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3879 orientation, uniqueId, NO_PORT, viewportType);
3880 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881};
3882
3883const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3886 int32_t originalY, int32_t rotatedX,
3887 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888 NotifyMotionArgs args;
3889
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003890 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3891 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3892 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3896 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3897 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3898 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3899}
3900
3901TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003903 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003905 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906}
3907
3908TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003910 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003912 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913}
3914
3915TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003917 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
3919 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003920 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
3922 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003923 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3924 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3926 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3927
3928 // When the bounds are set, then there should be a valid motion range.
3929 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3930
3931 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003932 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933
3934 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3935 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3936 1, 800 - 1, 0.0f, 0.0f));
3937 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3938 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3939 2, 480 - 1, 0.0f, 0.0f));
3940 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3941 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3942 0.0f, 1.0f, 0.0f, 0.0f));
3943}
3944
3945TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003947 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948
3949 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003950 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951
3952 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3953 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3954 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3955 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3956 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3957 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3958 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3959 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3960 0.0f, 1.0f, 0.0f, 0.0f));
3961}
3962
3963TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003965 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966
arthurhungdcef2dc2020-08-11 14:47:50 +08003967 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968
3969 NotifyMotionArgs args;
3970
3971 // Button press.
3972 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08003973 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3974 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3976 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3977 ASSERT_EQ(DEVICE_ID, args.deviceId);
3978 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3979 ASSERT_EQ(uint32_t(0), args.policyFlags);
3980 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3981 ASSERT_EQ(0, args.flags);
3982 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3983 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3984 ASSERT_EQ(0, args.edgeFlags);
3985 ASSERT_EQ(uint32_t(1), args.pointerCount);
3986 ASSERT_EQ(0, args.pointerProperties[0].id);
3987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3989 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3990 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3991 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3992 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3993
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3995 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3996 ASSERT_EQ(DEVICE_ID, args.deviceId);
3997 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3998 ASSERT_EQ(uint32_t(0), args.policyFlags);
3999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4000 ASSERT_EQ(0, args.flags);
4001 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4002 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4003 ASSERT_EQ(0, args.edgeFlags);
4004 ASSERT_EQ(uint32_t(1), args.pointerCount);
4005 ASSERT_EQ(0, args.pointerProperties[0].id);
4006 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
4007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4008 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4009 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4010 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4011 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4012
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 // Button release. Should have same down time.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004014 process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
4015 process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4017 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4018 ASSERT_EQ(DEVICE_ID, args.deviceId);
4019 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4020 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4022 ASSERT_EQ(0, args.flags);
4023 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4024 ASSERT_EQ(0, args.buttonState);
4025 ASSERT_EQ(0, args.edgeFlags);
4026 ASSERT_EQ(uint32_t(1), args.pointerCount);
4027 ASSERT_EQ(0, args.pointerProperties[0].id);
4028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
4029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4030 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4031 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4032 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4033 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4034
4035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4036 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4037 ASSERT_EQ(DEVICE_ID, args.deviceId);
4038 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4039 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4041 ASSERT_EQ(0, args.flags);
4042 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4043 ASSERT_EQ(0, args.buttonState);
4044 ASSERT_EQ(0, args.edgeFlags);
4045 ASSERT_EQ(uint32_t(1), args.pointerCount);
4046 ASSERT_EQ(0, args.pointerProperties[0].id);
4047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
4048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4049 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4050 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4051 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4052 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4053}
4054
4055TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004057 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058
4059 NotifyMotionArgs args;
4060
4061 // Motion in X but not Y.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004062 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
4063 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4067 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4068
4069 // Motion in Y but not X.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004070 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
4071 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4075 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4076}
4077
4078TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004080 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081
4082 NotifyMotionArgs args;
4083
4084 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004085 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
4086 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4088 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4090 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4091
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4095 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4096
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004098 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
4099 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4103 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4104
4105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4108 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4109}
4110
4111TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004113 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114
4115 NotifyMotionArgs args;
4116
4117 // Combined X, Y and Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004118 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
4119 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
4120 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
4121 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4125 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4126 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4127
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4131 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4132 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4133
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 // Move X, Y a bit while pressed.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004135 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
4136 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
4137 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4141 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4142 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4143
4144 // Release Button.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004145 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
4146 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4150 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4151
4152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4155 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4156}
4157
4158TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004160 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004162 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4171}
4172
4173TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 addConfigurationProperty("cursor.mode", "navigation");
4175 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004176 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004178 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4187
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004188 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
4197
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004198 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4207
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004208 prepareDisplay(DISPLAY_ORIENTATION_270);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
4217}
4218
4219TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222
4223 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4224 mFakePointerController->setPosition(100, 200);
4225 mFakePointerController->setButtonState(0);
4226
4227 NotifyMotionArgs motionArgs;
4228 NotifyKeyArgs keyArgs;
4229
4230 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004231 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
4232 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4234 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4235 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4236 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4238 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4239
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4242 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4243 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
4244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4245 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4246
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004247 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
4248 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_EQ(0, motionArgs.buttonState);
4252 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4254 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4255
4256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004257 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 ASSERT_EQ(0, motionArgs.buttonState);
4259 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4261 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4262
4263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004265 ASSERT_EQ(0, motionArgs.buttonState);
4266 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4268 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4269
4270 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004271 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
4272 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
4273 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4276 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4277 motionArgs.buttonState);
4278 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4279 mFakePointerController->getButtonState());
4280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4281 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4282
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4285 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4286 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4287 mFakePointerController->getButtonState());
4288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4289 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4290
4291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4293 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4294 motionArgs.buttonState);
4295 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4296 mFakePointerController->getButtonState());
4297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4298 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4299
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004300 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
4301 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4305 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4307 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4308
4309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4312 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4314 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4315
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004316 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
4317 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4320 ASSERT_EQ(0, motionArgs.buttonState);
4321 ASSERT_EQ(0, mFakePointerController->getButtonState());
4322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4323 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 -08004324 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
4325 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326
4327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_EQ(0, motionArgs.buttonState);
4329 ASSERT_EQ(0, mFakePointerController->getButtonState());
4330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4332 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 -08004333
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4335 ASSERT_EQ(0, motionArgs.buttonState);
4336 ASSERT_EQ(0, mFakePointerController->getButtonState());
4337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4339 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4340
4341 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004342 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
4343 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4345 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4346 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004349 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4351 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4353 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4354
4355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4356 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4357 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4358 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4360 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4361
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004362 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
4363 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 ASSERT_EQ(0, motionArgs.buttonState);
4367 ASSERT_EQ(0, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4369 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4370
4371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004373 ASSERT_EQ(0, motionArgs.buttonState);
4374 ASSERT_EQ(0, mFakePointerController->getButtonState());
4375
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4377 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4379 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4380 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4381
4382 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004383 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
4384 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4387 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004388
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004390 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4392 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4394 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4395
4396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4397 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4398 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4399 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4401 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4402
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004403 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
4404 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_EQ(0, motionArgs.buttonState);
4408 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4410 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 -08004411
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4414 ASSERT_EQ(0, motionArgs.buttonState);
4415 ASSERT_EQ(0, mFakePointerController->getButtonState());
4416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4417 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4418
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4420 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4421 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4422
4423 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004424 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
4425 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4427 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4428 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004429
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4433 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4435 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4436
4437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4439 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4440 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4442 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4443
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004444 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
4445 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_EQ(0, motionArgs.buttonState);
4449 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4451 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 -08004452
4453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4454 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4455 ASSERT_EQ(0, motionArgs.buttonState);
4456 ASSERT_EQ(0, mFakePointerController->getButtonState());
4457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4458 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4459
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4461 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4462 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4463
4464 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004465 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
4466 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4468 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4469 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4474 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4476 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4477
4478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4480 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4481 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4483 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4484
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004485 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
4486 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_EQ(0, motionArgs.buttonState);
4490 ASSERT_EQ(0, mFakePointerController->getButtonState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4492 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 -08004493
4494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4495 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4496 ASSERT_EQ(0, motionArgs.buttonState);
4497 ASSERT_EQ(0, mFakePointerController->getButtonState());
4498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4499 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4500
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4502 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4503 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4504}
4505
4506TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004508 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
4510 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4511 mFakePointerController->setPosition(100, 200);
4512 mFakePointerController->setButtonState(0);
4513
4514 NotifyMotionArgs args;
4515
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004516 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4517 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4518 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004520 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4523 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 +01004524 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004525}
4526
4527TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004528 addConfigurationProperty("cursor.mode", "pointer");
4529 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004530 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004531
4532 NotifyDeviceResetArgs resetArgs;
4533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4534 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4535 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4536
4537 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4538 mFakePointerController->setPosition(100, 200);
4539 mFakePointerController->setButtonState(0);
4540
4541 NotifyMotionArgs args;
4542
4543 // Move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004544 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4545 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4546 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4548 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4551 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 +01004552 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004553
4554 // Button press.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004555 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
4556 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4558 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4559 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4561 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4563 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4566 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4567
4568 // Button release.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004569 process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
4570 process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4572 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4575 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4577 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4578 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4580 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4581
4582 // Another move.
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004583 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
4584 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
4585 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4587 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4590 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 +01004591 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004592
4593 // Disable pointer capture and check that the device generation got bumped
4594 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004595 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004596 mFakePolicy->setPointerCapture(false);
4597 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004598 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004599
4600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4601 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4602 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4603
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004604 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4605 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4606 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4608 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4611 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 +01004612 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613}
4614
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004615TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004616 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004617
Garfield Tan888a6a42020-01-09 11:39:16 -08004618 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004619 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004620 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4621 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004622 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4623 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004624 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4625 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4626
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004627 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4628 mFakePointerController->setPosition(100, 200);
4629 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004630
4631 NotifyMotionArgs args;
4632 process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
4633 process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
4634 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4636 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4639 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 +01004640 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004641 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4642}
4643
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644// --- TouchInputMapperTest ---
4645
4646class TouchInputMapperTest : public InputMapperTest {
4647protected:
4648 static const int32_t RAW_X_MIN;
4649 static const int32_t RAW_X_MAX;
4650 static const int32_t RAW_Y_MIN;
4651 static const int32_t RAW_Y_MAX;
4652 static const int32_t RAW_TOUCH_MIN;
4653 static const int32_t RAW_TOUCH_MAX;
4654 static const int32_t RAW_TOOL_MIN;
4655 static const int32_t RAW_TOOL_MAX;
4656 static const int32_t RAW_PRESSURE_MIN;
4657 static const int32_t RAW_PRESSURE_MAX;
4658 static const int32_t RAW_ORIENTATION_MIN;
4659 static const int32_t RAW_ORIENTATION_MAX;
4660 static const int32_t RAW_DISTANCE_MIN;
4661 static const int32_t RAW_DISTANCE_MAX;
4662 static const int32_t RAW_TILT_MIN;
4663 static const int32_t RAW_TILT_MAX;
4664 static const int32_t RAW_ID_MIN;
4665 static const int32_t RAW_ID_MAX;
4666 static const int32_t RAW_SLOT_MIN;
4667 static const int32_t RAW_SLOT_MAX;
4668 static const float X_PRECISION;
4669 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004670 static const float X_PRECISION_VIRTUAL;
4671 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672
4673 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004674 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675
4676 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4677
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004678 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004679 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004680
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 enum Axes {
4682 POSITION = 1 << 0,
4683 TOUCH = 1 << 1,
4684 TOOL = 1 << 2,
4685 PRESSURE = 1 << 3,
4686 ORIENTATION = 1 << 4,
4687 MINOR = 1 << 5,
4688 ID = 1 << 6,
4689 DISTANCE = 1 << 7,
4690 TILT = 1 << 8,
4691 SLOT = 1 << 9,
4692 TOOL_TYPE = 1 << 10,
4693 };
4694
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004695 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4696 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004697 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004699 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 int32_t toRawX(float displayX);
4701 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004702 float toCookedX(float rawX, float rawY);
4703 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004705 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004707 float toDisplayY(int32_t rawY, int32_t displayHeight);
4708
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709};
4710
4711const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4712const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4713const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4714const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4715const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4716const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4717const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4718const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004719const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4720const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4722const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4723const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4724const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4725const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4726const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4727const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4728const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4729const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4730const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4731const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4732const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004733const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4734 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4735const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4736 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004737const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4738 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739
4740const float TouchInputMapperTest::GEOMETRIC_SCALE =
4741 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4742 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4743
4744const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4745 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4746 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4747};
4748
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004749void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004750 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4751 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004752}
4753
4754void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4755 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4756 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757}
4758
Santos Cordonfa5cf462017-04-05 10:37:00 -07004759void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004760 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4761 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4762 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004763}
4764
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004766 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4767 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4768 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4769 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770}
4771
Jason Gerecke489fda82012-09-07 17:19:40 -07004772void TouchInputMapperTest::prepareLocationCalibration() {
4773 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4774}
4775
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776int32_t TouchInputMapperTest::toRawX(float displayX) {
4777 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4778}
4779
4780int32_t TouchInputMapperTest::toRawY(float displayY) {
4781 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4782}
4783
Jason Gerecke489fda82012-09-07 17:19:40 -07004784float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4785 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4786 return rawX;
4787}
4788
4789float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4790 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4791 return rawY;
4792}
4793
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004795 return toDisplayX(rawX, DISPLAY_WIDTH);
4796}
4797
4798float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4799 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800}
4801
4802float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004803 return toDisplayY(rawY, DISPLAY_HEIGHT);
4804}
4805
4806float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4807 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808}
4809
4810
4811// --- SingleTouchInputMapperTest ---
4812
4813class SingleTouchInputMapperTest : public TouchInputMapperTest {
4814protected:
4815 void prepareButtons();
4816 void prepareAxes(int axes);
4817
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004818 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4819 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4820 void processUp(SingleTouchInputMapper& mappery);
4821 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4822 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4823 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4824 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4825 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4826 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827};
4828
4829void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004830 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831}
4832
4833void SingleTouchInputMapperTest::prepareAxes(int axes) {
4834 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004835 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4836 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 }
4838 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004839 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4840 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 }
4842 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004843 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4844 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 }
4846 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004847 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4848 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849 }
4850 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004851 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4852 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 }
4854}
4855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004856void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004857 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4858 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4859 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860}
4861
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004862void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004863 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4864 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865}
4866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004867void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004868 process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869}
4870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004871void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004872 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873}
4874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004875void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4876 int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004877 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878}
4879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004880void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004881 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882}
4883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004884void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4885 int32_t tiltY) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004886 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4887 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888}
4889
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004890void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4891 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004892 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893}
4894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004895void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08004896 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897}
4898
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900 prepareButtons();
4901 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004902 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004904 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905}
4906
4907TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004908 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4909 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 prepareButtons();
4911 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004912 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915}
4916
4917TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918 prepareButtons();
4919 prepareAxes(POSITION);
4920 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004921 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924}
4925
4926TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927 prepareButtons();
4928 prepareAxes(POSITION);
4929 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004930 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004932 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933}
4934
4935TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 addConfigurationProperty("touch.deviceType", "touchScreen");
4937 prepareDisplay(DISPLAY_ORIENTATION_0);
4938 prepareButtons();
4939 prepareAxes(POSITION);
4940 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004941 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942
4943 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004944 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945
4946 // Virtual key is down.
4947 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4948 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4949 processDown(mapper, x, y);
4950 processSync(mapper);
4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004953 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954
4955 // Virtual key is up.
4956 processUp(mapper);
4957 processSync(mapper);
4958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004960 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961}
4962
4963TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 addConfigurationProperty("touch.deviceType", "touchScreen");
4965 prepareDisplay(DISPLAY_ORIENTATION_0);
4966 prepareButtons();
4967 prepareAxes(POSITION);
4968 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004969 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
4971 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004972 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973
4974 // Virtual key is down.
4975 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4976 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4977 processDown(mapper, x, y);
4978 processSync(mapper);
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004981 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982
4983 // Virtual key is up.
4984 processUp(mapper);
4985 processSync(mapper);
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004988 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989}
4990
4991TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 addConfigurationProperty("touch.deviceType", "touchScreen");
4993 prepareDisplay(DISPLAY_ORIENTATION_0);
4994 prepareButtons();
4995 prepareAxes(POSITION);
4996 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998
4999 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5000 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005001 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 ASSERT_TRUE(flags[0]);
5003 ASSERT_FALSE(flags[1]);
5004}
5005
5006TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007 addConfigurationProperty("touch.deviceType", "touchScreen");
5008 prepareDisplay(DISPLAY_ORIENTATION_0);
5009 prepareButtons();
5010 prepareAxes(POSITION);
5011 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005012 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013
arthurhungdcef2dc2020-08-11 14:47:50 +08005014 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015
5016 NotifyKeyArgs args;
5017
5018 // Press virtual key.
5019 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5020 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5021 processDown(mapper, x, y);
5022 processSync(mapper);
5023
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5025 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5026 ASSERT_EQ(DEVICE_ID, args.deviceId);
5027 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5028 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5029 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5030 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5031 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5032 ASSERT_EQ(KEY_HOME, args.scanCode);
5033 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5034 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5035
5036 // Release virtual key.
5037 processUp(mapper);
5038 processSync(mapper);
5039
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5041 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5042 ASSERT_EQ(DEVICE_ID, args.deviceId);
5043 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5044 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5045 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5046 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5047 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5048 ASSERT_EQ(KEY_HOME, args.scanCode);
5049 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5050 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5051
5052 // Should not have sent any motions.
5053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5054}
5055
5056TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 addConfigurationProperty("touch.deviceType", "touchScreen");
5058 prepareDisplay(DISPLAY_ORIENTATION_0);
5059 prepareButtons();
5060 prepareAxes(POSITION);
5061 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005062 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063
arthurhungdcef2dc2020-08-11 14:47:50 +08005064 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065
5066 NotifyKeyArgs keyArgs;
5067
5068 // Press virtual key.
5069 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5070 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5071 processDown(mapper, x, y);
5072 processSync(mapper);
5073
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5075 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5076 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5077 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5078 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5079 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5080 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5081 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5082 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5084 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5085
5086 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5087 // into the display area.
5088 y -= 100;
5089 processMove(mapper, x, y);
5090 processSync(mapper);
5091
5092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5093 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5094 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5095 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5096 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5097 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5098 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5099 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5100 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5101 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5102 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5103 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5104
5105 NotifyMotionArgs motionArgs;
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5107 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5108 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5109 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5110 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5111 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5112 ASSERT_EQ(0, motionArgs.flags);
5113 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5114 ASSERT_EQ(0, motionArgs.buttonState);
5115 ASSERT_EQ(0, motionArgs.edgeFlags);
5116 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5117 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5120 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5121 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5122 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5123 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5124
5125 // Keep moving out of bounds. Should generate a pointer move.
5126 y -= 50;
5127 processMove(mapper, x, y);
5128 processSync(mapper);
5129
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5131 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5132 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5133 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5134 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5136 ASSERT_EQ(0, motionArgs.flags);
5137 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5138 ASSERT_EQ(0, motionArgs.buttonState);
5139 ASSERT_EQ(0, motionArgs.edgeFlags);
5140 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5141 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5144 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5145 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5146 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5147 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5148
5149 // Release out of bounds. Should generate a pointer up.
5150 processUp(mapper);
5151 processSync(mapper);
5152
5153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5154 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5155 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5156 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5157 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5158 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5159 ASSERT_EQ(0, motionArgs.flags);
5160 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5161 ASSERT_EQ(0, motionArgs.buttonState);
5162 ASSERT_EQ(0, motionArgs.edgeFlags);
5163 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5164 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5167 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5168 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5169 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5170 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5171
5172 // Should not have sent any more keys or motions.
5173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5175}
5176
5177TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178 addConfigurationProperty("touch.deviceType", "touchScreen");
5179 prepareDisplay(DISPLAY_ORIENTATION_0);
5180 prepareButtons();
5181 prepareAxes(POSITION);
5182 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005183 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184
arthurhungdcef2dc2020-08-11 14:47:50 +08005185 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005186
5187 NotifyMotionArgs motionArgs;
5188
5189 // Initially go down out of bounds.
5190 int32_t x = -10;
5191 int32_t y = -10;
5192 processDown(mapper, x, y);
5193 processSync(mapper);
5194
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5196
5197 // Move into the display area. Should generate a pointer down.
5198 x = 50;
5199 y = 75;
5200 processMove(mapper, x, y);
5201 processSync(mapper);
5202
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5204 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5205 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5206 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5207 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5209 ASSERT_EQ(0, motionArgs.flags);
5210 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5211 ASSERT_EQ(0, motionArgs.buttonState);
5212 ASSERT_EQ(0, motionArgs.edgeFlags);
5213 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5214 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5217 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5218 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5219 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5220 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5221
5222 // Release. Should generate a pointer up.
5223 processUp(mapper);
5224 processSync(mapper);
5225
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5227 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5228 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5229 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5230 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5231 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5232 ASSERT_EQ(0, motionArgs.flags);
5233 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5234 ASSERT_EQ(0, motionArgs.buttonState);
5235 ASSERT_EQ(0, motionArgs.edgeFlags);
5236 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5237 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5240 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5241 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5242 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5244
5245 // Should not have sent any more keys or motions.
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5248}
5249
Santos Cordonfa5cf462017-04-05 10:37:00 -07005250TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005251 addConfigurationProperty("touch.deviceType", "touchScreen");
5252 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5253
5254 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5255 prepareButtons();
5256 prepareAxes(POSITION);
5257 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005258 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005259
arthurhungdcef2dc2020-08-11 14:47:50 +08005260 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005261
5262 NotifyMotionArgs motionArgs;
5263
5264 // Down.
5265 int32_t x = 100;
5266 int32_t y = 125;
5267 processDown(mapper, x, y);
5268 processSync(mapper);
5269
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5272 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5273 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5274 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5275 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5276 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5277 ASSERT_EQ(0, motionArgs.flags);
5278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5279 ASSERT_EQ(0, motionArgs.buttonState);
5280 ASSERT_EQ(0, motionArgs.edgeFlags);
5281 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5282 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5285 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5286 1, 0, 0, 0, 0, 0, 0, 0));
5287 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5288 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5290
5291 // Move.
5292 x += 50;
5293 y += 75;
5294 processMove(mapper, x, y);
5295 processSync(mapper);
5296
5297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5298 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5299 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5300 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5301 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5302 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5304 ASSERT_EQ(0, motionArgs.flags);
5305 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5306 ASSERT_EQ(0, motionArgs.buttonState);
5307 ASSERT_EQ(0, motionArgs.edgeFlags);
5308 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5309 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5312 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5313 1, 0, 0, 0, 0, 0, 0, 0));
5314 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5315 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5317
5318 // Up.
5319 processUp(mapper);
5320 processSync(mapper);
5321
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5324 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5325 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5326 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5327 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5328 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5329 ASSERT_EQ(0, motionArgs.flags);
5330 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5331 ASSERT_EQ(0, motionArgs.buttonState);
5332 ASSERT_EQ(0, motionArgs.edgeFlags);
5333 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5334 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5337 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5338 1, 0, 0, 0, 0, 0, 0, 0));
5339 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5340 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5342
5343 // Should not have sent any more keys or motions.
5344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5346}
5347
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349 addConfigurationProperty("touch.deviceType", "touchScreen");
5350 prepareDisplay(DISPLAY_ORIENTATION_0);
5351 prepareButtons();
5352 prepareAxes(POSITION);
5353 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005354 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355
arthurhungdcef2dc2020-08-11 14:47:50 +08005356 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357
5358 NotifyMotionArgs motionArgs;
5359
5360 // Down.
5361 int32_t x = 100;
5362 int32_t y = 125;
5363 processDown(mapper, x, y);
5364 processSync(mapper);
5365
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5367 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5368 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5369 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5370 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5371 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5372 ASSERT_EQ(0, motionArgs.flags);
5373 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5374 ASSERT_EQ(0, motionArgs.buttonState);
5375 ASSERT_EQ(0, motionArgs.edgeFlags);
5376 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5377 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5380 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5381 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5382 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5383 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5384
5385 // Move.
5386 x += 50;
5387 y += 75;
5388 processMove(mapper, x, y);
5389 processSync(mapper);
5390
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5392 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5393 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5394 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5395 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5397 ASSERT_EQ(0, motionArgs.flags);
5398 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5399 ASSERT_EQ(0, motionArgs.buttonState);
5400 ASSERT_EQ(0, motionArgs.edgeFlags);
5401 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5402 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5405 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5406 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5407 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5408 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5409
5410 // Up.
5411 processUp(mapper);
5412 processSync(mapper);
5413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5416 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5419 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5420 ASSERT_EQ(0, motionArgs.flags);
5421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5422 ASSERT_EQ(0, motionArgs.buttonState);
5423 ASSERT_EQ(0, motionArgs.edgeFlags);
5424 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5428 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5429 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5430 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5432
5433 // Should not have sent any more keys or motions.
5434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5436}
5437
5438TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439 addConfigurationProperty("touch.deviceType", "touchScreen");
5440 prepareButtons();
5441 prepareAxes(POSITION);
5442 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005443 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444
5445 NotifyMotionArgs args;
5446
5447 // Rotation 90.
5448 prepareDisplay(DISPLAY_ORIENTATION_90);
5449 processDown(mapper, toRawX(50), toRawY(75));
5450 processSync(mapper);
5451
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5453 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5454 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5455
5456 processUp(mapper);
5457 processSync(mapper);
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5459}
5460
5461TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462 addConfigurationProperty("touch.deviceType", "touchScreen");
5463 prepareButtons();
5464 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005465 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466
5467 NotifyMotionArgs args;
5468
5469 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005470 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471 prepareDisplay(DISPLAY_ORIENTATION_0);
5472 processDown(mapper, toRawX(50), toRawY(75));
5473 processSync(mapper);
5474
5475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5476 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5477 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5478
5479 processUp(mapper);
5480 processSync(mapper);
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5482
5483 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005484 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485 prepareDisplay(DISPLAY_ORIENTATION_90);
5486 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
5487 processSync(mapper);
5488
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5490 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5491 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5492
5493 processUp(mapper);
5494 processSync(mapper);
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5496
5497 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005498 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 prepareDisplay(DISPLAY_ORIENTATION_180);
5500 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5501 processSync(mapper);
5502
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5504 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5505 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5506
5507 processUp(mapper);
5508 processSync(mapper);
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5510
5511 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005512 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513 prepareDisplay(DISPLAY_ORIENTATION_270);
5514 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
5515 processSync(mapper);
5516
5517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5518 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5519 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5520
5521 processUp(mapper);
5522 processSync(mapper);
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5524}
5525
5526TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 addConfigurationProperty("touch.deviceType", "touchScreen");
5528 prepareDisplay(DISPLAY_ORIENTATION_0);
5529 prepareButtons();
5530 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005531 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532
5533 // These calculations are based on the input device calibration documentation.
5534 int32_t rawX = 100;
5535 int32_t rawY = 200;
5536 int32_t rawPressure = 10;
5537 int32_t rawToolMajor = 12;
5538 int32_t rawDistance = 2;
5539 int32_t rawTiltX = 30;
5540 int32_t rawTiltY = 110;
5541
5542 float x = toDisplayX(rawX);
5543 float y = toDisplayY(rawY);
5544 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5545 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5546 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5547 float distance = float(rawDistance);
5548
5549 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5550 float tiltScale = M_PI / 180;
5551 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5552 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5553 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5554 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5555
5556 processDown(mapper, rawX, rawY);
5557 processPressure(mapper, rawPressure);
5558 processToolMajor(mapper, rawToolMajor);
5559 processDistance(mapper, rawDistance);
5560 processTilt(mapper, rawTiltX, rawTiltY);
5561 processSync(mapper);
5562
5563 NotifyMotionArgs args;
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5566 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5567 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5568}
5569
Jason Gerecke489fda82012-09-07 17:19:40 -07005570TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005571 addConfigurationProperty("touch.deviceType", "touchScreen");
5572 prepareDisplay(DISPLAY_ORIENTATION_0);
5573 prepareLocationCalibration();
5574 prepareButtons();
5575 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005576 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005577
5578 int32_t rawX = 100;
5579 int32_t rawY = 200;
5580
5581 float x = toDisplayX(toCookedX(rawX, rawY));
5582 float y = toDisplayY(toCookedY(rawX, rawY));
5583
5584 processDown(mapper, rawX, rawY);
5585 processSync(mapper);
5586
5587 NotifyMotionArgs args;
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5590 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5591}
5592
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 addConfigurationProperty("touch.deviceType", "touchScreen");
5595 prepareDisplay(DISPLAY_ORIENTATION_0);
5596 prepareButtons();
5597 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005598 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599
5600 NotifyMotionArgs motionArgs;
5601 NotifyKeyArgs keyArgs;
5602
5603 processDown(mapper, 100, 200);
5604 processSync(mapper);
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5606 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5607 ASSERT_EQ(0, motionArgs.buttonState);
5608
5609 // press BTN_LEFT, release BTN_LEFT
5610 processKey(mapper, BTN_LEFT, 1);
5611 processSync(mapper);
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5614 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5615
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5618 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5619
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 processKey(mapper, BTN_LEFT, 0);
5621 processSync(mapper);
5622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005625
5626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005628 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629
5630 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5631 processKey(mapper, BTN_RIGHT, 1);
5632 processKey(mapper, BTN_MIDDLE, 1);
5633 processSync(mapper);
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5636 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5637 motionArgs.buttonState);
5638
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5641 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5642
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5645 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5646 motionArgs.buttonState);
5647
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 processKey(mapper, BTN_RIGHT, 0);
5649 processSync(mapper);
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005651 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005653
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005656 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657
5658 processKey(mapper, BTN_MIDDLE, 0);
5659 processSync(mapper);
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005662 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005663
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005666 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667
5668 // press BTN_BACK, release BTN_BACK
5669 processKey(mapper, BTN_BACK, 1);
5670 processSync(mapper);
5671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5672 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5673 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005674
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005677 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5678
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5681 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682
5683 processKey(mapper, BTN_BACK, 0);
5684 processSync(mapper);
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005686 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005688
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005691 ASSERT_EQ(0, motionArgs.buttonState);
5692
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5694 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5695 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5696
5697 // press BTN_SIDE, release BTN_SIDE
5698 processKey(mapper, BTN_SIDE, 1);
5699 processSync(mapper);
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5701 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5702 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005703
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005706 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5707
5708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5710 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005711
5712 processKey(mapper, BTN_SIDE, 0);
5713 processSync(mapper);
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005716 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005717
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005720 ASSERT_EQ(0, motionArgs.buttonState);
5721
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5723 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5724 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5725
5726 // press BTN_FORWARD, release BTN_FORWARD
5727 processKey(mapper, BTN_FORWARD, 1);
5728 processSync(mapper);
5729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5730 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5731 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005732
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005735 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5736
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5739 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005740
5741 processKey(mapper, BTN_FORWARD, 0);
5742 processSync(mapper);
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005746
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005749 ASSERT_EQ(0, motionArgs.buttonState);
5750
Michael Wrightd02c5b62014-02-10 15:10:22 -08005751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5752 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5753 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5754
5755 // press BTN_EXTRA, release BTN_EXTRA
5756 processKey(mapper, BTN_EXTRA, 1);
5757 processSync(mapper);
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5759 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5760 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005761
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005764 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5765
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5767 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5768 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769
5770 processKey(mapper, BTN_EXTRA, 0);
5771 processSync(mapper);
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005773 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005775
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005778 ASSERT_EQ(0, motionArgs.buttonState);
5779
Michael Wrightd02c5b62014-02-10 15:10:22 -08005780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5781 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5782 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5783
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5785
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786 // press BTN_STYLUS, release BTN_STYLUS
5787 processKey(mapper, BTN_STYLUS, 1);
5788 processSync(mapper);
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005791 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5792
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5795 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796
5797 processKey(mapper, BTN_STYLUS, 0);
5798 processSync(mapper);
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005801 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005802
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005805 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806
5807 // press BTN_STYLUS2, release BTN_STYLUS2
5808 processKey(mapper, BTN_STYLUS2, 1);
5809 processSync(mapper);
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005812 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5813
5814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5815 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5816 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817
5818 processKey(mapper, BTN_STYLUS2, 0);
5819 processSync(mapper);
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005823
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005826 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827
5828 // release touch
5829 processUp(mapper);
5830 processSync(mapper);
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5832 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5833 ASSERT_EQ(0, motionArgs.buttonState);
5834}
5835
5836TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837 addConfigurationProperty("touch.deviceType", "touchScreen");
5838 prepareDisplay(DISPLAY_ORIENTATION_0);
5839 prepareButtons();
5840 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005841 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842
5843 NotifyMotionArgs motionArgs;
5844
5845 // default tool type is finger
5846 processDown(mapper, 100, 200);
5847 processSync(mapper);
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5851
5852 // eraser
5853 processKey(mapper, BTN_TOOL_RUBBER, 1);
5854 processSync(mapper);
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5858
5859 // stylus
5860 processKey(mapper, BTN_TOOL_RUBBER, 0);
5861 processKey(mapper, BTN_TOOL_PEN, 1);
5862 processSync(mapper);
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5866
5867 // brush
5868 processKey(mapper, BTN_TOOL_PEN, 0);
5869 processKey(mapper, BTN_TOOL_BRUSH, 1);
5870 processSync(mapper);
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5874
5875 // pencil
5876 processKey(mapper, BTN_TOOL_BRUSH, 0);
5877 processKey(mapper, BTN_TOOL_PENCIL, 1);
5878 processSync(mapper);
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5882
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005883 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 processKey(mapper, BTN_TOOL_PENCIL, 0);
5885 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5886 processSync(mapper);
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5890
5891 // mouse
5892 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5893 processKey(mapper, BTN_TOOL_MOUSE, 1);
5894 processSync(mapper);
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5898
5899 // lens
5900 processKey(mapper, BTN_TOOL_MOUSE, 0);
5901 processKey(mapper, BTN_TOOL_LENS, 1);
5902 processSync(mapper);
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5906
5907 // double-tap
5908 processKey(mapper, BTN_TOOL_LENS, 0);
5909 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5914
5915 // triple-tap
5916 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5917 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5918 processSync(mapper);
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5921 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5922
5923 // quad-tap
5924 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5925 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5926 processSync(mapper);
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5930
5931 // finger
5932 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5933 processKey(mapper, BTN_TOOL_FINGER, 1);
5934 processSync(mapper);
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5938
5939 // stylus trumps finger
5940 processKey(mapper, BTN_TOOL_PEN, 1);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5945
5946 // eraser trumps stylus
5947 processKey(mapper, BTN_TOOL_RUBBER, 1);
5948 processSync(mapper);
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5951 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5952
5953 // mouse trumps eraser
5954 processKey(mapper, BTN_TOOL_MOUSE, 1);
5955 processSync(mapper);
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5958 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5959
5960 // back to default tool type
5961 processKey(mapper, BTN_TOOL_MOUSE, 0);
5962 processKey(mapper, BTN_TOOL_RUBBER, 0);
5963 processKey(mapper, BTN_TOOL_PEN, 0);
5964 processKey(mapper, BTN_TOOL_FINGER, 0);
5965 processSync(mapper);
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5969}
5970
5971TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972 addConfigurationProperty("touch.deviceType", "touchScreen");
5973 prepareDisplay(DISPLAY_ORIENTATION_0);
5974 prepareButtons();
5975 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005976 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005977 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978
5979 NotifyMotionArgs motionArgs;
5980
5981 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5982 processKey(mapper, BTN_TOOL_FINGER, 1);
5983 processMove(mapper, 100, 200);
5984 processSync(mapper);
5985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5986 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5988 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5989
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5993 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5994
5995 // move a little
5996 processMove(mapper, 150, 250);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6001 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6002
6003 // down when BTN_TOUCH is pressed, pressure defaults to 1
6004 processKey(mapper, BTN_TOUCH, 1);
6005 processSync(mapper);
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6009 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6010
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6014 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6015
6016 // up when BTN_TOUCH is released, hover restored
6017 processKey(mapper, BTN_TOUCH, 0);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6022 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6023
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6025 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6027 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6028
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6030 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6032 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6033
6034 // exit hover when pointer goes away
6035 processKey(mapper, BTN_TOOL_FINGER, 0);
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6040 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6041}
6042
6043TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 addConfigurationProperty("touch.deviceType", "touchScreen");
6045 prepareDisplay(DISPLAY_ORIENTATION_0);
6046 prepareButtons();
6047 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006048 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049
6050 NotifyMotionArgs motionArgs;
6051
6052 // initially hovering because pressure is 0
6053 processDown(mapper, 100, 200);
6054 processPressure(mapper, 0);
6055 processSync(mapper);
6056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6057 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6059 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6060
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6062 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6064 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6065
6066 // move a little
6067 processMove(mapper, 150, 250);
6068 processSync(mapper);
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6070 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6072 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6073
6074 // down when pressure is non-zero
6075 processPressure(mapper, RAW_PRESSURE_MAX);
6076 processSync(mapper);
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6080 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6081
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6085 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6086
6087 // up when pressure becomes 0, hover restored
6088 processPressure(mapper, 0);
6089 processSync(mapper);
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6091 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6093 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6094
6095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6096 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6098 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6099
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6103 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6104
6105 // exit hover when pointer goes away
6106 processUp(mapper);
6107 processSync(mapper);
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6109 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6111 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6112}
6113
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114// --- MultiTouchInputMapperTest ---
6115
6116class MultiTouchInputMapperTest : public TouchInputMapperTest {
6117protected:
6118 void prepareAxes(int axes);
6119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006120 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6121 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6122 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6123 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6124 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6125 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6126 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6127 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6128 void processId(MultiTouchInputMapper& mapper, int32_t id);
6129 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6130 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6131 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6132 void processMTSync(MultiTouchInputMapper& mapper);
6133 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134};
6135
6136void MultiTouchInputMapperTest::prepareAxes(int axes) {
6137 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006138 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140 }
6141 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006142 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6143 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006145 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6146 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147 }
6148 }
6149 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006150 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6151 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006153 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6154 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 }
6156 }
6157 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006158 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6159 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160 }
6161 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6163 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 }
6165 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006166 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6167 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 }
6169 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006170 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6171 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006172 }
6173 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006174 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6175 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 }
6177 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006178 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 }
6180}
6181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006182void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6183 int32_t y) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006184 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6185 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186}
6187
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006188void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6189 int32_t touchMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006190 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191}
6192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006193void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6194 int32_t touchMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006195 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196}
6197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006198void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006199 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200}
6201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006202void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006203 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204}
6205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006206void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6207 int32_t orientation) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006208 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209}
6210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006211void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006212 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213}
6214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006215void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006216 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217}
6218
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006219void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006220 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221}
6222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006223void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006224 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006225}
6226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006227void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006228 process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229}
6230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006231void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6232 int32_t value) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006233 process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234}
6235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006236void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006237 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238}
6239
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006240void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakouad71ad02018-11-16 11:24:35 -08006241 process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242}
6243
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245 addConfigurationProperty("touch.deviceType", "touchScreen");
6246 prepareDisplay(DISPLAY_ORIENTATION_0);
6247 prepareAxes(POSITION);
6248 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006249 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250
arthurhungdcef2dc2020-08-11 14:47:50 +08006251 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252
6253 NotifyMotionArgs motionArgs;
6254
6255 // Two fingers down at once.
6256 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6257 processPosition(mapper, x1, y1);
6258 processMTSync(mapper);
6259 processPosition(mapper, x2, y2);
6260 processMTSync(mapper);
6261 processSync(mapper);
6262
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6264 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6265 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6266 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6267 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6269 ASSERT_EQ(0, motionArgs.flags);
6270 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6271 ASSERT_EQ(0, motionArgs.buttonState);
6272 ASSERT_EQ(0, motionArgs.edgeFlags);
6273 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6274 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6277 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6278 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6279 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6280 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6281
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6284 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6285 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6286 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6288 motionArgs.action);
6289 ASSERT_EQ(0, motionArgs.flags);
6290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6291 ASSERT_EQ(0, motionArgs.buttonState);
6292 ASSERT_EQ(0, motionArgs.edgeFlags);
6293 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6294 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6296 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6299 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6301 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6302 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6303 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6304 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6305
6306 // Move.
6307 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6308 processPosition(mapper, x1, y1);
6309 processMTSync(mapper);
6310 processPosition(mapper, x2, y2);
6311 processMTSync(mapper);
6312 processSync(mapper);
6313
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6315 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6316 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6317 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6318 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6320 ASSERT_EQ(0, motionArgs.flags);
6321 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6322 ASSERT_EQ(0, motionArgs.buttonState);
6323 ASSERT_EQ(0, motionArgs.edgeFlags);
6324 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6325 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6327 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6330 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6332 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6333 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6334 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6335 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6336
6337 // First finger up.
6338 x2 += 15; y2 -= 20;
6339 processPosition(mapper, x2, y2);
6340 processMTSync(mapper);
6341 processSync(mapper);
6342
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6345 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6346 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6347 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6349 motionArgs.action);
6350 ASSERT_EQ(0, motionArgs.flags);
6351 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6352 ASSERT_EQ(0, motionArgs.buttonState);
6353 ASSERT_EQ(0, motionArgs.edgeFlags);
6354 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6355 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6357 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6360 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6362 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6363 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6364 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6366
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6369 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6370 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6371 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6373 ASSERT_EQ(0, motionArgs.flags);
6374 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6375 ASSERT_EQ(0, motionArgs.buttonState);
6376 ASSERT_EQ(0, motionArgs.edgeFlags);
6377 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6378 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6381 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6382 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6383 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6384 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6385
6386 // Move.
6387 x2 += 20; y2 -= 25;
6388 processPosition(mapper, x2, y2);
6389 processMTSync(mapper);
6390 processSync(mapper);
6391
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6394 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6395 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6396 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6398 ASSERT_EQ(0, motionArgs.flags);
6399 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6400 ASSERT_EQ(0, motionArgs.buttonState);
6401 ASSERT_EQ(0, motionArgs.edgeFlags);
6402 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6403 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6406 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6407 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6408 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6410
6411 // New finger down.
6412 int32_t x3 = 700, y3 = 300;
6413 processPosition(mapper, x2, y2);
6414 processMTSync(mapper);
6415 processPosition(mapper, x3, y3);
6416 processMTSync(mapper);
6417 processSync(mapper);
6418
6419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6421 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6422 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6423 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6424 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6425 motionArgs.action);
6426 ASSERT_EQ(0, motionArgs.flags);
6427 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6428 ASSERT_EQ(0, motionArgs.buttonState);
6429 ASSERT_EQ(0, motionArgs.edgeFlags);
6430 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6431 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6433 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6436 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6438 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6439 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6440 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6442
6443 // Second finger up.
6444 x3 += 30; y3 -= 20;
6445 processPosition(mapper, x3, y3);
6446 processMTSync(mapper);
6447 processSync(mapper);
6448
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6451 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6454 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6455 motionArgs.action);
6456 ASSERT_EQ(0, motionArgs.flags);
6457 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6458 ASSERT_EQ(0, motionArgs.buttonState);
6459 ASSERT_EQ(0, motionArgs.edgeFlags);
6460 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6463 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6466 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6468 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6469 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6470 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6471 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6475 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6476 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6477 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6479 ASSERT_EQ(0, motionArgs.flags);
6480 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6481 ASSERT_EQ(0, motionArgs.buttonState);
6482 ASSERT_EQ(0, motionArgs.edgeFlags);
6483 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6484 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6487 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6488 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6489 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6490 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6491
6492 // Last finger up.
6493 processMTSync(mapper);
6494 processSync(mapper);
6495
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6497 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6498 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6499 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6500 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6501 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6502 ASSERT_EQ(0, motionArgs.flags);
6503 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6504 ASSERT_EQ(0, motionArgs.buttonState);
6505 ASSERT_EQ(0, motionArgs.edgeFlags);
6506 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6510 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6511 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6512 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6513 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6514
6515 // Should not have sent any more keys or motions.
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6518}
6519
6520TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006521 addConfigurationProperty("touch.deviceType", "touchScreen");
6522 prepareDisplay(DISPLAY_ORIENTATION_0);
6523 prepareAxes(POSITION | ID);
6524 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006525 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006526
arthurhungdcef2dc2020-08-11 14:47:50 +08006527 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528
6529 NotifyMotionArgs motionArgs;
6530
6531 // Two fingers down at once.
6532 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6533 processPosition(mapper, x1, y1);
6534 processId(mapper, 1);
6535 processMTSync(mapper);
6536 processPosition(mapper, x2, y2);
6537 processId(mapper, 2);
6538 processMTSync(mapper);
6539 processSync(mapper);
6540
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6543 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6547 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6548
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6551 motionArgs.action);
6552 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6553 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6555 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6558 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6560 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6561
6562 // Move.
6563 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6564 processPosition(mapper, x1, y1);
6565 processId(mapper, 1);
6566 processMTSync(mapper);
6567 processPosition(mapper, x2, y2);
6568 processId(mapper, 2);
6569 processMTSync(mapper);
6570 processSync(mapper);
6571
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6574 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6575 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6577 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6580 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6582 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6583
6584 // First finger up.
6585 x2 += 15; y2 -= 20;
6586 processPosition(mapper, x2, y2);
6587 processId(mapper, 2);
6588 processMTSync(mapper);
6589 processSync(mapper);
6590
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6593 motionArgs.action);
6594 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6595 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6597 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6600 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6602 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6603
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6606 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6607 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6610 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6611
6612 // Move.
6613 x2 += 20; y2 -= 25;
6614 processPosition(mapper, x2, y2);
6615 processId(mapper, 2);
6616 processMTSync(mapper);
6617 processSync(mapper);
6618
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6621 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6622 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6625 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6626
6627 // New finger down.
6628 int32_t x3 = 700, y3 = 300;
6629 processPosition(mapper, x2, y2);
6630 processId(mapper, 2);
6631 processMTSync(mapper);
6632 processPosition(mapper, x3, y3);
6633 processId(mapper, 3);
6634 processMTSync(mapper);
6635 processSync(mapper);
6636
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6639 motionArgs.action);
6640 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6641 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6643 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6646 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6648 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6649
6650 // Second finger up.
6651 x3 += 30; y3 -= 20;
6652 processPosition(mapper, x3, y3);
6653 processId(mapper, 3);
6654 processMTSync(mapper);
6655 processSync(mapper);
6656
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6659 motionArgs.action);
6660 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6661 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6663 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6666 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6668 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6669
6670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6672 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6676 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6677
6678 // Last finger up.
6679 processMTSync(mapper);
6680 processSync(mapper);
6681
6682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6683 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6684 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6685 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6689
6690 // Should not have sent any more keys or motions.
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6693}
6694
6695TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006696 addConfigurationProperty("touch.deviceType", "touchScreen");
6697 prepareDisplay(DISPLAY_ORIENTATION_0);
6698 prepareAxes(POSITION | ID | SLOT);
6699 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006700 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006701
arthurhungdcef2dc2020-08-11 14:47:50 +08006702 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006703
6704 NotifyMotionArgs motionArgs;
6705
6706 // Two fingers down at once.
6707 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6708 processPosition(mapper, x1, y1);
6709 processId(mapper, 1);
6710 processSlot(mapper, 1);
6711 processPosition(mapper, x2, y2);
6712 processId(mapper, 2);
6713 processSync(mapper);
6714
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6721 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6722
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6724 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6725 motionArgs.action);
6726 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6727 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6729 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6732 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6734 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6735
6736 // Move.
6737 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6738 processSlot(mapper, 0);
6739 processPosition(mapper, x1, y1);
6740 processSlot(mapper, 1);
6741 processPosition(mapper, x2, y2);
6742 processSync(mapper);
6743
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6746 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6747 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6749 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6752 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6754 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6755
6756 // First finger up.
6757 x2 += 15; y2 -= 20;
6758 processSlot(mapper, 0);
6759 processId(mapper, -1);
6760 processSlot(mapper, 1);
6761 processPosition(mapper, x2, y2);
6762 processSync(mapper);
6763
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6766 motionArgs.action);
6767 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6768 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6770 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6773 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6775 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6776
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6779 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6780 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6783 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6784
6785 // Move.
6786 x2 += 20; y2 -= 25;
6787 processPosition(mapper, x2, y2);
6788 processSync(mapper);
6789
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6792 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6793 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6796 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6797
6798 // New finger down.
6799 int32_t x3 = 700, y3 = 300;
6800 processPosition(mapper, x2, y2);
6801 processSlot(mapper, 0);
6802 processId(mapper, 3);
6803 processPosition(mapper, x3, y3);
6804 processSync(mapper);
6805
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6807 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6808 motionArgs.action);
6809 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6810 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6812 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6815 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6817 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6818
6819 // Second finger up.
6820 x3 += 30; y3 -= 20;
6821 processSlot(mapper, 1);
6822 processId(mapper, -1);
6823 processSlot(mapper, 0);
6824 processPosition(mapper, x3, y3);
6825 processSync(mapper);
6826
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6828 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6829 motionArgs.action);
6830 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6831 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6833 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6836 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6838 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6839
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6842 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6843 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6846 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6847
6848 // Last finger up.
6849 processId(mapper, -1);
6850 processSync(mapper);
6851
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6853 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6854 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6855 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6858 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6859
6860 // Should not have sent any more keys or motions.
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6863}
6864
6865TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006866 addConfigurationProperty("touch.deviceType", "touchScreen");
6867 prepareDisplay(DISPLAY_ORIENTATION_0);
6868 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006869 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006870
6871 // These calculations are based on the input device calibration documentation.
6872 int32_t rawX = 100;
6873 int32_t rawY = 200;
6874 int32_t rawTouchMajor = 7;
6875 int32_t rawTouchMinor = 6;
6876 int32_t rawToolMajor = 9;
6877 int32_t rawToolMinor = 8;
6878 int32_t rawPressure = 11;
6879 int32_t rawDistance = 0;
6880 int32_t rawOrientation = 3;
6881 int32_t id = 5;
6882
6883 float x = toDisplayX(rawX);
6884 float y = toDisplayY(rawY);
6885 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6886 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6887 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6888 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6889 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6890 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6891 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6892 float distance = float(rawDistance);
6893
6894 processPosition(mapper, rawX, rawY);
6895 processTouchMajor(mapper, rawTouchMajor);
6896 processTouchMinor(mapper, rawTouchMinor);
6897 processToolMajor(mapper, rawToolMajor);
6898 processToolMinor(mapper, rawToolMinor);
6899 processPressure(mapper, rawPressure);
6900 processOrientation(mapper, rawOrientation);
6901 processDistance(mapper, rawDistance);
6902 processId(mapper, id);
6903 processMTSync(mapper);
6904 processSync(mapper);
6905
6906 NotifyMotionArgs args;
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6908 ASSERT_EQ(0, args.pointerProperties[0].id);
6909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6910 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6911 orientation, distance));
6912}
6913
6914TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006915 addConfigurationProperty("touch.deviceType", "touchScreen");
6916 prepareDisplay(DISPLAY_ORIENTATION_0);
6917 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6918 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006919 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006920
6921 // These calculations are based on the input device calibration documentation.
6922 int32_t rawX = 100;
6923 int32_t rawY = 200;
6924 int32_t rawTouchMajor = 140;
6925 int32_t rawTouchMinor = 120;
6926 int32_t rawToolMajor = 180;
6927 int32_t rawToolMinor = 160;
6928
6929 float x = toDisplayX(rawX);
6930 float y = toDisplayY(rawY);
6931 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6932 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6933 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6934 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6935 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6936
6937 processPosition(mapper, rawX, rawY);
6938 processTouchMajor(mapper, rawTouchMajor);
6939 processTouchMinor(mapper, rawTouchMinor);
6940 processToolMajor(mapper, rawToolMajor);
6941 processToolMinor(mapper, rawToolMinor);
6942 processMTSync(mapper);
6943 processSync(mapper);
6944
6945 NotifyMotionArgs args;
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6948 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6949}
6950
6951TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006952 addConfigurationProperty("touch.deviceType", "touchScreen");
6953 prepareDisplay(DISPLAY_ORIENTATION_0);
6954 prepareAxes(POSITION | TOUCH | TOOL);
6955 addConfigurationProperty("touch.size.calibration", "diameter");
6956 addConfigurationProperty("touch.size.scale", "10");
6957 addConfigurationProperty("touch.size.bias", "160");
6958 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006960
6961 // These calculations are based on the input device calibration documentation.
6962 // Note: We only provide a single common touch/tool value because the device is assumed
6963 // not to emit separate values for each pointer (isSummed = 1).
6964 int32_t rawX = 100;
6965 int32_t rawY = 200;
6966 int32_t rawX2 = 150;
6967 int32_t rawY2 = 250;
6968 int32_t rawTouchMajor = 5;
6969 int32_t rawToolMajor = 8;
6970
6971 float x = toDisplayX(rawX);
6972 float y = toDisplayY(rawY);
6973 float x2 = toDisplayX(rawX2);
6974 float y2 = toDisplayY(rawY2);
6975 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6976 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6977 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6978
6979 processPosition(mapper, rawX, rawY);
6980 processTouchMajor(mapper, rawTouchMajor);
6981 processToolMajor(mapper, rawToolMajor);
6982 processMTSync(mapper);
6983 processPosition(mapper, rawX2, rawY2);
6984 processTouchMajor(mapper, rawTouchMajor);
6985 processToolMajor(mapper, rawToolMajor);
6986 processMTSync(mapper);
6987 processSync(mapper);
6988
6989 NotifyMotionArgs args;
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6991 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6992
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6994 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6995 args.action);
6996 ASSERT_EQ(size_t(2), args.pointerCount);
6997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6998 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7000 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7001}
7002
7003TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004 addConfigurationProperty("touch.deviceType", "touchScreen");
7005 prepareDisplay(DISPLAY_ORIENTATION_0);
7006 prepareAxes(POSITION | TOUCH | TOOL);
7007 addConfigurationProperty("touch.size.calibration", "area");
7008 addConfigurationProperty("touch.size.scale", "43");
7009 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007010 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007011
7012 // These calculations are based on the input device calibration documentation.
7013 int32_t rawX = 100;
7014 int32_t rawY = 200;
7015 int32_t rawTouchMajor = 5;
7016 int32_t rawToolMajor = 8;
7017
7018 float x = toDisplayX(rawX);
7019 float y = toDisplayY(rawY);
7020 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7021 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7022 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7023
7024 processPosition(mapper, rawX, rawY);
7025 processTouchMajor(mapper, rawTouchMajor);
7026 processToolMajor(mapper, rawToolMajor);
7027 processMTSync(mapper);
7028 processSync(mapper);
7029
7030 NotifyMotionArgs args;
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7033 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7034}
7035
7036TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007037 addConfigurationProperty("touch.deviceType", "touchScreen");
7038 prepareDisplay(DISPLAY_ORIENTATION_0);
7039 prepareAxes(POSITION | PRESSURE);
7040 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7041 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007042 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007043
Michael Wrightaa449c92017-12-13 21:21:43 +00007044 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007045 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007046 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7047 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7048 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7049
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 // These calculations are based on the input device calibration documentation.
7051 int32_t rawX = 100;
7052 int32_t rawY = 200;
7053 int32_t rawPressure = 60;
7054
7055 float x = toDisplayX(rawX);
7056 float y = toDisplayY(rawY);
7057 float pressure = float(rawPressure) * 0.01f;
7058
7059 processPosition(mapper, rawX, rawY);
7060 processPressure(mapper, rawPressure);
7061 processMTSync(mapper);
7062 processSync(mapper);
7063
7064 NotifyMotionArgs args;
7065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7067 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7068}
7069
7070TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071 addConfigurationProperty("touch.deviceType", "touchScreen");
7072 prepareDisplay(DISPLAY_ORIENTATION_0);
7073 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007074 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075
7076 NotifyMotionArgs motionArgs;
7077 NotifyKeyArgs keyArgs;
7078
7079 processId(mapper, 1);
7080 processPosition(mapper, 100, 200);
7081 processSync(mapper);
7082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7084 ASSERT_EQ(0, motionArgs.buttonState);
7085
7086 // press BTN_LEFT, release BTN_LEFT
7087 processKey(mapper, BTN_LEFT, 1);
7088 processSync(mapper);
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7091 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7092
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7095 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7096
Michael Wrightd02c5b62014-02-10 15:10:22 -08007097 processKey(mapper, BTN_LEFT, 0);
7098 processSync(mapper);
7099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007101 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007102
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007105 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106
7107 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7108 processKey(mapper, BTN_RIGHT, 1);
7109 processKey(mapper, BTN_MIDDLE, 1);
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_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7114 motionArgs.buttonState);
7115
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7118 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7119
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7122 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7123 motionArgs.buttonState);
7124
Michael Wrightd02c5b62014-02-10 15:10:22 -08007125 processKey(mapper, BTN_RIGHT, 0);
7126 processSync(mapper);
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007128 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007130
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007133 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134
7135 processKey(mapper, BTN_MIDDLE, 0);
7136 processSync(mapper);
7137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007140
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007143 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144
7145 // press BTN_BACK, release BTN_BACK
7146 processKey(mapper, BTN_BACK, 1);
7147 processSync(mapper);
7148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7149 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7150 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007151
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007154 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7155
7156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7158 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159
7160 processKey(mapper, BTN_BACK, 0);
7161 processSync(mapper);
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007164 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007165
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007168 ASSERT_EQ(0, motionArgs.buttonState);
7169
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7171 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7172 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7173
7174 // press BTN_SIDE, release BTN_SIDE
7175 processKey(mapper, BTN_SIDE, 1);
7176 processSync(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7178 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7179 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007180
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007183 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7184
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7187 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188
7189 processKey(mapper, BTN_SIDE, 0);
7190 processSync(mapper);
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007194
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007197 ASSERT_EQ(0, motionArgs.buttonState);
7198
Michael Wrightd02c5b62014-02-10 15:10:22 -08007199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7200 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7201 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7202
7203 // press BTN_FORWARD, release BTN_FORWARD
7204 processKey(mapper, BTN_FORWARD, 1);
7205 processSync(mapper);
7206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7207 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7208 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007209
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007212 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7213
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7216 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007217
7218 processKey(mapper, BTN_FORWARD, 0);
7219 processSync(mapper);
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007222 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007223
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007226 ASSERT_EQ(0, motionArgs.buttonState);
7227
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7229 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7230 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7231
7232 // press BTN_EXTRA, release BTN_EXTRA
7233 processKey(mapper, BTN_EXTRA, 1);
7234 processSync(mapper);
7235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7236 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7237 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007238
Michael Wrightd02c5b62014-02-10 15:10:22 -08007239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007241 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7242
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7245 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246
7247 processKey(mapper, BTN_EXTRA, 0);
7248 processSync(mapper);
7249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007251 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007252
7253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007255 ASSERT_EQ(0, motionArgs.buttonState);
7256
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7258 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7259 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7260
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7262
Michael Wrightd02c5b62014-02-10 15:10:22 -08007263 // press BTN_STYLUS, release BTN_STYLUS
7264 processKey(mapper, BTN_STYLUS, 1);
7265 processSync(mapper);
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007268 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7269
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7272 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007273
7274 processKey(mapper, BTN_STYLUS, 0);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007278 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007279
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007282 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007283
7284 // press BTN_STYLUS2, release BTN_STYLUS2
7285 processKey(mapper, BTN_STYLUS2, 1);
7286 processSync(mapper);
7287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007289 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7290
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7293 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007294
7295 processKey(mapper, BTN_STYLUS2, 0);
7296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007299 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007300
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007303 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304
7305 // release touch
7306 processId(mapper, -1);
7307 processSync(mapper);
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7309 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7310 ASSERT_EQ(0, motionArgs.buttonState);
7311}
7312
7313TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314 addConfigurationProperty("touch.deviceType", "touchScreen");
7315 prepareDisplay(DISPLAY_ORIENTATION_0);
7316 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007317 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007318
7319 NotifyMotionArgs motionArgs;
7320
7321 // default tool type is finger
7322 processId(mapper, 1);
7323 processPosition(mapper, 100, 200);
7324 processSync(mapper);
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7326 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7328
7329 // eraser
7330 processKey(mapper, BTN_TOOL_RUBBER, 1);
7331 processSync(mapper);
7332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7335
7336 // stylus
7337 processKey(mapper, BTN_TOOL_RUBBER, 0);
7338 processKey(mapper, BTN_TOOL_PEN, 1);
7339 processSync(mapper);
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7343
7344 // brush
7345 processKey(mapper, BTN_TOOL_PEN, 0);
7346 processKey(mapper, BTN_TOOL_BRUSH, 1);
7347 processSync(mapper);
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7351
7352 // pencil
7353 processKey(mapper, BTN_TOOL_BRUSH, 0);
7354 processKey(mapper, BTN_TOOL_PENCIL, 1);
7355 processSync(mapper);
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7359
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007360 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007361 processKey(mapper, BTN_TOOL_PENCIL, 0);
7362 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7363 processSync(mapper);
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7367
7368 // mouse
7369 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7370 processKey(mapper, BTN_TOOL_MOUSE, 1);
7371 processSync(mapper);
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7375
7376 // lens
7377 processKey(mapper, BTN_TOOL_MOUSE, 0);
7378 processKey(mapper, BTN_TOOL_LENS, 1);
7379 processSync(mapper);
7380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7383
7384 // double-tap
7385 processKey(mapper, BTN_TOOL_LENS, 0);
7386 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7387 processSync(mapper);
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7391
7392 // triple-tap
7393 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7394 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7395 processSync(mapper);
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7399
7400 // quad-tap
7401 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7402 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7403 processSync(mapper);
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7407
7408 // finger
7409 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7410 processKey(mapper, BTN_TOOL_FINGER, 1);
7411 processSync(mapper);
7412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7415
7416 // stylus trumps finger
7417 processKey(mapper, BTN_TOOL_PEN, 1);
7418 processSync(mapper);
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7422
7423 // eraser trumps stylus
7424 processKey(mapper, BTN_TOOL_RUBBER, 1);
7425 processSync(mapper);
7426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7429
7430 // mouse trumps eraser
7431 processKey(mapper, BTN_TOOL_MOUSE, 1);
7432 processSync(mapper);
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7436
7437 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7438 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7439 processSync(mapper);
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7443
7444 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7445 processToolType(mapper, MT_TOOL_PEN);
7446 processSync(mapper);
7447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7450
7451 // back to default tool type
7452 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7453 processKey(mapper, BTN_TOOL_MOUSE, 0);
7454 processKey(mapper, BTN_TOOL_RUBBER, 0);
7455 processKey(mapper, BTN_TOOL_PEN, 0);
7456 processKey(mapper, BTN_TOOL_FINGER, 0);
7457 processSync(mapper);
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7461}
7462
7463TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464 addConfigurationProperty("touch.deviceType", "touchScreen");
7465 prepareDisplay(DISPLAY_ORIENTATION_0);
7466 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007467 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007468 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007469
7470 NotifyMotionArgs motionArgs;
7471
7472 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7473 processId(mapper, 1);
7474 processPosition(mapper, 100, 200);
7475 processSync(mapper);
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7477 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7479 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7480
7481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7482 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7484 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7485
7486 // move a little
7487 processPosition(mapper, 150, 250);
7488 processSync(mapper);
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7490 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7492 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7493
7494 // down when BTN_TOUCH is pressed, pressure defaults to 1
7495 processKey(mapper, BTN_TOUCH, 1);
7496 processSync(mapper);
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7498 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7500 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7501
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7503 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7505 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7506
7507 // up when BTN_TOUCH is released, hover restored
7508 processKey(mapper, BTN_TOUCH, 0);
7509 processSync(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7511 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7513 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7514
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7516 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7518 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7519
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7523 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7524
7525 // exit hover when pointer goes away
7526 processId(mapper, -1);
7527 processSync(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7531 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7532}
7533
7534TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007535 addConfigurationProperty("touch.deviceType", "touchScreen");
7536 prepareDisplay(DISPLAY_ORIENTATION_0);
7537 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007538 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007539
7540 NotifyMotionArgs motionArgs;
7541
7542 // initially hovering because pressure is 0
7543 processId(mapper, 1);
7544 processPosition(mapper, 100, 200);
7545 processPressure(mapper, 0);
7546 processSync(mapper);
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7548 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7550 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7551
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7553 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7555 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7556
7557 // move a little
7558 processPosition(mapper, 150, 250);
7559 processSync(mapper);
7560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7563 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7564
7565 // down when pressure becomes non-zero
7566 processPressure(mapper, RAW_PRESSURE_MAX);
7567 processSync(mapper);
7568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7571 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7572
7573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7574 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7576 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7577
7578 // up when pressure becomes 0, hover restored
7579 processPressure(mapper, 0);
7580 processSync(mapper);
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7582 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7584 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7585
7586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7587 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7589 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7590
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7594 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7595
7596 // exit hover when pointer goes away
7597 processId(mapper, -1);
7598 processSync(mapper);
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7602 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7603}
7604
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007605/**
7606 * Set the input device port <--> display port associations, and check that the
7607 * events are routed to the display that matches the display port.
7608 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7609 */
7610TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007611 const std::string usb2 = "USB2";
7612 const uint8_t hdmi1 = 0;
7613 const uint8_t hdmi2 = 1;
7614 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007615 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007616
7617 addConfigurationProperty("touch.deviceType", "touchScreen");
7618 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007619 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007620
7621 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7622 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7623
7624 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7625 // for this input device is specified, and the matching viewport is not present,
7626 // the input device should be disabled (at the mapper level).
7627
7628 // Add viewport for display 2 on hdmi2
7629 prepareSecondaryDisplay(type, hdmi2);
7630 // Send a touch event
7631 processPosition(mapper, 100, 100);
7632 processSync(mapper);
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7634
7635 // Add viewport for display 1 on hdmi1
7636 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7637 // Send a touch event again
7638 processPosition(mapper, 100, 100);
7639 processSync(mapper);
7640
7641 NotifyMotionArgs args;
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7643 ASSERT_EQ(DISPLAY_ID, args.displayId);
7644}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007645
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007646TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007647 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007648 std::shared_ptr<FakePointerController> fakePointerController =
7649 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007650 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007651 fakePointerController->setPosition(100, 200);
7652 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007653 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7654
Garfield Tan888a6a42020-01-09 11:39:16 -08007655 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007656 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007657
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007658 prepareDisplay(DISPLAY_ORIENTATION_0);
7659 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007660 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007661
7662 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007663 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007664
7665 NotifyMotionArgs motionArgs;
7666 processPosition(mapper, 100, 100);
7667 processSync(mapper);
7668
7669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7670 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7671 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7672}
7673
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007674/**
7675 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7676 * events should not be delivered to the listener.
7677 */
7678TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7679 addConfigurationProperty("touch.deviceType", "touchScreen");
7680 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7681 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7682 ViewportType::INTERNAL);
7683 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7684 prepareAxes(POSITION);
7685 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7686
7687 NotifyMotionArgs motionArgs;
7688 processPosition(mapper, 100, 100);
7689 processSync(mapper);
7690
7691 mFakeListener->assertNotifyMotionWasNotCalled();
7692}
7693
Garfield Tanc734e4f2021-01-15 20:01:39 -08007694TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7695 addConfigurationProperty("touch.deviceType", "touchScreen");
7696 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7697 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7698 ViewportType::INTERNAL);
7699 std::optional<DisplayViewport> optionalDisplayViewport =
7700 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7701 ASSERT_TRUE(optionalDisplayViewport.has_value());
7702 DisplayViewport displayViewport = *optionalDisplayViewport;
7703
7704 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7705 prepareAxes(POSITION);
7706 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7707
7708 // Finger down
7709 int32_t x = 100, y = 100;
7710 processPosition(mapper, x, y);
7711 processSync(mapper);
7712
7713 NotifyMotionArgs motionArgs;
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7715 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7716
7717 // Deactivate display viewport
7718 displayViewport.isActive = false;
7719 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7720 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7721
7722 // Finger move
7723 x += 10, y += 10;
7724 processPosition(mapper, x, y);
7725 processSync(mapper);
7726
7727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7728 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7729
7730 // Reactivate display viewport
7731 displayViewport.isActive = true;
7732 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7733 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7734
7735 // Finger move again
7736 x += 10, y += 10;
7737 processPosition(mapper, x, y);
7738 processSync(mapper);
7739
7740 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7741 // no pointer on the touch device.
7742 mFakeListener->assertNotifyMotionWasNotCalled();
7743}
7744
Arthur Hung7c645402019-01-25 17:45:42 +08007745TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7746 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007747 prepareAxes(POSITION | ID | SLOT);
7748 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007749 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007750
7751 // Create the second touch screen device, and enable multi fingers.
7752 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007753 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007754 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007755 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007756 std::shared_ptr<InputDevice> device2 =
7757 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7758 Flags<InputDeviceClass>(0));
7759
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007760 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7761 0 /*flat*/, 0 /*fuzz*/);
7762 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7763 0 /*flat*/, 0 /*fuzz*/);
7764 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7765 0 /*flat*/, 0 /*fuzz*/);
7766 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7767 0 /*flat*/, 0 /*fuzz*/);
7768 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7769 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7770 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007771
7772 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007773 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007774 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7775 device2->reset(ARBITRARY_TIME);
7776
7777 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007778 std::shared_ptr<FakePointerController> fakePointerController =
7779 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007780 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7781 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7782
7783 // Setup policy for associated displays and show touches.
7784 const uint8_t hdmi1 = 0;
7785 const uint8_t hdmi2 = 1;
7786 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7787 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7788 mFakePolicy->setShowTouches(true);
7789
7790 // Create displays.
7791 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007792 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007793
7794 // Default device will reconfigure above, need additional reconfiguration for another device.
7795 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007796 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007797
7798 // Two fingers down at default display.
7799 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7800 processPosition(mapper, x1, y1);
7801 processId(mapper, 1);
7802 processSlot(mapper, 1);
7803 processPosition(mapper, x2, y2);
7804 processId(mapper, 2);
7805 processSync(mapper);
7806
7807 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7808 fakePointerController->getSpots().find(DISPLAY_ID);
7809 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7810 ASSERT_EQ(size_t(2), iter->second.size());
7811
7812 // Two fingers down at second display.
7813 processPosition(mapper2, x1, y1);
7814 processId(mapper2, 1);
7815 processSlot(mapper2, 1);
7816 processPosition(mapper2, x2, y2);
7817 processId(mapper2, 2);
7818 processSync(mapper2);
7819
7820 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7821 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7822 ASSERT_EQ(size_t(2), iter->second.size());
7823}
7824
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007825TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007826 prepareAxes(POSITION);
7827 addConfigurationProperty("touch.deviceType", "touchScreen");
7828 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007829 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007830
7831 NotifyMotionArgs motionArgs;
7832 // Unrotated video frame
7833 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7834 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007835 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007836 processPosition(mapper, 100, 200);
7837 processSync(mapper);
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7839 ASSERT_EQ(frames, motionArgs.videoFrames);
7840
7841 // Subsequent touch events should not have any videoframes
7842 // This is implemented separately in FakeEventHub,
7843 // but that should match the behaviour of TouchVideoDevice.
7844 processPosition(mapper, 200, 200);
7845 processSync(mapper);
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7847 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7848}
7849
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007850TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007851 prepareAxes(POSITION);
7852 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007853 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007854 // Unrotated video frame
7855 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7856 NotifyMotionArgs motionArgs;
7857
7858 // Test all 4 orientations
7859 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
7860 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7861 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7862 clearViewports();
7863 prepareDisplay(orientation);
7864 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007865 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007866 processPosition(mapper, 100, 200);
7867 processSync(mapper);
7868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7869 frames[0].rotate(orientation);
7870 ASSERT_EQ(frames, motionArgs.videoFrames);
7871 }
7872}
7873
7874TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007875 prepareAxes(POSITION);
7876 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007877 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007878 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7879 // so mix these.
7880 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7881 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7882 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7883 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7884 NotifyMotionArgs motionArgs;
7885
7886 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007887 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007888 processPosition(mapper, 100, 200);
7889 processSync(mapper);
7890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7891 std::for_each(frames.begin(), frames.end(),
7892 [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
7893 ASSERT_EQ(frames, motionArgs.videoFrames);
7894}
7895
Arthur Hung9da14732019-09-02 16:16:58 +08007896/**
7897 * If we had defined port associations, but the viewport is not ready, the touch device would be
7898 * expected to be disabled, and it should be enabled after the viewport has found.
7899 */
7900TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007901 constexpr uint8_t hdmi2 = 1;
7902 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007903 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007904
7905 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7906
7907 addConfigurationProperty("touch.deviceType", "touchScreen");
7908 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007909 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007910
7911 ASSERT_EQ(mDevice->isEnabled(), false);
7912
7913 // Add display on hdmi2, the device should be enabled and can receive touch event.
7914 prepareSecondaryDisplay(type, hdmi2);
7915 ASSERT_EQ(mDevice->isEnabled(), true);
7916
7917 // Send a touch event.
7918 processPosition(mapper, 100, 100);
7919 processSync(mapper);
7920
7921 NotifyMotionArgs args;
7922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7923 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7924}
7925
Arthur Hung6cd19a42019-08-30 19:04:12 +08007926
Arthur Hung6cd19a42019-08-30 19:04:12 +08007927
Arthur Hung421eb1c2020-01-16 00:09:42 +08007928TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007929 addConfigurationProperty("touch.deviceType", "touchScreen");
7930 prepareDisplay(DISPLAY_ORIENTATION_0);
7931 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007932 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007933
7934 NotifyMotionArgs motionArgs;
7935
7936 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7937 // finger down
7938 processId(mapper, 1);
7939 processPosition(mapper, x1, y1);
7940 processSync(mapper);
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7944
7945 // finger move
7946 processId(mapper, 1);
7947 processPosition(mapper, x2, y2);
7948 processSync(mapper);
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7951 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7952
7953 // finger up.
7954 processId(mapper, -1);
7955 processSync(mapper);
7956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7957 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7958 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7959
7960 // new finger down
7961 processId(mapper, 1);
7962 processPosition(mapper, x3, y3);
7963 processSync(mapper);
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7965 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7967}
7968
7969/**
arthurhungcc7f9802020-04-30 17:55:40 +08007970 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
7971 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08007972 */
arthurhungcc7f9802020-04-30 17:55:40 +08007973TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007974 addConfigurationProperty("touch.deviceType", "touchScreen");
7975 prepareDisplay(DISPLAY_ORIENTATION_0);
7976 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007977 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007978
7979 NotifyMotionArgs motionArgs;
7980
7981 // default tool type is finger
7982 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08007983 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007984 processPosition(mapper, x1, y1);
7985 processSync(mapper);
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7989
7990 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7991 processToolType(mapper, MT_TOOL_PALM);
7992 processSync(mapper);
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7994 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7995
7996 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08007997 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08007998 processPosition(mapper, x2, y2);
7999 processSync(mapper);
8000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8001
8002 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008003 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008004 processSync(mapper);
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8006
8007 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008008 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008009 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008010 processPosition(mapper, x3, y3);
8011 processSync(mapper);
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8013 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8014 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8015}
8016
arthurhungbf89a482020-04-17 17:37:55 +08008017/**
arthurhungcc7f9802020-04-30 17:55:40 +08008018 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8019 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008020 */
arthurhungcc7f9802020-04-30 17:55:40 +08008021TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008022 addConfigurationProperty("touch.deviceType", "touchScreen");
8023 prepareDisplay(DISPLAY_ORIENTATION_0);
8024 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8025 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8026
8027 NotifyMotionArgs motionArgs;
8028
8029 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008030 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8031 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008032 processPosition(mapper, x1, y1);
8033 processSync(mapper);
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8035 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8037
8038 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008039 processSlot(mapper, SECOND_SLOT);
8040 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008041 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008042 processSync(mapper);
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8044 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8045 motionArgs.action);
8046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8047
8048 // If the tool type of the first finger changes to MT_TOOL_PALM,
8049 // we expect to receive ACTION_POINTER_UP with cancel flag.
8050 processSlot(mapper, FIRST_SLOT);
8051 processId(mapper, FIRST_TRACKING_ID);
8052 processToolType(mapper, MT_TOOL_PALM);
8053 processSync(mapper);
8054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8055 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8056 motionArgs.action);
8057 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8058
8059 // The following MOVE events of second finger should be processed.
8060 processSlot(mapper, SECOND_SLOT);
8061 processId(mapper, SECOND_TRACKING_ID);
8062 processPosition(mapper, x2 + 1, y2 + 1);
8063 processSync(mapper);
8064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8066 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8067
8068 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8069 // it. Second finger receive move.
8070 processSlot(mapper, FIRST_SLOT);
8071 processId(mapper, INVALID_TRACKING_ID);
8072 processSync(mapper);
8073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8075 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8076
8077 // Second finger keeps moving.
8078 processSlot(mapper, SECOND_SLOT);
8079 processId(mapper, SECOND_TRACKING_ID);
8080 processPosition(mapper, x2 + 2, y2 + 2);
8081 processSync(mapper);
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8084 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8085
8086 // Second finger up.
8087 processId(mapper, INVALID_TRACKING_ID);
8088 processSync(mapper);
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8091 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8092}
8093
8094/**
8095 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8096 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8097 */
8098TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8099 addConfigurationProperty("touch.deviceType", "touchScreen");
8100 prepareDisplay(DISPLAY_ORIENTATION_0);
8101 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8102 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8103
8104 NotifyMotionArgs motionArgs;
8105
8106 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8107 // First finger down.
8108 processId(mapper, FIRST_TRACKING_ID);
8109 processPosition(mapper, x1, y1);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8114
8115 // Second finger down.
8116 processSlot(mapper, SECOND_SLOT);
8117 processId(mapper, SECOND_TRACKING_ID);
8118 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008119 processSync(mapper);
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8121 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8122 motionArgs.action);
8123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8124
arthurhungcc7f9802020-04-30 17:55:40 +08008125 // If the tool type of the first finger changes to MT_TOOL_PALM,
8126 // we expect to receive ACTION_POINTER_UP with cancel flag.
8127 processSlot(mapper, FIRST_SLOT);
8128 processId(mapper, FIRST_TRACKING_ID);
8129 processToolType(mapper, MT_TOOL_PALM);
8130 processSync(mapper);
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8132 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8133 motionArgs.action);
8134 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8135
8136 // Second finger keeps moving.
8137 processSlot(mapper, SECOND_SLOT);
8138 processId(mapper, SECOND_TRACKING_ID);
8139 processPosition(mapper, x2 + 1, y2 + 1);
8140 processSync(mapper);
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8143
8144 // second finger becomes palm, receive cancel due to only 1 finger is active.
8145 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008146 processToolType(mapper, MT_TOOL_PALM);
8147 processSync(mapper);
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8149 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8150
arthurhungcc7f9802020-04-30 17:55:40 +08008151 // third finger down.
8152 processSlot(mapper, THIRD_SLOT);
8153 processId(mapper, THIRD_TRACKING_ID);
8154 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008155 processPosition(mapper, x3, y3);
8156 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8158 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008160 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8161
8162 // third finger move
8163 processId(mapper, THIRD_TRACKING_ID);
8164 processPosition(mapper, x3 + 1, y3 + 1);
8165 processSync(mapper);
8166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8168
8169 // first finger up, third finger receive move.
8170 processSlot(mapper, FIRST_SLOT);
8171 processId(mapper, INVALID_TRACKING_ID);
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8175 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8176
8177 // second finger up, third finger receive move.
8178 processSlot(mapper, SECOND_SLOT);
8179 processId(mapper, INVALID_TRACKING_ID);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8183 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8184
8185 // third finger up.
8186 processSlot(mapper, THIRD_SLOT);
8187 processId(mapper, INVALID_TRACKING_ID);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8191 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8192}
8193
8194/**
8195 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8196 * and the active finger could still be allowed to receive the events
8197 */
8198TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8199 addConfigurationProperty("touch.deviceType", "touchScreen");
8200 prepareDisplay(DISPLAY_ORIENTATION_0);
8201 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8202 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8203
8204 NotifyMotionArgs motionArgs;
8205
8206 // default tool type is finger
8207 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8208 processId(mapper, FIRST_TRACKING_ID);
8209 processPosition(mapper, x1, y1);
8210 processSync(mapper);
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8214
8215 // Second finger down.
8216 processSlot(mapper, SECOND_SLOT);
8217 processId(mapper, SECOND_TRACKING_ID);
8218 processPosition(mapper, x2, y2);
8219 processSync(mapper);
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8221 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8222 motionArgs.action);
8223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8224
8225 // If the tool type of the second finger changes to MT_TOOL_PALM,
8226 // we expect to receive ACTION_POINTER_UP with cancel flag.
8227 processId(mapper, SECOND_TRACKING_ID);
8228 processToolType(mapper, MT_TOOL_PALM);
8229 processSync(mapper);
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8231 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8232 motionArgs.action);
8233 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8234
8235 // The following MOVE event should be processed.
8236 processSlot(mapper, FIRST_SLOT);
8237 processId(mapper, FIRST_TRACKING_ID);
8238 processPosition(mapper, x1 + 1, y1 + 1);
8239 processSync(mapper);
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8242 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8243
8244 // second finger up.
8245 processSlot(mapper, SECOND_SLOT);
8246 processId(mapper, INVALID_TRACKING_ID);
8247 processSync(mapper);
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8250
8251 // first finger keep moving
8252 processSlot(mapper, FIRST_SLOT);
8253 processId(mapper, FIRST_TRACKING_ID);
8254 processPosition(mapper, x1 + 2, y1 + 2);
8255 processSync(mapper);
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8258
8259 // first finger up.
8260 processId(mapper, INVALID_TRACKING_ID);
8261 processSync(mapper);
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8263 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8264 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008265}
8266
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008267// --- MultiTouchInputMapperTest_ExternalDevice ---
8268
8269class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8270protected:
Chris Yea52ade12020-08-27 16:49:20 -07008271 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008272};
8273
8274/**
8275 * Expect fallback to internal viewport if device is external and external viewport is not present.
8276 */
8277TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8278 prepareAxes(POSITION);
8279 addConfigurationProperty("touch.deviceType", "touchScreen");
8280 prepareDisplay(DISPLAY_ORIENTATION_0);
8281 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8282
8283 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8284
8285 NotifyMotionArgs motionArgs;
8286
8287 // Expect the event to be sent to the internal viewport,
8288 // because an external viewport is not present.
8289 processPosition(mapper, 100, 100);
8290 processSync(mapper);
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8292 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8293
8294 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008295 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008296 processPosition(mapper, 100, 100);
8297 processSync(mapper);
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8300}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008301
8302/**
8303 * Test touch should not work if outside of surface.
8304 */
8305class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8306protected:
8307 void halfDisplayToCenterHorizontal(int32_t orientation) {
8308 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008309 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008310
8311 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8312 internalViewport->orientation = orientation;
8313 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8314 internalViewport->logicalLeft = 0;
8315 internalViewport->logicalTop = 0;
8316 internalViewport->logicalRight = DISPLAY_HEIGHT;
8317 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8318
8319 internalViewport->physicalLeft = 0;
8320 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8321 internalViewport->physicalRight = DISPLAY_HEIGHT;
8322 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8323
8324 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8325 internalViewport->deviceHeight = DISPLAY_WIDTH;
8326 } else {
8327 internalViewport->logicalLeft = 0;
8328 internalViewport->logicalTop = 0;
8329 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8330 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8331
8332 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8333 internalViewport->physicalTop = 0;
8334 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8335 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8336
8337 internalViewport->deviceWidth = DISPLAY_WIDTH;
8338 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8339 }
8340
8341 mFakePolicy->updateViewport(internalViewport.value());
8342 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8343 }
8344
arthurhung5d547942020-12-14 17:04:45 +08008345 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8346 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008347 int32_t yExpected) {
8348 // touch on outside area should not work.
8349 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8350 processSync(mapper);
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8352
8353 // touch on inside area should receive the event.
8354 NotifyMotionArgs args;
8355 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8356 processSync(mapper);
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8358 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8359 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8360
8361 // Reset.
8362 mapper.reset(ARBITRARY_TIME);
8363 }
8364};
8365
arthurhung5d547942020-12-14 17:04:45 +08008366TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008367 addConfigurationProperty("touch.deviceType", "touchScreen");
8368 prepareDisplay(DISPLAY_ORIENTATION_0);
8369 prepareAxes(POSITION);
8370 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8371
8372 // Touch on center of normal display should work.
8373 const int32_t x = DISPLAY_WIDTH / 4;
8374 const int32_t y = DISPLAY_HEIGHT / 2;
8375 processPosition(mapper, toRawX(x), toRawY(y));
8376 processSync(mapper);
8377 NotifyMotionArgs args;
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8380 0.0f, 0.0f, 0.0f, 0.0f));
8381 // Reset.
8382 mapper.reset(ARBITRARY_TIME);
8383
8384 // Let physical display be different to device, and make surface and physical could be 1:1.
8385 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
8386
8387 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8388 const int32_t yExpected = y;
8389 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8390}
8391
arthurhung5d547942020-12-14 17:04:45 +08008392TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008393 addConfigurationProperty("touch.deviceType", "touchScreen");
8394 prepareDisplay(DISPLAY_ORIENTATION_0);
8395 prepareAxes(POSITION);
8396 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8397
8398 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8399 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8400
8401 const int32_t x = DISPLAY_WIDTH / 4;
8402 const int32_t y = DISPLAY_HEIGHT / 2;
8403
8404 // expect x/y = swap x/y then reverse y.
8405 const int32_t xExpected = y;
8406 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8407 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8408}
8409
arthurhung5d547942020-12-14 17:04:45 +08008410TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008411 addConfigurationProperty("touch.deviceType", "touchScreen");
8412 prepareDisplay(DISPLAY_ORIENTATION_0);
8413 prepareAxes(POSITION);
8414 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8415
8416 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8417 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8418
8419 const int32_t x = DISPLAY_WIDTH / 4;
8420 const int32_t y = DISPLAY_HEIGHT / 2;
8421
8422 // expect x/y = swap x/y then reverse x.
8423 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8424 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8425 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8426}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008427
arthurhunga36b28e2020-12-29 20:28:15 +08008428TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner) {
8429 addConfigurationProperty("touch.deviceType", "touchScreen");
8430 prepareDisplay(DISPLAY_ORIENTATION_0);
8431 prepareAxes(POSITION);
8432 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8433
8434 const int32_t x = 0;
8435 const int32_t y = 0;
8436
8437 const int32_t xExpected = x;
8438 const int32_t yExpected = y;
8439 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8440
8441 clearViewports();
8442 prepareDisplay(DISPLAY_ORIENTATION_90);
8443 // expect x/y = swap x/y then reverse y.
8444 const int32_t xExpected90 = y;
8445 const int32_t yExpected90 = DISPLAY_WIDTH - 1;
8446 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8447
8448 clearViewports();
8449 prepareDisplay(DISPLAY_ORIENTATION_270);
8450 // expect x/y = swap x/y then reverse x.
8451 const int32_t xExpected270 = DISPLAY_HEIGHT - 1;
8452 const int32_t yExpected270 = x;
8453 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8454}
8455
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008456TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8457 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8458 std::shared_ptr<FakePointerController> fakePointerController =
8459 std::make_shared<FakePointerController>();
8460 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8461 fakePointerController->setPosition(0, 0);
8462 fakePointerController->setButtonState(0);
8463
8464 // prepare device and capture
8465 prepareDisplay(DISPLAY_ORIENTATION_0);
8466 prepareAxes(POSITION | ID | SLOT);
8467 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8468 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8469 mFakePolicy->setPointerCapture(true);
8470 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8471 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8472
8473 // captured touchpad should be a touchpad source
8474 NotifyDeviceResetArgs resetArgs;
8475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8476 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8477
Chris Yef74dc422020-09-02 22:41:50 -07008478 InputDeviceInfo deviceInfo;
8479 mDevice->getDeviceInfo(&deviceInfo);
8480
8481 const InputDeviceInfo::MotionRange* relRangeX =
8482 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8483 ASSERT_NE(relRangeX, nullptr);
8484 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8485 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8486 const InputDeviceInfo::MotionRange* relRangeY =
8487 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8488 ASSERT_NE(relRangeY, nullptr);
8489 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8490 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8491
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008492 // run captured pointer tests - note that this is unscaled, so input listener events should be
8493 // identical to what the hardware sends (accounting for any
8494 // calibration).
8495 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008496 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008497 processId(mapper, 1);
8498 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8499 processKey(mapper, BTN_TOUCH, 1);
8500 processSync(mapper);
8501
8502 // expect coord[0] to contain initial location of touch 0
8503 NotifyMotionArgs args;
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8506 ASSERT_EQ(1U, args.pointerCount);
8507 ASSERT_EQ(0, args.pointerProperties[0].id);
8508 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8509 ASSERT_NO_FATAL_FAILURE(
8510 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8511
8512 // FINGER 1 DOWN
8513 processSlot(mapper, 1);
8514 processId(mapper, 2);
8515 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8516 processSync(mapper);
8517
8518 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008520 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8521 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008522 ASSERT_EQ(2U, args.pointerCount);
8523 ASSERT_EQ(0, args.pointerProperties[0].id);
8524 ASSERT_EQ(1, args.pointerProperties[1].id);
8525 ASSERT_NO_FATAL_FAILURE(
8526 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8527 ASSERT_NO_FATAL_FAILURE(
8528 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8529
8530 // FINGER 1 MOVE
8531 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8532 processSync(mapper);
8533
8534 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8535 // from move
8536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8538 ASSERT_NO_FATAL_FAILURE(
8539 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8540 ASSERT_NO_FATAL_FAILURE(
8541 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8542
8543 // FINGER 0 MOVE
8544 processSlot(mapper, 0);
8545 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8546 processSync(mapper);
8547
8548 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8551 ASSERT_NO_FATAL_FAILURE(
8552 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8553 ASSERT_NO_FATAL_FAILURE(
8554 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8555
8556 // BUTTON DOWN
8557 processKey(mapper, BTN_LEFT, 1);
8558 processSync(mapper);
8559
8560 // touchinputmapper design sends a move before button press
8561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8565
8566 // BUTTON UP
8567 processKey(mapper, BTN_LEFT, 0);
8568 processSync(mapper);
8569
8570 // touchinputmapper design sends a move after button release
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8575
8576 // FINGER 0 UP
8577 processId(mapper, -1);
8578 processSync(mapper);
8579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8580 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8581
8582 // FINGER 1 MOVE
8583 processSlot(mapper, 1);
8584 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8585 processSync(mapper);
8586
8587 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8590 ASSERT_EQ(1U, args.pointerCount);
8591 ASSERT_EQ(1, args.pointerProperties[0].id);
8592 ASSERT_NO_FATAL_FAILURE(
8593 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8594
8595 // FINGER 1 UP
8596 processId(mapper, -1);
8597 processKey(mapper, BTN_TOUCH, 0);
8598 processSync(mapper);
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8601
8602 // non captured touchpad should be a mouse source
8603 mFakePolicy->setPointerCapture(false);
8604 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8606 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8607}
8608
8609TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8610 std::shared_ptr<FakePointerController> fakePointerController =
8611 std::make_shared<FakePointerController>();
8612 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8613 fakePointerController->setPosition(0, 0);
8614 fakePointerController->setButtonState(0);
8615
8616 // prepare device and capture
8617 prepareDisplay(DISPLAY_ORIENTATION_0);
8618 prepareAxes(POSITION | ID | SLOT);
8619 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8620 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8621 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8622 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8623 // run uncaptured pointer tests - pushes out generic events
8624 // FINGER 0 DOWN
8625 processId(mapper, 3);
8626 processPosition(mapper, 100, 100);
8627 processKey(mapper, BTN_TOUCH, 1);
8628 processSync(mapper);
8629
8630 // start at (100,100), cursor should be at (0,0) * scale
8631 NotifyMotionArgs args;
8632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8634 ASSERT_NO_FATAL_FAILURE(
8635 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8636
8637 // FINGER 0 MOVE
8638 processPosition(mapper, 200, 200);
8639 processSync(mapper);
8640
8641 // compute scaling to help with touch position checking
8642 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8643 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8644 float scale =
8645 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8646
8647 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8649 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8651 0, 0, 0, 0, 0, 0, 0));
8652}
8653
8654TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8655 std::shared_ptr<FakePointerController> fakePointerController =
8656 std::make_shared<FakePointerController>();
8657
8658 prepareDisplay(DISPLAY_ORIENTATION_0);
8659 prepareAxes(POSITION | ID | SLOT);
8660 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8661 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8662 mFakePolicy->setPointerCapture(false);
8663 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8664
8665 // uncaptured touchpad should be a pointer device
8666 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8667
8668 // captured touchpad should be a touchpad device
8669 mFakePolicy->setPointerCapture(true);
8670 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8671 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8672}
8673
Michael Wrightd02c5b62014-02-10 15:10:22 -08008674} // namespace android