1 /*
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 
17 #include <CursorInputMapper.h>
18 #include <InputDevice.h>
19 #include <InputMapper.h>
20 #include <InputReader.h>
21 #include <InputReaderBase.h>
22 #include <InputReaderFactory.h>
23 #include <KeyboardInputMapper.h>
24 #include <MultiTouchInputMapper.h>
25 #include <SingleTouchInputMapper.h>
26 #include <SwitchInputMapper.h>
27 #include <TestInputListener.h>
28 #include <TouchInputMapper.h>
29 #include <UinputDevice.h>
30 
31 #include <android-base/thread_annotations.h>
32 #include <gtest/gtest.h>
33 #include <inttypes.h>
34 #include <math.h>
35 
36 namespace android {
37 
38 using std::chrono_literals::operator""ms;
39 
40 // Timeout for waiting for an expected event
41 static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
42 
43 // An arbitrary time value.
44 static const nsecs_t ARBITRARY_TIME = 1234;
45 
46 // Arbitrary display properties.
47 static const int32_t DISPLAY_ID = 0;
48 static const int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
49 static const int32_t DISPLAY_WIDTH = 480;
50 static const int32_t DISPLAY_HEIGHT = 800;
51 static const int32_t VIRTUAL_DISPLAY_ID = 1;
52 static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
53 static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
54 static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
55 static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
56 
57 // Error tolerance for floating point assertions.
58 static const float EPSILON = 0.001f;
59 
60 template<typename T>
min(T a,T b)61 static inline T min(T a, T b) {
62     return a < b ? a : b;
63 }
64 
avg(float x,float y)65 static inline float avg(float x, float y) {
66     return (x + y) / 2;
67 }
68 
69 
70 // --- FakePointerController ---
71 
72 class FakePointerController : public PointerControllerInterface {
73     bool mHaveBounds;
74     float mMinX, mMinY, mMaxX, mMaxY;
75     float mX, mY;
76     int32_t mButtonState;
77     int32_t mDisplayId;
78 
79 protected:
~FakePointerController()80     virtual ~FakePointerController() { }
81 
82 public:
FakePointerController()83     FakePointerController() :
84         mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
85         mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
86     }
87 
setBounds(float minX,float minY,float maxX,float maxY)88     void setBounds(float minX, float minY, float maxX, float maxY) {
89         mHaveBounds = true;
90         mMinX = minX;
91         mMinY = minY;
92         mMaxX = maxX;
93         mMaxY = maxY;
94     }
95 
setPosition(float x,float y)96     virtual void setPosition(float x, float y) {
97         mX = x;
98         mY = y;
99     }
100 
setButtonState(int32_t buttonState)101     virtual void setButtonState(int32_t buttonState) {
102         mButtonState = buttonState;
103     }
104 
getButtonState() const105     virtual int32_t getButtonState() const {
106         return mButtonState;
107     }
108 
getPosition(float * outX,float * outY) const109     virtual void getPosition(float* outX, float* outY) const {
110         *outX = mX;
111         *outY = mY;
112     }
113 
getDisplayId() const114     virtual int32_t getDisplayId() const {
115         return mDisplayId;
116     }
117 
setDisplayViewport(const DisplayViewport & viewport)118     virtual void setDisplayViewport(const DisplayViewport& viewport) {
119         mDisplayId = viewport.displayId;
120     }
121 
getSpots()122     const std::map<int32_t, std::vector<int32_t>>& getSpots() {
123         return mSpotsByDisplay;
124     }
125 
126 private:
getBounds(float * outMinX,float * outMinY,float * outMaxX,float * outMaxY) const127     virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
128         *outMinX = mMinX;
129         *outMinY = mMinY;
130         *outMaxX = mMaxX;
131         *outMaxY = mMaxY;
132         return mHaveBounds;
133     }
134 
move(float deltaX,float deltaY)135     virtual void move(float deltaX, float deltaY) {
136         mX += deltaX;
137         if (mX < mMinX) mX = mMinX;
138         if (mX > mMaxX) mX = mMaxX;
139         mY += deltaY;
140         if (mY < mMinY) mY = mMinY;
141         if (mY > mMaxY) mY = mMaxY;
142     }
143 
fade(Transition)144     virtual void fade(Transition) {
145     }
146 
unfade(Transition)147     virtual void unfade(Transition) {
148     }
149 
setPresentation(Presentation)150     virtual void setPresentation(Presentation) {
151     }
152 
setSpots(const PointerCoords *,const uint32_t *,BitSet32 spotIdBits,int32_t displayId)153     virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
154             int32_t displayId) {
155         std::vector<int32_t> newSpots;
156         // Add spots for fingers that are down.
157         for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
158             uint32_t id = idBits.clearFirstMarkedBit();
159             newSpots.push_back(id);
160         }
161 
162         mSpotsByDisplay[displayId] = newSpots;
163     }
164 
clearSpots()165     virtual void clearSpots() {
166     }
167 
168     std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
169 };
170 
171 
172 // --- FakeInputReaderPolicy ---
173 
174 class FakeInputReaderPolicy : public InputReaderPolicyInterface {
175     std::mutex mLock;
176     std::condition_variable mDevicesChangedCondition;
177 
178     InputReaderConfiguration mConfig;
179     KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
180     std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
GUARDED_BY(mLock)181     bool mInputDevicesChanged GUARDED_BY(mLock){false};
182     std::vector<DisplayViewport> mViewports;
183     TouchAffineTransformation transform;
184 
185 protected:
~FakeInputReaderPolicy()186     virtual ~FakeInputReaderPolicy() { }
187 
188 public:
FakeInputReaderPolicy()189     FakeInputReaderPolicy() {
190     }
191 
assertInputDevicesChanged()192     void assertInputDevicesChanged() {
193         waitForInputDevices([](bool devicesChanged) {
194             if (!devicesChanged) {
195                 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
196             }
197         });
198     }
199 
assertInputDevicesNotChanged()200     void assertInputDevicesNotChanged() {
201         waitForInputDevices([](bool devicesChanged) {
202             if (devicesChanged) {
203                 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
204             }
205         });
206     }
207 
clearViewports()208     virtual void clearViewports() {
209         mViewports.clear();
210         mConfig.setDisplayViewports(mViewports);
211     }
212 
getDisplayViewportByUniqueId(const std::string & uniqueId) const213     std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
214         return mConfig.getDisplayViewportByUniqueId(uniqueId);
215     }
getDisplayViewportByType(ViewportType type) const216     std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
217         return mConfig.getDisplayViewportByType(type);
218     }
219 
getDisplayViewportByPort(uint8_t displayPort) const220     std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
221         return mConfig.getDisplayViewportByPort(displayPort);
222     }
223 
addDisplayViewport(int32_t displayId,int32_t width,int32_t height,int32_t orientation,const std::string & uniqueId,std::optional<uint8_t> physicalPort,ViewportType viewportType)224     void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
225             const std::string& uniqueId, std::optional<uint8_t> physicalPort,
226             ViewportType viewportType) {
227         const DisplayViewport viewport = createDisplayViewport(displayId, width, height,
228                 orientation, uniqueId, physicalPort, viewportType);
229         mViewports.push_back(viewport);
230         mConfig.setDisplayViewports(mViewports);
231     }
232 
updateViewport(const DisplayViewport & viewport)233     bool updateViewport(const DisplayViewport& viewport) {
234         size_t count = mViewports.size();
235         for (size_t i = 0; i < count; i++) {
236             const DisplayViewport& currentViewport = mViewports[i];
237             if (currentViewport.displayId == viewport.displayId) {
238                 mViewports[i] = viewport;
239                 mConfig.setDisplayViewports(mViewports);
240                 return true;
241             }
242         }
243         // no viewport found.
244         return false;
245     }
246 
addExcludedDeviceName(const std::string & deviceName)247     void addExcludedDeviceName(const std::string& deviceName) {
248         mConfig.excludedDeviceNames.push_back(deviceName);
249     }
250 
addInputPortAssociation(const std::string & inputPort,uint8_t displayPort)251     void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
252         mConfig.portAssociations.insert({inputPort, displayPort});
253     }
254 
addDisabledDevice(int32_t deviceId)255     void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
256 
removeDisabledDevice(int32_t deviceId)257     void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
258 
setPointerController(int32_t deviceId,const sp<FakePointerController> & controller)259     void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
260         mPointerControllers.add(deviceId, controller);
261     }
262 
getReaderConfiguration() const263     const InputReaderConfiguration* getReaderConfiguration() const {
264         return &mConfig;
265     }
266 
getInputDevices() const267     const std::vector<InputDeviceInfo>& getInputDevices() const {
268         return mInputDevices;
269     }
270 
getTouchAffineTransformation(const std::string & inputDeviceDescriptor,int32_t surfaceRotation)271     TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
272             int32_t surfaceRotation) {
273         return transform;
274     }
275 
setTouchAffineTransformation(const TouchAffineTransformation t)276     void setTouchAffineTransformation(const TouchAffineTransformation t) {
277         transform = t;
278     }
279 
setPointerCapture(bool enabled)280     void setPointerCapture(bool enabled) {
281         mConfig.pointerCapture = enabled;
282     }
283 
setShowTouches(bool enabled)284     void setShowTouches(bool enabled) {
285         mConfig.showTouches = enabled;
286     }
287 
setDefaultPointerDisplayId(int32_t pointerDisplayId)288     void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
289         mConfig.defaultPointerDisplayId = pointerDisplayId;
290     }
291 
292 private:
createDisplayViewport(int32_t displayId,int32_t width,int32_t height,int32_t orientation,const std::string & uniqueId,std::optional<uint8_t> physicalPort,ViewportType type)293     DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
294             int32_t orientation, const std::string& uniqueId, std::optional<uint8_t> physicalPort,
295             ViewportType type) {
296         bool isRotated = (orientation == DISPLAY_ORIENTATION_90
297                 || orientation == DISPLAY_ORIENTATION_270);
298         DisplayViewport v;
299         v.displayId = displayId;
300         v.orientation = orientation;
301         v.logicalLeft = 0;
302         v.logicalTop = 0;
303         v.logicalRight = isRotated ? height : width;
304         v.logicalBottom = isRotated ? width : height;
305         v.physicalLeft = 0;
306         v.physicalTop = 0;
307         v.physicalRight = isRotated ? height : width;
308         v.physicalBottom = isRotated ? width : height;
309         v.deviceWidth = isRotated ? height : width;
310         v.deviceHeight = isRotated ? width : height;
311         v.uniqueId = uniqueId;
312         v.physicalPort = physicalPort;
313         v.type = type;
314         return v;
315     }
316 
getReaderConfiguration(InputReaderConfiguration * outConfig)317     virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
318         *outConfig = mConfig;
319     }
320 
obtainPointerController(int32_t deviceId)321     virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
322         return mPointerControllers.valueFor(deviceId);
323     }
324 
notifyInputDevicesChanged(const std::vector<InputDeviceInfo> & inputDevices)325     virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
326         std::scoped_lock<std::mutex> lock(mLock);
327         mInputDevices = inputDevices;
328         mInputDevicesChanged = true;
329         mDevicesChangedCondition.notify_all();
330     }
331 
getKeyboardLayoutOverlay(const InputDeviceIdentifier &)332     virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
333         return nullptr;
334     }
335 
getDeviceAlias(const InputDeviceIdentifier &)336     virtual std::string getDeviceAlias(const InputDeviceIdentifier&) {
337         return "";
338     }
339 
waitForInputDevices(std::function<void (bool)> processDevicesChanged)340     void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
341         std::unique_lock<std::mutex> lock(mLock);
342         base::ScopedLockAssertion assumeLocked(mLock);
343 
344         const bool devicesChanged =
345                 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
346                     return mInputDevicesChanged;
347                 });
348         ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
349         mInputDevicesChanged = false;
350     }
351 };
352 
353 // --- FakeEventHub ---
354 
355 class FakeEventHub : public EventHubInterface {
356     struct KeyInfo {
357         int32_t keyCode;
358         uint32_t flags;
359     };
360 
361     struct Device {
362         InputDeviceIdentifier identifier;
363         uint32_t classes;
364         PropertyMap configuration;
365         KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
366         KeyedVector<int, bool> relativeAxes;
367         KeyedVector<int32_t, int32_t> keyCodeStates;
368         KeyedVector<int32_t, int32_t> scanCodeStates;
369         KeyedVector<int32_t, int32_t> switchStates;
370         KeyedVector<int32_t, int32_t> absoluteAxisValue;
371         KeyedVector<int32_t, KeyInfo> keysByScanCode;
372         KeyedVector<int32_t, KeyInfo> keysByUsageCode;
373         KeyedVector<int32_t, bool> leds;
374         std::vector<VirtualKeyDefinition> virtualKeys;
375         bool enabled;
376 
enableandroid::FakeEventHub::Device377         status_t enable() {
378             enabled = true;
379             return OK;
380         }
381 
disableandroid::FakeEventHub::Device382         status_t disable() {
383             enabled = false;
384             return OK;
385         }
386 
Deviceandroid::FakeEventHub::Device387         explicit Device(uint32_t classes) :
388                 classes(classes), enabled(true) {
389         }
390     };
391 
392     std::mutex mLock;
393     std::condition_variable mEventsCondition;
394 
395     KeyedVector<int32_t, Device*> mDevices;
396     std::vector<std::string> mExcludedDevices;
397     List<RawEvent> mEvents GUARDED_BY(mLock);
398     std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
399 
400 public:
~FakeEventHub()401     virtual ~FakeEventHub() {
402         for (size_t i = 0; i < mDevices.size(); i++) {
403             delete mDevices.valueAt(i);
404         }
405     }
406 
FakeEventHub()407     FakeEventHub() { }
408 
addDevice(int32_t deviceId,const std::string & name,uint32_t classes)409     void addDevice(int32_t deviceId, const std::string& name, uint32_t classes) {
410         Device* device = new Device(classes);
411         device->identifier.name = name;
412         mDevices.add(deviceId, device);
413 
414         enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
415     }
416 
removeDevice(int32_t deviceId)417     void removeDevice(int32_t deviceId) {
418         delete mDevices.valueFor(deviceId);
419         mDevices.removeItem(deviceId);
420 
421         enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
422     }
423 
isDeviceEnabled(int32_t deviceId)424     bool isDeviceEnabled(int32_t deviceId) {
425         Device* device = getDevice(deviceId);
426         if (device == nullptr) {
427             ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
428             return false;
429         }
430         return device->enabled;
431     }
432 
enableDevice(int32_t deviceId)433     status_t enableDevice(int32_t deviceId) {
434         status_t result;
435         Device* device = getDevice(deviceId);
436         if (device == nullptr) {
437             ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
438             return BAD_VALUE;
439         }
440         if (device->enabled) {
441             ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
442             return OK;
443         }
444         result = device->enable();
445         return result;
446     }
447 
disableDevice(int32_t deviceId)448     status_t disableDevice(int32_t deviceId) {
449         Device* device = getDevice(deviceId);
450         if (device == nullptr) {
451             ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
452             return BAD_VALUE;
453         }
454         if (!device->enabled) {
455             ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
456             return OK;
457         }
458         return device->disable();
459     }
460 
finishDeviceScan()461     void finishDeviceScan() {
462         enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
463     }
464 
addConfigurationProperty(int32_t deviceId,const String8 & key,const String8 & value)465     void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
466         Device* device = getDevice(deviceId);
467         device->configuration.addProperty(key, value);
468     }
469 
addConfigurationMap(int32_t deviceId,const PropertyMap * configuration)470     void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
471         Device* device = getDevice(deviceId);
472         device->configuration.addAll(configuration);
473     }
474 
addAbsoluteAxis(int32_t deviceId,int axis,int32_t minValue,int32_t maxValue,int flat,int fuzz,int resolution=0)475     void addAbsoluteAxis(int32_t deviceId, int axis,
476             int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
477         Device* device = getDevice(deviceId);
478 
479         RawAbsoluteAxisInfo info;
480         info.valid = true;
481         info.minValue = minValue;
482         info.maxValue = maxValue;
483         info.flat = flat;
484         info.fuzz = fuzz;
485         info.resolution = resolution;
486         device->absoluteAxes.add(axis, info);
487     }
488 
addRelativeAxis(int32_t deviceId,int32_t axis)489     void addRelativeAxis(int32_t deviceId, int32_t axis) {
490         Device* device = getDevice(deviceId);
491         device->relativeAxes.add(axis, true);
492     }
493 
setKeyCodeState(int32_t deviceId,int32_t keyCode,int32_t state)494     void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
495         Device* device = getDevice(deviceId);
496         device->keyCodeStates.replaceValueFor(keyCode, state);
497     }
498 
setScanCodeState(int32_t deviceId,int32_t scanCode,int32_t state)499     void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
500         Device* device = getDevice(deviceId);
501         device->scanCodeStates.replaceValueFor(scanCode, state);
502     }
503 
setSwitchState(int32_t deviceId,int32_t switchCode,int32_t state)504     void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
505         Device* device = getDevice(deviceId);
506         device->switchStates.replaceValueFor(switchCode, state);
507     }
508 
setAbsoluteAxisValue(int32_t deviceId,int32_t axis,int32_t value)509     void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
510         Device* device = getDevice(deviceId);
511         device->absoluteAxisValue.replaceValueFor(axis, value);
512     }
513 
addKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t keyCode,uint32_t flags)514     void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
515             int32_t keyCode, uint32_t flags) {
516         Device* device = getDevice(deviceId);
517         KeyInfo info;
518         info.keyCode = keyCode;
519         info.flags = flags;
520         if (scanCode) {
521             device->keysByScanCode.add(scanCode, info);
522         }
523         if (usageCode) {
524             device->keysByUsageCode.add(usageCode, info);
525         }
526     }
527 
addLed(int32_t deviceId,int32_t led,bool initialState)528     void addLed(int32_t deviceId, int32_t led, bool initialState) {
529         Device* device = getDevice(deviceId);
530         device->leds.add(led, initialState);
531     }
532 
getLedState(int32_t deviceId,int32_t led)533     bool getLedState(int32_t deviceId, int32_t led) {
534         Device* device = getDevice(deviceId);
535         return device->leds.valueFor(led);
536     }
537 
getExcludedDevices()538     std::vector<std::string>& getExcludedDevices() {
539         return mExcludedDevices;
540     }
541 
addVirtualKeyDefinition(int32_t deviceId,const VirtualKeyDefinition & definition)542     void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
543         Device* device = getDevice(deviceId);
544         device->virtualKeys.push_back(definition);
545     }
546 
enqueueEvent(nsecs_t when,int32_t deviceId,int32_t type,int32_t code,int32_t value)547     void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
548             int32_t code, int32_t value) {
549         std::scoped_lock<std::mutex> lock(mLock);
550         RawEvent event;
551         event.when = when;
552         event.deviceId = deviceId;
553         event.type = type;
554         event.code = code;
555         event.value = value;
556         mEvents.push_back(event);
557 
558         if (type == EV_ABS) {
559             setAbsoluteAxisValue(deviceId, code, value);
560         }
561     }
562 
setVideoFrames(std::unordered_map<int32_t,std::vector<TouchVideoFrame>> videoFrames)563     void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
564             std::vector<TouchVideoFrame>> videoFrames) {
565         mVideoFrames = std::move(videoFrames);
566     }
567 
assertQueueIsEmpty()568     void assertQueueIsEmpty() {
569         std::unique_lock<std::mutex> lock(mLock);
570         base::ScopedLockAssertion assumeLocked(mLock);
571         const bool queueIsEmpty =
572                 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
573                                           [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
574         if (!queueIsEmpty) {
575             FAIL() << "Timed out waiting for EventHub queue to be emptied.";
576         }
577     }
578 
579 private:
getDevice(int32_t deviceId) const580     Device* getDevice(int32_t deviceId) const {
581         ssize_t index = mDevices.indexOfKey(deviceId);
582         return index >= 0 ? mDevices.valueAt(index) : nullptr;
583     }
584 
getDeviceClasses(int32_t deviceId) const585     virtual uint32_t getDeviceClasses(int32_t deviceId) const {
586         Device* device = getDevice(deviceId);
587         return device ? device->classes : 0;
588     }
589 
getDeviceIdentifier(int32_t deviceId) const590     virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
591         Device* device = getDevice(deviceId);
592         return device ? device->identifier : InputDeviceIdentifier();
593     }
594 
getDeviceControllerNumber(int32_t) const595     virtual int32_t getDeviceControllerNumber(int32_t) const {
596         return 0;
597     }
598 
getConfiguration(int32_t deviceId,PropertyMap * outConfiguration) const599     virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
600         Device* device = getDevice(deviceId);
601         if (device) {
602             *outConfiguration = device->configuration;
603         }
604     }
605 
getAbsoluteAxisInfo(int32_t deviceId,int axis,RawAbsoluteAxisInfo * outAxisInfo) const606     virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
607             RawAbsoluteAxisInfo* outAxisInfo) const {
608         Device* device = getDevice(deviceId);
609         if (device && device->enabled) {
610             ssize_t index = device->absoluteAxes.indexOfKey(axis);
611             if (index >= 0) {
612                 *outAxisInfo = device->absoluteAxes.valueAt(index);
613                 return OK;
614             }
615         }
616         outAxisInfo->clear();
617         return -1;
618     }
619 
hasRelativeAxis(int32_t deviceId,int axis) const620     virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
621         Device* device = getDevice(deviceId);
622         if (device) {
623             return device->relativeAxes.indexOfKey(axis) >= 0;
624         }
625         return false;
626     }
627 
hasInputProperty(int32_t,int) const628     virtual bool hasInputProperty(int32_t, int) const {
629         return false;
630     }
631 
mapKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t metaState,int32_t * outKeycode,int32_t * outMetaState,uint32_t * outFlags) const632     virtual status_t mapKey(int32_t deviceId,
633             int32_t scanCode, int32_t usageCode, int32_t metaState,
634             int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
635         Device* device = getDevice(deviceId);
636         if (device) {
637             const KeyInfo* key = getKey(device, scanCode, usageCode);
638             if (key) {
639                 if (outKeycode) {
640                     *outKeycode = key->keyCode;
641                 }
642                 if (outFlags) {
643                     *outFlags = key->flags;
644                 }
645                 if (outMetaState) {
646                     *outMetaState = metaState;
647                 }
648                 return OK;
649             }
650         }
651         return NAME_NOT_FOUND;
652     }
653 
getKey(Device * device,int32_t scanCode,int32_t usageCode) const654     const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
655         if (usageCode) {
656             ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
657             if (index >= 0) {
658                 return &device->keysByUsageCode.valueAt(index);
659             }
660         }
661         if (scanCode) {
662             ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
663             if (index >= 0) {
664                 return &device->keysByScanCode.valueAt(index);
665             }
666         }
667         return nullptr;
668     }
669 
mapAxis(int32_t,int32_t,AxisInfo *) const670     virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
671         return NAME_NOT_FOUND;
672     }
673 
setExcludedDevices(const std::vector<std::string> & devices)674     virtual void setExcludedDevices(const std::vector<std::string>& devices) {
675         mExcludedDevices = devices;
676     }
677 
getEvents(int,RawEvent * buffer,size_t)678     virtual size_t getEvents(int, RawEvent* buffer, size_t) {
679         std::scoped_lock<std::mutex> lock(mLock);
680         if (mEvents.empty()) {
681             return 0;
682         }
683 
684         *buffer = *mEvents.begin();
685         mEvents.erase(mEvents.begin());
686         mEventsCondition.notify_all();
687         return 1;
688     }
689 
getVideoFrames(int32_t deviceId)690     virtual std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) {
691         auto it = mVideoFrames.find(deviceId);
692         if (it != mVideoFrames.end()) {
693             std::vector<TouchVideoFrame> frames = std::move(it->second);
694             mVideoFrames.erase(deviceId);
695             return frames;
696         }
697         return {};
698     }
699 
getScanCodeState(int32_t deviceId,int32_t scanCode) const700     virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
701         Device* device = getDevice(deviceId);
702         if (device) {
703             ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
704             if (index >= 0) {
705                 return device->scanCodeStates.valueAt(index);
706             }
707         }
708         return AKEY_STATE_UNKNOWN;
709     }
710 
getKeyCodeState(int32_t deviceId,int32_t keyCode) const711     virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
712         Device* device = getDevice(deviceId);
713         if (device) {
714             ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
715             if (index >= 0) {
716                 return device->keyCodeStates.valueAt(index);
717             }
718         }
719         return AKEY_STATE_UNKNOWN;
720     }
721 
getSwitchState(int32_t deviceId,int32_t sw) const722     virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
723         Device* device = getDevice(deviceId);
724         if (device) {
725             ssize_t index = device->switchStates.indexOfKey(sw);
726             if (index >= 0) {
727                 return device->switchStates.valueAt(index);
728             }
729         }
730         return AKEY_STATE_UNKNOWN;
731     }
732 
getAbsoluteAxisValue(int32_t deviceId,int32_t axis,int32_t * outValue) const733     virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
734             int32_t* outValue) const {
735         Device* device = getDevice(deviceId);
736         if (device) {
737             ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
738             if (index >= 0) {
739                 *outValue = device->absoluteAxisValue.valueAt(index);
740                 return OK;
741             }
742         }
743         *outValue = 0;
744         return -1;
745     }
746 
markSupportedKeyCodes(int32_t deviceId,size_t numCodes,const int32_t * keyCodes,uint8_t * outFlags) const747     virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
748             uint8_t* outFlags) const {
749         bool result = false;
750         Device* device = getDevice(deviceId);
751         if (device) {
752             for (size_t i = 0; i < numCodes; i++) {
753                 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
754                     if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
755                         outFlags[i] = 1;
756                         result = true;
757                     }
758                 }
759                 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
760                     if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
761                         outFlags[i] = 1;
762                         result = true;
763                     }
764                 }
765             }
766         }
767         return result;
768     }
769 
hasScanCode(int32_t deviceId,int32_t scanCode) const770     virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
771         Device* device = getDevice(deviceId);
772         if (device) {
773             ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
774             return index >= 0;
775         }
776         return false;
777     }
778 
hasLed(int32_t deviceId,int32_t led) const779     virtual bool hasLed(int32_t deviceId, int32_t led) const {
780         Device* device = getDevice(deviceId);
781         return device && device->leds.indexOfKey(led) >= 0;
782     }
783 
setLedState(int32_t deviceId,int32_t led,bool on)784     virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
785         Device* device = getDevice(deviceId);
786         if (device) {
787             ssize_t index = device->leds.indexOfKey(led);
788             if (index >= 0) {
789                 device->leds.replaceValueAt(led, on);
790             } else {
791                 ADD_FAILURE()
792                         << "Attempted to set the state of an LED that the EventHub declared "
793                         "was not present.  led=" << led;
794             }
795         }
796     }
797 
getVirtualKeyDefinitions(int32_t deviceId,std::vector<VirtualKeyDefinition> & outVirtualKeys) const798     virtual void getVirtualKeyDefinitions(int32_t deviceId,
799             std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
800         outVirtualKeys.clear();
801 
802         Device* device = getDevice(deviceId);
803         if (device) {
804             outVirtualKeys = device->virtualKeys;
805         }
806     }
807 
getKeyCharacterMap(int32_t) const808     virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
809         return nullptr;
810     }
811 
setKeyboardLayoutOverlay(int32_t,const sp<KeyCharacterMap> &)812     virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
813         return false;
814     }
815 
vibrate(int32_t,nsecs_t)816     virtual void vibrate(int32_t, nsecs_t) {
817     }
818 
cancelVibrate(int32_t)819     virtual void cancelVibrate(int32_t) {
820     }
821 
isExternal(int32_t) const822     virtual bool isExternal(int32_t) const {
823         return false;
824     }
825 
dump(std::string &)826     virtual void dump(std::string&) {
827     }
828 
monitor()829     virtual void monitor() {
830     }
831 
requestReopenDevices()832     virtual void requestReopenDevices() {
833     }
834 
wake()835     virtual void wake() {
836     }
837 };
838 
839 
840 // --- FakeInputReaderContext ---
841 
842 class FakeInputReaderContext : public InputReaderContext {
843     std::shared_ptr<EventHubInterface> mEventHub;
844     sp<InputReaderPolicyInterface> mPolicy;
845     sp<InputListenerInterface> mListener;
846     int32_t mGlobalMetaState;
847     bool mUpdateGlobalMetaStateWasCalled;
848     int32_t mGeneration;
849     int32_t mNextId;
850     wp<PointerControllerInterface> mPointerController;
851 
852 public:
FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,const sp<InputReaderPolicyInterface> & policy,const sp<InputListenerInterface> & listener)853     FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
854                            const sp<InputReaderPolicyInterface>& policy,
855                            const sp<InputListenerInterface>& listener)
856           : mEventHub(eventHub),
857             mPolicy(policy),
858             mListener(listener),
859             mGlobalMetaState(0),
860             mNextId(1) {}
861 
~FakeInputReaderContext()862     virtual ~FakeInputReaderContext() { }
863 
assertUpdateGlobalMetaStateWasCalled()864     void assertUpdateGlobalMetaStateWasCalled() {
865         ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
866                 << "Expected updateGlobalMetaState() to have been called.";
867         mUpdateGlobalMetaStateWasCalled = false;
868     }
869 
setGlobalMetaState(int32_t state)870     void setGlobalMetaState(int32_t state) {
871         mGlobalMetaState = state;
872     }
873 
getGeneration()874     uint32_t getGeneration() {
875         return mGeneration;
876     }
877 
updatePointerDisplay()878     void updatePointerDisplay() {
879         sp<PointerControllerInterface> controller = mPointerController.promote();
880         if (controller != nullptr) {
881             InputReaderConfiguration config;
882             mPolicy->getReaderConfiguration(&config);
883             auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
884             if (viewport) {
885                 controller->setDisplayViewport(*viewport);
886             }
887         }
888     }
889 
890 private:
updateGlobalMetaState()891     virtual void updateGlobalMetaState() {
892         mUpdateGlobalMetaStateWasCalled = true;
893     }
894 
getGlobalMetaState()895     virtual int32_t getGlobalMetaState() {
896         return mGlobalMetaState;
897     }
898 
getEventHub()899     virtual EventHubInterface* getEventHub() {
900         return mEventHub.get();
901     }
902 
getPolicy()903     virtual InputReaderPolicyInterface* getPolicy() {
904         return mPolicy.get();
905     }
906 
getListener()907     virtual InputListenerInterface* getListener() {
908         return mListener.get();
909     }
910 
disableVirtualKeysUntil(nsecs_t)911     virtual void disableVirtualKeysUntil(nsecs_t) {
912     }
913 
shouldDropVirtualKey(nsecs_t,int32_t,int32_t)914     virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
915 
getPointerController(int32_t deviceId)916     virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) {
917         sp<PointerControllerInterface> controller = mPointerController.promote();
918         if (controller == nullptr) {
919             controller = mPolicy->obtainPointerController(deviceId);
920             mPointerController = controller;
921             updatePointerDisplay();
922         }
923         return controller;
924     }
925 
fadePointer()926     virtual void fadePointer() {
927     }
928 
requestTimeoutAtTime(nsecs_t)929     virtual void requestTimeoutAtTime(nsecs_t) {
930     }
931 
bumpGeneration()932     virtual int32_t bumpGeneration() {
933         return ++mGeneration;
934     }
935 
getExternalStylusDevices(std::vector<InputDeviceInfo> & outDevices)936     virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) {
937 
938     }
939 
dispatchExternalStylusState(const StylusState &)940     virtual void dispatchExternalStylusState(const StylusState&) {
941 
942     }
943 
getNextId()944     virtual int32_t getNextId() { return mNextId++; }
945 };
946 
947 
948 // --- FakeInputMapper ---
949 
950 class FakeInputMapper : public InputMapper {
951     uint32_t mSources;
952     int32_t mKeyboardType;
953     int32_t mMetaState;
954     KeyedVector<int32_t, int32_t> mKeyCodeStates;
955     KeyedVector<int32_t, int32_t> mScanCodeStates;
956     KeyedVector<int32_t, int32_t> mSwitchStates;
957     std::vector<int32_t> mSupportedKeyCodes;
958 
959     std::mutex mLock;
960     std::condition_variable mStateChangedCondition;
961     bool mConfigureWasCalled GUARDED_BY(mLock);
962     bool mResetWasCalled GUARDED_BY(mLock);
963     bool mProcessWasCalled GUARDED_BY(mLock);
964     RawEvent mLastEvent GUARDED_BY(mLock);
965 
966     std::optional<DisplayViewport> mViewport;
967 public:
FakeInputMapper(InputDeviceContext & deviceContext,uint32_t sources)968     FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
969           : InputMapper(deviceContext),
970             mSources(sources),
971             mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
972             mMetaState(0),
973             mConfigureWasCalled(false),
974             mResetWasCalled(false),
975             mProcessWasCalled(false) {}
976 
~FakeInputMapper()977     virtual ~FakeInputMapper() { }
978 
setKeyboardType(int32_t keyboardType)979     void setKeyboardType(int32_t keyboardType) {
980         mKeyboardType = keyboardType;
981     }
982 
setMetaState(int32_t metaState)983     void setMetaState(int32_t metaState) {
984         mMetaState = metaState;
985     }
986 
assertConfigureWasCalled()987     void assertConfigureWasCalled() {
988         std::unique_lock<std::mutex> lock(mLock);
989         base::ScopedLockAssertion assumeLocked(mLock);
990         const bool configureCalled =
991                 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
992                     return mConfigureWasCalled;
993                 });
994         if (!configureCalled) {
995             FAIL() << "Expected configure() to have been called.";
996         }
997         mConfigureWasCalled = false;
998     }
999 
assertResetWasCalled()1000     void assertResetWasCalled() {
1001         std::unique_lock<std::mutex> lock(mLock);
1002         base::ScopedLockAssertion assumeLocked(mLock);
1003         const bool resetCalled =
1004                 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1005                     return mResetWasCalled;
1006                 });
1007         if (!resetCalled) {
1008             FAIL() << "Expected reset() to have been called.";
1009         }
1010         mResetWasCalled = false;
1011     }
1012 
assertProcessWasCalled(RawEvent * outLastEvent=nullptr)1013     void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
1014         std::unique_lock<std::mutex> lock(mLock);
1015         base::ScopedLockAssertion assumeLocked(mLock);
1016         const bool processCalled =
1017                 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1018                     return mProcessWasCalled;
1019                 });
1020         if (!processCalled) {
1021             FAIL() << "Expected process() to have been called.";
1022         }
1023         if (outLastEvent) {
1024             *outLastEvent = mLastEvent;
1025         }
1026         mProcessWasCalled = false;
1027     }
1028 
setKeyCodeState(int32_t keyCode,int32_t state)1029     void setKeyCodeState(int32_t keyCode, int32_t state) {
1030         mKeyCodeStates.replaceValueFor(keyCode, state);
1031     }
1032 
setScanCodeState(int32_t scanCode,int32_t state)1033     void setScanCodeState(int32_t scanCode, int32_t state) {
1034         mScanCodeStates.replaceValueFor(scanCode, state);
1035     }
1036 
setSwitchState(int32_t switchCode,int32_t state)1037     void setSwitchState(int32_t switchCode, int32_t state) {
1038         mSwitchStates.replaceValueFor(switchCode, state);
1039     }
1040 
addSupportedKeyCode(int32_t keyCode)1041     void addSupportedKeyCode(int32_t keyCode) {
1042         mSupportedKeyCodes.push_back(keyCode);
1043     }
1044 
1045 private:
getSources()1046     virtual uint32_t getSources() {
1047         return mSources;
1048     }
1049 
populateDeviceInfo(InputDeviceInfo * deviceInfo)1050     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
1051         InputMapper::populateDeviceInfo(deviceInfo);
1052 
1053         if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1054             deviceInfo->setKeyboardType(mKeyboardType);
1055         }
1056     }
1057 
configure(nsecs_t,const InputReaderConfiguration * config,uint32_t changes)1058     virtual void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) {
1059         std::scoped_lock<std::mutex> lock(mLock);
1060         mConfigureWasCalled = true;
1061 
1062         // Find the associated viewport if exist.
1063         const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
1064         if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1065             mViewport = config->getDisplayViewportByPort(*displayPort);
1066         }
1067 
1068         mStateChangedCondition.notify_all();
1069     }
1070 
reset(nsecs_t)1071     virtual void reset(nsecs_t) {
1072         std::scoped_lock<std::mutex> lock(mLock);
1073         mResetWasCalled = true;
1074         mStateChangedCondition.notify_all();
1075     }
1076 
process(const RawEvent * rawEvent)1077     virtual void process(const RawEvent* rawEvent) {
1078         std::scoped_lock<std::mutex> lock(mLock);
1079         mLastEvent = *rawEvent;
1080         mProcessWasCalled = true;
1081         mStateChangedCondition.notify_all();
1082     }
1083 
getKeyCodeState(uint32_t,int32_t keyCode)1084     virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
1085         ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1086         return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1087     }
1088 
getScanCodeState(uint32_t,int32_t scanCode)1089     virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
1090         ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1091         return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1092     }
1093 
getSwitchState(uint32_t,int32_t switchCode)1094     virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
1095         ssize_t index = mSwitchStates.indexOfKey(switchCode);
1096         return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1097     }
1098 
markSupportedKeyCodes(uint32_t,size_t numCodes,const int32_t * keyCodes,uint8_t * outFlags)1099     virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
1100             const int32_t* keyCodes, uint8_t* outFlags) {
1101         bool result = false;
1102         for (size_t i = 0; i < numCodes; i++) {
1103             for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1104                 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1105                     outFlags[i] = 1;
1106                     result = true;
1107                 }
1108             }
1109         }
1110         return result;
1111     }
1112 
getMetaState()1113     virtual int32_t getMetaState() {
1114         return mMetaState;
1115     }
1116 
fadePointer()1117     virtual void fadePointer() {
1118     }
1119 
getAssociatedDisplay()1120     virtual std::optional<int32_t> getAssociatedDisplay() {
1121         if (mViewport) {
1122             return std::make_optional(mViewport->displayId);
1123         }
1124         return std::nullopt;
1125     }
1126 };
1127 
1128 
1129 // --- InstrumentedInputReader ---
1130 
1131 class InstrumentedInputReader : public InputReader {
1132     std::shared_ptr<InputDevice> mNextDevice;
1133 
1134 public:
InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,const sp<InputReaderPolicyInterface> & policy,const sp<InputListenerInterface> & listener)1135     InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1136                             const sp<InputReaderPolicyInterface>& policy,
1137                             const sp<InputListenerInterface>& listener)
1138           : InputReader(eventHub, policy, listener), mNextDevice(nullptr) {}
1139 
~InstrumentedInputReader()1140     virtual ~InstrumentedInputReader() {}
1141 
setNextDevice(std::shared_ptr<InputDevice> device)1142     void setNextDevice(std::shared_ptr<InputDevice> device) { mNextDevice = device; }
1143 
newDevice(int32_t deviceId,const std::string & name,const std::string & location="")1144     std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
1145                                            const std::string& location = "") {
1146         InputDeviceIdentifier identifier;
1147         identifier.name = name;
1148         identifier.location = location;
1149         int32_t generation = deviceId + 1;
1150         return std::make_shared<InputDevice>(&mContext, deviceId, generation, identifier);
1151     }
1152 
1153     // Make the protected loopOnce method accessible to tests.
1154     using InputReader::loopOnce;
1155 
1156 protected:
createDeviceLocked(int32_t eventHubId,const InputDeviceIdentifier & identifier)1157     virtual std::shared_ptr<InputDevice> createDeviceLocked(
1158             int32_t eventHubId, const InputDeviceIdentifier& identifier) {
1159         if (mNextDevice) {
1160             std::shared_ptr<InputDevice> device(mNextDevice);
1161             mNextDevice = nullptr;
1162             return device;
1163         }
1164         return InputReader::createDeviceLocked(eventHubId, identifier);
1165     }
1166 
1167     friend class InputReaderTest;
1168 };
1169 
1170 // --- InputReaderPolicyTest ---
1171 class InputReaderPolicyTest : public testing::Test {
1172 protected:
1173     sp<FakeInputReaderPolicy> mFakePolicy;
1174 
SetUp()1175     virtual void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
TearDown()1176     virtual void TearDown() override { mFakePolicy.clear(); }
1177 };
1178 
1179 /**
1180  * Check that empty set of viewports is an acceptable configuration.
1181  * Also try to get internal viewport two different ways - by type and by uniqueId.
1182  *
1183  * There will be confusion if two viewports with empty uniqueId and identical type are present.
1184  * Such configuration is not currently allowed.
1185  */
TEST_F(InputReaderPolicyTest,Viewports_GetCleared)1186 TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
1187     static const std::string uniqueId = "local:0";
1188 
1189     // We didn't add any viewports yet, so there shouldn't be any.
1190     std::optional<DisplayViewport> internalViewport =
1191             mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
1192     ASSERT_FALSE(internalViewport);
1193 
1194     // Add an internal viewport, then clear it
1195     mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1196             DISPLAY_ORIENTATION_0, uniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1197 
1198     // Check matching by uniqueId
1199     internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
1200     ASSERT_TRUE(internalViewport);
1201     ASSERT_EQ(ViewportType::VIEWPORT_INTERNAL, internalViewport->type);
1202 
1203     // Check matching by viewport type
1204     internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
1205     ASSERT_TRUE(internalViewport);
1206     ASSERT_EQ(uniqueId, internalViewport->uniqueId);
1207 
1208     mFakePolicy->clearViewports();
1209     // Make sure nothing is found after clear
1210     internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
1211     ASSERT_FALSE(internalViewport);
1212     internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
1213     ASSERT_FALSE(internalViewport);
1214 }
1215 
TEST_F(InputReaderPolicyTest,Viewports_GetByType)1216 TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1217     const std::string internalUniqueId = "local:0";
1218     const std::string externalUniqueId = "local:1";
1219     const std::string virtualUniqueId1 = "virtual:2";
1220     const std::string virtualUniqueId2 = "virtual:3";
1221     constexpr int32_t virtualDisplayId1 = 2;
1222     constexpr int32_t virtualDisplayId2 = 3;
1223 
1224     // Add an internal viewport
1225     mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1226             DISPLAY_ORIENTATION_0, internalUniqueId, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1227     // Add an external viewport
1228     mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1229             DISPLAY_ORIENTATION_0, externalUniqueId, NO_PORT, ViewportType::VIEWPORT_EXTERNAL);
1230     // Add an virtual viewport
1231     mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1232             DISPLAY_ORIENTATION_0, virtualUniqueId1, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
1233     // Add another virtual viewport
1234     mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1235             DISPLAY_ORIENTATION_0, virtualUniqueId2, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
1236 
1237     // Check matching by type for internal
1238     std::optional<DisplayViewport> internalViewport =
1239             mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
1240     ASSERT_TRUE(internalViewport);
1241     ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1242 
1243     // Check matching by type for external
1244     std::optional<DisplayViewport> externalViewport =
1245             mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_EXTERNAL);
1246     ASSERT_TRUE(externalViewport);
1247     ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1248 
1249     // Check matching by uniqueId for virtual viewport #1
1250     std::optional<DisplayViewport> virtualViewport1 =
1251             mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
1252     ASSERT_TRUE(virtualViewport1);
1253     ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport1->type);
1254     ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1255     ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1256 
1257     // Check matching by uniqueId for virtual viewport #2
1258     std::optional<DisplayViewport> virtualViewport2 =
1259             mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
1260     ASSERT_TRUE(virtualViewport2);
1261     ASSERT_EQ(ViewportType::VIEWPORT_VIRTUAL, virtualViewport2->type);
1262     ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1263     ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1264 }
1265 
1266 
1267 /**
1268  * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1269  * that lookup works by checking display id.
1270  * Check that 2 viewports of each kind is possible, for all existing viewport types.
1271  */
TEST_F(InputReaderPolicyTest,Viewports_TwoOfSameType)1272 TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1273     const std::string uniqueId1 = "uniqueId1";
1274     const std::string uniqueId2 = "uniqueId2";
1275     constexpr int32_t displayId1 = 2;
1276     constexpr int32_t displayId2 = 3;
1277 
1278     std::vector<ViewportType> types = {ViewportType::VIEWPORT_INTERNAL,
1279             ViewportType::VIEWPORT_EXTERNAL, ViewportType::VIEWPORT_VIRTUAL};
1280     for (const ViewportType& type : types) {
1281         mFakePolicy->clearViewports();
1282         // Add a viewport
1283         mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1284             DISPLAY_ORIENTATION_0, uniqueId1, NO_PORT, type);
1285         // Add another viewport
1286         mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1287             DISPLAY_ORIENTATION_0, uniqueId2, NO_PORT, type);
1288 
1289         // Check that correct display viewport was returned by comparing the display IDs.
1290         std::optional<DisplayViewport> viewport1 =
1291                 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
1292         ASSERT_TRUE(viewport1);
1293         ASSERT_EQ(displayId1, viewport1->displayId);
1294         ASSERT_EQ(type, viewport1->type);
1295 
1296         std::optional<DisplayViewport> viewport2 =
1297                 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1298         ASSERT_TRUE(viewport2);
1299         ASSERT_EQ(displayId2, viewport2->displayId);
1300         ASSERT_EQ(type, viewport2->type);
1301 
1302         // When there are multiple viewports of the same kind, and uniqueId is not specified
1303         // in the call to getDisplayViewport, then that situation is not supported.
1304         // The viewports can be stored in any order, so we cannot rely on the order, since that
1305         // is just implementation detail.
1306         // However, we can check that it still returns *a* viewport, we just cannot assert
1307         // which one specifically is returned.
1308         std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
1309         ASSERT_TRUE(someViewport);
1310     }
1311 }
1312 
1313 /**
1314  * Check getDisplayViewportByPort
1315  */
TEST_F(InputReaderPolicyTest,Viewports_GetByPort)1316 TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
1317     constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
1318     const std::string uniqueId1 = "uniqueId1";
1319     const std::string uniqueId2 = "uniqueId2";
1320     constexpr int32_t displayId1 = 1;
1321     constexpr int32_t displayId2 = 2;
1322     const uint8_t hdmi1 = 0;
1323     const uint8_t hdmi2 = 1;
1324     const uint8_t hdmi3 = 2;
1325 
1326     mFakePolicy->clearViewports();
1327     // Add a viewport that's associated with some display port that's not of interest.
1328     mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1329             DISPLAY_ORIENTATION_0, uniqueId1, hdmi3, type);
1330     // Add another viewport, connected to HDMI1 port
1331     mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1332             DISPLAY_ORIENTATION_0, uniqueId2, hdmi1, type);
1333 
1334     // Check that correct display viewport was returned by comparing the display ports.
1335     std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1336     ASSERT_TRUE(hdmi1Viewport);
1337     ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1338     ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1339 
1340     // Check that we can still get the same viewport using the uniqueId
1341     hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1342     ASSERT_TRUE(hdmi1Viewport);
1343     ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1344     ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1345     ASSERT_EQ(type, hdmi1Viewport->type);
1346 
1347     // Check that we cannot find a port with "HDMI2", because we never added one
1348     std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1349     ASSERT_FALSE(hdmi2Viewport);
1350 }
1351 
1352 // --- InputReaderTest ---
1353 
1354 class InputReaderTest : public testing::Test {
1355 protected:
1356     sp<TestInputListener> mFakeListener;
1357     sp<FakeInputReaderPolicy> mFakePolicy;
1358     std::shared_ptr<FakeEventHub> mFakeEventHub;
1359     std::unique_ptr<InstrumentedInputReader> mReader;
1360 
SetUp()1361     virtual void SetUp() override {
1362         mFakeEventHub = std::make_unique<FakeEventHub>();
1363         mFakePolicy = new FakeInputReaderPolicy();
1364         mFakeListener = new TestInputListener();
1365 
1366         mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1367                                                             mFakeListener);
1368     }
1369 
TearDown()1370     virtual void TearDown() override {
1371         mFakeListener.clear();
1372         mFakePolicy.clear();
1373     }
1374 
addDevice(int32_t eventHubId,const std::string & name,uint32_t classes,const PropertyMap * configuration)1375     void addDevice(int32_t eventHubId, const std::string& name, uint32_t classes,
1376                    const PropertyMap* configuration) {
1377         mFakeEventHub->addDevice(eventHubId, name, classes);
1378 
1379         if (configuration) {
1380             mFakeEventHub->addConfigurationMap(eventHubId, configuration);
1381         }
1382         mFakeEventHub->finishDeviceScan();
1383         mReader->loopOnce();
1384         mReader->loopOnce();
1385         ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1386         ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1387     }
1388 
disableDevice(int32_t deviceId)1389     void disableDevice(int32_t deviceId) {
1390         mFakePolicy->addDisabledDevice(deviceId);
1391         mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
1392     }
1393 
enableDevice(int32_t deviceId)1394     void enableDevice(int32_t deviceId) {
1395         mFakePolicy->removeDisabledDevice(deviceId);
1396         mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
1397     }
1398 
addDeviceWithFakeInputMapper(int32_t deviceId,int32_t eventHubId,const std::string & name,uint32_t classes,uint32_t sources,const PropertyMap * configuration)1399     FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
1400                                                   const std::string& name, uint32_t classes,
1401                                                   uint32_t sources,
1402                                                   const PropertyMap* configuration) {
1403         std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1404         FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
1405         mReader->setNextDevice(device);
1406         addDevice(eventHubId, name, classes, configuration);
1407         return mapper;
1408     }
1409 };
1410 
TEST_F(InputReaderTest,GetInputDevices)1411 TEST_F(InputReaderTest, GetInputDevices) {
1412     ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard",
1413             INPUT_DEVICE_CLASS_KEYBOARD, nullptr));
1414     ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored",
1415             0, nullptr)); // no classes so device will be ignored
1416 
1417     std::vector<InputDeviceInfo> inputDevices;
1418     mReader->getInputDevices(inputDevices);
1419     ASSERT_EQ(1U, inputDevices.size());
1420     ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
1421     ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
1422     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1423     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1424     ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1425 
1426     // Should also have received a notification describing the new input devices.
1427     inputDevices = mFakePolicy->getInputDevices();
1428     ASSERT_EQ(1U, inputDevices.size());
1429     ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
1430     ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
1431     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1432     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1433     ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1434 }
1435 
TEST_F(InputReaderTest,WhenEnabledChanges_SendsDeviceResetNotification)1436 TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1437     constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1438     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1439     constexpr int32_t eventHubId = 1;
1440     std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1441     // Must add at least one mapper or the device will be ignored!
1442     device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1443     mReader->setNextDevice(device);
1444     ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1445 
1446     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1447 
1448     NotifyDeviceResetArgs resetArgs;
1449     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1450     ASSERT_EQ(deviceId, resetArgs.deviceId);
1451 
1452     ASSERT_EQ(device->isEnabled(), true);
1453     disableDevice(deviceId);
1454     mReader->loopOnce();
1455 
1456     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1457     ASSERT_EQ(deviceId, resetArgs.deviceId);
1458     ASSERT_EQ(device->isEnabled(), false);
1459 
1460     disableDevice(deviceId);
1461     mReader->loopOnce();
1462     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1463     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
1464     ASSERT_EQ(device->isEnabled(), false);
1465 
1466     enableDevice(deviceId);
1467     mReader->loopOnce();
1468     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1469     ASSERT_EQ(deviceId, resetArgs.deviceId);
1470     ASSERT_EQ(device->isEnabled(), true);
1471 }
1472 
TEST_F(InputReaderTest,GetKeyCodeState_ForwardsRequestsToMappers)1473 TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
1474     constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1475     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1476     constexpr int32_t eventHubId = 1;
1477     FakeInputMapper& mapper =
1478             addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
1479                                          AINPUT_SOURCE_KEYBOARD, nullptr);
1480     mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1481 
1482     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1483             AINPUT_SOURCE_ANY, AKEYCODE_A))
1484             << "Should return unknown when the device id is >= 0 but unknown.";
1485 
1486     ASSERT_EQ(AKEY_STATE_UNKNOWN,
1487               mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1488             << "Should return unknown when the device id is valid but the sources are not "
1489                "supported by the device.";
1490 
1491     ASSERT_EQ(AKEY_STATE_DOWN,
1492               mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1493                                        AKEYCODE_A))
1494             << "Should return value provided by mapper when device id is valid and the device "
1495                "supports some of the sources.";
1496 
1497     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1498             AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1499             << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1500 
1501     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1502             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1503             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1504 }
1505 
TEST_F(InputReaderTest,GetScanCodeState_ForwardsRequestsToMappers)1506 TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
1507     constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1508     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1509     constexpr int32_t eventHubId = 1;
1510     FakeInputMapper& mapper =
1511             addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
1512                                          AINPUT_SOURCE_KEYBOARD, nullptr);
1513     mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1514 
1515     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1516             AINPUT_SOURCE_ANY, KEY_A))
1517             << "Should return unknown when the device id is >= 0 but unknown.";
1518 
1519     ASSERT_EQ(AKEY_STATE_UNKNOWN,
1520               mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1521             << "Should return unknown when the device id is valid but the sources are not "
1522                "supported by the device.";
1523 
1524     ASSERT_EQ(AKEY_STATE_DOWN,
1525               mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1526                                         KEY_A))
1527             << "Should return value provided by mapper when device id is valid and the device "
1528                "supports some of the sources.";
1529 
1530     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1531             AINPUT_SOURCE_TRACKBALL, KEY_A))
1532             << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1533 
1534     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1535             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1536             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1537 }
1538 
TEST_F(InputReaderTest,GetSwitchState_ForwardsRequestsToMappers)1539 TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
1540     constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1541     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1542     constexpr int32_t eventHubId = 1;
1543     FakeInputMapper& mapper =
1544             addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
1545                                          AINPUT_SOURCE_KEYBOARD, nullptr);
1546     mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
1547 
1548     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1549             AINPUT_SOURCE_ANY, SW_LID))
1550             << "Should return unknown when the device id is >= 0 but unknown.";
1551 
1552     ASSERT_EQ(AKEY_STATE_UNKNOWN,
1553               mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1554             << "Should return unknown when the device id is valid but the sources are not "
1555                "supported by the device.";
1556 
1557     ASSERT_EQ(AKEY_STATE_DOWN,
1558               mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1559                                       SW_LID))
1560             << "Should return value provided by mapper when device id is valid and the device "
1561                "supports some of the sources.";
1562 
1563     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1564             AINPUT_SOURCE_TRACKBALL, SW_LID))
1565             << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1566 
1567     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1568             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1569             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1570 }
1571 
TEST_F(InputReaderTest,MarkSupportedKeyCodes_ForwardsRequestsToMappers)1572 TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
1573     constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1574     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1575     constexpr int32_t eventHubId = 1;
1576     FakeInputMapper& mapper =
1577             addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
1578                                          AINPUT_SOURCE_KEYBOARD, nullptr);
1579 
1580     mapper.addSupportedKeyCode(AKEYCODE_A);
1581     mapper.addSupportedKeyCode(AKEYCODE_B);
1582 
1583     const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1584     uint8_t flags[4] = { 0, 0, 0, 1 };
1585 
1586     ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1587             << "Should return false when device id is >= 0 but unknown.";
1588     ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1589 
1590     flags[3] = 1;
1591     ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1592             << "Should return false when device id is valid but the sources are not supported by "
1593                "the device.";
1594     ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1595 
1596     flags[3] = 1;
1597     ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1598                                  keyCodes, flags))
1599             << "Should return value provided by mapper when device id is valid and the device "
1600                "supports some of the sources.";
1601     ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1602 
1603     flags[3] = 1;
1604     ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1605             << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1606     ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1607 
1608     flags[3] = 1;
1609     ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1610             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1611     ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1612 }
1613 
TEST_F(InputReaderTest,LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged)1614 TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
1615     constexpr int32_t eventHubId = 1;
1616     addDevice(eventHubId, "ignored", INPUT_DEVICE_CLASS_KEYBOARD, nullptr);
1617 
1618     NotifyConfigurationChangedArgs args;
1619 
1620     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1621     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1622 }
1623 
TEST_F(InputReaderTest,LoopOnce_ForwardsRawEventsToMappers)1624 TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
1625     constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1626     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1627     constexpr int32_t eventHubId = 1;
1628     FakeInputMapper& mapper =
1629             addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
1630                                          AINPUT_SOURCE_KEYBOARD, nullptr);
1631 
1632     mFakeEventHub->enqueueEvent(0, eventHubId, EV_KEY, KEY_A, 1);
1633     mReader->loopOnce();
1634     ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1635 
1636     RawEvent event;
1637     ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
1638     ASSERT_EQ(0, event.when);
1639     ASSERT_EQ(eventHubId, event.deviceId);
1640     ASSERT_EQ(EV_KEY, event.type);
1641     ASSERT_EQ(KEY_A, event.code);
1642     ASSERT_EQ(1, event.value);
1643 }
1644 
TEST_F(InputReaderTest,DeviceReset_RandomId)1645 TEST_F(InputReaderTest, DeviceReset_RandomId) {
1646     constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1647     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1648     constexpr int32_t eventHubId = 1;
1649     std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1650     // Must add at least one mapper or the device will be ignored!
1651     device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1652     mReader->setNextDevice(device);
1653     ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1654 
1655     NotifyDeviceResetArgs resetArgs;
1656     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1657     int32_t prevId = resetArgs.id;
1658 
1659     disableDevice(deviceId);
1660     mReader->loopOnce();
1661     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1662     ASSERT_NE(prevId, resetArgs.id);
1663     prevId = resetArgs.id;
1664 
1665     enableDevice(deviceId);
1666     mReader->loopOnce();
1667     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1668     ASSERT_NE(prevId, resetArgs.id);
1669     prevId = resetArgs.id;
1670 
1671     disableDevice(deviceId);
1672     mReader->loopOnce();
1673     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1674     ASSERT_NE(prevId, resetArgs.id);
1675     prevId = resetArgs.id;
1676 }
1677 
TEST_F(InputReaderTest,DeviceReset_GenerateIdWithInputReaderSource)1678 TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1679     constexpr int32_t deviceId = 1;
1680     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1681     constexpr int32_t eventHubId = 1;
1682     std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1683     // Must add at least one mapper or the device will be ignored!
1684     device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1685     mReader->setNextDevice(device);
1686     ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1687 
1688     NotifyDeviceResetArgs resetArgs;
1689     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1690     ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1691 }
1692 
TEST_F(InputReaderTest,Device_CanDispatchToDisplay)1693 TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
1694     constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1695     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1696     constexpr int32_t eventHubId = 1;
1697     const char* DEVICE_LOCATION = "USB1";
1698     std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1699     FakeInputMapper& mapper =
1700             device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
1701     mReader->setNextDevice(device);
1702 
1703     const uint8_t hdmi1 = 1;
1704 
1705     // Associated touch screen with second display.
1706     mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1707 
1708     // Add default and second display.
1709     mFakePolicy->clearViewports();
1710     mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1711             DISPLAY_ORIENTATION_0, "local:0", NO_PORT, ViewportType::VIEWPORT_INTERNAL);
1712     mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1713             DISPLAY_ORIENTATION_0, "local:1", hdmi1, ViewportType::VIEWPORT_EXTERNAL);
1714     mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1715     mReader->loopOnce();
1716 
1717     // Add the device, and make sure all of the callbacks are triggered.
1718     // The device is added after the input port associations are processed since
1719     // we do not yet support dynamic device-to-display associations.
1720     ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1721     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
1722     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
1723     ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1724 
1725     // Device should only dispatch to the specified display.
1726     ASSERT_EQ(deviceId, device->getId());
1727     ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1728     ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
1729 
1730     // Can't dispatch event from a disabled device.
1731     disableDevice(deviceId);
1732     mReader->loopOnce();
1733     ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
1734 }
1735 
1736 // --- InputReaderIntegrationTest ---
1737 
1738 // These tests create and interact with the InputReader only through its interface.
1739 // The InputReader is started during SetUp(), which starts its processing in its own
1740 // thread. The tests use linux uinput to emulate input devices.
1741 // NOTE: Interacting with the physical device while these tests are running may cause
1742 // the tests to fail.
1743 class InputReaderIntegrationTest : public testing::Test {
1744 protected:
1745     sp<TestInputListener> mTestListener;
1746     sp<FakeInputReaderPolicy> mFakePolicy;
1747     sp<InputReaderInterface> mReader;
1748 
SetUp()1749     virtual void SetUp() override {
1750         mFakePolicy = new FakeInputReaderPolicy();
1751         mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
1752                                               30ms /*eventDidNotHappenTimeout*/);
1753 
1754         mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
1755         ASSERT_EQ(mReader->start(), OK);
1756 
1757         // Since this test is run on a real device, all the input devices connected
1758         // to the test device will show up in mReader. We wait for those input devices to
1759         // show up before beginning the tests.
1760         ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1761         ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1762     }
1763 
TearDown()1764     virtual void TearDown() override {
1765         ASSERT_EQ(mReader->stop(), OK);
1766         mTestListener.clear();
1767         mFakePolicy.clear();
1768     }
1769 };
1770 
TEST_F(InputReaderIntegrationTest,TestInvalidDevice)1771 TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1772     // An invalid input device that is only used for this test.
1773     class InvalidUinputDevice : public UinputDevice {
1774     public:
1775         InvalidUinputDevice() : UinputDevice("Invalid Device") {}
1776 
1777     private:
1778         void configureDevice(int fd, uinput_user_dev* device) override {}
1779     };
1780 
1781     const size_t numDevices = mFakePolicy->getInputDevices().size();
1782 
1783     // UinputDevice does not set any event or key bits, so InputReader should not
1784     // consider it as a valid device.
1785     std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1786     ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1787     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1788     ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1789 
1790     invalidDevice.reset();
1791     ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1792     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1793     ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1794 }
1795 
TEST_F(InputReaderIntegrationTest,AddNewDevice)1796 TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1797     const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1798 
1799     std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1800     ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1801     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1802     ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1803 
1804     // Find the test device by its name.
1805     std::vector<InputDeviceInfo> inputDevices;
1806     mReader->getInputDevices(inputDevices);
1807     InputDeviceInfo* keyboardInfo = nullptr;
1808     const char* keyboardName = keyboard->getName();
1809     for (unsigned int i = 0; i < initialNumDevices + 1; i++) {
1810         if (!strcmp(inputDevices[i].getIdentifier().name.c_str(), keyboardName)) {
1811             keyboardInfo = &inputDevices[i];
1812             break;
1813         }
1814     }
1815     ASSERT_NE(keyboardInfo, nullptr);
1816     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, keyboardInfo->getKeyboardType());
1817     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyboardInfo->getSources());
1818     ASSERT_EQ(0U, keyboardInfo->getMotionRanges().size());
1819 
1820     keyboard.reset();
1821     ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1822     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1823     ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1824 }
1825 
TEST_F(InputReaderIntegrationTest,SendsEventsToInputListener)1826 TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1827     std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1828     ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1829 
1830     NotifyConfigurationChangedArgs configChangedArgs;
1831     ASSERT_NO_FATAL_FAILURE(
1832             mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
1833     int32_t prevId = configChangedArgs.id;
1834     nsecs_t prevTimestamp = configChangedArgs.eventTime;
1835 
1836     NotifyKeyArgs keyArgs;
1837     keyboard->pressAndReleaseHomeKey();
1838     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1839     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
1840     ASSERT_NE(prevId, keyArgs.id);
1841     prevId = keyArgs.id;
1842     ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1843     prevTimestamp = keyArgs.eventTime;
1844 
1845     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1846     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
1847     ASSERT_NE(prevId, keyArgs.id);
1848     ASSERT_LE(prevTimestamp, keyArgs.eventTime);
1849 }
1850 
1851 /**
1852  * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1853  * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1854  * are passed to the listener.
1855  */
1856 static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
TEST_F(InputReaderIntegrationTest,SendsGearDownAndUpToInputListener)1857 TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1858     std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1859     ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1860     NotifyKeyArgs keyArgs;
1861 
1862     controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1863     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1864     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1865     ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1866 
1867     controller->pressAndReleaseKey(BTN_GEAR_UP);
1868     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1869     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1870     ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1871 }
1872 
1873 // --- TouchProcessTest ---
1874 class TouchIntegrationTest : public InputReaderIntegrationTest {
1875 protected:
1876     static const int32_t FIRST_SLOT = 0;
1877     static const int32_t SECOND_SLOT = 1;
1878     static const int32_t FIRST_TRACKING_ID = 0;
1879     static const int32_t SECOND_TRACKING_ID = 1;
1880     const std::string UNIQUE_ID = "local:0";
1881 
SetUp()1882     virtual void SetUp() override {
1883         InputReaderIntegrationTest::SetUp();
1884         // At least add an internal display.
1885         setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1886                                      DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
1887                                      ViewportType::VIEWPORT_INTERNAL);
1888 
1889         mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1890         ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1891         ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1892     }
1893 
setDisplayInfoAndReconfigure(int32_t displayId,int32_t width,int32_t height,int32_t orientation,const std::string & uniqueId,std::optional<uint8_t> physicalPort,ViewportType viewportType)1894     void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1895                                       int32_t orientation, const std::string& uniqueId,
1896                                       std::optional<uint8_t> physicalPort,
1897                                       ViewportType viewportType) {
1898         mFakePolicy->addDisplayViewport(displayId, width, height, orientation, uniqueId,
1899                                         physicalPort, viewportType);
1900         mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1901     }
1902 
1903     std::unique_ptr<UinputTouchScreen> mDevice;
1904 };
1905 
TEST_F(TouchIntegrationTest,InputEvent_ProcessSingleTouch)1906 TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1907     NotifyMotionArgs args;
1908     const Point centerPoint = mDevice->getCenterPoint();
1909 
1910     // ACTION_DOWN
1911     mDevice->sendDown(centerPoint);
1912     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1913     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1914 
1915     // ACTION_MOVE
1916     mDevice->sendMove(centerPoint + Point(1, 1));
1917     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1918     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1919 
1920     // ACTION_UP
1921     mDevice->sendUp();
1922     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1923     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1924 }
1925 
TEST_F(TouchIntegrationTest,InputEvent_ProcessMultiTouch)1926 TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1927     NotifyMotionArgs args;
1928     const Point centerPoint = mDevice->getCenterPoint();
1929 
1930     // ACTION_DOWN
1931     mDevice->sendDown(centerPoint);
1932     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1933     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1934 
1935     // ACTION_POINTER_DOWN (Second slot)
1936     const Point secondPoint = centerPoint + Point(100, 100);
1937     mDevice->sendSlot(SECOND_SLOT);
1938     mDevice->sendTrackingId(SECOND_TRACKING_ID);
1939     mDevice->sendDown(secondPoint + Point(1, 1));
1940     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1941     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1942               args.action);
1943 
1944     // ACTION_MOVE (Second slot)
1945     mDevice->sendMove(secondPoint);
1946     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1947     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1948 
1949     // ACTION_POINTER_UP (Second slot)
1950     mDevice->sendUp();
1951     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1952     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1953               args.action);
1954 
1955     // ACTION_UP
1956     mDevice->sendSlot(FIRST_SLOT);
1957     mDevice->sendUp();
1958     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1959     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1960 }
1961 
TEST_F(TouchIntegrationTest,InputEvent_ProcessPalm)1962 TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1963     NotifyMotionArgs args;
1964     const Point centerPoint = mDevice->getCenterPoint();
1965 
1966     // ACTION_DOWN
1967     mDevice->sendDown(centerPoint);
1968     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1969     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1970 
1971     // ACTION_POINTER_DOWN (Second slot)
1972     const Point secondPoint = centerPoint + Point(100, 100);
1973     mDevice->sendSlot(SECOND_SLOT);
1974     mDevice->sendTrackingId(SECOND_TRACKING_ID);
1975     mDevice->sendDown(secondPoint);
1976     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1977     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
1978               args.action);
1979 
1980     // ACTION_MOVE (Second slot)
1981     mDevice->sendMove(secondPoint + Point(1, 1));
1982     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1983     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1984 
1985     // Send MT_TOOL_PALM, which indicates that the touch IC has determined this to be a grip event.
1986     // Expect to receive ACTION_CANCEL, to abort the entire gesture.
1987     mDevice->sendToolType(MT_TOOL_PALM);
1988     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1989     ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, args.action);
1990 
1991     // ACTION_POINTER_UP (Second slot)
1992     mDevice->sendUp();
1993 
1994     // ACTION_UP
1995     mDevice->sendSlot(FIRST_SLOT);
1996     mDevice->sendUp();
1997 
1998     // Expect no event received after abort the entire gesture.
1999     ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2000 }
2001 
2002 // --- InputDeviceTest ---
2003 class InputDeviceTest : public testing::Test {
2004 protected:
2005     static const char* DEVICE_NAME;
2006     static const char* DEVICE_LOCATION;
2007     static const int32_t DEVICE_ID;
2008     static const int32_t DEVICE_GENERATION;
2009     static const int32_t DEVICE_CONTROLLER_NUMBER;
2010     static const uint32_t DEVICE_CLASSES;
2011     static const int32_t EVENTHUB_ID;
2012 
2013     std::shared_ptr<FakeEventHub> mFakeEventHub;
2014     sp<FakeInputReaderPolicy> mFakePolicy;
2015     sp<TestInputListener> mFakeListener;
2016     FakeInputReaderContext* mFakeContext;
2017 
2018     std::shared_ptr<InputDevice> mDevice;
2019 
SetUp()2020     virtual void SetUp() override {
2021         mFakeEventHub = std::make_unique<FakeEventHub>();
2022         mFakePolicy = new FakeInputReaderPolicy();
2023         mFakeListener = new TestInputListener();
2024         mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2025 
2026         mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, 0);
2027         InputDeviceIdentifier identifier;
2028         identifier.name = DEVICE_NAME;
2029         identifier.location = DEVICE_LOCATION;
2030         mDevice = std::make_shared<InputDevice>(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
2031                                                 identifier);
2032     }
2033 
TearDown()2034     virtual void TearDown() override {
2035         mDevice = nullptr;
2036         delete mFakeContext;
2037         mFakeListener.clear();
2038         mFakePolicy.clear();
2039     }
2040 };
2041 
2042 const char* InputDeviceTest::DEVICE_NAME = "device";
2043 const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2044 const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2045 const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2046 const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
2047 const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
2048         | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
2049 const int32_t InputDeviceTest::EVENTHUB_ID = 1;
2050 
TEST_F(InputDeviceTest,ImmutableProperties)2051 TEST_F(InputDeviceTest, ImmutableProperties) {
2052     ASSERT_EQ(DEVICE_ID, mDevice->getId());
2053     ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2054     ASSERT_EQ(0U, mDevice->getClasses());
2055 }
2056 
TEST_F(InputDeviceTest,WhenDeviceCreated_EnabledIsFalse)2057 TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2058     ASSERT_EQ(mDevice->isEnabled(), false);
2059 }
2060 
TEST_F(InputDeviceTest,WhenNoMappersAreRegistered_DeviceIsIgnored)2061 TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2062     // Configuration.
2063     InputReaderConfiguration config;
2064     mDevice->configure(ARBITRARY_TIME, &config, 0);
2065 
2066     // Reset.
2067     mDevice->reset(ARBITRARY_TIME);
2068 
2069     NotifyDeviceResetArgs resetArgs;
2070     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2071     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2072     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2073 
2074     // Metadata.
2075     ASSERT_TRUE(mDevice->isIgnored());
2076     ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2077 
2078     InputDeviceInfo info;
2079     mDevice->getDeviceInfo(&info);
2080     ASSERT_EQ(DEVICE_ID, info.getId());
2081     ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2082     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2083     ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2084 
2085     // State queries.
2086     ASSERT_EQ(0, mDevice->getMetaState());
2087 
2088     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2089             << "Ignored device should return unknown key code state.";
2090     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2091             << "Ignored device should return unknown scan code state.";
2092     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2093             << "Ignored device should return unknown switch state.";
2094 
2095     const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2096     uint8_t flags[2] = { 0, 1 };
2097     ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2098             << "Ignored device should never mark any key codes.";
2099     ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2100     ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2101 }
2102 
TEST_F(InputDeviceTest,WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers)2103 TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2104     // Configuration.
2105     mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
2106 
2107     FakeInputMapper& mapper1 =
2108             mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2109     mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2110     mapper1.setMetaState(AMETA_ALT_ON);
2111     mapper1.addSupportedKeyCode(AKEYCODE_A);
2112     mapper1.addSupportedKeyCode(AKEYCODE_B);
2113     mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2114     mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2115     mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2116     mapper1.setScanCodeState(3, AKEY_STATE_UP);
2117     mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2118 
2119     FakeInputMapper& mapper2 =
2120             mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2121     mapper2.setMetaState(AMETA_SHIFT_ON);
2122 
2123     InputReaderConfiguration config;
2124     mDevice->configure(ARBITRARY_TIME, &config, 0);
2125 
2126     String8 propertyValue;
2127     ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2128             << "Device should have read configuration during configuration phase.";
2129     ASSERT_STREQ("value", propertyValue.string());
2130 
2131     ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2132     ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
2133 
2134     // Reset
2135     mDevice->reset(ARBITRARY_TIME);
2136     ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2137     ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
2138 
2139     NotifyDeviceResetArgs resetArgs;
2140     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2141     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2142     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2143 
2144     // Metadata.
2145     ASSERT_FALSE(mDevice->isIgnored());
2146     ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2147 
2148     InputDeviceInfo info;
2149     mDevice->getDeviceInfo(&info);
2150     ASSERT_EQ(DEVICE_ID, info.getId());
2151     ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2152     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2153     ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2154 
2155     // State queries.
2156     ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2157             << "Should query mappers and combine meta states.";
2158 
2159     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2160             << "Should return unknown key code state when source not supported.";
2161     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2162             << "Should return unknown scan code state when source not supported.";
2163     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2164             << "Should return unknown switch state when source not supported.";
2165 
2166     ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2167             << "Should query mapper when source is supported.";
2168     ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2169             << "Should query mapper when source is supported.";
2170     ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2171             << "Should query mapper when source is supported.";
2172 
2173     const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2174     uint8_t flags[4] = { 0, 0, 0, 1 };
2175     ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2176             << "Should do nothing when source is unsupported.";
2177     ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2178     ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2179     ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2180     ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2181 
2182     ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2183             << "Should query mapper when source is supported.";
2184     ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2185     ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2186     ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2187     ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2188 
2189     // Event handling.
2190     RawEvent event;
2191     event.deviceId = EVENTHUB_ID;
2192     mDevice->process(&event, 1);
2193 
2194     ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2195     ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
2196 }
2197 
2198 // A single input device is associated with a specific display. Check that:
2199 // 1. Device is disabled if the viewport corresponding to the associated display is not found
2200 // 2. Device is disabled when setEnabled API is called
TEST_F(InputDeviceTest,Configure_AssignsDisplayPort)2201 TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
2202     mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2203 
2204     // First Configuration.
2205     mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2206 
2207     // Device should be enabled by default.
2208     ASSERT_TRUE(mDevice->isEnabled());
2209 
2210     // Prepare associated info.
2211     constexpr uint8_t hdmi = 1;
2212     const std::string UNIQUE_ID = "local:1";
2213 
2214     mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2215     mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2216                        InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2217     // Device should be disabled because it is associated with a specific display via
2218     // input port <-> display port association, but the corresponding display is not found
2219     ASSERT_FALSE(mDevice->isEnabled());
2220 
2221     // Prepare displays.
2222     mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2223                                     DISPLAY_ORIENTATION_0, UNIQUE_ID, hdmi,
2224                                     ViewportType::VIEWPORT_INTERNAL);
2225     mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2226                        InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2227     ASSERT_TRUE(mDevice->isEnabled());
2228 
2229     // Device should be disabled after set disable.
2230     mFakePolicy->addDisabledDevice(mDevice->getId());
2231     mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2232                        InputReaderConfiguration::CHANGE_ENABLED_STATE);
2233     ASSERT_FALSE(mDevice->isEnabled());
2234 
2235     // Device should still be disabled even found the associated display.
2236     mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2237                        InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2238     ASSERT_FALSE(mDevice->isEnabled());
2239 }
2240 
2241 // --- InputMapperTest ---
2242 
2243 class InputMapperTest : public testing::Test {
2244 protected:
2245     static const char* DEVICE_NAME;
2246     static const char* DEVICE_LOCATION;
2247     static const int32_t DEVICE_ID;
2248     static const int32_t DEVICE_GENERATION;
2249     static const int32_t DEVICE_CONTROLLER_NUMBER;
2250     static const uint32_t DEVICE_CLASSES;
2251     static const int32_t EVENTHUB_ID;
2252 
2253     std::shared_ptr<FakeEventHub> mFakeEventHub;
2254     sp<FakeInputReaderPolicy> mFakePolicy;
2255     sp<TestInputListener> mFakeListener;
2256     FakeInputReaderContext* mFakeContext;
2257     InputDevice* mDevice;
2258 
SetUp(uint32_t classes)2259     virtual void SetUp(uint32_t classes) {
2260         mFakeEventHub = std::make_unique<FakeEventHub>();
2261         mFakePolicy = new FakeInputReaderPolicy();
2262         mFakeListener = new TestInputListener();
2263         mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
2264         InputDeviceIdentifier identifier;
2265         identifier.name = DEVICE_NAME;
2266         identifier.location = DEVICE_LOCATION;
2267         mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, identifier);
2268 
2269         mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, classes);
2270     }
2271 
SetUp()2272     virtual void SetUp() override { SetUp(DEVICE_CLASSES); }
2273 
TearDown()2274     virtual void TearDown() override {
2275         delete mDevice;
2276         delete mFakeContext;
2277         mFakeListener.clear();
2278         mFakePolicy.clear();
2279     }
2280 
addConfigurationProperty(const char * key,const char * value)2281     void addConfigurationProperty(const char* key, const char* value) {
2282         mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
2283     }
2284 
configureDevice(uint32_t changes)2285     void configureDevice(uint32_t changes) {
2286         if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2287             mFakeContext->updatePointerDisplay();
2288         }
2289         mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2290     }
2291 
2292     template <class T, typename... Args>
addMapperAndConfigure(Args...args)2293     T& addMapperAndConfigure(Args... args) {
2294         T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
2295         configureDevice(0);
2296         mDevice->reset(ARBITRARY_TIME);
2297         return mapper;
2298     }
2299 
setDisplayInfoAndReconfigure(int32_t displayId,int32_t width,int32_t height,int32_t orientation,const std::string & uniqueId,std::optional<uint8_t> physicalPort,ViewportType viewportType)2300     void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2301             int32_t orientation, const std::string& uniqueId,
2302             std::optional<uint8_t> physicalPort, ViewportType viewportType) {
2303         mFakePolicy->addDisplayViewport(
2304                 displayId, width, height, orientation, uniqueId, physicalPort, viewportType);
2305         configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2306     }
2307 
clearViewports()2308     void clearViewports() {
2309         mFakePolicy->clearViewports();
2310     }
2311 
process(InputMapper & mapper,nsecs_t when,int32_t type,int32_t code,int32_t value)2312     static void process(InputMapper& mapper, nsecs_t when, int32_t type, int32_t code,
2313                         int32_t value) {
2314         RawEvent event;
2315         event.when = when;
2316         event.deviceId = mapper.getDeviceContext().getEventHubId();
2317         event.type = type;
2318         event.code = code;
2319         event.value = value;
2320         mapper.process(&event);
2321     }
2322 
assertMotionRange(const InputDeviceInfo & info,int32_t axis,uint32_t source,float min,float max,float flat,float fuzz)2323     static void assertMotionRange(const InputDeviceInfo& info,
2324             int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2325         const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
2326         ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
2327         ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2328         ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2329         ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2330         ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2331         ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2332         ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2333     }
2334 
assertPointerCoords(const PointerCoords & coords,float x,float y,float pressure,float size,float touchMajor,float touchMinor,float toolMajor,float toolMinor,float orientation,float distance)2335     static void assertPointerCoords(const PointerCoords& coords,
2336             float x, float y, float pressure, float size,
2337             float touchMajor, float touchMinor, float toolMajor, float toolMinor,
2338             float orientation, float distance) {
2339         ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2340         ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2341         ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2342         ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
2343         ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
2344         ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
2345         ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
2346         ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
2347         ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2348         ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2349     }
2350 
assertPosition(const sp<FakePointerController> & controller,float x,float y)2351     static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
2352         float actualX, actualY;
2353         controller->getPosition(&actualX, &actualY);
2354         ASSERT_NEAR(x, actualX, 1);
2355         ASSERT_NEAR(y, actualY, 1);
2356     }
2357 };
2358 
2359 const char* InputMapperTest::DEVICE_NAME = "device";
2360 const char* InputMapperTest::DEVICE_LOCATION = "USB1";
2361 const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
2362 const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2363 const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
2364 const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
2365 const int32_t InputMapperTest::EVENTHUB_ID = 1;
2366 
2367 // --- SwitchInputMapperTest ---
2368 
2369 class SwitchInputMapperTest : public InputMapperTest {
2370 protected:
2371 };
2372 
TEST_F(SwitchInputMapperTest,GetSources)2373 TEST_F(SwitchInputMapperTest, GetSources) {
2374     SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
2375 
2376     ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
2377 }
2378 
TEST_F(SwitchInputMapperTest,GetSwitchState)2379 TEST_F(SwitchInputMapperTest, GetSwitchState) {
2380     SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
2381 
2382     mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
2383     ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2384 
2385     mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
2386     ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
2387 }
2388 
TEST_F(SwitchInputMapperTest,Process)2389 TEST_F(SwitchInputMapperTest, Process) {
2390     SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
2391 
2392     process(mapper, ARBITRARY_TIME, EV_SW, SW_LID, 1);
2393     process(mapper, ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2394     process(mapper, ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2395     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
2396 
2397     NotifySwitchArgs args;
2398     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2399     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2400     ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2401     ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
2402             args.switchMask);
2403     ASSERT_EQ(uint32_t(0), args.policyFlags);
2404 }
2405 
2406 
2407 // --- KeyboardInputMapperTest ---
2408 
2409 class KeyboardInputMapperTest : public InputMapperTest {
2410 protected:
2411     const std::string UNIQUE_ID = "local:0";
2412 
2413     void prepareDisplay(int32_t orientation);
2414 
2415     void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
2416                              int32_t originalKeyCode, int32_t rotatedKeyCode,
2417                              int32_t displayId = ADISPLAY_ID_NONE);
2418 };
2419 
2420 /* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2421  * orientation.
2422  */
prepareDisplay(int32_t orientation)2423 void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
2424     setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2425             orientation, UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
2426 }
2427 
testDPadKeyRotation(KeyboardInputMapper & mapper,int32_t originalScanCode,int32_t originalKeyCode,int32_t rotatedKeyCode,int32_t displayId)2428 void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
2429                                                   int32_t originalScanCode, int32_t originalKeyCode,
2430                                                   int32_t rotatedKeyCode, int32_t displayId) {
2431     NotifyKeyArgs args;
2432 
2433     process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 1);
2434     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2435     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2436     ASSERT_EQ(originalScanCode, args.scanCode);
2437     ASSERT_EQ(rotatedKeyCode, args.keyCode);
2438     ASSERT_EQ(displayId, args.displayId);
2439 
2440     process(mapper, ARBITRARY_TIME, EV_KEY, originalScanCode, 0);
2441     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2442     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2443     ASSERT_EQ(originalScanCode, args.scanCode);
2444     ASSERT_EQ(rotatedKeyCode, args.keyCode);
2445     ASSERT_EQ(displayId, args.displayId);
2446 }
2447 
TEST_F(KeyboardInputMapperTest,GetSources)2448 TEST_F(KeyboardInputMapperTest, GetSources) {
2449     KeyboardInputMapper& mapper =
2450             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2451                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2452 
2453     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
2454 }
2455 
TEST_F(KeyboardInputMapperTest,Process_SimpleKeyPress)2456 TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2457     const int32_t USAGE_A = 0x070004;
2458     const int32_t USAGE_UNKNOWN = 0x07ffff;
2459     mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2460     mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
2461 
2462     KeyboardInputMapper& mapper =
2463             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2464                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2465 
2466     // Key down by scan code.
2467     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2468     NotifyKeyArgs args;
2469     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2470     ASSERT_EQ(DEVICE_ID, args.deviceId);
2471     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2472     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2473     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2474     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2475     ASSERT_EQ(KEY_HOME, args.scanCode);
2476     ASSERT_EQ(AMETA_NONE, args.metaState);
2477     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2478     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2479     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2480 
2481     // Key up by scan code.
2482     process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2483     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2484     ASSERT_EQ(DEVICE_ID, args.deviceId);
2485     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2486     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2487     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2488     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2489     ASSERT_EQ(KEY_HOME, args.scanCode);
2490     ASSERT_EQ(AMETA_NONE, args.metaState);
2491     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2492     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2493     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2494 
2495     // Key down by usage code.
2496     process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2497     process(mapper, ARBITRARY_TIME, EV_KEY, 0, 1);
2498     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2499     ASSERT_EQ(DEVICE_ID, args.deviceId);
2500     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2501     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2502     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2503     ASSERT_EQ(AKEYCODE_A, args.keyCode);
2504     ASSERT_EQ(0, args.scanCode);
2505     ASSERT_EQ(AMETA_NONE, args.metaState);
2506     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2507     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2508     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2509 
2510     // Key up by usage code.
2511     process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2512     process(mapper, ARBITRARY_TIME + 1, EV_KEY, 0, 0);
2513     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2514     ASSERT_EQ(DEVICE_ID, args.deviceId);
2515     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2516     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2517     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2518     ASSERT_EQ(AKEYCODE_A, args.keyCode);
2519     ASSERT_EQ(0, args.scanCode);
2520     ASSERT_EQ(AMETA_NONE, args.metaState);
2521     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2522     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2523     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2524 
2525     // Key down with unknown scan code or usage code.
2526     process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2527     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UNKNOWN, 1);
2528     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2529     ASSERT_EQ(DEVICE_ID, args.deviceId);
2530     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2531     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2532     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2533     ASSERT_EQ(0, args.keyCode);
2534     ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2535     ASSERT_EQ(AMETA_NONE, args.metaState);
2536     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2537     ASSERT_EQ(0U, args.policyFlags);
2538     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2539 
2540     // Key up with unknown scan code or usage code.
2541     process(mapper, ARBITRARY_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2542     process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_UNKNOWN, 0);
2543     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2544     ASSERT_EQ(DEVICE_ID, args.deviceId);
2545     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2546     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2547     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2548     ASSERT_EQ(0, args.keyCode);
2549     ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2550     ASSERT_EQ(AMETA_NONE, args.metaState);
2551     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2552     ASSERT_EQ(0U, args.policyFlags);
2553     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2554 }
2555 
TEST_F(KeyboardInputMapperTest,Process_ShouldUpdateMetaState)2556 TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
2557     mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2558     mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2559 
2560     KeyboardInputMapper& mapper =
2561             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2562                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2563 
2564     // Initial metastate.
2565     ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2566 
2567     // Metakey down.
2568     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
2569     NotifyKeyArgs args;
2570     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2571     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2572     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
2573     ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2574 
2575     // Key down.
2576     process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_A, 1);
2577     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2578     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2579     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
2580 
2581     // Key up.
2582     process(mapper, ARBITRARY_TIME + 2, EV_KEY, KEY_A, 0);
2583     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2584     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2585     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
2586 
2587     // Metakey up.
2588     process(mapper, ARBITRARY_TIME + 3, EV_KEY, KEY_LEFTSHIFT, 0);
2589     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2590     ASSERT_EQ(AMETA_NONE, args.metaState);
2591     ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2592     ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
2593 }
2594 
TEST_F(KeyboardInputMapperTest,Process_WhenNotOrientationAware_ShouldNotRotateDPad)2595 TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
2596     mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2597     mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2598     mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2599     mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2600 
2601     KeyboardInputMapper& mapper =
2602             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2603                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2604 
2605     prepareDisplay(DISPLAY_ORIENTATION_90);
2606     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2607             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
2608     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2609             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
2610     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2611             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
2612     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
2613             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
2614 }
2615 
TEST_F(KeyboardInputMapperTest,Process_WhenOrientationAware_ShouldRotateDPad)2616 TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
2617     mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2618     mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2619     mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2620     mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2621 
2622     addConfigurationProperty("keyboard.orientationAware", "1");
2623     KeyboardInputMapper& mapper =
2624             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2625                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2626 
2627     prepareDisplay(DISPLAY_ORIENTATION_0);
2628     ASSERT_NO_FATAL_FAILURE(
2629             testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2630     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2631                                                 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2632     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2633                                                 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2634     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2635                                                 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2636 
2637     clearViewports();
2638     prepareDisplay(DISPLAY_ORIENTATION_90);
2639     ASSERT_NO_FATAL_FAILURE(
2640             testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2641     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2642                                                 AKEYCODE_DPAD_UP, DISPLAY_ID));
2643     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2644                                                 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2645     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2646                                                 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2647 
2648     clearViewports();
2649     prepareDisplay(DISPLAY_ORIENTATION_180);
2650     ASSERT_NO_FATAL_FAILURE(
2651             testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2652     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2653                                                 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2654     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2655                                                 AKEYCODE_DPAD_UP, DISPLAY_ID));
2656     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2657                                                 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2658 
2659     clearViewports();
2660     prepareDisplay(DISPLAY_ORIENTATION_270);
2661     ASSERT_NO_FATAL_FAILURE(
2662             testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2663     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2664                                                 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2665     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2666                                                 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2667     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2668                                                 AKEYCODE_DPAD_UP, DISPLAY_ID));
2669 
2670     // Special case: if orientation changes while key is down, we still emit the same keycode
2671     // in the key up as we did in the key down.
2672     NotifyKeyArgs args;
2673     clearViewports();
2674     prepareDisplay(DISPLAY_ORIENTATION_270);
2675     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
2676     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2677     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2678     ASSERT_EQ(KEY_UP, args.scanCode);
2679     ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2680 
2681     clearViewports();
2682     prepareDisplay(DISPLAY_ORIENTATION_180);
2683     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
2684     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2685     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2686     ASSERT_EQ(KEY_UP, args.scanCode);
2687     ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
2688 }
2689 
TEST_F(KeyboardInputMapperTest,DisplayIdConfigurationChange_NotOrientationAware)2690 TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
2691     // If the keyboard is not orientation aware,
2692     // key events should not be associated with a specific display id
2693     mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2694 
2695     KeyboardInputMapper& mapper =
2696             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2697                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2698     NotifyKeyArgs args;
2699 
2700     // Display id should be ADISPLAY_ID_NONE without any display configuration.
2701     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
2702     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2703     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
2704     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2705     ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2706 
2707     prepareDisplay(DISPLAY_ORIENTATION_0);
2708     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
2709     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2710     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
2711     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2712     ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
2713 }
2714 
TEST_F(KeyboardInputMapperTest,DisplayIdConfigurationChange_OrientationAware)2715 TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
2716     // If the keyboard is orientation aware,
2717     // key events should be associated with the internal viewport
2718     mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2719 
2720     addConfigurationProperty("keyboard.orientationAware", "1");
2721     KeyboardInputMapper& mapper =
2722             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2723                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2724     NotifyKeyArgs args;
2725 
2726     // Display id should be ADISPLAY_ID_NONE without any display configuration.
2727     // ^--- already checked by the previous test
2728 
2729     setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2730             UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
2731     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
2732     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2733     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
2734     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2735     ASSERT_EQ(DISPLAY_ID, args.displayId);
2736 
2737     constexpr int32_t newDisplayId = 2;
2738     clearViewports();
2739     setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2740             UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_INTERNAL);
2741     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 1);
2742     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2743     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_UP, 0);
2744     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2745     ASSERT_EQ(newDisplayId, args.displayId);
2746 }
2747 
TEST_F(KeyboardInputMapperTest,GetKeyCodeState)2748 TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2749     KeyboardInputMapper& mapper =
2750             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2751                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2752 
2753     mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
2754     ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2755 
2756     mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
2757     ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2758 }
2759 
TEST_F(KeyboardInputMapperTest,GetScanCodeState)2760 TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2761     KeyboardInputMapper& mapper =
2762             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2763                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2764 
2765     mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
2766     ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2767 
2768     mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
2769     ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2770 }
2771 
TEST_F(KeyboardInputMapperTest,MarkSupportedKeyCodes)2772 TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2773     KeyboardInputMapper& mapper =
2774             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2775                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2776 
2777     mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2778 
2779     const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2780     uint8_t flags[2] = { 0, 0 };
2781     ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2782     ASSERT_TRUE(flags[0]);
2783     ASSERT_FALSE(flags[1]);
2784 }
2785 
TEST_F(KeyboardInputMapperTest,Process_LockedKeysShouldToggleMetaStateAndLeds)2786 TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2787     mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
2788     mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
2789     mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
2790     mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2791     mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2792     mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2793 
2794     KeyboardInputMapper& mapper =
2795             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2796                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2797 
2798     // Initialization should have turned all of the lights off.
2799     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2800     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2801     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
2802 
2803     // Toggle caps lock on.
2804     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2805     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
2806     ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2807     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2808     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
2809     ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
2810 
2811     // Toggle num lock on.
2812     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2813     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
2814     ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2815     ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2816     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
2817     ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
2818 
2819     // Toggle caps lock off.
2820     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 1);
2821     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_CAPSLOCK, 0);
2822     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2823     ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2824     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
2825     ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
2826 
2827     // Toggle scroll lock on.
2828     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2829     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
2830     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2831     ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2832     ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
2833     ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
2834 
2835     // Toggle num lock off.
2836     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 1);
2837     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_NUMLOCK, 0);
2838     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2839     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2840     ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
2841     ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
2842 
2843     // Toggle scroll lock off.
2844     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
2845     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
2846     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
2847     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
2848     ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
2849     ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
2850 }
2851 
TEST_F(KeyboardInputMapperTest,Configure_AssignsDisplayPort)2852 TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
2853     // keyboard 1.
2854     mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2855     mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2856     mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2857     mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2858 
2859     // keyboard 2.
2860     const std::string USB2 = "USB2";
2861     constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
2862     constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
2863     InputDeviceIdentifier identifier;
2864     identifier.name = "KEYBOARD2";
2865     identifier.location = USB2;
2866     std::unique_ptr<InputDevice> device2 =
2867             std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
2868                                           identifier);
2869     mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
2870     mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
2871     mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
2872     mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2873     mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
2874 
2875     KeyboardInputMapper& mapper =
2876             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2877                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2878 
2879     KeyboardInputMapper& mapper2 =
2880             device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
2881                                                     AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2882     device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
2883     device2->reset(ARBITRARY_TIME);
2884 
2885     // Prepared displays and associated info.
2886     constexpr uint8_t hdmi1 = 0;
2887     constexpr uint8_t hdmi2 = 1;
2888     const std::string SECONDARY_UNIQUE_ID = "local:1";
2889 
2890     mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2891     mFakePolicy->addInputPortAssociation(USB2, hdmi2);
2892 
2893     // No associated display viewport found, should disable the device.
2894     device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2895                        InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2896     ASSERT_FALSE(device2->isEnabled());
2897 
2898     // Prepare second display.
2899     constexpr int32_t newDisplayId = 2;
2900     setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2901                                  UNIQUE_ID, hdmi1, ViewportType::VIEWPORT_INTERNAL);
2902     setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
2903                                  SECONDARY_UNIQUE_ID, hdmi2, ViewportType::VIEWPORT_EXTERNAL);
2904     // Default device will reconfigure above, need additional reconfiguration for another device.
2905     device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2906                        InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2907 
2908     // Device should be enabled after the associated display is found.
2909     ASSERT_TRUE(mDevice->isEnabled());
2910     ASSERT_TRUE(device2->isEnabled());
2911 
2912     // Test pad key events
2913     ASSERT_NO_FATAL_FAILURE(
2914             testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
2915     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2916                                                 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
2917     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2918                                                 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
2919     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2920                                                 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
2921 
2922     ASSERT_NO_FATAL_FAILURE(
2923             testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
2924     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
2925                                                 AKEYCODE_DPAD_RIGHT, newDisplayId));
2926     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
2927                                                 AKEYCODE_DPAD_DOWN, newDisplayId));
2928     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
2929                                                 AKEYCODE_DPAD_LEFT, newDisplayId));
2930 }
2931 
2932 // --- KeyboardInputMapperTest_ExternalDevice ---
2933 
2934 class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
2935 protected:
SetUp()2936     virtual void SetUp() override {
2937         InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
2938     }
2939 };
2940 
TEST_F(KeyboardInputMapperTest_ExternalDevice,WakeBehavior)2941 TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
2942     // For external devices, non-media keys will trigger wake on key down. Media keys need to be
2943     // marked as WAKE in the keylayout file to trigger wake.
2944 
2945     mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
2946     mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
2947     mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
2948                           POLICY_FLAG_WAKE);
2949 
2950     KeyboardInputMapper& mapper =
2951             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2952                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2953 
2954     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2955     NotifyKeyArgs args;
2956     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2957     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2958 
2959     process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2960     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2961     ASSERT_EQ(uint32_t(0), args.policyFlags);
2962 
2963     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
2964     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2965     ASSERT_EQ(uint32_t(0), args.policyFlags);
2966 
2967     process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
2968     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2969     ASSERT_EQ(uint32_t(0), args.policyFlags);
2970 
2971     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
2972     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2973     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2974 
2975     process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAYPAUSE, 0);
2976     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2977     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2978 }
2979 
TEST_F(KeyboardInputMapperTest_ExternalDevice,DoNotWakeByDefaultBehavior)2980 TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
2981     // Tv Remote key's wake behavior is prescribed by the keylayout file.
2982 
2983     mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2984     mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
2985     mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
2986 
2987     addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
2988     KeyboardInputMapper& mapper =
2989             addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2990                                                        AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2991 
2992     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_HOME, 1);
2993     NotifyKeyArgs args;
2994     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2996 
2997     process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_HOME, 0);
2998     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2999     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3000 
3001     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_DOWN, 1);
3002     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3003     ASSERT_EQ(uint32_t(0), args.policyFlags);
3004 
3005     process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_DOWN, 0);
3006     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3007     ASSERT_EQ(uint32_t(0), args.policyFlags);
3008 
3009     process(mapper, ARBITRARY_TIME, EV_KEY, KEY_PLAY, 1);
3010     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3011     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3012 
3013     process(mapper, ARBITRARY_TIME + 1, EV_KEY, KEY_PLAY, 0);
3014     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3015     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3016 }
3017 
3018 // --- CursorInputMapperTest ---
3019 
3020 class CursorInputMapperTest : public InputMapperTest {
3021 protected:
3022     static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3023 
3024     sp<FakePointerController> mFakePointerController;
3025 
SetUp()3026     virtual void SetUp() override {
3027         InputMapperTest::SetUp();
3028 
3029         mFakePointerController = new FakePointerController();
3030         mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
3031     }
3032 
3033     void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3034                             int32_t rotatedX, int32_t rotatedY);
3035 
prepareDisplay(int32_t orientation)3036     void prepareDisplay(int32_t orientation) {
3037         const std::string uniqueId = "local:0";
3038         const ViewportType viewportType = ViewportType::VIEWPORT_INTERNAL;
3039         setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3040                 orientation, uniqueId, NO_PORT, viewportType);
3041     }
3042 };
3043 
3044 const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3045 
testMotionRotation(CursorInputMapper & mapper,int32_t originalX,int32_t originalY,int32_t rotatedX,int32_t rotatedY)3046 void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3047                                                int32_t originalY, int32_t rotatedX,
3048                                                int32_t rotatedY) {
3049     NotifyMotionArgs args;
3050 
3051     process(mapper, ARBITRARY_TIME, EV_REL, REL_X, originalX);
3052     process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, originalY);
3053     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3054     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3055     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3056     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3057             float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3058             float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
3059             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3060 }
3061 
TEST_F(CursorInputMapperTest,WhenModeIsPointer_GetSources_ReturnsMouse)3062 TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
3063     addConfigurationProperty("cursor.mode", "pointer");
3064     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3065 
3066     ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
3067 }
3068 
TEST_F(CursorInputMapperTest,WhenModeIsNavigation_GetSources_ReturnsTrackball)3069 TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
3070     addConfigurationProperty("cursor.mode", "navigation");
3071     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3072 
3073     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
3074 }
3075 
TEST_F(CursorInputMapperTest,WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController)3076 TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
3077     addConfigurationProperty("cursor.mode", "pointer");
3078     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3079 
3080     InputDeviceInfo info;
3081     mapper.populateDeviceInfo(&info);
3082 
3083     // Initially there may not be a valid motion range.
3084     ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3085     ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
3086     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3087             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3088 
3089     // When the bounds are set, then there should be a valid motion range.
3090     mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3091 
3092     InputDeviceInfo info2;
3093     mapper.populateDeviceInfo(&info2);
3094 
3095     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3096             AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3097             1, 800 - 1, 0.0f, 0.0f));
3098     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3099             AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3100             2, 480 - 1, 0.0f, 0.0f));
3101     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3102             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3103             0.0f, 1.0f, 0.0f, 0.0f));
3104 }
3105 
TEST_F(CursorInputMapperTest,WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange)3106 TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
3107     addConfigurationProperty("cursor.mode", "navigation");
3108     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3109 
3110     InputDeviceInfo info;
3111     mapper.populateDeviceInfo(&info);
3112 
3113     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3114             AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3115             -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3116     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3117             AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3118             -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3119     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3120             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3121             0.0f, 1.0f, 0.0f, 0.0f));
3122 }
3123 
TEST_F(CursorInputMapperTest,Process_ShouldSetAllFieldsAndIncludeGlobalMetaState)3124 TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
3125     addConfigurationProperty("cursor.mode", "navigation");
3126     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3127 
3128     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3129 
3130     NotifyMotionArgs args;
3131 
3132     // Button press.
3133     // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
3134     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3135     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3136     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3137     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3138     ASSERT_EQ(DEVICE_ID, args.deviceId);
3139     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3140     ASSERT_EQ(uint32_t(0), args.policyFlags);
3141     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3142     ASSERT_EQ(0, args.flags);
3143     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3144     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3145     ASSERT_EQ(0, args.edgeFlags);
3146     ASSERT_EQ(uint32_t(1), args.pointerCount);
3147     ASSERT_EQ(0, args.pointerProperties[0].id);
3148     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3149     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3150             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3151     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3152     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3153     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3154 
3155     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3156     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3157     ASSERT_EQ(DEVICE_ID, args.deviceId);
3158     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3159     ASSERT_EQ(uint32_t(0), args.policyFlags);
3160     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3161     ASSERT_EQ(0, args.flags);
3162     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3163     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3164     ASSERT_EQ(0, args.edgeFlags);
3165     ASSERT_EQ(uint32_t(1), args.pointerCount);
3166     ASSERT_EQ(0, args.pointerProperties[0].id);
3167     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3168     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3169             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3170     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3171     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3172     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3173 
3174     // Button release.  Should have same down time.
3175     process(mapper, ARBITRARY_TIME + 1, EV_KEY, BTN_MOUSE, 0);
3176     process(mapper, ARBITRARY_TIME + 1, EV_SYN, SYN_REPORT, 0);
3177     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3178     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3179     ASSERT_EQ(DEVICE_ID, args.deviceId);
3180     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3181     ASSERT_EQ(uint32_t(0), args.policyFlags);
3182     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3183     ASSERT_EQ(0, args.flags);
3184     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3185     ASSERT_EQ(0, args.buttonState);
3186     ASSERT_EQ(0, args.edgeFlags);
3187     ASSERT_EQ(uint32_t(1), args.pointerCount);
3188     ASSERT_EQ(0, args.pointerProperties[0].id);
3189     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3190     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3191             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3192     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3193     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3194     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3195 
3196     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3197     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3198     ASSERT_EQ(DEVICE_ID, args.deviceId);
3199     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3200     ASSERT_EQ(uint32_t(0), args.policyFlags);
3201     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3202     ASSERT_EQ(0, args.flags);
3203     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3204     ASSERT_EQ(0, args.buttonState);
3205     ASSERT_EQ(0, args.edgeFlags);
3206     ASSERT_EQ(uint32_t(1), args.pointerCount);
3207     ASSERT_EQ(0, args.pointerProperties[0].id);
3208     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
3209     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3210             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3211     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3212     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3213     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3214 }
3215 
TEST_F(CursorInputMapperTest,Process_ShouldHandleIndependentXYUpdates)3216 TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
3217     addConfigurationProperty("cursor.mode", "navigation");
3218     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3219 
3220     NotifyMotionArgs args;
3221 
3222     // Motion in X but not Y.
3223     process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3224     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3225     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3226     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3227     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3228             1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3229 
3230     // Motion in Y but not X.
3231     process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3232     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3233     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3234     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3235     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3236             0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3237 }
3238 
TEST_F(CursorInputMapperTest,Process_ShouldHandleIndependentButtonUpdates)3239 TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
3240     addConfigurationProperty("cursor.mode", "navigation");
3241     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3242 
3243     NotifyMotionArgs args;
3244 
3245     // Button press.
3246     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3247     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3248     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3249     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3250     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3251             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3252 
3253     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3254     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3255     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3256             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3257 
3258     // Button release.
3259     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3260     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3261     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3262     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3263     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3264             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3265 
3266     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3267     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3268     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3269             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3270 }
3271 
TEST_F(CursorInputMapperTest,Process_ShouldHandleCombinedXYAndButtonUpdates)3272 TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
3273     addConfigurationProperty("cursor.mode", "navigation");
3274     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3275 
3276     NotifyMotionArgs args;
3277 
3278     // Combined X, Y and Button.
3279     process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 1);
3280     process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, -2);
3281     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3282     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3283     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3284     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3285     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3286             1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3287             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3288 
3289     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3290     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3291     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3292             1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3293             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3294 
3295     // Move X, Y a bit while pressed.
3296     process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 2);
3297     process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 1);
3298     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3299     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3300     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3301     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3302             2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3303             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3304 
3305     // Release Button.
3306     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 0);
3307     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3308     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3309     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3310     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3311             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3312 
3313     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3314     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3315     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3316             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3317 }
3318 
TEST_F(CursorInputMapperTest,Process_WhenNotOrientationAware_ShouldNotRotateMotions)3319 TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
3320     addConfigurationProperty("cursor.mode", "navigation");
3321     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3322 
3323     prepareDisplay(DISPLAY_ORIENTATION_90);
3324     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
3325     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1,  1));
3326     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  1,  0));
3327     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1, -1));
3328     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0, -1));
3329     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3330     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0, -1,  0));
3331     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1,  1));
3332 }
3333 
TEST_F(CursorInputMapperTest,Process_WhenOrientationAware_ShouldRotateMotions)3334 TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
3335     addConfigurationProperty("cursor.mode", "navigation");
3336     addConfigurationProperty("cursor.orientationAware", "1");
3337     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3338 
3339     prepareDisplay(DISPLAY_ORIENTATION_0);
3340     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
3341     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1,  1));
3342     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  1,  0));
3343     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1, -1));
3344     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0, -1));
3345     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3346     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0, -1,  0));
3347     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1,  1));
3348 
3349     prepareDisplay(DISPLAY_ORIENTATION_90);
3350     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  1,  0));
3351     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1, -1));
3352     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  0, -1));
3353     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1, -1, -1));
3354     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1, -1,  0));
3355     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1,  1));
3356     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  0,  1));
3357     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1,  1));
3358 
3359     prepareDisplay(DISPLAY_ORIENTATION_180);
3360     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0, -1));
3361     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1, -1));
3362     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0, -1,  0));
3363     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1, -1,  1));
3364     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0,  1));
3365     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1,  1,  1));
3366     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  1,  0));
3367     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1, -1));
3368 
3369     prepareDisplay(DISPLAY_ORIENTATION_270);
3370     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1, -1,  0));
3371     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1,  1));
3372     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  0,  1));
3373     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1,  1));
3374     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  1,  0));
3375     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1,  1, -1));
3376     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  0, -1));
3377     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1, -1));
3378 }
3379 
TEST_F(CursorInputMapperTest,Process_ShouldHandleAllButtons)3380 TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
3381     addConfigurationProperty("cursor.mode", "pointer");
3382     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3383 
3384     mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3385     mFakePointerController->setPosition(100, 200);
3386     mFakePointerController->setButtonState(0);
3387 
3388     NotifyMotionArgs motionArgs;
3389     NotifyKeyArgs keyArgs;
3390 
3391     // press BTN_LEFT, release BTN_LEFT
3392     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 1);
3393     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3394     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3395     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3396     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3397     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3398     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3399             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3400 
3401     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3402     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3403     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3404     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
3405     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3406             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3407 
3408     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_LEFT, 0);
3409     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3410     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3411     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3412     ASSERT_EQ(0, motionArgs.buttonState);
3413     ASSERT_EQ(0, mFakePointerController->getButtonState());
3414     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3415             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3416 
3417     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3418     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3419     ASSERT_EQ(0, motionArgs.buttonState);
3420     ASSERT_EQ(0, mFakePointerController->getButtonState());
3421     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3422             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3423 
3424     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3425     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3426     ASSERT_EQ(0, motionArgs.buttonState);
3427     ASSERT_EQ(0, mFakePointerController->getButtonState());
3428     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3429             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3430 
3431     // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
3432     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 1);
3433     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 1);
3434     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3435     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3436     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3437     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3438             motionArgs.buttonState);
3439     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3440             mFakePointerController->getButtonState());
3441     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3442             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3443 
3444     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3445     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3446     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3447     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3448             mFakePointerController->getButtonState());
3449     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3450             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3451 
3452     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3453     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3454     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3455             motionArgs.buttonState);
3456     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3457             mFakePointerController->getButtonState());
3458     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3459             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3460 
3461     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_RIGHT, 0);
3462     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3463     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3464     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3465     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3466     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
3467     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3468             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3469 
3470     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3471     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3472     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3473     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
3474     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3475             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3476 
3477     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3478     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3479     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3480     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3481     ASSERT_EQ(0, motionArgs.buttonState);
3482     ASSERT_EQ(0, mFakePointerController->getButtonState());
3483     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3484             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3485     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MIDDLE, 0);
3486     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3487 
3488     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3489     ASSERT_EQ(0, motionArgs.buttonState);
3490     ASSERT_EQ(0, mFakePointerController->getButtonState());
3491     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3492     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3493             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3494 
3495     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3496     ASSERT_EQ(0, motionArgs.buttonState);
3497     ASSERT_EQ(0, mFakePointerController->getButtonState());
3498     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3499     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3500             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3501 
3502     // press BTN_BACK, release BTN_BACK
3503     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 1);
3504     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3505     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3506     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3507     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3508 
3509     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3510     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3511     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3512     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
3513     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3514             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3515 
3516     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3517     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3518     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3519     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
3520     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3521             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3522 
3523     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_BACK, 0);
3524     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3525     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3526     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3527     ASSERT_EQ(0, motionArgs.buttonState);
3528     ASSERT_EQ(0, mFakePointerController->getButtonState());
3529     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3530             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3531 
3532     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3533     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3534     ASSERT_EQ(0, motionArgs.buttonState);
3535     ASSERT_EQ(0, mFakePointerController->getButtonState());
3536 
3537     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3538             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3539     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3540     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3541     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3542 
3543     // press BTN_SIDE, release BTN_SIDE
3544     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 1);
3545     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3546     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3547     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3548     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3549 
3550     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3551     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3552     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3553     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
3554     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3555             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3556 
3557     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3558     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3559     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3560     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
3561     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3562             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3563 
3564     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_SIDE, 0);
3565     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3566     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3567     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3568     ASSERT_EQ(0, motionArgs.buttonState);
3569     ASSERT_EQ(0, mFakePointerController->getButtonState());
3570     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3571             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3572 
3573     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3574     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3575     ASSERT_EQ(0, motionArgs.buttonState);
3576     ASSERT_EQ(0, mFakePointerController->getButtonState());
3577     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3578             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3579 
3580     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3581     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3582     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3583 
3584     // press BTN_FORWARD, release BTN_FORWARD
3585     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 1);
3586     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3587     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3588     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3589     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3590 
3591     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3592     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3593     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3594     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
3595     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3596             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3597 
3598     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3599     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3600     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3601     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
3602     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3603             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3604 
3605     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_FORWARD, 0);
3606     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3607     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3608     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3609     ASSERT_EQ(0, motionArgs.buttonState);
3610     ASSERT_EQ(0, mFakePointerController->getButtonState());
3611     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3612             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3613 
3614     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3615     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3616     ASSERT_EQ(0, motionArgs.buttonState);
3617     ASSERT_EQ(0, mFakePointerController->getButtonState());
3618     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3619             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3620 
3621     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3622     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3623     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3624 
3625     // press BTN_EXTRA, release BTN_EXTRA
3626     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 1);
3627     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3628     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3629     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3630     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3631 
3632     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3633     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3634     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3635     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
3636     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3637             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3638 
3639     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3640     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3641     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3642     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
3643     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3644             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3645 
3646     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_EXTRA, 0);
3647     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3648     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3649     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3650     ASSERT_EQ(0, motionArgs.buttonState);
3651     ASSERT_EQ(0, mFakePointerController->getButtonState());
3652     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3653             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3654 
3655     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3656     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3657     ASSERT_EQ(0, motionArgs.buttonState);
3658     ASSERT_EQ(0, mFakePointerController->getButtonState());
3659     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3660             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3661 
3662     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3663     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3664     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3665 }
3666 
TEST_F(CursorInputMapperTest,Process_WhenModeIsPointer_ShouldMoveThePointerAround)3667 TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
3668     addConfigurationProperty("cursor.mode", "pointer");
3669     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3670 
3671     mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3672     mFakePointerController->setPosition(100, 200);
3673     mFakePointerController->setButtonState(0);
3674 
3675     NotifyMotionArgs args;
3676 
3677     process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3678     process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3679     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3680     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3681     ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3682     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3683     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3684             110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3685     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3686 }
3687 
TEST_F(CursorInputMapperTest,Process_PointerCapture)3688 TEST_F(CursorInputMapperTest, Process_PointerCapture) {
3689     addConfigurationProperty("cursor.mode", "pointer");
3690     mFakePolicy->setPointerCapture(true);
3691     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3692 
3693     NotifyDeviceResetArgs resetArgs;
3694     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3695     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3696     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3697 
3698     mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3699     mFakePointerController->setPosition(100, 200);
3700     mFakePointerController->setButtonState(0);
3701 
3702     NotifyMotionArgs args;
3703 
3704     // Move.
3705     process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3706     process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3707     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3708     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3709     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3710     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3711     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3712             10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3713     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3714 
3715     // Button press.
3716     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1);
3717     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3718     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3719     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3720     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3721     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3722             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3723     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3724     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3725     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3726     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3727             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3728 
3729     // Button release.
3730     process(mapper, ARBITRARY_TIME + 2, EV_KEY, BTN_MOUSE, 0);
3731     process(mapper, ARBITRARY_TIME + 2, EV_SYN, SYN_REPORT, 0);
3732     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3733     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3734     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3735     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3736             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3737     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3738     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3739     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3740     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3741             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3742 
3743     // Another move.
3744     process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 30);
3745     process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 40);
3746     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3747     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3748     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
3749     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
3750     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3751             30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3752     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
3753 
3754     // Disable pointer capture and check that the device generation got bumped
3755     // and events are generated the usual way.
3756     const uint32_t generation = mFakeContext->getGeneration();
3757     mFakePolicy->setPointerCapture(false);
3758     configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
3759     ASSERT_TRUE(mFakeContext->getGeneration() != generation);
3760 
3761     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3762     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3763     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3764 
3765     process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3766     process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3767     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3768     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3769     ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3770     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3771     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3772             110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3773     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3774 }
3775 
TEST_F(CursorInputMapperTest,Process_ShouldHandleDisplayId)3776 TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
3777     CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
3778 
3779     // Setup for second display.
3780     constexpr int32_t SECOND_DISPLAY_ID = 1;
3781     const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
3782     mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
3783                                     SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
3784                                     ViewportType::VIEWPORT_EXTERNAL);
3785     mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
3786     configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3787 
3788     mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
3789     mFakePointerController->setPosition(100, 200);
3790     mFakePointerController->setButtonState(0);
3791 
3792     NotifyMotionArgs args;
3793     process(mapper, ARBITRARY_TIME, EV_REL, REL_X, 10);
3794     process(mapper, ARBITRARY_TIME, EV_REL, REL_Y, 20);
3795     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
3796     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3797     ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
3798     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
3799     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3800             110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
3801     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
3802     ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
3803 }
3804 
3805 // --- TouchInputMapperTest ---
3806 
3807 class TouchInputMapperTest : public InputMapperTest {
3808 protected:
3809     static const int32_t RAW_X_MIN;
3810     static const int32_t RAW_X_MAX;
3811     static const int32_t RAW_Y_MIN;
3812     static const int32_t RAW_Y_MAX;
3813     static const int32_t RAW_TOUCH_MIN;
3814     static const int32_t RAW_TOUCH_MAX;
3815     static const int32_t RAW_TOOL_MIN;
3816     static const int32_t RAW_TOOL_MAX;
3817     static const int32_t RAW_PRESSURE_MIN;
3818     static const int32_t RAW_PRESSURE_MAX;
3819     static const int32_t RAW_ORIENTATION_MIN;
3820     static const int32_t RAW_ORIENTATION_MAX;
3821     static const int32_t RAW_DISTANCE_MIN;
3822     static const int32_t RAW_DISTANCE_MAX;
3823     static const int32_t RAW_TILT_MIN;
3824     static const int32_t RAW_TILT_MAX;
3825     static const int32_t RAW_ID_MIN;
3826     static const int32_t RAW_ID_MAX;
3827     static const int32_t RAW_SLOT_MIN;
3828     static const int32_t RAW_SLOT_MAX;
3829     static const float X_PRECISION;
3830     static const float Y_PRECISION;
3831     static const float X_PRECISION_VIRTUAL;
3832     static const float Y_PRECISION_VIRTUAL;
3833 
3834     static const float GEOMETRIC_SCALE;
3835     static const TouchAffineTransformation AFFINE_TRANSFORM;
3836 
3837     static const VirtualKeyDefinition VIRTUAL_KEYS[2];
3838 
3839     const std::string UNIQUE_ID = "local:0";
3840     const std::string SECONDARY_UNIQUE_ID = "local:1";
3841 
3842     enum Axes {
3843         POSITION = 1 << 0,
3844         TOUCH = 1 << 1,
3845         TOOL = 1 << 2,
3846         PRESSURE = 1 << 3,
3847         ORIENTATION = 1 << 4,
3848         MINOR = 1 << 5,
3849         ID = 1 << 6,
3850         DISTANCE = 1 << 7,
3851         TILT = 1 << 8,
3852         SLOT = 1 << 9,
3853         TOOL_TYPE = 1 << 10,
3854     };
3855 
3856     void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
3857     void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
3858     void prepareVirtualDisplay(int32_t orientation);
3859     void prepareVirtualKeys();
3860     void prepareLocationCalibration();
3861     int32_t toRawX(float displayX);
3862     int32_t toRawY(float displayY);
3863     float toCookedX(float rawX, float rawY);
3864     float toCookedY(float rawX, float rawY);
3865     float toDisplayX(int32_t rawX);
3866     float toDisplayX(int32_t rawX, int32_t displayWidth);
3867     float toDisplayY(int32_t rawY);
3868     float toDisplayY(int32_t rawY, int32_t displayHeight);
3869 
3870 };
3871 
3872 const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
3873 const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
3874 const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
3875 const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
3876 const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
3877 const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
3878 const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
3879 const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
3880 const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
3881 const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
3882 const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
3883 const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
3884 const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
3885 const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
3886 const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
3887 const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
3888 const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
3889 const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
3890 const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
3891 const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
3892 const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
3893 const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
3894 const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
3895         float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
3896 const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
3897         float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
3898 const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
3899         TouchAffineTransformation(1, -2, 3, -4, 5, -6);
3900 
3901 const float TouchInputMapperTest::GEOMETRIC_SCALE =
3902         avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3903                 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3904 
3905 const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
3906         { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
3907         { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
3908 };
3909 
prepareDisplay(int32_t orientation,std::optional<uint8_t> port)3910 void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
3911     setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3912             UNIQUE_ID, port, ViewportType::VIEWPORT_INTERNAL);
3913 }
3914 
prepareSecondaryDisplay(ViewportType type,std::optional<uint8_t> port)3915 void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
3916     setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3917             DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
3918 }
3919 
prepareVirtualDisplay(int32_t orientation)3920 void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
3921     setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
3922         VIRTUAL_DISPLAY_HEIGHT, orientation,
3923         VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::VIEWPORT_VIRTUAL);
3924 }
3925 
prepareVirtualKeys()3926 void TouchInputMapperTest::prepareVirtualKeys() {
3927     mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
3928     mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
3929     mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3930     mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3931 }
3932 
prepareLocationCalibration()3933 void TouchInputMapperTest::prepareLocationCalibration() {
3934     mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3935 }
3936 
toRawX(float displayX)3937 int32_t TouchInputMapperTest::toRawX(float displayX) {
3938     return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3939 }
3940 
toRawY(float displayY)3941 int32_t TouchInputMapperTest::toRawY(float displayY) {
3942     return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3943 }
3944 
toCookedX(float rawX,float rawY)3945 float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3946     AFFINE_TRANSFORM.applyTo(rawX, rawY);
3947     return rawX;
3948 }
3949 
toCookedY(float rawX,float rawY)3950 float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3951     AFFINE_TRANSFORM.applyTo(rawX, rawY);
3952     return rawY;
3953 }
3954 
toDisplayX(int32_t rawX)3955 float TouchInputMapperTest::toDisplayX(int32_t rawX) {
3956     return toDisplayX(rawX, DISPLAY_WIDTH);
3957 }
3958 
toDisplayX(int32_t rawX,int32_t displayWidth)3959 float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3960     return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
3961 }
3962 
toDisplayY(int32_t rawY)3963 float TouchInputMapperTest::toDisplayY(int32_t rawY) {
3964     return toDisplayY(rawY, DISPLAY_HEIGHT);
3965 }
3966 
toDisplayY(int32_t rawY,int32_t displayHeight)3967 float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3968     return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
3969 }
3970 
3971 
3972 // --- SingleTouchInputMapperTest ---
3973 
3974 class SingleTouchInputMapperTest : public TouchInputMapperTest {
3975 protected:
3976     void prepareButtons();
3977     void prepareAxes(int axes);
3978 
3979     void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3980     void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
3981     void processUp(SingleTouchInputMapper& mappery);
3982     void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
3983     void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
3984     void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
3985     void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
3986     void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
3987     void processSync(SingleTouchInputMapper& mapper);
3988 };
3989 
prepareButtons()3990 void SingleTouchInputMapperTest::prepareButtons() {
3991     mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3992 }
3993 
prepareAxes(int axes)3994 void SingleTouchInputMapperTest::prepareAxes(int axes) {
3995     if (axes & POSITION) {
3996         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
3997         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3998     }
3999     if (axes & PRESSURE) {
4000         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4001                                        RAW_PRESSURE_MAX, 0, 0);
4002     }
4003     if (axes & TOOL) {
4004         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4005                                        0);
4006     }
4007     if (axes & DISTANCE) {
4008         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4009                                        RAW_DISTANCE_MAX, 0, 0);
4010     }
4011     if (axes & TILT) {
4012         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4013         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4014     }
4015 }
4016 
processDown(SingleTouchInputMapper & mapper,int32_t x,int32_t y)4017 void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
4018     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 1);
4019     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4020     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
4021 }
4022 
processMove(SingleTouchInputMapper & mapper,int32_t x,int32_t y)4023 void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
4024     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_X, x);
4025     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_Y, y);
4026 }
4027 
processUp(SingleTouchInputMapper & mapper)4028 void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
4029     process(mapper, ARBITRARY_TIME, EV_KEY, BTN_TOUCH, 0);
4030 }
4031 
processPressure(SingleTouchInputMapper & mapper,int32_t pressure)4032 void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
4033     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_PRESSURE, pressure);
4034 }
4035 
processToolMajor(SingleTouchInputMapper & mapper,int32_t toolMajor)4036 void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4037                                                   int32_t toolMajor) {
4038     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
4039 }
4040 
processDistance(SingleTouchInputMapper & mapper,int32_t distance)4041 void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
4042     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_DISTANCE, distance);
4043 }
4044 
processTilt(SingleTouchInputMapper & mapper,int32_t tiltX,int32_t tiltY)4045 void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4046                                              int32_t tiltY) {
4047     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_X, tiltX);
4048     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_TILT_Y, tiltY);
4049 }
4050 
processKey(SingleTouchInputMapper & mapper,int32_t code,int32_t value)4051 void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4052                                             int32_t value) {
4053     process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
4054 }
4055 
processSync(SingleTouchInputMapper & mapper)4056 void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
4057     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
4058 }
4059 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer)4060 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
4061     prepareButtons();
4062     prepareAxes(POSITION);
4063     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4064 
4065     ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
4066 }
4067 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad)4068 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
4069     mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4070     mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
4071     prepareButtons();
4072     prepareAxes(POSITION);
4073     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4074 
4075     ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
4076 }
4077 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad)4078 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
4079     prepareButtons();
4080     prepareAxes(POSITION);
4081     addConfigurationProperty("touch.deviceType", "touchPad");
4082     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4083 
4084     ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
4085 }
4086 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen)4087 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
4088     prepareButtons();
4089     prepareAxes(POSITION);
4090     addConfigurationProperty("touch.deviceType", "touchScreen");
4091     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4092 
4093     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
4094 }
4095 
TEST_F(SingleTouchInputMapperTest,GetKeyCodeState)4096 TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
4097     addConfigurationProperty("touch.deviceType", "touchScreen");
4098     prepareDisplay(DISPLAY_ORIENTATION_0);
4099     prepareButtons();
4100     prepareAxes(POSITION);
4101     prepareVirtualKeys();
4102     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4103 
4104     // Unknown key.
4105     ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
4106 
4107     // Virtual key is down.
4108     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4109     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4110     processDown(mapper, x, y);
4111     processSync(mapper);
4112     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4113 
4114     ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
4115 
4116     // Virtual key is up.
4117     processUp(mapper);
4118     processSync(mapper);
4119     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4120 
4121     ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
4122 }
4123 
TEST_F(SingleTouchInputMapperTest,GetScanCodeState)4124 TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
4125     addConfigurationProperty("touch.deviceType", "touchScreen");
4126     prepareDisplay(DISPLAY_ORIENTATION_0);
4127     prepareButtons();
4128     prepareAxes(POSITION);
4129     prepareVirtualKeys();
4130     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4131 
4132     // Unknown key.
4133     ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
4134 
4135     // Virtual key is down.
4136     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4137     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4138     processDown(mapper, x, y);
4139     processSync(mapper);
4140     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4141 
4142     ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
4143 
4144     // Virtual key is up.
4145     processUp(mapper);
4146     processSync(mapper);
4147     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4148 
4149     ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
4150 }
4151 
TEST_F(SingleTouchInputMapperTest,MarkSupportedKeyCodes)4152 TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
4153     addConfigurationProperty("touch.deviceType", "touchScreen");
4154     prepareDisplay(DISPLAY_ORIENTATION_0);
4155     prepareButtons();
4156     prepareAxes(POSITION);
4157     prepareVirtualKeys();
4158     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4159 
4160     const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4161     uint8_t flags[2] = { 0, 0 };
4162     ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
4163     ASSERT_TRUE(flags[0]);
4164     ASSERT_FALSE(flags[1]);
4165 }
4166 
TEST_F(SingleTouchInputMapperTest,Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp)4167 TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
4168     addConfigurationProperty("touch.deviceType", "touchScreen");
4169     prepareDisplay(DISPLAY_ORIENTATION_0);
4170     prepareButtons();
4171     prepareAxes(POSITION);
4172     prepareVirtualKeys();
4173     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4174 
4175     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4176 
4177     NotifyKeyArgs args;
4178 
4179     // Press virtual key.
4180     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4181     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4182     processDown(mapper, x, y);
4183     processSync(mapper);
4184 
4185     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4186     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4187     ASSERT_EQ(DEVICE_ID, args.deviceId);
4188     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4189     ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4190     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4191     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4192     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4193     ASSERT_EQ(KEY_HOME, args.scanCode);
4194     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4195     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4196 
4197     // Release virtual key.
4198     processUp(mapper);
4199     processSync(mapper);
4200 
4201     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4202     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4203     ASSERT_EQ(DEVICE_ID, args.deviceId);
4204     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4205     ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4206     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4207     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4208     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4209     ASSERT_EQ(KEY_HOME, args.scanCode);
4210     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4211     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4212 
4213     // Should not have sent any motions.
4214     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4215 }
4216 
TEST_F(SingleTouchInputMapperTest,Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel)4217 TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
4218     addConfigurationProperty("touch.deviceType", "touchScreen");
4219     prepareDisplay(DISPLAY_ORIENTATION_0);
4220     prepareButtons();
4221     prepareAxes(POSITION);
4222     prepareVirtualKeys();
4223     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4224 
4225     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4226 
4227     NotifyKeyArgs keyArgs;
4228 
4229     // Press virtual key.
4230     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4231     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4232     processDown(mapper, x, y);
4233     processSync(mapper);
4234 
4235     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4236     ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4237     ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4238     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4239     ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4240     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4241     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4242     ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4243     ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4244     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4245     ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4246 
4247     // Move out of bounds.  This should generate a cancel and a pointer down since we moved
4248     // into the display area.
4249     y -= 100;
4250     processMove(mapper, x, y);
4251     processSync(mapper);
4252 
4253     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4254     ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4255     ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4256     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4257     ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4258     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4259     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4260             | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4261     ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4262     ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4263     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4264     ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4265 
4266     NotifyMotionArgs motionArgs;
4267     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4268     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4269     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4270     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4271     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4272     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4273     ASSERT_EQ(0, motionArgs.flags);
4274     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4275     ASSERT_EQ(0, motionArgs.buttonState);
4276     ASSERT_EQ(0, motionArgs.edgeFlags);
4277     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4278     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4279     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4280     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4281             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4282     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4283     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4284     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4285 
4286     // Keep moving out of bounds.  Should generate a pointer move.
4287     y -= 50;
4288     processMove(mapper, x, y);
4289     processSync(mapper);
4290 
4291     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4292     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4293     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4294     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4295     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4296     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4297     ASSERT_EQ(0, motionArgs.flags);
4298     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4299     ASSERT_EQ(0, motionArgs.buttonState);
4300     ASSERT_EQ(0, motionArgs.edgeFlags);
4301     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4302     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4303     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4304     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4305             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4306     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4307     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4308     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4309 
4310     // Release out of bounds.  Should generate a pointer up.
4311     processUp(mapper);
4312     processSync(mapper);
4313 
4314     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4315     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4316     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4317     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4318     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4319     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4320     ASSERT_EQ(0, motionArgs.flags);
4321     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4322     ASSERT_EQ(0, motionArgs.buttonState);
4323     ASSERT_EQ(0, motionArgs.edgeFlags);
4324     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4325     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4326     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4327     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4328             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4329     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4330     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4331     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4332 
4333     // Should not have sent any more keys or motions.
4334     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4335     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4336 }
4337 
TEST_F(SingleTouchInputMapperTest,Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay)4338 TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
4339     addConfigurationProperty("touch.deviceType", "touchScreen");
4340     prepareDisplay(DISPLAY_ORIENTATION_0);
4341     prepareButtons();
4342     prepareAxes(POSITION);
4343     prepareVirtualKeys();
4344     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4345 
4346     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4347 
4348     NotifyMotionArgs motionArgs;
4349 
4350     // Initially go down out of bounds.
4351     int32_t x = -10;
4352     int32_t y = -10;
4353     processDown(mapper, x, y);
4354     processSync(mapper);
4355 
4356     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4357 
4358     // Move into the display area.  Should generate a pointer down.
4359     x = 50;
4360     y = 75;
4361     processMove(mapper, x, y);
4362     processSync(mapper);
4363 
4364     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4365     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4366     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4367     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4368     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4369     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4370     ASSERT_EQ(0, motionArgs.flags);
4371     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4372     ASSERT_EQ(0, motionArgs.buttonState);
4373     ASSERT_EQ(0, motionArgs.edgeFlags);
4374     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4375     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4376     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4377     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4378             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4379     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4380     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4381     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4382 
4383     // Release.  Should generate a pointer up.
4384     processUp(mapper);
4385     processSync(mapper);
4386 
4387     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4388     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4389     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4390     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4391     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4392     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4393     ASSERT_EQ(0, motionArgs.flags);
4394     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4395     ASSERT_EQ(0, motionArgs.buttonState);
4396     ASSERT_EQ(0, motionArgs.edgeFlags);
4397     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4398     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4399     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4400     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4401             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4402     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4403     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4404     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4405 
4406     // Should not have sent any more keys or motions.
4407     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4408     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4409 }
4410 
TEST_F(SingleTouchInputMapperTest,Process_NormalSingleTouchGesture_VirtualDisplay)4411 TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
4412     addConfigurationProperty("touch.deviceType", "touchScreen");
4413     addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4414 
4415     prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
4416     prepareButtons();
4417     prepareAxes(POSITION);
4418     prepareVirtualKeys();
4419     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4420 
4421     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4422 
4423     NotifyMotionArgs motionArgs;
4424 
4425     // Down.
4426     int32_t x = 100;
4427     int32_t y = 125;
4428     processDown(mapper, x, y);
4429     processSync(mapper);
4430 
4431     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4432     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4433     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4434     ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4435     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4436     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4437     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4438     ASSERT_EQ(0, motionArgs.flags);
4439     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4440     ASSERT_EQ(0, motionArgs.buttonState);
4441     ASSERT_EQ(0, motionArgs.edgeFlags);
4442     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4443     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4444     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4445     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4446             toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4447             1, 0, 0, 0, 0, 0, 0, 0));
4448     ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4449     ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4450     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4451 
4452     // Move.
4453     x += 50;
4454     y += 75;
4455     processMove(mapper, x, y);
4456     processSync(mapper);
4457 
4458     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4459     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4460     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4461     ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4462     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4463     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4464     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4465     ASSERT_EQ(0, motionArgs.flags);
4466     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4467     ASSERT_EQ(0, motionArgs.buttonState);
4468     ASSERT_EQ(0, motionArgs.edgeFlags);
4469     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4470     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4471     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4472     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4473             toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4474             1, 0, 0, 0, 0, 0, 0, 0));
4475     ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4476     ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4477     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4478 
4479     // Up.
4480     processUp(mapper);
4481     processSync(mapper);
4482 
4483     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4485     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4486     ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4487     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4488     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4489     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4490     ASSERT_EQ(0, motionArgs.flags);
4491     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4492     ASSERT_EQ(0, motionArgs.buttonState);
4493     ASSERT_EQ(0, motionArgs.edgeFlags);
4494     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4495     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4496     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4497     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4498             toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4499             1, 0, 0, 0, 0, 0, 0, 0));
4500     ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4501     ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4502     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4503 
4504     // Should not have sent any more keys or motions.
4505     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4506     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4507 }
4508 
TEST_F(SingleTouchInputMapperTest,Process_NormalSingleTouchGesture)4509 TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
4510     addConfigurationProperty("touch.deviceType", "touchScreen");
4511     prepareDisplay(DISPLAY_ORIENTATION_0);
4512     prepareButtons();
4513     prepareAxes(POSITION);
4514     prepareVirtualKeys();
4515     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4516 
4517     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4518 
4519     NotifyMotionArgs motionArgs;
4520 
4521     // Down.
4522     int32_t x = 100;
4523     int32_t y = 125;
4524     processDown(mapper, x, y);
4525     processSync(mapper);
4526 
4527     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4528     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4529     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4530     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4531     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4532     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4533     ASSERT_EQ(0, motionArgs.flags);
4534     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4535     ASSERT_EQ(0, motionArgs.buttonState);
4536     ASSERT_EQ(0, motionArgs.edgeFlags);
4537     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4538     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4539     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4540     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4541             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4542     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4543     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4544     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4545 
4546     // Move.
4547     x += 50;
4548     y += 75;
4549     processMove(mapper, x, y);
4550     processSync(mapper);
4551 
4552     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4553     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4554     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4555     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4556     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4557     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4558     ASSERT_EQ(0, motionArgs.flags);
4559     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4560     ASSERT_EQ(0, motionArgs.buttonState);
4561     ASSERT_EQ(0, motionArgs.edgeFlags);
4562     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4563     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4564     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4565     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4566             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4567     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4568     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4569     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4570 
4571     // Up.
4572     processUp(mapper);
4573     processSync(mapper);
4574 
4575     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4576     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4577     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4578     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4579     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4580     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4581     ASSERT_EQ(0, motionArgs.flags);
4582     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4583     ASSERT_EQ(0, motionArgs.buttonState);
4584     ASSERT_EQ(0, motionArgs.edgeFlags);
4585     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4586     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4587     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4588     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4589             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4590     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4591     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4592     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4593 
4594     // Should not have sent any more keys or motions.
4595     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4596     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4597 }
4598 
TEST_F(SingleTouchInputMapperTest,Process_WhenNotOrientationAware_DoesNotRotateMotions)4599 TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
4600     addConfigurationProperty("touch.deviceType", "touchScreen");
4601     prepareButtons();
4602     prepareAxes(POSITION);
4603     addConfigurationProperty("touch.orientationAware", "0");
4604     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4605 
4606     NotifyMotionArgs args;
4607 
4608     // Rotation 90.
4609     prepareDisplay(DISPLAY_ORIENTATION_90);
4610     processDown(mapper, toRawX(50), toRawY(75));
4611     processSync(mapper);
4612 
4613     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4614     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4615     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4616 
4617     processUp(mapper);
4618     processSync(mapper);
4619     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4620 }
4621 
TEST_F(SingleTouchInputMapperTest,Process_WhenOrientationAware_RotatesMotions)4622 TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
4623     addConfigurationProperty("touch.deviceType", "touchScreen");
4624     prepareButtons();
4625     prepareAxes(POSITION);
4626     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4627 
4628     NotifyMotionArgs args;
4629 
4630     // Rotation 0.
4631     clearViewports();
4632     prepareDisplay(DISPLAY_ORIENTATION_0);
4633     processDown(mapper, toRawX(50), toRawY(75));
4634     processSync(mapper);
4635 
4636     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4637     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4638     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4639 
4640     processUp(mapper);
4641     processSync(mapper);
4642     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4643 
4644     // Rotation 90.
4645     clearViewports();
4646     prepareDisplay(DISPLAY_ORIENTATION_90);
4647     processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
4648     processSync(mapper);
4649 
4650     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4651     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4652     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4653 
4654     processUp(mapper);
4655     processSync(mapper);
4656     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4657 
4658     // Rotation 180.
4659     clearViewports();
4660     prepareDisplay(DISPLAY_ORIENTATION_180);
4661     processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4662     processSync(mapper);
4663 
4664     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4665     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4666     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4667 
4668     processUp(mapper);
4669     processSync(mapper);
4670     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4671 
4672     // Rotation 270.
4673     clearViewports();
4674     prepareDisplay(DISPLAY_ORIENTATION_270);
4675     processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
4676     processSync(mapper);
4677 
4678     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4679     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4680     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4681 
4682     processUp(mapper);
4683     processSync(mapper);
4684     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4685 }
4686 
TEST_F(SingleTouchInputMapperTest,Process_AllAxes_DefaultCalibration)4687 TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
4688     addConfigurationProperty("touch.deviceType", "touchScreen");
4689     prepareDisplay(DISPLAY_ORIENTATION_0);
4690     prepareButtons();
4691     prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
4692     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4693 
4694     // These calculations are based on the input device calibration documentation.
4695     int32_t rawX = 100;
4696     int32_t rawY = 200;
4697     int32_t rawPressure = 10;
4698     int32_t rawToolMajor = 12;
4699     int32_t rawDistance = 2;
4700     int32_t rawTiltX = 30;
4701     int32_t rawTiltY = 110;
4702 
4703     float x = toDisplayX(rawX);
4704     float y = toDisplayY(rawY);
4705     float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4706     float size = float(rawToolMajor) / RAW_TOOL_MAX;
4707     float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
4708     float distance = float(rawDistance);
4709 
4710     float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
4711     float tiltScale = M_PI / 180;
4712     float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
4713     float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
4714     float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4715     float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4716 
4717     processDown(mapper, rawX, rawY);
4718     processPressure(mapper, rawPressure);
4719     processToolMajor(mapper, rawToolMajor);
4720     processDistance(mapper, rawDistance);
4721     processTilt(mapper, rawTiltX, rawTiltY);
4722     processSync(mapper);
4723 
4724     NotifyMotionArgs args;
4725     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4726     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4727             x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
4728     ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
4729 }
4730 
TEST_F(SingleTouchInputMapperTest,Process_XYAxes_AffineCalibration)4731 TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
4732     addConfigurationProperty("touch.deviceType", "touchScreen");
4733     prepareDisplay(DISPLAY_ORIENTATION_0);
4734     prepareLocationCalibration();
4735     prepareButtons();
4736     prepareAxes(POSITION);
4737     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4738 
4739     int32_t rawX = 100;
4740     int32_t rawY = 200;
4741 
4742     float x = toDisplayX(toCookedX(rawX, rawY));
4743     float y = toDisplayY(toCookedY(rawX, rawY));
4744 
4745     processDown(mapper, rawX, rawY);
4746     processSync(mapper);
4747 
4748     NotifyMotionArgs args;
4749     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4750     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4751             x, y, 1, 0, 0, 0, 0, 0, 0, 0));
4752 }
4753 
TEST_F(SingleTouchInputMapperTest,Process_ShouldHandleAllButtons)4754 TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4755     addConfigurationProperty("touch.deviceType", "touchScreen");
4756     prepareDisplay(DISPLAY_ORIENTATION_0);
4757     prepareButtons();
4758     prepareAxes(POSITION);
4759     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
4760 
4761     NotifyMotionArgs motionArgs;
4762     NotifyKeyArgs keyArgs;
4763 
4764     processDown(mapper, 100, 200);
4765     processSync(mapper);
4766     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4767     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4768     ASSERT_EQ(0, motionArgs.buttonState);
4769 
4770     // press BTN_LEFT, release BTN_LEFT
4771     processKey(mapper, BTN_LEFT, 1);
4772     processSync(mapper);
4773     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4774     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4775     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4776 
4777     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4778     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4779     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4780 
4781     processKey(mapper, BTN_LEFT, 0);
4782     processSync(mapper);
4783     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4784     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4785     ASSERT_EQ(0, motionArgs.buttonState);
4786 
4787     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4788     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4789     ASSERT_EQ(0, motionArgs.buttonState);
4790 
4791     // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4792     processKey(mapper, BTN_RIGHT, 1);
4793     processKey(mapper, BTN_MIDDLE, 1);
4794     processSync(mapper);
4795     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4796     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4797     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4798             motionArgs.buttonState);
4799 
4800     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4801     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4802     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4803 
4804     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4805     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4806     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4807             motionArgs.buttonState);
4808 
4809     processKey(mapper, BTN_RIGHT, 0);
4810     processSync(mapper);
4811     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4812     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4813     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4814 
4815     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4817     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4818 
4819     processKey(mapper, BTN_MIDDLE, 0);
4820     processSync(mapper);
4821     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4822     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4823     ASSERT_EQ(0, motionArgs.buttonState);
4824 
4825     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4826     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4827     ASSERT_EQ(0, motionArgs.buttonState);
4828 
4829     // press BTN_BACK, release BTN_BACK
4830     processKey(mapper, BTN_BACK, 1);
4831     processSync(mapper);
4832     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4833     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4834     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4835 
4836     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4837     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4838     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4839 
4840     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4841     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4842     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4843 
4844     processKey(mapper, BTN_BACK, 0);
4845     processSync(mapper);
4846     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4847     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4848     ASSERT_EQ(0, motionArgs.buttonState);
4849 
4850     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4851     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4852     ASSERT_EQ(0, motionArgs.buttonState);
4853 
4854     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4855     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4856     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4857 
4858     // press BTN_SIDE, release BTN_SIDE
4859     processKey(mapper, BTN_SIDE, 1);
4860     processSync(mapper);
4861     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4862     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4863     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4864 
4865     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4866     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4867     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4868 
4869     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4870     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4871     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4872 
4873     processKey(mapper, BTN_SIDE, 0);
4874     processSync(mapper);
4875     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4876     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4877     ASSERT_EQ(0, motionArgs.buttonState);
4878 
4879     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4880     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4881     ASSERT_EQ(0, motionArgs.buttonState);
4882 
4883     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4884     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4885     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4886 
4887     // press BTN_FORWARD, release BTN_FORWARD
4888     processKey(mapper, BTN_FORWARD, 1);
4889     processSync(mapper);
4890     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4891     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4892     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4893 
4894     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4895     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4896     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4897 
4898     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4899     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4900     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4901 
4902     processKey(mapper, BTN_FORWARD, 0);
4903     processSync(mapper);
4904     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4905     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4906     ASSERT_EQ(0, motionArgs.buttonState);
4907 
4908     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4909     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4910     ASSERT_EQ(0, motionArgs.buttonState);
4911 
4912     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4913     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4914     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4915 
4916     // press BTN_EXTRA, release BTN_EXTRA
4917     processKey(mapper, BTN_EXTRA, 1);
4918     processSync(mapper);
4919     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4920     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4921     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4922 
4923     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4924     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4925     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4926 
4927     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4928     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4929     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4930 
4931     processKey(mapper, BTN_EXTRA, 0);
4932     processSync(mapper);
4933     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4934     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4935     ASSERT_EQ(0, motionArgs.buttonState);
4936 
4937     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4938     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4939     ASSERT_EQ(0, motionArgs.buttonState);
4940 
4941     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4942     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4943     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4944 
4945     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4946 
4947     // press BTN_STYLUS, release BTN_STYLUS
4948     processKey(mapper, BTN_STYLUS, 1);
4949     processSync(mapper);
4950     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4951     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4952     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4953 
4954     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4955     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4956     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4957 
4958     processKey(mapper, BTN_STYLUS, 0);
4959     processSync(mapper);
4960     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4961     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4962     ASSERT_EQ(0, motionArgs.buttonState);
4963 
4964     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4965     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4966     ASSERT_EQ(0, motionArgs.buttonState);
4967 
4968     // press BTN_STYLUS2, release BTN_STYLUS2
4969     processKey(mapper, BTN_STYLUS2, 1);
4970     processSync(mapper);
4971     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4972     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4973     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4974 
4975     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4976     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4977     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4978 
4979     processKey(mapper, BTN_STYLUS2, 0);
4980     processSync(mapper);
4981     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4982     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4983     ASSERT_EQ(0, motionArgs.buttonState);
4984 
4985     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4986     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4987     ASSERT_EQ(0, motionArgs.buttonState);
4988 
4989     // release touch
4990     processUp(mapper);
4991     processSync(mapper);
4992     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4993     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4994     ASSERT_EQ(0, motionArgs.buttonState);
4995 }
4996 
TEST_F(SingleTouchInputMapperTest,Process_ShouldHandleAllToolTypes)4997 TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4998     addConfigurationProperty("touch.deviceType", "touchScreen");
4999     prepareDisplay(DISPLAY_ORIENTATION_0);
5000     prepareButtons();
5001     prepareAxes(POSITION);
5002     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5003 
5004     NotifyMotionArgs motionArgs;
5005 
5006     // default tool type is finger
5007     processDown(mapper, 100, 200);
5008     processSync(mapper);
5009     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5011     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5012 
5013     // eraser
5014     processKey(mapper, BTN_TOOL_RUBBER, 1);
5015     processSync(mapper);
5016     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5017     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5018     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5019 
5020     // stylus
5021     processKey(mapper, BTN_TOOL_RUBBER, 0);
5022     processKey(mapper, BTN_TOOL_PEN, 1);
5023     processSync(mapper);
5024     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5025     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5026     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5027 
5028     // brush
5029     processKey(mapper, BTN_TOOL_PEN, 0);
5030     processKey(mapper, BTN_TOOL_BRUSH, 1);
5031     processSync(mapper);
5032     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5033     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5034     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5035 
5036     // pencil
5037     processKey(mapper, BTN_TOOL_BRUSH, 0);
5038     processKey(mapper, BTN_TOOL_PENCIL, 1);
5039     processSync(mapper);
5040     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5041     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5042     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5043 
5044     // air-brush
5045     processKey(mapper, BTN_TOOL_PENCIL, 0);
5046     processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5047     processSync(mapper);
5048     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5049     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5050     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5051 
5052     // mouse
5053     processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5054     processKey(mapper, BTN_TOOL_MOUSE, 1);
5055     processSync(mapper);
5056     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5057     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5058     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5059 
5060     // lens
5061     processKey(mapper, BTN_TOOL_MOUSE, 0);
5062     processKey(mapper, BTN_TOOL_LENS, 1);
5063     processSync(mapper);
5064     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5065     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5066     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5067 
5068     // double-tap
5069     processKey(mapper, BTN_TOOL_LENS, 0);
5070     processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5071     processSync(mapper);
5072     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5073     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5074     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5075 
5076     // triple-tap
5077     processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5078     processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5079     processSync(mapper);
5080     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5082     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5083 
5084     // quad-tap
5085     processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5086     processKey(mapper, BTN_TOOL_QUADTAP, 1);
5087     processSync(mapper);
5088     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5089     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5090     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5091 
5092     // finger
5093     processKey(mapper, BTN_TOOL_QUADTAP, 0);
5094     processKey(mapper, BTN_TOOL_FINGER, 1);
5095     processSync(mapper);
5096     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5097     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5098     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5099 
5100     // stylus trumps finger
5101     processKey(mapper, BTN_TOOL_PEN, 1);
5102     processSync(mapper);
5103     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5104     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5105     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5106 
5107     // eraser trumps stylus
5108     processKey(mapper, BTN_TOOL_RUBBER, 1);
5109     processSync(mapper);
5110     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5111     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5112     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5113 
5114     // mouse trumps eraser
5115     processKey(mapper, BTN_TOOL_MOUSE, 1);
5116     processSync(mapper);
5117     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5118     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5119     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5120 
5121     // back to default tool type
5122     processKey(mapper, BTN_TOOL_MOUSE, 0);
5123     processKey(mapper, BTN_TOOL_RUBBER, 0);
5124     processKey(mapper, BTN_TOOL_PEN, 0);
5125     processKey(mapper, BTN_TOOL_FINGER, 0);
5126     processSync(mapper);
5127     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5128     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5129     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5130 }
5131 
TEST_F(SingleTouchInputMapperTest,Process_WhenBtnTouchPresent_HoversIfItsValueIsZero)5132 TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
5133     addConfigurationProperty("touch.deviceType", "touchScreen");
5134     prepareDisplay(DISPLAY_ORIENTATION_0);
5135     prepareButtons();
5136     prepareAxes(POSITION);
5137     mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
5138     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5139 
5140     NotifyMotionArgs motionArgs;
5141 
5142     // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5143     processKey(mapper, BTN_TOOL_FINGER, 1);
5144     processMove(mapper, 100, 200);
5145     processSync(mapper);
5146     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5147     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5148     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5149             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5150 
5151     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5152     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5153     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5154             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5155 
5156     // move a little
5157     processMove(mapper, 150, 250);
5158     processSync(mapper);
5159     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5160     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5161     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5162             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5163 
5164     // down when BTN_TOUCH is pressed, pressure defaults to 1
5165     processKey(mapper, BTN_TOUCH, 1);
5166     processSync(mapper);
5167     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5168     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5169     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5170             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5171 
5172     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5173     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5174     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5175             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5176 
5177     // up when BTN_TOUCH is released, hover restored
5178     processKey(mapper, BTN_TOUCH, 0);
5179     processSync(mapper);
5180     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5182     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5183             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5184 
5185     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5186     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5187     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5188             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5189 
5190     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5191     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5192     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5193             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5194 
5195     // exit hover when pointer goes away
5196     processKey(mapper, BTN_TOOL_FINGER, 0);
5197     processSync(mapper);
5198     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5199     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5200     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5201             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5202 }
5203 
TEST_F(SingleTouchInputMapperTest,Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero)5204 TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
5205     addConfigurationProperty("touch.deviceType", "touchScreen");
5206     prepareDisplay(DISPLAY_ORIENTATION_0);
5207     prepareButtons();
5208     prepareAxes(POSITION | PRESSURE);
5209     SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5210 
5211     NotifyMotionArgs motionArgs;
5212 
5213     // initially hovering because pressure is 0
5214     processDown(mapper, 100, 200);
5215     processPressure(mapper, 0);
5216     processSync(mapper);
5217     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5218     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5219     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5220             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5221 
5222     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5223     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5224     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5225             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5226 
5227     // move a little
5228     processMove(mapper, 150, 250);
5229     processSync(mapper);
5230     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5231     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5232     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5233             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5234 
5235     // down when pressure is non-zero
5236     processPressure(mapper, RAW_PRESSURE_MAX);
5237     processSync(mapper);
5238     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5239     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5240     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5241             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5242 
5243     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5244     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5245     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5246             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5247 
5248     // up when pressure becomes 0, hover restored
5249     processPressure(mapper, 0);
5250     processSync(mapper);
5251     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5253     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5254             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5255 
5256     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5257     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5258     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5259             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5260 
5261     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5262     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5263     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5264             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5265 
5266     // exit hover when pointer goes away
5267     processUp(mapper);
5268     processSync(mapper);
5269     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5270     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5271     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5272             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5273 }
5274 
5275 // --- MultiTouchInputMapperTest ---
5276 
5277 class MultiTouchInputMapperTest : public TouchInputMapperTest {
5278 protected:
5279     void prepareAxes(int axes);
5280 
5281     void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
5282     void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
5283     void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
5284     void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
5285     void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
5286     void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
5287     void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
5288     void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
5289     void processId(MultiTouchInputMapper& mapper, int32_t id);
5290     void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
5291     void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
5292     void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
5293     void processMTSync(MultiTouchInputMapper& mapper);
5294     void processSync(MultiTouchInputMapper& mapper);
5295 };
5296 
prepareAxes(int axes)5297 void MultiTouchInputMapperTest::prepareAxes(int axes) {
5298     if (axes & POSITION) {
5299         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5300         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
5301     }
5302     if (axes & TOUCH) {
5303         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
5304                                        RAW_TOUCH_MAX, 0, 0);
5305         if (axes & MINOR) {
5306             mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
5307                                            RAW_TOUCH_MAX, 0, 0);
5308         }
5309     }
5310     if (axes & TOOL) {
5311         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
5312                                        0, 0);
5313         if (axes & MINOR) {
5314             mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
5315                                            RAW_TOOL_MAX, 0, 0);
5316         }
5317     }
5318     if (axes & ORIENTATION) {
5319         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
5320                                        RAW_ORIENTATION_MAX, 0, 0);
5321     }
5322     if (axes & PRESSURE) {
5323         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
5324                                        RAW_PRESSURE_MAX, 0, 0);
5325     }
5326     if (axes & DISTANCE) {
5327         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
5328                                        RAW_DISTANCE_MAX, 0, 0);
5329     }
5330     if (axes & ID) {
5331         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
5332                                        0);
5333     }
5334     if (axes & SLOT) {
5335         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
5336         mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
5337     }
5338     if (axes & TOOL_TYPE) {
5339         mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
5340     }
5341 }
5342 
processPosition(MultiTouchInputMapper & mapper,int32_t x,int32_t y)5343 void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
5344                                                 int32_t y) {
5345     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_X, x);
5346     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
5347 }
5348 
processTouchMajor(MultiTouchInputMapper & mapper,int32_t touchMajor)5349 void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
5350                                                   int32_t touchMajor) {
5351     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
5352 }
5353 
processTouchMinor(MultiTouchInputMapper & mapper,int32_t touchMinor)5354 void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
5355                                                   int32_t touchMinor) {
5356     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
5357 }
5358 
processToolMajor(MultiTouchInputMapper & mapper,int32_t toolMajor)5359 void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
5360     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
5361 }
5362 
processToolMinor(MultiTouchInputMapper & mapper,int32_t toolMinor)5363 void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
5364     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
5365 }
5366 
processOrientation(MultiTouchInputMapper & mapper,int32_t orientation)5367 void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
5368                                                    int32_t orientation) {
5369     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
5370 }
5371 
processPressure(MultiTouchInputMapper & mapper,int32_t pressure)5372 void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
5373     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
5374 }
5375 
processDistance(MultiTouchInputMapper & mapper,int32_t distance)5376 void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
5377     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
5378 }
5379 
processId(MultiTouchInputMapper & mapper,int32_t id)5380 void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
5381     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
5382 }
5383 
processSlot(MultiTouchInputMapper & mapper,int32_t slot)5384 void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
5385     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_SLOT, slot);
5386 }
5387 
processToolType(MultiTouchInputMapper & mapper,int32_t toolType)5388 void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
5389     process(mapper, ARBITRARY_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
5390 }
5391 
processKey(MultiTouchInputMapper & mapper,int32_t code,int32_t value)5392 void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
5393                                            int32_t value) {
5394     process(mapper, ARBITRARY_TIME, EV_KEY, code, value);
5395 }
5396 
processMTSync(MultiTouchInputMapper & mapper)5397 void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
5398     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_MT_REPORT, 0);
5399 }
5400 
processSync(MultiTouchInputMapper & mapper)5401 void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
5402     process(mapper, ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
5403 }
5404 
TEST_F(MultiTouchInputMapperTest,Process_NormalMultiTouchGesture_WithoutTrackingIds)5405 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
5406     addConfigurationProperty("touch.deviceType", "touchScreen");
5407     prepareDisplay(DISPLAY_ORIENTATION_0);
5408     prepareAxes(POSITION);
5409     prepareVirtualKeys();
5410     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
5411 
5412     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5413 
5414     NotifyMotionArgs motionArgs;
5415 
5416     // Two fingers down at once.
5417     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5418     processPosition(mapper, x1, y1);
5419     processMTSync(mapper);
5420     processPosition(mapper, x2, y2);
5421     processMTSync(mapper);
5422     processSync(mapper);
5423 
5424     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5425     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5426     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5427     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5428     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5429     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5430     ASSERT_EQ(0, motionArgs.flags);
5431     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5432     ASSERT_EQ(0, motionArgs.buttonState);
5433     ASSERT_EQ(0, motionArgs.edgeFlags);
5434     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5435     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5436     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5437     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5438             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5439     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5440     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5441     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5442 
5443     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5444     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5445     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5446     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5447     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5448     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5449             motionArgs.action);
5450     ASSERT_EQ(0, motionArgs.flags);
5451     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5452     ASSERT_EQ(0, motionArgs.buttonState);
5453     ASSERT_EQ(0, motionArgs.edgeFlags);
5454     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5455     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5456     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5457     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5458     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5459     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5460             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5461     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5462             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5463     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5464     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5465     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5466 
5467     // Move.
5468     x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5469     processPosition(mapper, x1, y1);
5470     processMTSync(mapper);
5471     processPosition(mapper, x2, y2);
5472     processMTSync(mapper);
5473     processSync(mapper);
5474 
5475     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5476     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5477     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5478     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5479     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5480     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5481     ASSERT_EQ(0, motionArgs.flags);
5482     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5483     ASSERT_EQ(0, motionArgs.buttonState);
5484     ASSERT_EQ(0, motionArgs.edgeFlags);
5485     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5486     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5487     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5488     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5489     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5490     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5491             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5492     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5493             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5494     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5495     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5496     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5497 
5498     // First finger up.
5499     x2 += 15; y2 -= 20;
5500     processPosition(mapper, x2, y2);
5501     processMTSync(mapper);
5502     processSync(mapper);
5503 
5504     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5505     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5506     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5507     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5508     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5509     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5510             motionArgs.action);
5511     ASSERT_EQ(0, motionArgs.flags);
5512     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5513     ASSERT_EQ(0, motionArgs.buttonState);
5514     ASSERT_EQ(0, motionArgs.edgeFlags);
5515     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5516     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5517     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5518     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5519     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5520     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5521             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5522     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5523             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5524     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5525     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5526     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5527 
5528     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5529     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5530     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5531     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5532     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5533     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5534     ASSERT_EQ(0, motionArgs.flags);
5535     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5536     ASSERT_EQ(0, motionArgs.buttonState);
5537     ASSERT_EQ(0, motionArgs.edgeFlags);
5538     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5539     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5540     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5541     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5542             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5543     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5544     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5545     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5546 
5547     // Move.
5548     x2 += 20; y2 -= 25;
5549     processPosition(mapper, x2, y2);
5550     processMTSync(mapper);
5551     processSync(mapper);
5552 
5553     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5554     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5555     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5556     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5557     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5558     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5559     ASSERT_EQ(0, motionArgs.flags);
5560     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5561     ASSERT_EQ(0, motionArgs.buttonState);
5562     ASSERT_EQ(0, motionArgs.edgeFlags);
5563     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5564     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5565     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5566     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5567             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5568     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5569     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5570     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5571 
5572     // New finger down.
5573     int32_t x3 = 700, y3 = 300;
5574     processPosition(mapper, x2, y2);
5575     processMTSync(mapper);
5576     processPosition(mapper, x3, y3);
5577     processMTSync(mapper);
5578     processSync(mapper);
5579 
5580     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5581     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5582     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5583     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5584     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5585     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5586             motionArgs.action);
5587     ASSERT_EQ(0, motionArgs.flags);
5588     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5589     ASSERT_EQ(0, motionArgs.buttonState);
5590     ASSERT_EQ(0, motionArgs.edgeFlags);
5591     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5592     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5593     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5594     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5595     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5596     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5597             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5598     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5599             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5600     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5601     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5602     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5603 
5604     // Second finger up.
5605     x3 += 30; y3 -= 20;
5606     processPosition(mapper, x3, y3);
5607     processMTSync(mapper);
5608     processSync(mapper);
5609 
5610     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5611     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5612     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5613     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5614     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5615     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5616             motionArgs.action);
5617     ASSERT_EQ(0, motionArgs.flags);
5618     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5619     ASSERT_EQ(0, motionArgs.buttonState);
5620     ASSERT_EQ(0, motionArgs.edgeFlags);
5621     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5622     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5623     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5624     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5625     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5626     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5627             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5628     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5629             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5630     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5631     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5632     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5633 
5634     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5635     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5636     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5637     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5638     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5639     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5640     ASSERT_EQ(0, motionArgs.flags);
5641     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5642     ASSERT_EQ(0, motionArgs.buttonState);
5643     ASSERT_EQ(0, motionArgs.edgeFlags);
5644     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5645     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5646     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5647     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5648             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5649     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5650     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5651     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5652 
5653     // Last finger up.
5654     processMTSync(mapper);
5655     processSync(mapper);
5656 
5657     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5658     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5659     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5660     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5661     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5662     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5663     ASSERT_EQ(0, motionArgs.flags);
5664     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5665     ASSERT_EQ(0, motionArgs.buttonState);
5666     ASSERT_EQ(0, motionArgs.edgeFlags);
5667     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5668     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5669     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5670     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5671             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5672     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5673     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5674     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5675 
5676     // Should not have sent any more keys or motions.
5677     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5678     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5679 }
5680 
TEST_F(MultiTouchInputMapperTest,Process_NormalMultiTouchGesture_WithTrackingIds)5681 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
5682     addConfigurationProperty("touch.deviceType", "touchScreen");
5683     prepareDisplay(DISPLAY_ORIENTATION_0);
5684     prepareAxes(POSITION | ID);
5685     prepareVirtualKeys();
5686     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
5687 
5688     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5689 
5690     NotifyMotionArgs motionArgs;
5691 
5692     // Two fingers down at once.
5693     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5694     processPosition(mapper, x1, y1);
5695     processId(mapper, 1);
5696     processMTSync(mapper);
5697     processPosition(mapper, x2, y2);
5698     processId(mapper, 2);
5699     processMTSync(mapper);
5700     processSync(mapper);
5701 
5702     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5703     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5704     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5705     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5706     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5707     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5708             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5709 
5710     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5712             motionArgs.action);
5713     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5714     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5715     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5716     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5717     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5718     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5719             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5720     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5721             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5722 
5723     // Move.
5724     x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5725     processPosition(mapper, x1, y1);
5726     processId(mapper, 1);
5727     processMTSync(mapper);
5728     processPosition(mapper, x2, y2);
5729     processId(mapper, 2);
5730     processMTSync(mapper);
5731     processSync(mapper);
5732 
5733     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5735     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5736     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5737     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5738     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5739     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5740     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5741             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5742     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5743             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5744 
5745     // First finger up.
5746     x2 += 15; y2 -= 20;
5747     processPosition(mapper, x2, y2);
5748     processId(mapper, 2);
5749     processMTSync(mapper);
5750     processSync(mapper);
5751 
5752     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5753     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5754             motionArgs.action);
5755     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5756     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5757     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5758     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5759     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5760     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5761             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5762     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5763             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5764 
5765     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5766     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5767     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5768     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5769     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5770     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5771             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5772 
5773     // Move.
5774     x2 += 20; y2 -= 25;
5775     processPosition(mapper, x2, y2);
5776     processId(mapper, 2);
5777     processMTSync(mapper);
5778     processSync(mapper);
5779 
5780     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5781     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5782     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5783     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5784     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5785     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5786             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5787 
5788     // New finger down.
5789     int32_t x3 = 700, y3 = 300;
5790     processPosition(mapper, x2, y2);
5791     processId(mapper, 2);
5792     processMTSync(mapper);
5793     processPosition(mapper, x3, y3);
5794     processId(mapper, 3);
5795     processMTSync(mapper);
5796     processSync(mapper);
5797 
5798     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5799     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5800             motionArgs.action);
5801     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5802     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5803     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5804     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5805     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5806     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5807             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5808     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5809             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5810 
5811     // Second finger up.
5812     x3 += 30; y3 -= 20;
5813     processPosition(mapper, x3, y3);
5814     processId(mapper, 3);
5815     processMTSync(mapper);
5816     processSync(mapper);
5817 
5818     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5819     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5820             motionArgs.action);
5821     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5822     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5823     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5824     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5825     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5826     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5827             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5828     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5829             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5830 
5831     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5832     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5833     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5834     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5835     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5836     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5837             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5838 
5839     // Last finger up.
5840     processMTSync(mapper);
5841     processSync(mapper);
5842 
5843     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5844     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5845     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5846     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5847     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5848     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5849             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5850 
5851     // Should not have sent any more keys or motions.
5852     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5853     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5854 }
5855 
TEST_F(MultiTouchInputMapperTest,Process_NormalMultiTouchGesture_WithSlots)5856 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
5857     addConfigurationProperty("touch.deviceType", "touchScreen");
5858     prepareDisplay(DISPLAY_ORIENTATION_0);
5859     prepareAxes(POSITION | ID | SLOT);
5860     prepareVirtualKeys();
5861     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
5862 
5863     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
5864 
5865     NotifyMotionArgs motionArgs;
5866 
5867     // Two fingers down at once.
5868     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
5869     processPosition(mapper, x1, y1);
5870     processId(mapper, 1);
5871     processSlot(mapper, 1);
5872     processPosition(mapper, x2, y2);
5873     processId(mapper, 2);
5874     processSync(mapper);
5875 
5876     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5877     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5878     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5879     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5880     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5881     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5882             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5883 
5884     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5885     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5886             motionArgs.action);
5887     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5888     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5889     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5890     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5891     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5892     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5893             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5894     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5895             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5896 
5897     // Move.
5898     x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5899     processSlot(mapper, 0);
5900     processPosition(mapper, x1, y1);
5901     processSlot(mapper, 1);
5902     processPosition(mapper, x2, y2);
5903     processSync(mapper);
5904 
5905     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5907     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5908     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5909     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5910     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5911     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5912     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5913             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5914     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5915             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5916 
5917     // First finger up.
5918     x2 += 15; y2 -= 20;
5919     processSlot(mapper, 0);
5920     processId(mapper, -1);
5921     processSlot(mapper, 1);
5922     processPosition(mapper, x2, y2);
5923     processSync(mapper);
5924 
5925     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5927             motionArgs.action);
5928     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5929     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5930     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5931     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5932     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5933     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5934             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5935     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5936             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5937 
5938     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5940     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5941     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5942     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5943     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5944             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5945 
5946     // Move.
5947     x2 += 20; y2 -= 25;
5948     processPosition(mapper, x2, y2);
5949     processSync(mapper);
5950 
5951     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5953     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5954     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5955     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5956     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5957             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5958 
5959     // New finger down.
5960     int32_t x3 = 700, y3 = 300;
5961     processPosition(mapper, x2, y2);
5962     processSlot(mapper, 0);
5963     processId(mapper, 3);
5964     processPosition(mapper, x3, y3);
5965     processSync(mapper);
5966 
5967     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5969             motionArgs.action);
5970     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5971     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5972     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5973     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5974     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5975     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5976             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5977     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5978             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5979 
5980     // Second finger up.
5981     x3 += 30; y3 -= 20;
5982     processSlot(mapper, 1);
5983     processId(mapper, -1);
5984     processSlot(mapper, 0);
5985     processPosition(mapper, x3, y3);
5986     processSync(mapper);
5987 
5988     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5990             motionArgs.action);
5991     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5992     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5993     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5994     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5995     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5996     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5998     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5999             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6000 
6001     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6003     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6004     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6005     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6006     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6007             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6008 
6009     // Last finger up.
6010     processId(mapper, -1);
6011     processSync(mapper);
6012 
6013     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6015     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6016     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6017     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6018     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6019             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6020 
6021     // Should not have sent any more keys or motions.
6022     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6023     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6024 }
6025 
TEST_F(MultiTouchInputMapperTest,Process_AllAxes_WithDefaultCalibration)6026 TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
6027     addConfigurationProperty("touch.deviceType", "touchScreen");
6028     prepareDisplay(DISPLAY_ORIENTATION_0);
6029     prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
6030     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6031 
6032     // These calculations are based on the input device calibration documentation.
6033     int32_t rawX = 100;
6034     int32_t rawY = 200;
6035     int32_t rawTouchMajor = 7;
6036     int32_t rawTouchMinor = 6;
6037     int32_t rawToolMajor = 9;
6038     int32_t rawToolMinor = 8;
6039     int32_t rawPressure = 11;
6040     int32_t rawDistance = 0;
6041     int32_t rawOrientation = 3;
6042     int32_t id = 5;
6043 
6044     float x = toDisplayX(rawX);
6045     float y = toDisplayY(rawY);
6046     float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6047     float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6048     float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6049     float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6050     float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6051     float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6052     float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6053     float distance = float(rawDistance);
6054 
6055     processPosition(mapper, rawX, rawY);
6056     processTouchMajor(mapper, rawTouchMajor);
6057     processTouchMinor(mapper, rawTouchMinor);
6058     processToolMajor(mapper, rawToolMajor);
6059     processToolMinor(mapper, rawToolMinor);
6060     processPressure(mapper, rawPressure);
6061     processOrientation(mapper, rawOrientation);
6062     processDistance(mapper, rawDistance);
6063     processId(mapper, id);
6064     processMTSync(mapper);
6065     processSync(mapper);
6066 
6067     NotifyMotionArgs args;
6068     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6069     ASSERT_EQ(0, args.pointerProperties[0].id);
6070     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6071             x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6072             orientation, distance));
6073 }
6074 
TEST_F(MultiTouchInputMapperTest,Process_TouchAndToolAxes_GeometricCalibration)6075 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
6076     addConfigurationProperty("touch.deviceType", "touchScreen");
6077     prepareDisplay(DISPLAY_ORIENTATION_0);
6078     prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6079     addConfigurationProperty("touch.size.calibration", "geometric");
6080     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6081 
6082     // These calculations are based on the input device calibration documentation.
6083     int32_t rawX = 100;
6084     int32_t rawY = 200;
6085     int32_t rawTouchMajor = 140;
6086     int32_t rawTouchMinor = 120;
6087     int32_t rawToolMajor = 180;
6088     int32_t rawToolMinor = 160;
6089 
6090     float x = toDisplayX(rawX);
6091     float y = toDisplayY(rawY);
6092     float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6093     float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6094     float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6095     float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6096     float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6097 
6098     processPosition(mapper, rawX, rawY);
6099     processTouchMajor(mapper, rawTouchMajor);
6100     processTouchMinor(mapper, rawTouchMinor);
6101     processToolMajor(mapper, rawToolMajor);
6102     processToolMinor(mapper, rawToolMinor);
6103     processMTSync(mapper);
6104     processSync(mapper);
6105 
6106     NotifyMotionArgs args;
6107     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6108     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6109             x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6110 }
6111 
TEST_F(MultiTouchInputMapperTest,Process_TouchAndToolAxes_SummedLinearCalibration)6112 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
6113     addConfigurationProperty("touch.deviceType", "touchScreen");
6114     prepareDisplay(DISPLAY_ORIENTATION_0);
6115     prepareAxes(POSITION | TOUCH | TOOL);
6116     addConfigurationProperty("touch.size.calibration", "diameter");
6117     addConfigurationProperty("touch.size.scale", "10");
6118     addConfigurationProperty("touch.size.bias", "160");
6119     addConfigurationProperty("touch.size.isSummed", "1");
6120     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6121 
6122     // These calculations are based on the input device calibration documentation.
6123     // Note: We only provide a single common touch/tool value because the device is assumed
6124     //       not to emit separate values for each pointer (isSummed = 1).
6125     int32_t rawX = 100;
6126     int32_t rawY = 200;
6127     int32_t rawX2 = 150;
6128     int32_t rawY2 = 250;
6129     int32_t rawTouchMajor = 5;
6130     int32_t rawToolMajor = 8;
6131 
6132     float x = toDisplayX(rawX);
6133     float y = toDisplayY(rawY);
6134     float x2 = toDisplayX(rawX2);
6135     float y2 = toDisplayY(rawY2);
6136     float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6137     float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6138     float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6139 
6140     processPosition(mapper, rawX, rawY);
6141     processTouchMajor(mapper, rawTouchMajor);
6142     processToolMajor(mapper, rawToolMajor);
6143     processMTSync(mapper);
6144     processPosition(mapper, rawX2, rawY2);
6145     processTouchMajor(mapper, rawTouchMajor);
6146     processToolMajor(mapper, rawToolMajor);
6147     processMTSync(mapper);
6148     processSync(mapper);
6149 
6150     NotifyMotionArgs args;
6151     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6152     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6153 
6154     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6155     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6156             args.action);
6157     ASSERT_EQ(size_t(2), args.pointerCount);
6158     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6159             x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6160     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6161             x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6162 }
6163 
TEST_F(MultiTouchInputMapperTest,Process_TouchAndToolAxes_AreaCalibration)6164 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
6165     addConfigurationProperty("touch.deviceType", "touchScreen");
6166     prepareDisplay(DISPLAY_ORIENTATION_0);
6167     prepareAxes(POSITION | TOUCH | TOOL);
6168     addConfigurationProperty("touch.size.calibration", "area");
6169     addConfigurationProperty("touch.size.scale", "43");
6170     addConfigurationProperty("touch.size.bias", "3");
6171     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6172 
6173     // These calculations are based on the input device calibration documentation.
6174     int32_t rawX = 100;
6175     int32_t rawY = 200;
6176     int32_t rawTouchMajor = 5;
6177     int32_t rawToolMajor = 8;
6178 
6179     float x = toDisplayX(rawX);
6180     float y = toDisplayY(rawY);
6181     float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6182     float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6183     float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6184 
6185     processPosition(mapper, rawX, rawY);
6186     processTouchMajor(mapper, rawTouchMajor);
6187     processToolMajor(mapper, rawToolMajor);
6188     processMTSync(mapper);
6189     processSync(mapper);
6190 
6191     NotifyMotionArgs args;
6192     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6193     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6194             x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6195 }
6196 
TEST_F(MultiTouchInputMapperTest,Process_PressureAxis_AmplitudeCalibration)6197 TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
6198     addConfigurationProperty("touch.deviceType", "touchScreen");
6199     prepareDisplay(DISPLAY_ORIENTATION_0);
6200     prepareAxes(POSITION | PRESSURE);
6201     addConfigurationProperty("touch.pressure.calibration", "amplitude");
6202     addConfigurationProperty("touch.pressure.scale", "0.01");
6203     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6204 
6205     InputDeviceInfo info;
6206     mapper.populateDeviceInfo(&info);
6207     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
6208             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
6209             0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
6210 
6211     // These calculations are based on the input device calibration documentation.
6212     int32_t rawX = 100;
6213     int32_t rawY = 200;
6214     int32_t rawPressure = 60;
6215 
6216     float x = toDisplayX(rawX);
6217     float y = toDisplayY(rawY);
6218     float pressure = float(rawPressure) * 0.01f;
6219 
6220     processPosition(mapper, rawX, rawY);
6221     processPressure(mapper, rawPressure);
6222     processMTSync(mapper);
6223     processSync(mapper);
6224 
6225     NotifyMotionArgs args;
6226     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6227     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6228             x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
6229 }
6230 
TEST_F(MultiTouchInputMapperTest,Process_ShouldHandleAllButtons)6231 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
6232     addConfigurationProperty("touch.deviceType", "touchScreen");
6233     prepareDisplay(DISPLAY_ORIENTATION_0);
6234     prepareAxes(POSITION | ID | SLOT);
6235     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6236 
6237     NotifyMotionArgs motionArgs;
6238     NotifyKeyArgs keyArgs;
6239 
6240     processId(mapper, 1);
6241     processPosition(mapper, 100, 200);
6242     processSync(mapper);
6243     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6245     ASSERT_EQ(0, motionArgs.buttonState);
6246 
6247     // press BTN_LEFT, release BTN_LEFT
6248     processKey(mapper, BTN_LEFT, 1);
6249     processSync(mapper);
6250     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6251     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6252     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6253 
6254     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6255     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6256     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6257 
6258     processKey(mapper, BTN_LEFT, 0);
6259     processSync(mapper);
6260     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
6262     ASSERT_EQ(0, motionArgs.buttonState);
6263 
6264     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6266     ASSERT_EQ(0, motionArgs.buttonState);
6267 
6268     // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6269     processKey(mapper, BTN_RIGHT, 1);
6270     processKey(mapper, BTN_MIDDLE, 1);
6271     processSync(mapper);
6272     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6274     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6275             motionArgs.buttonState);
6276 
6277     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6279     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6280 
6281     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6282     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6283     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6284             motionArgs.buttonState);
6285 
6286     processKey(mapper, BTN_RIGHT, 0);
6287     processSync(mapper);
6288     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
6290     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6291 
6292     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6294     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6295 
6296     processKey(mapper, BTN_MIDDLE, 0);
6297     processSync(mapper);
6298     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6299     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
6300     ASSERT_EQ(0, motionArgs.buttonState);
6301 
6302     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6304     ASSERT_EQ(0, motionArgs.buttonState);
6305 
6306     // press BTN_BACK, release BTN_BACK
6307     processKey(mapper, BTN_BACK, 1);
6308     processSync(mapper);
6309     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6310     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6311     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6312 
6313     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6315     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6316 
6317     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6319     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6320 
6321     processKey(mapper, BTN_BACK, 0);
6322     processSync(mapper);
6323     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6324     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
6325     ASSERT_EQ(0, motionArgs.buttonState);
6326 
6327     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6329     ASSERT_EQ(0, motionArgs.buttonState);
6330 
6331     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6332     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6333     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6334 
6335     // press BTN_SIDE, release BTN_SIDE
6336     processKey(mapper, BTN_SIDE, 1);
6337     processSync(mapper);
6338     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6339     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6340     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6341 
6342     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6344     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6345 
6346     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6348     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6349 
6350     processKey(mapper, BTN_SIDE, 0);
6351     processSync(mapper);
6352     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
6354     ASSERT_EQ(0, motionArgs.buttonState);
6355 
6356     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6358     ASSERT_EQ(0, motionArgs.buttonState);
6359 
6360     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6361     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6362     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6363 
6364     // press BTN_FORWARD, release BTN_FORWARD
6365     processKey(mapper, BTN_FORWARD, 1);
6366     processSync(mapper);
6367     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6368     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6369     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6370 
6371     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6372     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6373     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6374 
6375     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6376     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6377     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6378 
6379     processKey(mapper, BTN_FORWARD, 0);
6380     processSync(mapper);
6381     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
6383     ASSERT_EQ(0, motionArgs.buttonState);
6384 
6385     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6386     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6387     ASSERT_EQ(0, motionArgs.buttonState);
6388 
6389     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6390     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6391     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6392 
6393     // press BTN_EXTRA, release BTN_EXTRA
6394     processKey(mapper, BTN_EXTRA, 1);
6395     processSync(mapper);
6396     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6397     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6398     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6399 
6400     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6402     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6403 
6404     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6406     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6407 
6408     processKey(mapper, BTN_EXTRA, 0);
6409     processSync(mapper);
6410     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
6412     ASSERT_EQ(0, motionArgs.buttonState);
6413 
6414     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6415     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6416     ASSERT_EQ(0, motionArgs.buttonState);
6417 
6418     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6419     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6420     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6421 
6422     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6423 
6424     // press BTN_STYLUS, release BTN_STYLUS
6425     processKey(mapper, BTN_STYLUS, 1);
6426     processSync(mapper);
6427     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6428     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6429     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6430 
6431     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6433     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6434 
6435     processKey(mapper, BTN_STYLUS, 0);
6436     processSync(mapper);
6437     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
6439     ASSERT_EQ(0, motionArgs.buttonState);
6440 
6441     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6442     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6443     ASSERT_EQ(0, motionArgs.buttonState);
6444 
6445     // press BTN_STYLUS2, release BTN_STYLUS2
6446     processKey(mapper, BTN_STYLUS2, 1);
6447     processSync(mapper);
6448     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6450     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6451 
6452     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6454     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6455 
6456     processKey(mapper, BTN_STYLUS2, 0);
6457     processSync(mapper);
6458     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6459     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
6460     ASSERT_EQ(0, motionArgs.buttonState);
6461 
6462     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6463     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6464     ASSERT_EQ(0, motionArgs.buttonState);
6465 
6466     // release touch
6467     processId(mapper, -1);
6468     processSync(mapper);
6469     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6471     ASSERT_EQ(0, motionArgs.buttonState);
6472 }
6473 
TEST_F(MultiTouchInputMapperTest,Process_ShouldHandleAllToolTypes)6474 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
6475     addConfigurationProperty("touch.deviceType", "touchScreen");
6476     prepareDisplay(DISPLAY_ORIENTATION_0);
6477     prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
6478     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6479 
6480     NotifyMotionArgs motionArgs;
6481 
6482     // default tool type is finger
6483     processId(mapper, 1);
6484     processPosition(mapper, 100, 200);
6485     processSync(mapper);
6486     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6488     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6489 
6490     // eraser
6491     processKey(mapper, BTN_TOOL_RUBBER, 1);
6492     processSync(mapper);
6493     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6495     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6496 
6497     // stylus
6498     processKey(mapper, BTN_TOOL_RUBBER, 0);
6499     processKey(mapper, BTN_TOOL_PEN, 1);
6500     processSync(mapper);
6501     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6503     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6504 
6505     // brush
6506     processKey(mapper, BTN_TOOL_PEN, 0);
6507     processKey(mapper, BTN_TOOL_BRUSH, 1);
6508     processSync(mapper);
6509     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6511     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6512 
6513     // pencil
6514     processKey(mapper, BTN_TOOL_BRUSH, 0);
6515     processKey(mapper, BTN_TOOL_PENCIL, 1);
6516     processSync(mapper);
6517     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6519     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6520 
6521     // air-brush
6522     processKey(mapper, BTN_TOOL_PENCIL, 0);
6523     processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6524     processSync(mapper);
6525     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6527     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6528 
6529     // mouse
6530     processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6531     processKey(mapper, BTN_TOOL_MOUSE, 1);
6532     processSync(mapper);
6533     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6535     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6536 
6537     // lens
6538     processKey(mapper, BTN_TOOL_MOUSE, 0);
6539     processKey(mapper, BTN_TOOL_LENS, 1);
6540     processSync(mapper);
6541     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6543     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6544 
6545     // double-tap
6546     processKey(mapper, BTN_TOOL_LENS, 0);
6547     processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6548     processSync(mapper);
6549     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6551     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6552 
6553     // triple-tap
6554     processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6555     processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6556     processSync(mapper);
6557     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6559     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6560 
6561     // quad-tap
6562     processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6563     processKey(mapper, BTN_TOOL_QUADTAP, 1);
6564     processSync(mapper);
6565     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6567     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6568 
6569     // finger
6570     processKey(mapper, BTN_TOOL_QUADTAP, 0);
6571     processKey(mapper, BTN_TOOL_FINGER, 1);
6572     processSync(mapper);
6573     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6575     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6576 
6577     // stylus trumps finger
6578     processKey(mapper, BTN_TOOL_PEN, 1);
6579     processSync(mapper);
6580     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6582     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6583 
6584     // eraser trumps stylus
6585     processKey(mapper, BTN_TOOL_RUBBER, 1);
6586     processSync(mapper);
6587     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6589     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6590 
6591     // mouse trumps eraser
6592     processKey(mapper, BTN_TOOL_MOUSE, 1);
6593     processSync(mapper);
6594     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6595     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6596     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6597 
6598     // MT tool type trumps BTN tool types: MT_TOOL_FINGER
6599     processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
6600     processSync(mapper);
6601     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6603     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6604 
6605     // MT tool type trumps BTN tool types: MT_TOOL_PEN
6606     processToolType(mapper, MT_TOOL_PEN);
6607     processSync(mapper);
6608     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6610     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6611 
6612     // back to default tool type
6613     processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
6614     processKey(mapper, BTN_TOOL_MOUSE, 0);
6615     processKey(mapper, BTN_TOOL_RUBBER, 0);
6616     processKey(mapper, BTN_TOOL_PEN, 0);
6617     processKey(mapper, BTN_TOOL_FINGER, 0);
6618     processSync(mapper);
6619     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6621     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6622 }
6623 
TEST_F(MultiTouchInputMapperTest,Process_WhenBtnTouchPresent_HoversIfItsValueIsZero)6624 TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
6625     addConfigurationProperty("touch.deviceType", "touchScreen");
6626     prepareDisplay(DISPLAY_ORIENTATION_0);
6627     prepareAxes(POSITION | ID | SLOT);
6628     mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
6629     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6630 
6631     NotifyMotionArgs motionArgs;
6632 
6633     // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6634     processId(mapper, 1);
6635     processPosition(mapper, 100, 200);
6636     processSync(mapper);
6637     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6639     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6640             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6641 
6642     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6644     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6645             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6646 
6647     // move a little
6648     processPosition(mapper, 150, 250);
6649     processSync(mapper);
6650     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6652     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6653             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6654 
6655     // down when BTN_TOUCH is pressed, pressure defaults to 1
6656     processKey(mapper, BTN_TOUCH, 1);
6657     processSync(mapper);
6658     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6660     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6661             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6662 
6663     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6665     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6666             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6667 
6668     // up when BTN_TOUCH is released, hover restored
6669     processKey(mapper, BTN_TOUCH, 0);
6670     processSync(mapper);
6671     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6673     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6674             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6675 
6676     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6678     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6679             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6680 
6681     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6683     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6684             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6685 
6686     // exit hover when pointer goes away
6687     processId(mapper, -1);
6688     processSync(mapper);
6689     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6691     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6692             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6693 }
6694 
TEST_F(MultiTouchInputMapperTest,Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero)6695 TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
6696     addConfigurationProperty("touch.deviceType", "touchScreen");
6697     prepareDisplay(DISPLAY_ORIENTATION_0);
6698     prepareAxes(POSITION | ID | SLOT | PRESSURE);
6699     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6700 
6701     NotifyMotionArgs motionArgs;
6702 
6703     // initially hovering because pressure is 0
6704     processId(mapper, 1);
6705     processPosition(mapper, 100, 200);
6706     processPressure(mapper, 0);
6707     processSync(mapper);
6708     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6710     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6711             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6712 
6713     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6715     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6716             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6717 
6718     // move a little
6719     processPosition(mapper, 150, 250);
6720     processSync(mapper);
6721     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6723     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6724             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6725 
6726     // down when pressure becomes non-zero
6727     processPressure(mapper, RAW_PRESSURE_MAX);
6728     processSync(mapper);
6729     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6731     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6732             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6733 
6734     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6736     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6738 
6739     // up when pressure becomes 0, hover restored
6740     processPressure(mapper, 0);
6741     processSync(mapper);
6742     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6744     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6745             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6746 
6747     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6749     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6751 
6752     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6753     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6754     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6755             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6756 
6757     // exit hover when pointer goes away
6758     processId(mapper, -1);
6759     processSync(mapper);
6760     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6762     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6763             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6764 }
6765 
6766 /**
6767  * Set the input device port <--> display port associations, and check that the
6768  * events are routed to the display that matches the display port.
6769  * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
6770  */
TEST_F(MultiTouchInputMapperTest,Configure_AssignsDisplayPort)6771 TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
6772     const std::string usb2 = "USB2";
6773     const uint8_t hdmi1 = 0;
6774     const uint8_t hdmi2 = 1;
6775     const std::string secondaryUniqueId = "uniqueId2";
6776     constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6777 
6778     addConfigurationProperty("touch.deviceType", "touchScreen");
6779     prepareAxes(POSITION);
6780     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6781 
6782     mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6783     mFakePolicy->addInputPortAssociation(usb2, hdmi2);
6784 
6785     // We are intentionally not adding the viewport for display 1 yet. Since the port association
6786     // for this input device is specified, and the matching viewport is not present,
6787     // the input device should be disabled (at the mapper level).
6788 
6789     // Add viewport for display 2 on hdmi2
6790     prepareSecondaryDisplay(type, hdmi2);
6791     // Send a touch event
6792     processPosition(mapper, 100, 100);
6793     processSync(mapper);
6794     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6795 
6796     // Add viewport for display 1 on hdmi1
6797     prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6798     // Send a touch event again
6799     processPosition(mapper, 100, 100);
6800     processSync(mapper);
6801 
6802     NotifyMotionArgs args;
6803     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6804     ASSERT_EQ(DISPLAY_ID, args.displayId);
6805 }
6806 
TEST_F(MultiTouchInputMapperTest,Process_Pointer_ShouldHandleDisplayId)6807 TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
6808     // Setup for second display.
6809     sp<FakePointerController> fakePointerController = new FakePointerController();
6810     fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
6811     fakePointerController->setPosition(100, 200);
6812     fakePointerController->setButtonState(0);
6813     mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6814 
6815     mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
6816     prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
6817 
6818     prepareDisplay(DISPLAY_ORIENTATION_0);
6819     prepareAxes(POSITION);
6820     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6821 
6822     // Check source is mouse that would obtain the PointerController.
6823     ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
6824 
6825     NotifyMotionArgs motionArgs;
6826     processPosition(mapper, 100, 100);
6827     processSync(mapper);
6828 
6829     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6830     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6831     ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
6832 }
6833 
TEST_F(MultiTouchInputMapperTest,Process_Pointer_ShowTouches)6834 TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
6835     // Setup the first touch screen device.
6836     prepareAxes(POSITION | ID | SLOT);
6837     addConfigurationProperty("touch.deviceType", "touchScreen");
6838     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6839 
6840     // Create the second touch screen device, and enable multi fingers.
6841     const std::string USB2 = "USB2";
6842     constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
6843     constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
6844     InputDeviceIdentifier identifier;
6845     identifier.name = "TOUCHSCREEN2";
6846     identifier.location = USB2;
6847     std::unique_ptr<InputDevice> device2 =
6848             std::make_unique<InputDevice>(mFakeContext, SECOND_DEVICE_ID, DEVICE_GENERATION,
6849                                           identifier);
6850     mFakeEventHub->addDevice(SECOND_EVENTHUB_ID, DEVICE_NAME, 0 /*classes*/);
6851     mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
6852                                    0 /*flat*/, 0 /*fuzz*/);
6853     mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
6854                                    0 /*flat*/, 0 /*fuzz*/);
6855     mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
6856                                    0 /*flat*/, 0 /*fuzz*/);
6857     mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
6858                                    0 /*flat*/, 0 /*fuzz*/);
6859     mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
6860     mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
6861                                             String8("touchScreen"));
6862 
6863     // Setup the second touch screen device.
6864     MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
6865     device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
6866     device2->reset(ARBITRARY_TIME);
6867 
6868     // Setup PointerController.
6869     sp<FakePointerController> fakePointerController = new FakePointerController();
6870     mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
6871     mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
6872 
6873     // Setup policy for associated displays and show touches.
6874     const uint8_t hdmi1 = 0;
6875     const uint8_t hdmi2 = 1;
6876     mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
6877     mFakePolicy->addInputPortAssociation(USB2, hdmi2);
6878     mFakePolicy->setShowTouches(true);
6879 
6880     // Create displays.
6881     prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
6882     prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL, hdmi2);
6883 
6884     // Default device will reconfigure above, need additional reconfiguration for another device.
6885     device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6886             InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6887 
6888     // Two fingers down at default display.
6889     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6890     processPosition(mapper, x1, y1);
6891     processId(mapper, 1);
6892     processSlot(mapper, 1);
6893     processPosition(mapper, x2, y2);
6894     processId(mapper, 2);
6895     processSync(mapper);
6896 
6897     std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
6898             fakePointerController->getSpots().find(DISPLAY_ID);
6899     ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6900     ASSERT_EQ(size_t(2), iter->second.size());
6901 
6902     // Two fingers down at second display.
6903     processPosition(mapper2, x1, y1);
6904     processId(mapper2, 1);
6905     processSlot(mapper2, 1);
6906     processPosition(mapper2, x2, y2);
6907     processId(mapper2, 2);
6908     processSync(mapper2);
6909 
6910     iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
6911     ASSERT_TRUE(iter != fakePointerController->getSpots().end());
6912     ASSERT_EQ(size_t(2), iter->second.size());
6913 }
6914 
TEST_F(MultiTouchInputMapperTest,VideoFrames_ReceivedByListener)6915 TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
6916     prepareAxes(POSITION);
6917     addConfigurationProperty("touch.deviceType", "touchScreen");
6918     prepareDisplay(DISPLAY_ORIENTATION_0);
6919     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6920 
6921     NotifyMotionArgs motionArgs;
6922     // Unrotated video frame
6923     TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6924     std::vector<TouchVideoFrame> frames{frame};
6925     mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
6926     processPosition(mapper, 100, 200);
6927     processSync(mapper);
6928     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6929     ASSERT_EQ(frames, motionArgs.videoFrames);
6930 
6931     // Subsequent touch events should not have any videoframes
6932     // This is implemented separately in FakeEventHub,
6933     // but that should match the behaviour of TouchVideoDevice.
6934     processPosition(mapper, 200, 200);
6935     processSync(mapper);
6936     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6937     ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
6938 }
6939 
TEST_F(MultiTouchInputMapperTest,VideoFrames_AreRotated)6940 TEST_F(MultiTouchInputMapperTest, VideoFrames_AreRotated) {
6941     prepareAxes(POSITION);
6942     addConfigurationProperty("touch.deviceType", "touchScreen");
6943     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6944     // Unrotated video frame
6945     TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6946     NotifyMotionArgs motionArgs;
6947 
6948     // Test all 4 orientations
6949     for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
6950              DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
6951         SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
6952         clearViewports();
6953         prepareDisplay(orientation);
6954         std::vector<TouchVideoFrame> frames{frame};
6955         mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
6956         processPosition(mapper, 100, 200);
6957         processSync(mapper);
6958         ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6959         frames[0].rotate(orientation);
6960         ASSERT_EQ(frames, motionArgs.videoFrames);
6961     }
6962 }
6963 
TEST_F(MultiTouchInputMapperTest,VideoFrames_MultipleFramesAreRotated)6964 TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreRotated) {
6965     prepareAxes(POSITION);
6966     addConfigurationProperty("touch.deviceType", "touchScreen");
6967     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6968     // Unrotated video frames. There's no rule that they must all have the same dimensions,
6969     // so mix these.
6970     TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
6971     TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
6972     TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
6973     std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
6974     NotifyMotionArgs motionArgs;
6975 
6976     prepareDisplay(DISPLAY_ORIENTATION_90);
6977     mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
6978     processPosition(mapper, 100, 200);
6979     processSync(mapper);
6980     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6981     std::for_each(frames.begin(), frames.end(),
6982             [](TouchVideoFrame& frame) { frame.rotate(DISPLAY_ORIENTATION_90); });
6983     ASSERT_EQ(frames, motionArgs.videoFrames);
6984 }
6985 
6986 /**
6987  * If we had defined port associations, but the viewport is not ready, the touch device would be
6988  * expected to be disabled, and it should be enabled after the viewport has found.
6989  */
TEST_F(MultiTouchInputMapperTest,Configure_EnabledForAssociatedDisplay)6990 TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
6991     constexpr uint8_t hdmi2 = 1;
6992     const std::string secondaryUniqueId = "uniqueId2";
6993     constexpr ViewportType type = ViewportType::VIEWPORT_EXTERNAL;
6994 
6995     mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
6996 
6997     addConfigurationProperty("touch.deviceType", "touchScreen");
6998     prepareAxes(POSITION);
6999     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7000 
7001     ASSERT_EQ(mDevice->isEnabled(), false);
7002 
7003     // Add display on hdmi2, the device should be enabled and can receive touch event.
7004     prepareSecondaryDisplay(type, hdmi2);
7005     ASSERT_EQ(mDevice->isEnabled(), true);
7006 
7007     // Send a touch event.
7008     processPosition(mapper, 100, 100);
7009     processSync(mapper);
7010 
7011     NotifyMotionArgs args;
7012     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7013     ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7014 }
7015 
7016 
7017 
TEST_F(MultiTouchInputMapperTest,Process_ShouldHandleSingleTouch)7018 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
7019     addConfigurationProperty("touch.deviceType", "touchScreen");
7020     prepareDisplay(DISPLAY_ORIENTATION_0);
7021     prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7022     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7023 
7024     NotifyMotionArgs motionArgs;
7025 
7026     constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7027     // finger down
7028     processId(mapper, 1);
7029     processPosition(mapper, x1, y1);
7030     processSync(mapper);
7031     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7032     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7033     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7034 
7035     // finger move
7036     processId(mapper, 1);
7037     processPosition(mapper, x2, y2);
7038     processSync(mapper);
7039     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7041     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7042 
7043     // finger up.
7044     processId(mapper, -1);
7045     processSync(mapper);
7046     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7047     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7048     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7049 
7050     // new finger down
7051     processId(mapper, 1);
7052     processPosition(mapper, x3, y3);
7053     processSync(mapper);
7054     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7055     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7056     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7057 }
7058 
7059 /**
7060  * Test touch should be canceled when received the MT_TOOL_PALM event, and the following MOVE and
7061  * UP events should be ignored.
7062  */
TEST_F(MultiTouchInputMapperTest,Process_ShouldHandlePalmToolType)7063 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType) {
7064     addConfigurationProperty("touch.deviceType", "touchScreen");
7065     prepareDisplay(DISPLAY_ORIENTATION_0);
7066     prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7067     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7068 
7069     NotifyMotionArgs motionArgs;
7070 
7071     // default tool type is finger
7072     constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7073     processId(mapper, 1);
7074     processPosition(mapper, x1, y1);
7075     processSync(mapper);
7076     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7077     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7078     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7079 
7080     // Tool changed to MT_TOOL_PALM expect sending the cancel event.
7081     processToolType(mapper, MT_TOOL_PALM);
7082     processSync(mapper);
7083     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084     ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7085 
7086     // Ignore the following MOVE and UP events if had detect a palm event.
7087     processId(mapper, 1);
7088     processPosition(mapper, x2, y2);
7089     processSync(mapper);
7090     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7091 
7092     // finger up.
7093     processId(mapper, -1);
7094     processSync(mapper);
7095     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7096 
7097     // new finger down
7098     processToolType(mapper, MT_TOOL_FINGER);
7099     processId(mapper, 1);
7100     processPosition(mapper, x3, y3);
7101     processSync(mapper);
7102     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7103     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7104     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7105 }
7106 
7107 /**
7108  * Test multi-touch should be canceled when received the MT_TOOL_PALM event from some finger,
7109  * and could be allowed again after all non-MT_TOOL_PALM is release and the new point is
7110  * MT_TOOL_FINGER.
7111  */
TEST_F(MultiTouchInputMapperTest,Process_ShouldHandlePalmToolType2)7112 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType2) {
7113     addConfigurationProperty("touch.deviceType", "touchScreen");
7114     prepareDisplay(DISPLAY_ORIENTATION_0);
7115     prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
7116     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7117 
7118     NotifyMotionArgs motionArgs;
7119 
7120     // default tool type is finger
7121     constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7122     processId(mapper, 1);
7123     processPosition(mapper, x1, y1);
7124     processSync(mapper);
7125     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7126     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7127     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7128 
7129     // Second finger down.
7130     processSlot(mapper, 1);
7131     processPosition(mapper, x2, y2);
7132     processId(mapper, 2);
7133     processSync(mapper);
7134     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7135     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7136               motionArgs.action);
7137     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7138 
7139     // If the tool type of the first pointer changes to MT_TOOL_PALM,
7140     // the entire gesture should be aborted, so we expect to receive ACTION_CANCEL.
7141     processSlot(mapper, 0);
7142     processId(mapper, 1);
7143     processToolType(mapper, MT_TOOL_PALM);
7144     processSync(mapper);
7145     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7146     ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7147 
7148     // Ignore the following MOVE and UP events if had detect a palm event.
7149     processSlot(mapper, 1);
7150     processId(mapper, 2);
7151     processPosition(mapper, x3, y3);
7152     processSync(mapper);
7153     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7154 
7155     // second finger up.
7156     processId(mapper, -1);
7157     processSync(mapper);
7158     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7159 
7160     // first finger move, but still in palm
7161     processSlot(mapper, 0);
7162     processId(mapper, 1);
7163     processPosition(mapper, x1 - 1, y1 - 1);
7164     processSync(mapper);
7165     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7166 
7167     // second finger down, expect as new finger down.
7168     processSlot(mapper, 1);
7169     processId(mapper, 2);
7170     processPosition(mapper, x2, y2);
7171     processSync(mapper);
7172     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7173     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7174     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7175 }
7176 
7177 // --- MultiTouchInputMapperTest_ExternalDevice ---
7178 
7179 class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
7180 protected:
SetUp()7181     virtual void SetUp() override {
7182         InputMapperTest::SetUp(DEVICE_CLASSES | INPUT_DEVICE_CLASS_EXTERNAL);
7183     }
7184 };
7185 
7186 /**
7187  * Expect fallback to internal viewport if device is external and external viewport is not present.
7188  */
TEST_F(MultiTouchInputMapperTest_ExternalDevice,Viewports_Fallback)7189 TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
7190     prepareAxes(POSITION);
7191     addConfigurationProperty("touch.deviceType", "touchScreen");
7192     prepareDisplay(DISPLAY_ORIENTATION_0);
7193     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7194 
7195     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7196 
7197     NotifyMotionArgs motionArgs;
7198 
7199     // Expect the event to be sent to the internal viewport,
7200     // because an external viewport is not present.
7201     processPosition(mapper, 100, 100);
7202     processSync(mapper);
7203     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7204     ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
7205 
7206     // Expect the event to be sent to the external viewport if it is present.
7207     prepareSecondaryDisplay(ViewportType::VIEWPORT_EXTERNAL);
7208     processPosition(mapper, 100, 100);
7209     processSync(mapper);
7210     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7211     ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7212 }
7213 
7214 /**
7215  * Test touch should not work if outside of surface.
7216  */
7217 class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
7218 protected:
halfDisplayToCenterHorizontal(int32_t orientation)7219     void halfDisplayToCenterHorizontal(int32_t orientation) {
7220         std::optional<DisplayViewport> internalViewport =
7221                 mFakePolicy->getDisplayViewportByType(ViewportType::VIEWPORT_INTERNAL);
7222 
7223         // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
7224         internalViewport->orientation = orientation;
7225         if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
7226             internalViewport->logicalLeft = 0;
7227             internalViewport->logicalTop = 0;
7228             internalViewport->logicalRight = DISPLAY_HEIGHT;
7229             internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
7230 
7231             internalViewport->physicalLeft = 0;
7232             internalViewport->physicalTop = DISPLAY_WIDTH / 4;
7233             internalViewport->physicalRight = DISPLAY_HEIGHT;
7234             internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
7235 
7236             internalViewport->deviceWidth = DISPLAY_HEIGHT;
7237             internalViewport->deviceHeight = DISPLAY_WIDTH;
7238         } else {
7239             internalViewport->logicalLeft = 0;
7240             internalViewport->logicalTop = 0;
7241             internalViewport->logicalRight = DISPLAY_WIDTH / 2;
7242             internalViewport->logicalBottom = DISPLAY_HEIGHT;
7243 
7244             internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
7245             internalViewport->physicalTop = 0;
7246             internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
7247             internalViewport->physicalBottom = DISPLAY_HEIGHT;
7248 
7249             internalViewport->deviceWidth = DISPLAY_WIDTH;
7250             internalViewport->deviceHeight = DISPLAY_HEIGHT;
7251         }
7252 
7253         mFakePolicy->updateViewport(internalViewport.value());
7254         configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7255     }
7256 
processPositionAndVerify(MultiTouchInputMapper & mapper,int32_t xInside,int32_t yInside,int32_t xOutside,int32_t yOutside,int32_t xExpected,int32_t yExpected)7257     void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xInside, int32_t yInside,
7258                                   int32_t xOutside, int32_t yOutside, int32_t xExpected,
7259                                   int32_t yExpected) {
7260         // touch on outside area should not work.
7261         processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
7262         processSync(mapper);
7263         ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7264 
7265         // touch on inside area should receive the event.
7266         NotifyMotionArgs args;
7267         processPosition(mapper, toRawX(xInside), toRawY(yInside));
7268         processSync(mapper);
7269         ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7270         ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
7271         ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
7272 
7273         // Reset.
7274         mapper.reset(ARBITRARY_TIME);
7275     }
7276 };
7277 
TEST_F(MultiTouchInputMapperTest_SurfaceRange,Viewports_SurfaceRange)7278 TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
7279     addConfigurationProperty("touch.deviceType", "touchScreen");
7280     prepareDisplay(DISPLAY_ORIENTATION_0);
7281     prepareAxes(POSITION);
7282     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7283 
7284     // Touch on center of normal display should work.
7285     const int32_t x = DISPLAY_WIDTH / 4;
7286     const int32_t y = DISPLAY_HEIGHT / 2;
7287     processPosition(mapper, toRawX(x), toRawY(y));
7288     processSync(mapper);
7289     NotifyMotionArgs args;
7290     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7291     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
7292                                                 0.0f, 0.0f, 0.0f, 0.0f));
7293     // Reset.
7294     mapper.reset(ARBITRARY_TIME);
7295 
7296     // Let physical display be different to device, and make surface and physical could be 1:1.
7297     halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_0);
7298 
7299     const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
7300     const int32_t yExpected = y;
7301     processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7302 }
7303 
TEST_F(MultiTouchInputMapperTest_SurfaceRange,Viewports_SurfaceRange_90)7304 TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90) {
7305     addConfigurationProperty("touch.deviceType", "touchScreen");
7306     prepareDisplay(DISPLAY_ORIENTATION_0);
7307     prepareAxes(POSITION);
7308     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7309 
7310     // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
7311     halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
7312 
7313     const int32_t x = DISPLAY_WIDTH / 4;
7314     const int32_t y = DISPLAY_HEIGHT / 2;
7315 
7316     // expect x/y = swap x/y then reverse y.
7317     const int32_t xExpected = y;
7318     const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
7319     processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7320 }
7321 
TEST_F(MultiTouchInputMapperTest_SurfaceRange,Viewports_SurfaceRange_270)7322 TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270) {
7323     addConfigurationProperty("touch.deviceType", "touchScreen");
7324     prepareDisplay(DISPLAY_ORIENTATION_0);
7325     prepareAxes(POSITION);
7326     MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7327 
7328     // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
7329     halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
7330 
7331     const int32_t x = DISPLAY_WIDTH / 4;
7332     const int32_t y = DISPLAY_HEIGHT / 2;
7333 
7334     // expect x/y = swap x/y then reverse x.
7335     constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
7336     constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
7337     processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
7338 }
7339 } // namespace android
7340