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