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 "../InputReader.h"
18 
19 #include <utils/List.h>
20 #include <gtest/gtest.h>
21 #include <math.h>
22 
23 namespace android {
24 
25 // An arbitrary time value.
26 static const nsecs_t ARBITRARY_TIME = 1234;
27 
28 // Arbitrary display properties.
29 static const int32_t DISPLAY_ID = 0;
30 static const int32_t DISPLAY_WIDTH = 480;
31 static const int32_t DISPLAY_HEIGHT = 800;
32 
33 // Error tolerance for floating point assertions.
34 static const float EPSILON = 0.001f;
35 
36 template<typename T>
min(T a,T b)37 static inline T min(T a, T b) {
38     return a < b ? a : b;
39 }
40 
avg(float x,float y)41 static inline float avg(float x, float y) {
42     return (x + y) / 2;
43 }
44 
45 
46 // --- FakePointerController ---
47 
48 class FakePointerController : public PointerControllerInterface {
49     bool mHaveBounds;
50     float mMinX, mMinY, mMaxX, mMaxY;
51     float mX, mY;
52     int32_t mButtonState;
53 
54 protected:
~FakePointerController()55     virtual ~FakePointerController() { }
56 
57 public:
FakePointerController()58     FakePointerController() :
59         mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
60         mButtonState(0) {
61     }
62 
setBounds(float minX,float minY,float maxX,float maxY)63     void setBounds(float minX, float minY, float maxX, float maxY) {
64         mHaveBounds = true;
65         mMinX = minX;
66         mMinY = minY;
67         mMaxX = maxX;
68         mMaxY = maxY;
69     }
70 
setPosition(float x,float y)71     virtual void setPosition(float x, float y) {
72         mX = x;
73         mY = y;
74     }
75 
setButtonState(int32_t buttonState)76     virtual void setButtonState(int32_t buttonState) {
77         mButtonState = buttonState;
78     }
79 
getButtonState() const80     virtual int32_t getButtonState() const {
81         return mButtonState;
82     }
83 
getPosition(float * outX,float * outY) const84     virtual void getPosition(float* outX, float* outY) const {
85         *outX = mX;
86         *outY = mY;
87     }
88 
89 private:
getBounds(float * outMinX,float * outMinY,float * outMaxX,float * outMaxY) const90     virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
91         *outMinX = mMinX;
92         *outMinY = mMinY;
93         *outMaxX = mMaxX;
94         *outMaxY = mMaxY;
95         return mHaveBounds;
96     }
97 
move(float deltaX,float deltaY)98     virtual void move(float deltaX, float deltaY) {
99         mX += deltaX;
100         if (mX < mMinX) mX = mMinX;
101         if (mX > mMaxX) mX = mMaxX;
102         mY += deltaY;
103         if (mY < mMinY) mY = mMinY;
104         if (mY > mMaxY) mY = mMaxY;
105     }
106 
fade(Transition)107     virtual void fade(Transition) {
108     }
109 
unfade(Transition)110     virtual void unfade(Transition) {
111     }
112 
setPresentation(Presentation)113     virtual void setPresentation(Presentation) {
114     }
115 
setSpots(const PointerCoords *,const uint32_t *,BitSet32)116     virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32) {
117     }
118 
clearSpots()119     virtual void clearSpots() {
120     }
121 };
122 
123 
124 // --- FakeInputReaderPolicy ---
125 
126 class FakeInputReaderPolicy : public InputReaderPolicyInterface {
127     InputReaderConfiguration mConfig;
128     KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
129     Vector<InputDeviceInfo> mInputDevices;
130     TouchAffineTransformation transform;
131 
132 protected:
~FakeInputReaderPolicy()133     virtual ~FakeInputReaderPolicy() { }
134 
135 public:
FakeInputReaderPolicy()136     FakeInputReaderPolicy() {
137     }
138 
setDisplayInfo(int32_t displayId,int32_t width,int32_t height,int32_t orientation)139     void setDisplayInfo(int32_t displayId, int32_t width, int32_t height, int32_t orientation) {
140         // Set the size of both the internal and external display at the same time.
141         bool isRotated = (orientation == DISPLAY_ORIENTATION_90
142                 || orientation == DISPLAY_ORIENTATION_270);
143         DisplayViewport v;
144         v.displayId = displayId;
145         v.orientation = orientation;
146         v.logicalLeft = 0;
147         v.logicalTop = 0;
148         v.logicalRight = isRotated ? height : width;
149         v.logicalBottom = isRotated ? width : height;
150         v.physicalLeft = 0;
151         v.physicalTop = 0;
152         v.physicalRight = isRotated ? height : width;
153         v.physicalBottom = isRotated ? width : height;
154         v.deviceWidth = isRotated ? height : width;
155         v.deviceHeight = isRotated ? width : height;
156         mConfig.setDisplayInfo(false /*external*/, v);
157         mConfig.setDisplayInfo(true /*external*/, v);
158     }
159 
addExcludedDeviceName(const String8 & deviceName)160     void addExcludedDeviceName(const String8& deviceName) {
161         mConfig.excludedDeviceNames.push(deviceName);
162     }
163 
setPointerController(int32_t deviceId,const sp<FakePointerController> & controller)164     void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
165         mPointerControllers.add(deviceId, controller);
166     }
167 
getReaderConfiguration() const168     const InputReaderConfiguration* getReaderConfiguration() const {
169         return &mConfig;
170     }
171 
getInputDevices() const172     const Vector<InputDeviceInfo>& getInputDevices() const {
173         return mInputDevices;
174     }
175 
getTouchAffineTransformation(const String8 & inputDeviceDescriptor,int32_t surfaceRotation)176     TouchAffineTransformation getTouchAffineTransformation(const String8& inputDeviceDescriptor,
177             int32_t surfaceRotation) {
178         return transform;
179     }
180 
setTouchAffineTransformation(const TouchAffineTransformation t)181     void setTouchAffineTransformation(const TouchAffineTransformation t) {
182         transform = t;
183     }
184 
185 private:
getReaderConfiguration(InputReaderConfiguration * outConfig)186     virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
187         *outConfig = mConfig;
188     }
189 
obtainPointerController(int32_t deviceId)190     virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
191         return mPointerControllers.valueFor(deviceId);
192     }
193 
notifyInputDevicesChanged(const Vector<InputDeviceInfo> & inputDevices)194     virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
195         mInputDevices = inputDevices;
196     }
197 
getKeyboardLayoutOverlay(const InputDeviceIdentifier &)198     virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
199         return NULL;
200     }
201 
getDeviceAlias(const InputDeviceIdentifier &)202     virtual String8 getDeviceAlias(const InputDeviceIdentifier&) {
203         return String8::empty();
204     }
205 };
206 
207 
208 // --- FakeInputListener ---
209 
210 class FakeInputListener : public InputListenerInterface {
211 private:
212     List<NotifyConfigurationChangedArgs> mNotifyConfigurationChangedArgsQueue;
213     List<NotifyDeviceResetArgs> mNotifyDeviceResetArgsQueue;
214     List<NotifyKeyArgs> mNotifyKeyArgsQueue;
215     List<NotifyMotionArgs> mNotifyMotionArgsQueue;
216     List<NotifySwitchArgs> mNotifySwitchArgsQueue;
217 
218 protected:
~FakeInputListener()219     virtual ~FakeInputListener() { }
220 
221 public:
FakeInputListener()222     FakeInputListener() {
223     }
224 
assertNotifyConfigurationChangedWasCalled(NotifyConfigurationChangedArgs * outEventArgs=NULL)225     void assertNotifyConfigurationChangedWasCalled(
226             NotifyConfigurationChangedArgs* outEventArgs = NULL) {
227         ASSERT_FALSE(mNotifyConfigurationChangedArgsQueue.empty())
228                 << "Expected notifyConfigurationChanged() to have been called.";
229         if (outEventArgs) {
230             *outEventArgs = *mNotifyConfigurationChangedArgsQueue.begin();
231         }
232         mNotifyConfigurationChangedArgsQueue.erase(mNotifyConfigurationChangedArgsQueue.begin());
233     }
234 
assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs * outEventArgs=NULL)235     void assertNotifyDeviceResetWasCalled(
236             NotifyDeviceResetArgs* outEventArgs = NULL) {
237         ASSERT_FALSE(mNotifyDeviceResetArgsQueue.empty())
238                 << "Expected notifyDeviceReset() to have been called.";
239         if (outEventArgs) {
240             *outEventArgs = *mNotifyDeviceResetArgsQueue.begin();
241         }
242         mNotifyDeviceResetArgsQueue.erase(mNotifyDeviceResetArgsQueue.begin());
243     }
244 
assertNotifyKeyWasCalled(NotifyKeyArgs * outEventArgs=NULL)245     void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = NULL) {
246         ASSERT_FALSE(mNotifyKeyArgsQueue.empty())
247                 << "Expected notifyKey() to have been called.";
248         if (outEventArgs) {
249             *outEventArgs = *mNotifyKeyArgsQueue.begin();
250         }
251         mNotifyKeyArgsQueue.erase(mNotifyKeyArgsQueue.begin());
252     }
253 
assertNotifyKeyWasNotCalled()254     void assertNotifyKeyWasNotCalled() {
255         ASSERT_TRUE(mNotifyKeyArgsQueue.empty())
256                 << "Expected notifyKey() to not have been called.";
257     }
258 
assertNotifyMotionWasCalled(NotifyMotionArgs * outEventArgs=NULL)259     void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = NULL) {
260         ASSERT_FALSE(mNotifyMotionArgsQueue.empty())
261                 << "Expected notifyMotion() to have been called.";
262         if (outEventArgs) {
263             *outEventArgs = *mNotifyMotionArgsQueue.begin();
264         }
265         mNotifyMotionArgsQueue.erase(mNotifyMotionArgsQueue.begin());
266     }
267 
assertNotifyMotionWasNotCalled()268     void assertNotifyMotionWasNotCalled() {
269         ASSERT_TRUE(mNotifyMotionArgsQueue.empty())
270                 << "Expected notifyMotion() to not have been called.";
271     }
272 
assertNotifySwitchWasCalled(NotifySwitchArgs * outEventArgs=NULL)273     void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = NULL) {
274         ASSERT_FALSE(mNotifySwitchArgsQueue.empty())
275                 << "Expected notifySwitch() to have been called.";
276         if (outEventArgs) {
277             *outEventArgs = *mNotifySwitchArgsQueue.begin();
278         }
279         mNotifySwitchArgsQueue.erase(mNotifySwitchArgsQueue.begin());
280     }
281 
282 private:
notifyConfigurationChanged(const NotifyConfigurationChangedArgs * args)283     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
284         mNotifyConfigurationChangedArgsQueue.push_back(*args);
285     }
286 
notifyDeviceReset(const NotifyDeviceResetArgs * args)287     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) {
288         mNotifyDeviceResetArgsQueue.push_back(*args);
289     }
290 
notifyKey(const NotifyKeyArgs * args)291     virtual void notifyKey(const NotifyKeyArgs* args) {
292         mNotifyKeyArgsQueue.push_back(*args);
293     }
294 
notifyMotion(const NotifyMotionArgs * args)295     virtual void notifyMotion(const NotifyMotionArgs* args) {
296         mNotifyMotionArgsQueue.push_back(*args);
297     }
298 
notifySwitch(const NotifySwitchArgs * args)299     virtual void notifySwitch(const NotifySwitchArgs* args) {
300         mNotifySwitchArgsQueue.push_back(*args);
301     }
302 };
303 
304 
305 // --- FakeEventHub ---
306 
307 class FakeEventHub : public EventHubInterface {
308     struct KeyInfo {
309         int32_t keyCode;
310         uint32_t flags;
311     };
312 
313     struct Device {
314         InputDeviceIdentifier identifier;
315         uint32_t classes;
316         PropertyMap configuration;
317         KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
318         KeyedVector<int, bool> relativeAxes;
319         KeyedVector<int32_t, int32_t> keyCodeStates;
320         KeyedVector<int32_t, int32_t> scanCodeStates;
321         KeyedVector<int32_t, int32_t> switchStates;
322         KeyedVector<int32_t, int32_t> absoluteAxisValue;
323         KeyedVector<int32_t, KeyInfo> keysByScanCode;
324         KeyedVector<int32_t, KeyInfo> keysByUsageCode;
325         KeyedVector<int32_t, bool> leds;
326         Vector<VirtualKeyDefinition> virtualKeys;
327 
Deviceandroid::FakeEventHub::Device328         Device(uint32_t classes) :
329                 classes(classes) {
330         }
331     };
332 
333     KeyedVector<int32_t, Device*> mDevices;
334     Vector<String8> mExcludedDevices;
335     List<RawEvent> mEvents;
336 
337 protected:
~FakeEventHub()338     virtual ~FakeEventHub() {
339         for (size_t i = 0; i < mDevices.size(); i++) {
340             delete mDevices.valueAt(i);
341         }
342     }
343 
344 public:
FakeEventHub()345     FakeEventHub() { }
346 
addDevice(int32_t deviceId,const String8 & name,uint32_t classes)347     void addDevice(int32_t deviceId, const String8& name, uint32_t classes) {
348         Device* device = new Device(classes);
349         device->identifier.name = name;
350         mDevices.add(deviceId, device);
351 
352         enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
353     }
354 
removeDevice(int32_t deviceId)355     void removeDevice(int32_t deviceId) {
356         delete mDevices.valueFor(deviceId);
357         mDevices.removeItem(deviceId);
358 
359         enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
360     }
361 
finishDeviceScan()362     void finishDeviceScan() {
363         enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
364     }
365 
addConfigurationProperty(int32_t deviceId,const String8 & key,const String8 & value)366     void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
367         Device* device = getDevice(deviceId);
368         device->configuration.addProperty(key, value);
369     }
370 
addConfigurationMap(int32_t deviceId,const PropertyMap * configuration)371     void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
372         Device* device = getDevice(deviceId);
373         device->configuration.addAll(configuration);
374     }
375 
addAbsoluteAxis(int32_t deviceId,int axis,int32_t minValue,int32_t maxValue,int flat,int fuzz,int resolution=0)376     void addAbsoluteAxis(int32_t deviceId, int axis,
377             int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
378         Device* device = getDevice(deviceId);
379 
380         RawAbsoluteAxisInfo info;
381         info.valid = true;
382         info.minValue = minValue;
383         info.maxValue = maxValue;
384         info.flat = flat;
385         info.fuzz = fuzz;
386         info.resolution = resolution;
387         device->absoluteAxes.add(axis, info);
388     }
389 
addRelativeAxis(int32_t deviceId,int32_t axis)390     void addRelativeAxis(int32_t deviceId, int32_t axis) {
391         Device* device = getDevice(deviceId);
392         device->relativeAxes.add(axis, true);
393     }
394 
setKeyCodeState(int32_t deviceId,int32_t keyCode,int32_t state)395     void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
396         Device* device = getDevice(deviceId);
397         device->keyCodeStates.replaceValueFor(keyCode, state);
398     }
399 
setScanCodeState(int32_t deviceId,int32_t scanCode,int32_t state)400     void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
401         Device* device = getDevice(deviceId);
402         device->scanCodeStates.replaceValueFor(scanCode, state);
403     }
404 
setSwitchState(int32_t deviceId,int32_t switchCode,int32_t state)405     void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
406         Device* device = getDevice(deviceId);
407         device->switchStates.replaceValueFor(switchCode, state);
408     }
409 
setAbsoluteAxisValue(int32_t deviceId,int32_t axis,int32_t value)410     void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
411         Device* device = getDevice(deviceId);
412         device->absoluteAxisValue.replaceValueFor(axis, value);
413     }
414 
addKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t keyCode,uint32_t flags)415     void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
416             int32_t keyCode, uint32_t flags) {
417         Device* device = getDevice(deviceId);
418         KeyInfo info;
419         info.keyCode = keyCode;
420         info.flags = flags;
421         if (scanCode) {
422             device->keysByScanCode.add(scanCode, info);
423         }
424         if (usageCode) {
425             device->keysByUsageCode.add(usageCode, info);
426         }
427     }
428 
addLed(int32_t deviceId,int32_t led,bool initialState)429     void addLed(int32_t deviceId, int32_t led, bool initialState) {
430         Device* device = getDevice(deviceId);
431         device->leds.add(led, initialState);
432     }
433 
getLedState(int32_t deviceId,int32_t led)434     bool getLedState(int32_t deviceId, int32_t led) {
435         Device* device = getDevice(deviceId);
436         return device->leds.valueFor(led);
437     }
438 
getExcludedDevices()439     Vector<String8>& getExcludedDevices() {
440         return mExcludedDevices;
441     }
442 
addVirtualKeyDefinition(int32_t deviceId,const VirtualKeyDefinition & definition)443     void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
444         Device* device = getDevice(deviceId);
445         device->virtualKeys.push(definition);
446     }
447 
enqueueEvent(nsecs_t when,int32_t deviceId,int32_t type,int32_t code,int32_t value)448     void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
449             int32_t code, int32_t value) {
450         RawEvent event;
451         event.when = when;
452         event.deviceId = deviceId;
453         event.type = type;
454         event.code = code;
455         event.value = value;
456         mEvents.push_back(event);
457 
458         if (type == EV_ABS) {
459             setAbsoluteAxisValue(deviceId, code, value);
460         }
461     }
462 
assertQueueIsEmpty()463     void assertQueueIsEmpty() {
464         ASSERT_EQ(size_t(0), mEvents.size())
465                 << "Expected the event queue to be empty (fully consumed).";
466     }
467 
468 private:
getDevice(int32_t deviceId) const469     Device* getDevice(int32_t deviceId) const {
470         ssize_t index = mDevices.indexOfKey(deviceId);
471         return index >= 0 ? mDevices.valueAt(index) : NULL;
472     }
473 
getDeviceClasses(int32_t deviceId) const474     virtual uint32_t getDeviceClasses(int32_t deviceId) const {
475         Device* device = getDevice(deviceId);
476         return device ? device->classes : 0;
477     }
478 
getDeviceIdentifier(int32_t deviceId) const479     virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
480         Device* device = getDevice(deviceId);
481         return device ? device->identifier : InputDeviceIdentifier();
482     }
483 
getDeviceControllerNumber(int32_t) const484     virtual int32_t getDeviceControllerNumber(int32_t) const {
485         return 0;
486     }
487 
getConfiguration(int32_t deviceId,PropertyMap * outConfiguration) const488     virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
489         Device* device = getDevice(deviceId);
490         if (device) {
491             *outConfiguration = device->configuration;
492         }
493     }
494 
getAbsoluteAxisInfo(int32_t deviceId,int axis,RawAbsoluteAxisInfo * outAxisInfo) const495     virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
496             RawAbsoluteAxisInfo* outAxisInfo) const {
497         Device* device = getDevice(deviceId);
498         if (device) {
499             ssize_t index = device->absoluteAxes.indexOfKey(axis);
500             if (index >= 0) {
501                 *outAxisInfo = device->absoluteAxes.valueAt(index);
502                 return OK;
503             }
504         }
505         outAxisInfo->clear();
506         return -1;
507     }
508 
hasRelativeAxis(int32_t deviceId,int axis) const509     virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
510         Device* device = getDevice(deviceId);
511         if (device) {
512             return device->relativeAxes.indexOfKey(axis) >= 0;
513         }
514         return false;
515     }
516 
hasInputProperty(int32_t,int) const517     virtual bool hasInputProperty(int32_t, int) const {
518         return false;
519     }
520 
mapKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t * outKeycode,uint32_t * outFlags) const521     virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
522             int32_t* outKeycode, uint32_t* outFlags) const {
523         Device* device = getDevice(deviceId);
524         if (device) {
525             const KeyInfo* key = getKey(device, scanCode, usageCode);
526             if (key) {
527                 if (outKeycode) {
528                     *outKeycode = key->keyCode;
529                 }
530                 if (outFlags) {
531                     *outFlags = key->flags;
532                 }
533                 return OK;
534             }
535         }
536         return NAME_NOT_FOUND;
537     }
538 
getKey(Device * device,int32_t scanCode,int32_t usageCode) const539     const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
540         if (usageCode) {
541             ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
542             if (index >= 0) {
543                 return &device->keysByUsageCode.valueAt(index);
544             }
545         }
546         if (scanCode) {
547             ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
548             if (index >= 0) {
549                 return &device->keysByScanCode.valueAt(index);
550             }
551         }
552         return NULL;
553     }
554 
mapAxis(int32_t,int32_t,AxisInfo *) const555     virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
556         return NAME_NOT_FOUND;
557     }
558 
setExcludedDevices(const Vector<String8> & devices)559     virtual void setExcludedDevices(const Vector<String8>& devices) {
560         mExcludedDevices = devices;
561     }
562 
getEvents(int,RawEvent * buffer,size_t)563     virtual size_t getEvents(int, RawEvent* buffer, size_t) {
564         if (mEvents.empty()) {
565             return 0;
566         }
567 
568         *buffer = *mEvents.begin();
569         mEvents.erase(mEvents.begin());
570         return 1;
571     }
572 
getScanCodeState(int32_t deviceId,int32_t scanCode) const573     virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
574         Device* device = getDevice(deviceId);
575         if (device) {
576             ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
577             if (index >= 0) {
578                 return device->scanCodeStates.valueAt(index);
579             }
580         }
581         return AKEY_STATE_UNKNOWN;
582     }
583 
getKeyCodeState(int32_t deviceId,int32_t keyCode) const584     virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
585         Device* device = getDevice(deviceId);
586         if (device) {
587             ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
588             if (index >= 0) {
589                 return device->keyCodeStates.valueAt(index);
590             }
591         }
592         return AKEY_STATE_UNKNOWN;
593     }
594 
getSwitchState(int32_t deviceId,int32_t sw) const595     virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
596         Device* device = getDevice(deviceId);
597         if (device) {
598             ssize_t index = device->switchStates.indexOfKey(sw);
599             if (index >= 0) {
600                 return device->switchStates.valueAt(index);
601             }
602         }
603         return AKEY_STATE_UNKNOWN;
604     }
605 
getAbsoluteAxisValue(int32_t deviceId,int32_t axis,int32_t * outValue) const606     virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
607             int32_t* outValue) const {
608         Device* device = getDevice(deviceId);
609         if (device) {
610             ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
611             if (index >= 0) {
612                 *outValue = device->absoluteAxisValue.valueAt(index);
613                 return OK;
614             }
615         }
616         *outValue = 0;
617         return -1;
618     }
619 
markSupportedKeyCodes(int32_t deviceId,size_t numCodes,const int32_t * keyCodes,uint8_t * outFlags) const620     virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
621             uint8_t* outFlags) const {
622         bool result = false;
623         Device* device = getDevice(deviceId);
624         if (device) {
625             for (size_t i = 0; i < numCodes; i++) {
626                 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
627                     if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
628                         outFlags[i] = 1;
629                         result = true;
630                     }
631                 }
632                 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
633                     if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
634                         outFlags[i] = 1;
635                         result = true;
636                     }
637                 }
638             }
639         }
640         return result;
641     }
642 
hasScanCode(int32_t deviceId,int32_t scanCode) const643     virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
644         Device* device = getDevice(deviceId);
645         if (device) {
646             ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
647             return index >= 0;
648         }
649         return false;
650     }
651 
hasLed(int32_t deviceId,int32_t led) const652     virtual bool hasLed(int32_t deviceId, int32_t led) const {
653         Device* device = getDevice(deviceId);
654         return device && device->leds.indexOfKey(led) >= 0;
655     }
656 
setLedState(int32_t deviceId,int32_t led,bool on)657     virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
658         Device* device = getDevice(deviceId);
659         if (device) {
660             ssize_t index = device->leds.indexOfKey(led);
661             if (index >= 0) {
662                 device->leds.replaceValueAt(led, on);
663             } else {
664                 ADD_FAILURE()
665                         << "Attempted to set the state of an LED that the EventHub declared "
666                         "was not present.  led=" << led;
667             }
668         }
669     }
670 
getVirtualKeyDefinitions(int32_t deviceId,Vector<VirtualKeyDefinition> & outVirtualKeys) const671     virtual void getVirtualKeyDefinitions(int32_t deviceId,
672             Vector<VirtualKeyDefinition>& outVirtualKeys) const {
673         outVirtualKeys.clear();
674 
675         Device* device = getDevice(deviceId);
676         if (device) {
677             outVirtualKeys.appendVector(device->virtualKeys);
678         }
679     }
680 
getKeyCharacterMap(int32_t) const681     virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
682         return NULL;
683     }
684 
setKeyboardLayoutOverlay(int32_t,const sp<KeyCharacterMap> &)685     virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
686         return false;
687     }
688 
vibrate(int32_t,nsecs_t)689     virtual void vibrate(int32_t, nsecs_t) {
690     }
691 
cancelVibrate(int32_t)692     virtual void cancelVibrate(int32_t) {
693     }
694 
isExternal(int32_t) const695     virtual bool isExternal(int32_t) const {
696         return false;
697     }
698 
dump(String8 &)699     virtual void dump(String8&) {
700     }
701 
monitor()702     virtual void monitor() {
703     }
704 
requestReopenDevices()705     virtual void requestReopenDevices() {
706     }
707 
wake()708     virtual void wake() {
709     }
710 };
711 
712 
713 // --- FakeInputReaderContext ---
714 
715 class FakeInputReaderContext : public InputReaderContext {
716     sp<EventHubInterface> mEventHub;
717     sp<InputReaderPolicyInterface> mPolicy;
718     sp<InputListenerInterface> mListener;
719     int32_t mGlobalMetaState;
720     bool mUpdateGlobalMetaStateWasCalled;
721     int32_t mGeneration;
722 
723 public:
FakeInputReaderContext(const sp<EventHubInterface> & eventHub,const sp<InputReaderPolicyInterface> & policy,const sp<InputListenerInterface> & listener)724     FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
725             const sp<InputReaderPolicyInterface>& policy,
726             const sp<InputListenerInterface>& listener) :
727             mEventHub(eventHub), mPolicy(policy), mListener(listener),
728             mGlobalMetaState(0) {
729     }
730 
~FakeInputReaderContext()731     virtual ~FakeInputReaderContext() { }
732 
assertUpdateGlobalMetaStateWasCalled()733     void assertUpdateGlobalMetaStateWasCalled() {
734         ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
735                 << "Expected updateGlobalMetaState() to have been called.";
736         mUpdateGlobalMetaStateWasCalled = false;
737     }
738 
setGlobalMetaState(int32_t state)739     void setGlobalMetaState(int32_t state) {
740         mGlobalMetaState = state;
741     }
742 
743 private:
updateGlobalMetaState()744     virtual void updateGlobalMetaState() {
745         mUpdateGlobalMetaStateWasCalled = true;
746     }
747 
getGlobalMetaState()748     virtual int32_t getGlobalMetaState() {
749         return mGlobalMetaState;
750     }
751 
getEventHub()752     virtual EventHubInterface* getEventHub() {
753         return mEventHub.get();
754     }
755 
getPolicy()756     virtual InputReaderPolicyInterface* getPolicy() {
757         return mPolicy.get();
758     }
759 
getListener()760     virtual InputListenerInterface* getListener() {
761         return mListener.get();
762     }
763 
disableVirtualKeysUntil(nsecs_t)764     virtual void disableVirtualKeysUntil(nsecs_t) {
765     }
766 
shouldDropVirtualKey(nsecs_t,InputDevice *,int32_t,int32_t)767     virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
768         return false;
769     }
770 
fadePointer()771     virtual void fadePointer() {
772     }
773 
requestTimeoutAtTime(nsecs_t)774     virtual void requestTimeoutAtTime(nsecs_t) {
775     }
776 
bumpGeneration()777     virtual int32_t bumpGeneration() {
778         return ++mGeneration;
779     }
780 
getExternalStylusDevices(Vector<InputDeviceInfo> & outDevices)781     virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) {
782 
783     }
784 
dispatchExternalStylusState(const StylusState &)785     virtual void dispatchExternalStylusState(const StylusState&) {
786 
787     }
788 };
789 
790 
791 // --- FakeInputMapper ---
792 
793 class FakeInputMapper : public InputMapper {
794     uint32_t mSources;
795     int32_t mKeyboardType;
796     int32_t mMetaState;
797     KeyedVector<int32_t, int32_t> mKeyCodeStates;
798     KeyedVector<int32_t, int32_t> mScanCodeStates;
799     KeyedVector<int32_t, int32_t> mSwitchStates;
800     Vector<int32_t> mSupportedKeyCodes;
801     RawEvent mLastEvent;
802 
803     bool mConfigureWasCalled;
804     bool mResetWasCalled;
805     bool mProcessWasCalled;
806 
807 public:
FakeInputMapper(InputDevice * device,uint32_t sources)808     FakeInputMapper(InputDevice* device, uint32_t sources) :
809             InputMapper(device),
810             mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
811             mMetaState(0),
812             mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
813     }
814 
~FakeInputMapper()815     virtual ~FakeInputMapper() { }
816 
setKeyboardType(int32_t keyboardType)817     void setKeyboardType(int32_t keyboardType) {
818         mKeyboardType = keyboardType;
819     }
820 
setMetaState(int32_t metaState)821     void setMetaState(int32_t metaState) {
822         mMetaState = metaState;
823     }
824 
assertConfigureWasCalled()825     void assertConfigureWasCalled() {
826         ASSERT_TRUE(mConfigureWasCalled)
827                 << "Expected configure() to have been called.";
828         mConfigureWasCalled = false;
829     }
830 
assertResetWasCalled()831     void assertResetWasCalled() {
832         ASSERT_TRUE(mResetWasCalled)
833                 << "Expected reset() to have been called.";
834         mResetWasCalled = false;
835     }
836 
assertProcessWasCalled(RawEvent * outLastEvent=NULL)837     void assertProcessWasCalled(RawEvent* outLastEvent = NULL) {
838         ASSERT_TRUE(mProcessWasCalled)
839                 << "Expected process() to have been called.";
840         if (outLastEvent) {
841             *outLastEvent = mLastEvent;
842         }
843         mProcessWasCalled = false;
844     }
845 
setKeyCodeState(int32_t keyCode,int32_t state)846     void setKeyCodeState(int32_t keyCode, int32_t state) {
847         mKeyCodeStates.replaceValueFor(keyCode, state);
848     }
849 
setScanCodeState(int32_t scanCode,int32_t state)850     void setScanCodeState(int32_t scanCode, int32_t state) {
851         mScanCodeStates.replaceValueFor(scanCode, state);
852     }
853 
setSwitchState(int32_t switchCode,int32_t state)854     void setSwitchState(int32_t switchCode, int32_t state) {
855         mSwitchStates.replaceValueFor(switchCode, state);
856     }
857 
addSupportedKeyCode(int32_t keyCode)858     void addSupportedKeyCode(int32_t keyCode) {
859         mSupportedKeyCodes.add(keyCode);
860     }
861 
862 private:
getSources()863     virtual uint32_t getSources() {
864         return mSources;
865     }
866 
populateDeviceInfo(InputDeviceInfo * deviceInfo)867     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
868         InputMapper::populateDeviceInfo(deviceInfo);
869 
870         if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
871             deviceInfo->setKeyboardType(mKeyboardType);
872         }
873     }
874 
configure(nsecs_t,const InputReaderConfiguration *,uint32_t)875     virtual void configure(nsecs_t, const InputReaderConfiguration*, uint32_t) {
876         mConfigureWasCalled = true;
877     }
878 
reset(nsecs_t)879     virtual void reset(nsecs_t) {
880         mResetWasCalled = true;
881     }
882 
process(const RawEvent * rawEvent)883     virtual void process(const RawEvent* rawEvent) {
884         mLastEvent = *rawEvent;
885         mProcessWasCalled = true;
886     }
887 
getKeyCodeState(uint32_t,int32_t keyCode)888     virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
889         ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
890         return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
891     }
892 
getScanCodeState(uint32_t,int32_t scanCode)893     virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
894         ssize_t index = mScanCodeStates.indexOfKey(scanCode);
895         return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
896     }
897 
getSwitchState(uint32_t,int32_t switchCode)898     virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
899         ssize_t index = mSwitchStates.indexOfKey(switchCode);
900         return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
901     }
902 
markSupportedKeyCodes(uint32_t,size_t numCodes,const int32_t * keyCodes,uint8_t * outFlags)903     virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
904             const int32_t* keyCodes, uint8_t* outFlags) {
905         bool result = false;
906         for (size_t i = 0; i < numCodes; i++) {
907             for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
908                 if (keyCodes[i] == mSupportedKeyCodes[j]) {
909                     outFlags[i] = 1;
910                     result = true;
911                 }
912             }
913         }
914         return result;
915     }
916 
getMetaState()917     virtual int32_t getMetaState() {
918         return mMetaState;
919     }
920 
fadePointer()921     virtual void fadePointer() {
922     }
923 };
924 
925 
926 // --- InstrumentedInputReader ---
927 
928 class InstrumentedInputReader : public InputReader {
929     InputDevice* mNextDevice;
930 
931 public:
InstrumentedInputReader(const sp<EventHubInterface> & eventHub,const sp<InputReaderPolicyInterface> & policy,const sp<InputListenerInterface> & listener)932     InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
933             const sp<InputReaderPolicyInterface>& policy,
934             const sp<InputListenerInterface>& listener) :
935             InputReader(eventHub, policy, listener),
936             mNextDevice(NULL) {
937     }
938 
~InstrumentedInputReader()939     virtual ~InstrumentedInputReader() {
940         if (mNextDevice) {
941             delete mNextDevice;
942         }
943     }
944 
setNextDevice(InputDevice * device)945     void setNextDevice(InputDevice* device) {
946         mNextDevice = device;
947     }
948 
newDevice(int32_t deviceId,int32_t controllerNumber,const String8 & name,uint32_t classes)949     InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const String8& name,
950             uint32_t classes) {
951         InputDeviceIdentifier identifier;
952         identifier.name = name;
953         int32_t generation = deviceId + 1;
954         return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
955                 classes);
956     }
957 
958 protected:
createDeviceLocked(int32_t deviceId,int32_t controllerNumber,const InputDeviceIdentifier & identifier,uint32_t classes)959     virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
960             const InputDeviceIdentifier& identifier, uint32_t classes) {
961         if (mNextDevice) {
962             InputDevice* device = mNextDevice;
963             mNextDevice = NULL;
964             return device;
965         }
966         return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
967     }
968 
969     friend class InputReaderTest;
970 };
971 
972 
973 // --- InputReaderTest ---
974 
975 class InputReaderTest : public testing::Test {
976 protected:
977     sp<FakeInputListener> mFakeListener;
978     sp<FakeInputReaderPolicy> mFakePolicy;
979     sp<FakeEventHub> mFakeEventHub;
980     sp<InstrumentedInputReader> mReader;
981 
SetUp()982     virtual void SetUp() {
983         mFakeEventHub = new FakeEventHub();
984         mFakePolicy = new FakeInputReaderPolicy();
985         mFakeListener = new FakeInputListener();
986 
987         mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
988     }
989 
TearDown()990     virtual void TearDown() {
991         mReader.clear();
992 
993         mFakeListener.clear();
994         mFakePolicy.clear();
995         mFakeEventHub.clear();
996     }
997 
addDevice(int32_t deviceId,const String8 & name,uint32_t classes,const PropertyMap * configuration)998     void addDevice(int32_t deviceId, const String8& name, uint32_t classes,
999             const PropertyMap* configuration) {
1000         mFakeEventHub->addDevice(deviceId, name, classes);
1001 
1002         if (configuration) {
1003             mFakeEventHub->addConfigurationMap(deviceId, configuration);
1004         }
1005         mFakeEventHub->finishDeviceScan();
1006         mReader->loopOnce();
1007         mReader->loopOnce();
1008         mFakeEventHub->assertQueueIsEmpty();
1009     }
1010 
addDeviceWithFakeInputMapper(int32_t deviceId,int32_t controllerNumber,const String8 & name,uint32_t classes,uint32_t sources,const PropertyMap * configuration)1011     FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
1012             const String8& name, uint32_t classes, uint32_t sources,
1013             const PropertyMap* configuration) {
1014         InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1015         FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1016         device->addMapper(mapper);
1017         mReader->setNextDevice(device);
1018         addDevice(deviceId, name, classes, configuration);
1019         return mapper;
1020     }
1021 };
1022 
TEST_F(InputReaderTest,GetInputDevices)1023 TEST_F(InputReaderTest, GetInputDevices) {
1024     ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),
1025             INPUT_DEVICE_CLASS_KEYBOARD, NULL));
1026     ASSERT_NO_FATAL_FAILURE(addDevice(2, String8("ignored"),
1027             0, NULL)); // no classes so device will be ignored
1028 
1029     Vector<InputDeviceInfo> inputDevices;
1030     mReader->getInputDevices(inputDevices);
1031 
1032     ASSERT_EQ(1U, inputDevices.size());
1033     ASSERT_EQ(1, inputDevices[0].getId());
1034     ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.string());
1035     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1036     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1037     ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1038 
1039     // Should also have received a notification describing the new input devices.
1040     inputDevices = mFakePolicy->getInputDevices();
1041     ASSERT_EQ(1U, inputDevices.size());
1042     ASSERT_EQ(1, inputDevices[0].getId());
1043     ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.string());
1044     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1045     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1046     ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1047 }
1048 
TEST_F(InputReaderTest,GetKeyCodeState_ForwardsRequestsToMappers)1049 TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
1050     FakeInputMapper* mapper = NULL;
1051     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1052             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1053     mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1054 
1055     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1056             AINPUT_SOURCE_ANY, AKEYCODE_A))
1057             << "Should return unknown when the device id is >= 0 but unknown.";
1058 
1059     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1060             AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1061             << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1062 
1063     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1064             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1065             << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1066 
1067     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1068             AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1069             << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1070 
1071     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1072             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1073             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1074 }
1075 
TEST_F(InputReaderTest,GetScanCodeState_ForwardsRequestsToMappers)1076 TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
1077     FakeInputMapper* mapper = NULL;
1078     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1079             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1080     mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1081 
1082     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1083             AINPUT_SOURCE_ANY, KEY_A))
1084             << "Should return unknown when the device id is >= 0 but unknown.";
1085 
1086     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1087             AINPUT_SOURCE_TRACKBALL, KEY_A))
1088             << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1089 
1090     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1091             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1092             << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1093 
1094     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1095             AINPUT_SOURCE_TRACKBALL, KEY_A))
1096             << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1097 
1098     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1099             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1100             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1101 }
1102 
TEST_F(InputReaderTest,GetSwitchState_ForwardsRequestsToMappers)1103 TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
1104     FakeInputMapper* mapper = NULL;
1105     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1106             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1107     mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1108 
1109     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1110             AINPUT_SOURCE_ANY, SW_LID))
1111             << "Should return unknown when the device id is >= 0 but unknown.";
1112 
1113     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1114             AINPUT_SOURCE_TRACKBALL, SW_LID))
1115             << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1116 
1117     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1118             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1119             << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1120 
1121     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1122             AINPUT_SOURCE_TRACKBALL, SW_LID))
1123             << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1124 
1125     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1126             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1127             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1128 }
1129 
TEST_F(InputReaderTest,MarkSupportedKeyCodes_ForwardsRequestsToMappers)1130 TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
1131     FakeInputMapper* mapper = NULL;
1132     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1133             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1134     mapper->addSupportedKeyCode(AKEYCODE_A);
1135     mapper->addSupportedKeyCode(AKEYCODE_B);
1136 
1137     const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1138     uint8_t flags[4] = { 0, 0, 0, 1 };
1139 
1140     ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1141             << "Should return false when device id is >= 0 but unknown.";
1142     ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1143 
1144     flags[3] = 1;
1145     ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1146             << "Should return false when device id is valid but the sources are not supported by the device.";
1147     ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1148 
1149     flags[3] = 1;
1150     ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1151             << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1152     ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1153 
1154     flags[3] = 1;
1155     ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1156             << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1157     ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1158 
1159     flags[3] = 1;
1160     ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1161             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1162     ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1163 }
1164 
TEST_F(InputReaderTest,LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged)1165 TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
1166     addDevice(1, String8("ignored"), INPUT_DEVICE_CLASS_KEYBOARD, NULL);
1167 
1168     NotifyConfigurationChangedArgs args;
1169 
1170     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1171     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1172 }
1173 
TEST_F(InputReaderTest,LoopOnce_ForwardsRawEventsToMappers)1174 TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
1175     FakeInputMapper* mapper = NULL;
1176     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1177             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1178 
1179     mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1180     mReader->loopOnce();
1181     ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1182 
1183     RawEvent event;
1184     ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1185     ASSERT_EQ(0, event.when);
1186     ASSERT_EQ(1, event.deviceId);
1187     ASSERT_EQ(EV_KEY, event.type);
1188     ASSERT_EQ(KEY_A, event.code);
1189     ASSERT_EQ(1, event.value);
1190 }
1191 
1192 
1193 // --- InputDeviceTest ---
1194 
1195 class InputDeviceTest : public testing::Test {
1196 protected:
1197     static const char* DEVICE_NAME;
1198     static const int32_t DEVICE_ID;
1199     static const int32_t DEVICE_GENERATION;
1200     static const int32_t DEVICE_CONTROLLER_NUMBER;
1201     static const uint32_t DEVICE_CLASSES;
1202 
1203     sp<FakeEventHub> mFakeEventHub;
1204     sp<FakeInputReaderPolicy> mFakePolicy;
1205     sp<FakeInputListener> mFakeListener;
1206     FakeInputReaderContext* mFakeContext;
1207 
1208     InputDevice* mDevice;
1209 
SetUp()1210     virtual void SetUp() {
1211         mFakeEventHub = new FakeEventHub();
1212         mFakePolicy = new FakeInputReaderPolicy();
1213         mFakeListener = new FakeInputListener();
1214         mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1215 
1216         mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
1217         InputDeviceIdentifier identifier;
1218         identifier.name = DEVICE_NAME;
1219         mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1220                 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1221     }
1222 
TearDown()1223     virtual void TearDown() {
1224         delete mDevice;
1225 
1226         delete mFakeContext;
1227         mFakeListener.clear();
1228         mFakePolicy.clear();
1229         mFakeEventHub.clear();
1230     }
1231 };
1232 
1233 const char* InputDeviceTest::DEVICE_NAME = "device";
1234 const int32_t InputDeviceTest::DEVICE_ID = 1;
1235 const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1236 const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1237 const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1238         | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1239 
TEST_F(InputDeviceTest,ImmutableProperties)1240 TEST_F(InputDeviceTest, ImmutableProperties) {
1241     ASSERT_EQ(DEVICE_ID, mDevice->getId());
1242     ASSERT_STREQ(DEVICE_NAME, mDevice->getName());
1243     ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1244 }
1245 
TEST_F(InputDeviceTest,WhenNoMappersAreRegistered_DeviceIsIgnored)1246 TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1247     // Configuration.
1248     InputReaderConfiguration config;
1249     mDevice->configure(ARBITRARY_TIME, &config, 0);
1250 
1251     // Reset.
1252     mDevice->reset(ARBITRARY_TIME);
1253 
1254     NotifyDeviceResetArgs resetArgs;
1255     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1256     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1257     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1258 
1259     // Metadata.
1260     ASSERT_TRUE(mDevice->isIgnored());
1261     ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1262 
1263     InputDeviceInfo info;
1264     mDevice->getDeviceInfo(&info);
1265     ASSERT_EQ(DEVICE_ID, info.getId());
1266     ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.string());
1267     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1268     ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1269 
1270     // State queries.
1271     ASSERT_EQ(0, mDevice->getMetaState());
1272 
1273     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1274             << "Ignored device should return unknown key code state.";
1275     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1276             << "Ignored device should return unknown scan code state.";
1277     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1278             << "Ignored device should return unknown switch state.";
1279 
1280     const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1281     uint8_t flags[2] = { 0, 1 };
1282     ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1283             << "Ignored device should never mark any key codes.";
1284     ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1285     ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1286 }
1287 
TEST_F(InputDeviceTest,WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers)1288 TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1289     // Configuration.
1290     mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1291 
1292     FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1293     mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1294     mapper1->setMetaState(AMETA_ALT_ON);
1295     mapper1->addSupportedKeyCode(AKEYCODE_A);
1296     mapper1->addSupportedKeyCode(AKEYCODE_B);
1297     mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1298     mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1299     mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1300     mapper1->setScanCodeState(3, AKEY_STATE_UP);
1301     mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1302     mDevice->addMapper(mapper1);
1303 
1304     FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1305     mapper2->setMetaState(AMETA_SHIFT_ON);
1306     mDevice->addMapper(mapper2);
1307 
1308     InputReaderConfiguration config;
1309     mDevice->configure(ARBITRARY_TIME, &config, 0);
1310 
1311     String8 propertyValue;
1312     ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1313             << "Device should have read configuration during configuration phase.";
1314     ASSERT_STREQ("value", propertyValue.string());
1315 
1316     ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1317     ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1318 
1319     // Reset
1320     mDevice->reset(ARBITRARY_TIME);
1321     ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1322     ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1323 
1324     NotifyDeviceResetArgs resetArgs;
1325     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1326     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1327     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1328 
1329     // Metadata.
1330     ASSERT_FALSE(mDevice->isIgnored());
1331     ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1332 
1333     InputDeviceInfo info;
1334     mDevice->getDeviceInfo(&info);
1335     ASSERT_EQ(DEVICE_ID, info.getId());
1336     ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.string());
1337     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1338     ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1339 
1340     // State queries.
1341     ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1342             << "Should query mappers and combine meta states.";
1343 
1344     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1345             << "Should return unknown key code state when source not supported.";
1346     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1347             << "Should return unknown scan code state when source not supported.";
1348     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1349             << "Should return unknown switch state when source not supported.";
1350 
1351     ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1352             << "Should query mapper when source is supported.";
1353     ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1354             << "Should query mapper when source is supported.";
1355     ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1356             << "Should query mapper when source is supported.";
1357 
1358     const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1359     uint8_t flags[4] = { 0, 0, 0, 1 };
1360     ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1361             << "Should do nothing when source is unsupported.";
1362     ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1363     ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1364     ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1365     ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1366 
1367     ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1368             << "Should query mapper when source is supported.";
1369     ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1370     ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1371     ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1372     ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1373 
1374     // Event handling.
1375     RawEvent event;
1376     mDevice->process(&event, 1);
1377 
1378     ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1379     ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1380 }
1381 
1382 
1383 // --- InputMapperTest ---
1384 
1385 class InputMapperTest : public testing::Test {
1386 protected:
1387     static const char* DEVICE_NAME;
1388     static const int32_t DEVICE_ID;
1389     static const int32_t DEVICE_GENERATION;
1390     static const int32_t DEVICE_CONTROLLER_NUMBER;
1391     static const uint32_t DEVICE_CLASSES;
1392 
1393     sp<FakeEventHub> mFakeEventHub;
1394     sp<FakeInputReaderPolicy> mFakePolicy;
1395     sp<FakeInputListener> mFakeListener;
1396     FakeInputReaderContext* mFakeContext;
1397     InputDevice* mDevice;
1398 
SetUp()1399     virtual void SetUp() {
1400         mFakeEventHub = new FakeEventHub();
1401         mFakePolicy = new FakeInputReaderPolicy();
1402         mFakeListener = new FakeInputListener();
1403         mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1404         InputDeviceIdentifier identifier;
1405         identifier.name = DEVICE_NAME;
1406         mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1407                 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1408 
1409         mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
1410     }
1411 
TearDown()1412     virtual void TearDown() {
1413         delete mDevice;
1414         delete mFakeContext;
1415         mFakeListener.clear();
1416         mFakePolicy.clear();
1417         mFakeEventHub.clear();
1418     }
1419 
addConfigurationProperty(const char * key,const char * value)1420     void addConfigurationProperty(const char* key, const char* value) {
1421         mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8(key), String8(value));
1422     }
1423 
addMapperAndConfigure(InputMapper * mapper)1424     void addMapperAndConfigure(InputMapper* mapper) {
1425         mDevice->addMapper(mapper);
1426         mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
1427         mDevice->reset(ARBITRARY_TIME);
1428     }
1429 
setDisplayInfoAndReconfigure(int32_t displayId,int32_t width,int32_t height,int32_t orientation)1430     void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1431             int32_t orientation) {
1432         mFakePolicy->setDisplayInfo(displayId, width, height, orientation);
1433         mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
1434                 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1435     }
1436 
process(InputMapper * mapper,nsecs_t when,int32_t deviceId,int32_t type,int32_t code,int32_t value)1437     static void process(InputMapper* mapper, nsecs_t when, int32_t deviceId, int32_t type,
1438             int32_t code, int32_t value) {
1439         RawEvent event;
1440         event.when = when;
1441         event.deviceId = deviceId;
1442         event.type = type;
1443         event.code = code;
1444         event.value = value;
1445         mapper->process(&event);
1446     }
1447 
assertMotionRange(const InputDeviceInfo & info,int32_t axis,uint32_t source,float min,float max,float flat,float fuzz)1448     static void assertMotionRange(const InputDeviceInfo& info,
1449             int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1450         const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
1451         ASSERT_TRUE(range != NULL) << "Axis: " << axis << " Source: " << source;
1452         ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1453         ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1454         ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1455         ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1456         ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1457         ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1458     }
1459 
assertPointerCoords(const PointerCoords & coords,float x,float y,float pressure,float size,float touchMajor,float touchMinor,float toolMajor,float toolMinor,float orientation,float distance)1460     static void assertPointerCoords(const PointerCoords& coords,
1461             float x, float y, float pressure, float size,
1462             float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1463             float orientation, float distance) {
1464         ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1465         ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1466         ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1467         ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1468         ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1469         ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1470         ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1471         ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1472         ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1473         ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1474     }
1475 
assertPosition(const sp<FakePointerController> & controller,float x,float y)1476     static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1477         float actualX, actualY;
1478         controller->getPosition(&actualX, &actualY);
1479         ASSERT_NEAR(x, actualX, 1);
1480         ASSERT_NEAR(y, actualY, 1);
1481     }
1482 };
1483 
1484 const char* InputMapperTest::DEVICE_NAME = "device";
1485 const int32_t InputMapperTest::DEVICE_ID = 1;
1486 const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1487 const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1488 const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1489 
1490 
1491 // --- SwitchInputMapperTest ---
1492 
1493 class SwitchInputMapperTest : public InputMapperTest {
1494 protected:
1495 };
1496 
TEST_F(SwitchInputMapperTest,GetSources)1497 TEST_F(SwitchInputMapperTest, GetSources) {
1498     SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1499     addMapperAndConfigure(mapper);
1500 
1501     ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1502 }
1503 
TEST_F(SwitchInputMapperTest,GetSwitchState)1504 TEST_F(SwitchInputMapperTest, GetSwitchState) {
1505     SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1506     addMapperAndConfigure(mapper);
1507 
1508     mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1509     ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1510 
1511     mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1512     ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1513 }
1514 
TEST_F(SwitchInputMapperTest,Process)1515 TEST_F(SwitchInputMapperTest, Process) {
1516     SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1517     addMapperAndConfigure(mapper);
1518 
1519     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_LID, 1);
1520     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1521     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_HEADPHONE_INSERT, 0);
1522     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
1523 
1524     NotifySwitchArgs args;
1525     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1526     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1527     ASSERT_EQ((1 << SW_LID) | (1 << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1528     ASSERT_EQ((1 << SW_LID) | (1 << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
1529             args.switchMask);
1530     ASSERT_EQ(uint32_t(0), args.policyFlags);
1531 }
1532 
1533 
1534 // --- KeyboardInputMapperTest ---
1535 
1536 class KeyboardInputMapperTest : public InputMapperTest {
1537 protected:
1538     void testDPadKeyRotation(KeyboardInputMapper* mapper,
1539             int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1540 };
1541 
testDPadKeyRotation(KeyboardInputMapper * mapper,int32_t originalScanCode,int32_t,int32_t rotatedKeyCode)1542 void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
1543         int32_t originalScanCode, int32_t, int32_t rotatedKeyCode) {
1544     NotifyKeyArgs args;
1545 
1546     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, 1);
1547     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1548     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1549     ASSERT_EQ(originalScanCode, args.scanCode);
1550     ASSERT_EQ(rotatedKeyCode, args.keyCode);
1551 
1552     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, 0);
1553     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1554     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1555     ASSERT_EQ(originalScanCode, args.scanCode);
1556     ASSERT_EQ(rotatedKeyCode, args.keyCode);
1557 }
1558 
1559 
TEST_F(KeyboardInputMapperTest,GetSources)1560 TEST_F(KeyboardInputMapperTest, GetSources) {
1561     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1562             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1563     addMapperAndConfigure(mapper);
1564 
1565     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1566 }
1567 
TEST_F(KeyboardInputMapperTest,Process_SimpleKeyPress)1568 TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
1569     const int32_t USAGE_A = 0x070004;
1570     const int32_t USAGE_UNKNOWN = 0x07ffff;
1571     mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
1572     mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
1573 
1574     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1575             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1576     addMapperAndConfigure(mapper);
1577 
1578     // Key down by scan code.
1579     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1580             EV_KEY, KEY_HOME, 1);
1581     NotifyKeyArgs args;
1582     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1583     ASSERT_EQ(DEVICE_ID, args.deviceId);
1584     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1585     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1586     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1587     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1588     ASSERT_EQ(KEY_HOME, args.scanCode);
1589     ASSERT_EQ(AMETA_NONE, args.metaState);
1590     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1591     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1592     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1593 
1594     // Key up by scan code.
1595     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1596             EV_KEY, KEY_HOME, 0);
1597     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1598     ASSERT_EQ(DEVICE_ID, args.deviceId);
1599     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1600     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1601     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1602     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1603     ASSERT_EQ(KEY_HOME, args.scanCode);
1604     ASSERT_EQ(AMETA_NONE, args.metaState);
1605     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1606     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1607     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1608 
1609     // Key down by usage code.
1610     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1611             EV_MSC, MSC_SCAN, USAGE_A);
1612     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1613             EV_KEY, 0, 1);
1614     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1615     ASSERT_EQ(DEVICE_ID, args.deviceId);
1616     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1617     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1618     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1619     ASSERT_EQ(AKEYCODE_A, args.keyCode);
1620     ASSERT_EQ(0, args.scanCode);
1621     ASSERT_EQ(AMETA_NONE, args.metaState);
1622     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1623     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1624     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1625 
1626     // Key up by usage code.
1627     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1628             EV_MSC, MSC_SCAN, USAGE_A);
1629     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1630             EV_KEY, 0, 0);
1631     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1632     ASSERT_EQ(DEVICE_ID, args.deviceId);
1633     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1634     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1635     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1636     ASSERT_EQ(AKEYCODE_A, args.keyCode);
1637     ASSERT_EQ(0, args.scanCode);
1638     ASSERT_EQ(AMETA_NONE, args.metaState);
1639     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1640     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1641     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1642 
1643     // Key down with unknown scan code or usage code.
1644     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1645             EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
1646     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1647             EV_KEY, KEY_UNKNOWN, 1);
1648     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1649     ASSERT_EQ(DEVICE_ID, args.deviceId);
1650     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1651     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1652     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1653     ASSERT_EQ(0, args.keyCode);
1654     ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
1655     ASSERT_EQ(AMETA_NONE, args.metaState);
1656     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1657     ASSERT_EQ(0U, args.policyFlags);
1658     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1659 
1660     // Key up with unknown scan code or usage code.
1661     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1662             EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
1663     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1664             EV_KEY, KEY_UNKNOWN, 0);
1665     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1666     ASSERT_EQ(DEVICE_ID, args.deviceId);
1667     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1668     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1669     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1670     ASSERT_EQ(0, args.keyCode);
1671     ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
1672     ASSERT_EQ(AMETA_NONE, args.metaState);
1673     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1674     ASSERT_EQ(0U, args.policyFlags);
1675     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1676 }
1677 
TEST_F(KeyboardInputMapperTest,Process_ShouldUpdateMetaState)1678 TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
1679     mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
1680     mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
1681 
1682     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1683             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1684     addMapperAndConfigure(mapper);
1685 
1686     // Initial metastate.
1687     ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1688 
1689     // Metakey down.
1690     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1691             EV_KEY, KEY_LEFTSHIFT, 1);
1692     NotifyKeyArgs args;
1693     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1694     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1695     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1696     ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1697 
1698     // Key down.
1699     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1700             EV_KEY, KEY_A, 1);
1701     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1702     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1703     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1704 
1705     // Key up.
1706     process(mapper, ARBITRARY_TIME + 2, DEVICE_ID,
1707             EV_KEY, KEY_A, 0);
1708     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1709     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1710     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1711 
1712     // Metakey up.
1713     process(mapper, ARBITRARY_TIME + 3, DEVICE_ID,
1714             EV_KEY, KEY_LEFTSHIFT, 0);
1715     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1716     ASSERT_EQ(AMETA_NONE, args.metaState);
1717     ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1718     ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1719 }
1720 
TEST_F(KeyboardInputMapperTest,Process_WhenNotOrientationAware_ShouldNotRotateDPad)1721 TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
1722     mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
1723     mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
1724     mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
1725     mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
1726 
1727     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1728             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1729     addMapperAndConfigure(mapper);
1730 
1731     setDisplayInfoAndReconfigure(DISPLAY_ID,
1732             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1733             DISPLAY_ORIENTATION_90);
1734     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1735             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1736     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1737             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1738     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1739             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1740     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1741             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1742 }
1743 
TEST_F(KeyboardInputMapperTest,Process_WhenOrientationAware_ShouldRotateDPad)1744 TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
1745     mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
1746     mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
1747     mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
1748     mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
1749 
1750     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1751             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1752     addConfigurationProperty("keyboard.orientationAware", "1");
1753     addMapperAndConfigure(mapper);
1754 
1755     setDisplayInfoAndReconfigure(DISPLAY_ID,
1756             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1757             DISPLAY_ORIENTATION_0);
1758     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1759             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1760     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1761             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1762     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1763             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1764     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1765             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1766 
1767     setDisplayInfoAndReconfigure(DISPLAY_ID,
1768             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1769             DISPLAY_ORIENTATION_90);
1770     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1771             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
1772     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1773             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
1774     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1775             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
1776     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1777             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
1778 
1779     setDisplayInfoAndReconfigure(DISPLAY_ID,
1780             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1781             DISPLAY_ORIENTATION_180);
1782     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1783             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
1784     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1785             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
1786     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1787             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
1788     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1789             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
1790 
1791     setDisplayInfoAndReconfigure(DISPLAY_ID,
1792             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1793             DISPLAY_ORIENTATION_270);
1794     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1795             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
1796     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1797             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
1798     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1799             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
1800     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1801             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
1802 
1803     // Special case: if orientation changes while key is down, we still emit the same keycode
1804     // in the key up as we did in the key down.
1805     NotifyKeyArgs args;
1806 
1807     setDisplayInfoAndReconfigure(DISPLAY_ID,
1808             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1809             DISPLAY_ORIENTATION_270);
1810     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, 1);
1811     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1812     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1813     ASSERT_EQ(KEY_UP, args.scanCode);
1814     ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1815 
1816     setDisplayInfoAndReconfigure(DISPLAY_ID,
1817             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1818             DISPLAY_ORIENTATION_180);
1819     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, 0);
1820     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1821     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1822     ASSERT_EQ(KEY_UP, args.scanCode);
1823     ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1824 }
1825 
TEST_F(KeyboardInputMapperTest,GetKeyCodeState)1826 TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
1827     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1828             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1829     addMapperAndConfigure(mapper);
1830 
1831     mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
1832     ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
1833 
1834     mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
1835     ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
1836 }
1837 
TEST_F(KeyboardInputMapperTest,GetScanCodeState)1838 TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
1839     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1840             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1841     addMapperAndConfigure(mapper);
1842 
1843     mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
1844     ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
1845 
1846     mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
1847     ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
1848 }
1849 
TEST_F(KeyboardInputMapperTest,MarkSupportedKeyCodes)1850 TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
1851     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1852             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1853     addMapperAndConfigure(mapper);
1854 
1855     mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
1856 
1857     const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1858     uint8_t flags[2] = { 0, 0 };
1859     ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
1860     ASSERT_TRUE(flags[0]);
1861     ASSERT_FALSE(flags[1]);
1862 }
1863 
TEST_F(KeyboardInputMapperTest,Process_LockedKeysShouldToggleMetaStateAndLeds)1864 TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
1865     mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
1866     mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
1867     mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
1868     mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
1869     mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
1870     mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
1871 
1872     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1873             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1874     addMapperAndConfigure(mapper);
1875 
1876     // Initialization should have turned all of the lights off.
1877     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1878     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1879     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1880 
1881     // Toggle caps lock on.
1882     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1883             EV_KEY, KEY_CAPSLOCK, 1);
1884     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1885             EV_KEY, KEY_CAPSLOCK, 0);
1886     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1887     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1888     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1889     ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
1890 
1891     // Toggle num lock on.
1892     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1893             EV_KEY, KEY_NUMLOCK, 1);
1894     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1895             EV_KEY, KEY_NUMLOCK, 0);
1896     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1897     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1898     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1899     ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
1900 
1901     // Toggle caps lock off.
1902     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1903             EV_KEY, KEY_CAPSLOCK, 1);
1904     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1905             EV_KEY, KEY_CAPSLOCK, 0);
1906     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1907     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1908     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1909     ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
1910 
1911     // Toggle scroll lock on.
1912     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1913             EV_KEY, KEY_SCROLLLOCK, 1);
1914     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1915             EV_KEY, KEY_SCROLLLOCK, 0);
1916     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1917     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1918     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1919     ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
1920 
1921     // Toggle num lock off.
1922     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1923             EV_KEY, KEY_NUMLOCK, 1);
1924     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1925             EV_KEY, KEY_NUMLOCK, 0);
1926     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1927     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1928     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1929     ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
1930 
1931     // Toggle scroll lock off.
1932     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1933             EV_KEY, KEY_SCROLLLOCK, 1);
1934     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1935             EV_KEY, KEY_SCROLLLOCK, 0);
1936     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1937     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1938     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1939     ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1940 }
1941 
1942 
1943 // --- CursorInputMapperTest ---
1944 
1945 class CursorInputMapperTest : public InputMapperTest {
1946 protected:
1947     static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
1948 
1949     sp<FakePointerController> mFakePointerController;
1950 
SetUp()1951     virtual void SetUp() {
1952         InputMapperTest::SetUp();
1953 
1954         mFakePointerController = new FakePointerController();
1955         mFakePolicy->setPointerController(DEVICE_ID, mFakePointerController);
1956     }
1957 
1958     void testMotionRotation(CursorInputMapper* mapper,
1959             int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
1960 };
1961 
1962 const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
1963 
testMotionRotation(CursorInputMapper * mapper,int32_t originalX,int32_t originalY,int32_t rotatedX,int32_t rotatedY)1964 void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
1965         int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
1966     NotifyMotionArgs args;
1967 
1968     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, originalX);
1969     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, originalY);
1970     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
1971     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
1972     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1973     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
1974             float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
1975             float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
1976             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
1977 }
1978 
TEST_F(CursorInputMapperTest,WhenModeIsPointer_GetSources_ReturnsMouse)1979 TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
1980     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
1981     addConfigurationProperty("cursor.mode", "pointer");
1982     addMapperAndConfigure(mapper);
1983 
1984     ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
1985 }
1986 
TEST_F(CursorInputMapperTest,WhenModeIsNavigation_GetSources_ReturnsTrackball)1987 TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
1988     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
1989     addConfigurationProperty("cursor.mode", "navigation");
1990     addMapperAndConfigure(mapper);
1991 
1992     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
1993 }
1994 
TEST_F(CursorInputMapperTest,WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController)1995 TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
1996     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
1997     addConfigurationProperty("cursor.mode", "pointer");
1998     addMapperAndConfigure(mapper);
1999 
2000     InputDeviceInfo info;
2001     mapper->populateDeviceInfo(&info);
2002 
2003     // Initially there may not be a valid motion range.
2004     ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2005     ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
2006     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2007             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2008 
2009     // When the bounds are set, then there should be a valid motion range.
2010     mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2011 
2012     InputDeviceInfo info2;
2013     mapper->populateDeviceInfo(&info2);
2014 
2015     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2016             AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2017             1, 800 - 1, 0.0f, 0.0f));
2018     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2019             AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2020             2, 480 - 1, 0.0f, 0.0f));
2021     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2022             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2023             0.0f, 1.0f, 0.0f, 0.0f));
2024 }
2025 
TEST_F(CursorInputMapperTest,WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange)2026 TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2027     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2028     addConfigurationProperty("cursor.mode", "navigation");
2029     addMapperAndConfigure(mapper);
2030 
2031     InputDeviceInfo info;
2032     mapper->populateDeviceInfo(&info);
2033 
2034     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2035             AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2036             -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2037     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2038             AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2039             -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2040     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2041             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2042             0.0f, 1.0f, 0.0f, 0.0f));
2043 }
2044 
TEST_F(CursorInputMapperTest,Process_ShouldSetAllFieldsAndIncludeGlobalMetaState)2045 TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2046     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2047     addConfigurationProperty("cursor.mode", "navigation");
2048     addMapperAndConfigure(mapper);
2049 
2050     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2051 
2052     NotifyMotionArgs args;
2053 
2054     // Button press.
2055     // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
2056     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
2057     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2058     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2059     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2060     ASSERT_EQ(DEVICE_ID, args.deviceId);
2061     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2062     ASSERT_EQ(uint32_t(0), args.policyFlags);
2063     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2064     ASSERT_EQ(0, args.flags);
2065     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2066     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2067     ASSERT_EQ(0, args.edgeFlags);
2068     ASSERT_EQ(uint32_t(1), args.pointerCount);
2069     ASSERT_EQ(0, args.pointerProperties[0].id);
2070     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2071     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2072             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2073     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2074     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2075     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2076 
2077     // Button release.  Should have same down time.
2078     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
2079     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2080     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2081     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2082     ASSERT_EQ(DEVICE_ID, args.deviceId);
2083     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2084     ASSERT_EQ(uint32_t(0), args.policyFlags);
2085     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2086     ASSERT_EQ(0, args.flags);
2087     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2088     ASSERT_EQ(0, args.buttonState);
2089     ASSERT_EQ(0, args.edgeFlags);
2090     ASSERT_EQ(uint32_t(1), args.pointerCount);
2091     ASSERT_EQ(0, args.pointerProperties[0].id);
2092     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2093     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2094             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2095     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2096     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2097     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2098 }
2099 
TEST_F(CursorInputMapperTest,Process_ShouldHandleIndependentXYUpdates)2100 TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2101     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2102     addConfigurationProperty("cursor.mode", "navigation");
2103     addMapperAndConfigure(mapper);
2104 
2105     NotifyMotionArgs args;
2106 
2107     // Motion in X but not Y.
2108     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 1);
2109     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2110     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2111     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2112     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2113             1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2114 
2115     // Motion in Y but not X.
2116     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, -2);
2117     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2118     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2119     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2120     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2121             0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2122 }
2123 
TEST_F(CursorInputMapperTest,Process_ShouldHandleIndependentButtonUpdates)2124 TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2125     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2126     addConfigurationProperty("cursor.mode", "navigation");
2127     addMapperAndConfigure(mapper);
2128 
2129     NotifyMotionArgs args;
2130 
2131     // Button press.
2132     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
2133     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2134     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2135     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2136     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2137             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2138 
2139     // Button release.
2140     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
2141     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2142     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2143     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2144     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2145             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2146 }
2147 
TEST_F(CursorInputMapperTest,Process_ShouldHandleCombinedXYAndButtonUpdates)2148 TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2149     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2150     addConfigurationProperty("cursor.mode", "navigation");
2151     addMapperAndConfigure(mapper);
2152 
2153     NotifyMotionArgs args;
2154 
2155     // Combined X, Y and Button.
2156     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 1);
2157     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, -2);
2158     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
2159     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2160     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2161     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2162     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2163             1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2164             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2165 
2166     // Move X, Y a bit while pressed.
2167     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 2);
2168     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 1);
2169     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2170     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2171     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2172     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2173             2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2174             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2175 
2176     // Release Button.
2177     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
2178     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2179     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2180     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2181     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2182             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2183 }
2184 
TEST_F(CursorInputMapperTest,Process_WhenNotOrientationAware_ShouldNotRotateMotions)2185 TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2186     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2187     addConfigurationProperty("cursor.mode", "navigation");
2188     addMapperAndConfigure(mapper);
2189 
2190     setDisplayInfoAndReconfigure(DISPLAY_ID,
2191             DISPLAY_WIDTH, DISPLAY_HEIGHT,
2192             DISPLAY_ORIENTATION_90);
2193     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
2194     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1,  1));
2195     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  1,  0));
2196     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1, -1));
2197     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0, -1));
2198     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2199     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0, -1,  0));
2200     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1,  1));
2201 }
2202 
TEST_F(CursorInputMapperTest,Process_WhenOrientationAware_ShouldRotateMotions)2203 TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2204     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2205     addConfigurationProperty("cursor.mode", "navigation");
2206     addConfigurationProperty("cursor.orientationAware", "1");
2207     addMapperAndConfigure(mapper);
2208 
2209     setDisplayInfoAndReconfigure(DISPLAY_ID,
2210             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0);
2211     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
2212     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1,  1));
2213     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  1,  0));
2214     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1, -1));
2215     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0, -1));
2216     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2217     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0, -1,  0));
2218     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1,  1));
2219 
2220     setDisplayInfoAndReconfigure(DISPLAY_ID,
2221             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_90);
2222     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  1,  0));
2223     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1, -1));
2224     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  0, -1));
2225     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1, -1, -1));
2226     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1, -1,  0));
2227     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1,  1));
2228     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  0,  1));
2229     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1,  1));
2230 
2231     setDisplayInfoAndReconfigure(DISPLAY_ID,
2232             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_180);
2233     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0, -1));
2234     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1, -1));
2235     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0, -1,  0));
2236     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1, -1,  1));
2237     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0,  1));
2238     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1,  1,  1));
2239     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  1,  0));
2240     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1, -1));
2241 
2242     setDisplayInfoAndReconfigure(DISPLAY_ID,
2243             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_270);
2244     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1, -1,  0));
2245     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1,  1));
2246     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  0,  1));
2247     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1,  1));
2248     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  1,  0));
2249     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1,  1, -1));
2250     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  0, -1));
2251     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1, -1));
2252 }
2253 
TEST_F(CursorInputMapperTest,Process_ShouldHandleAllButtons)2254 TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2255     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2256     addConfigurationProperty("cursor.mode", "pointer");
2257     addMapperAndConfigure(mapper);
2258 
2259     mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2260     mFakePointerController->setPosition(100, 200);
2261     mFakePointerController->setButtonState(0);
2262 
2263     NotifyMotionArgs motionArgs;
2264     NotifyKeyArgs keyArgs;
2265 
2266     // press BTN_LEFT, release BTN_LEFT
2267     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_LEFT, 1);
2268     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2269     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2270     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2271     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2272     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2273     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2274             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2275 
2276     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_LEFT, 0);
2277     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2278     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2279     ASSERT_EQ(0, motionArgs.buttonState);
2280     ASSERT_EQ(0, mFakePointerController->getButtonState());
2281     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2282     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2283             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2284 
2285     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2286     ASSERT_EQ(0, motionArgs.buttonState);
2287     ASSERT_EQ(0, mFakePointerController->getButtonState());
2288     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2289     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2290             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2291 
2292     // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
2293     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_RIGHT, 1);
2294     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 1);
2295     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2296     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2297     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2298     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2299             motionArgs.buttonState);
2300     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2301             mFakePointerController->getButtonState());
2302     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2303             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2304 
2305     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_RIGHT, 0);
2306     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2307     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2308     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2309     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
2310     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2311     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2312             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2313 
2314     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 0);
2315     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2316     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2317     ASSERT_EQ(0, motionArgs.buttonState);
2318     ASSERT_EQ(0, mFakePointerController->getButtonState());
2319     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2320     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2321             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2322     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2323     ASSERT_EQ(0, motionArgs.buttonState);
2324     ASSERT_EQ(0, mFakePointerController->getButtonState());
2325     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2326     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2327             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2328 
2329     // press BTN_BACK, release BTN_BACK
2330     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_BACK, 1);
2331     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2332     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2333     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2334     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2335     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2336     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2337     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
2338     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2339     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2340             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2341 
2342     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_BACK, 0);
2343     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2344     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2345     ASSERT_EQ(0, motionArgs.buttonState);
2346     ASSERT_EQ(0, mFakePointerController->getButtonState());
2347     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2348     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2349             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2350     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2351     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2352     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2353 
2354     // press BTN_SIDE, release BTN_SIDE
2355     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_SIDE, 1);
2356     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2357     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2358     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2359     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2360     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2361     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2362     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
2363     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2364     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2365             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2366 
2367     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_SIDE, 0);
2368     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2369     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2370     ASSERT_EQ(0, motionArgs.buttonState);
2371     ASSERT_EQ(0, mFakePointerController->getButtonState());
2372     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2373     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2374             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2375     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2376     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2377     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2378 
2379     // press BTN_FORWARD, release BTN_FORWARD
2380     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_FORWARD, 1);
2381     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2382     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2383     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2384     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2385     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2386     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2387     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
2388     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2389     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2390             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2391 
2392     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_FORWARD, 0);
2393     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2394     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2395     ASSERT_EQ(0, motionArgs.buttonState);
2396     ASSERT_EQ(0, mFakePointerController->getButtonState());
2397     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2398     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2399             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2400     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2401     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2402     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2403 
2404     // press BTN_EXTRA, release BTN_EXTRA
2405     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_EXTRA, 1);
2406     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2407     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2408     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2409     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2410     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2411     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2412     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
2413     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2414     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2415             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2416 
2417     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_EXTRA, 0);
2418     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2419     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2420     ASSERT_EQ(0, motionArgs.buttonState);
2421     ASSERT_EQ(0, mFakePointerController->getButtonState());
2422     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2423     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2424             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2425     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2426     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2427     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2428 }
2429 
TEST_F(CursorInputMapperTest,Process_WhenModeIsPointer_ShouldMoveThePointerAround)2430 TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
2431     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2432     addConfigurationProperty("cursor.mode", "pointer");
2433     addMapperAndConfigure(mapper);
2434 
2435     mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2436     mFakePointerController->setPosition(100, 200);
2437     mFakePointerController->setButtonState(0);
2438 
2439     NotifyMotionArgs args;
2440 
2441     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 10);
2442     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 20);
2443     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2444     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2445     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
2446     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2447             110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2448     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
2449 }
2450 
2451 
2452 // --- TouchInputMapperTest ---
2453 
2454 class TouchInputMapperTest : public InputMapperTest {
2455 protected:
2456     static const int32_t RAW_X_MIN;
2457     static const int32_t RAW_X_MAX;
2458     static const int32_t RAW_Y_MIN;
2459     static const int32_t RAW_Y_MAX;
2460     static const int32_t RAW_TOUCH_MIN;
2461     static const int32_t RAW_TOUCH_MAX;
2462     static const int32_t RAW_TOOL_MIN;
2463     static const int32_t RAW_TOOL_MAX;
2464     static const int32_t RAW_PRESSURE_MIN;
2465     static const int32_t RAW_PRESSURE_MAX;
2466     static const int32_t RAW_ORIENTATION_MIN;
2467     static const int32_t RAW_ORIENTATION_MAX;
2468     static const int32_t RAW_DISTANCE_MIN;
2469     static const int32_t RAW_DISTANCE_MAX;
2470     static const int32_t RAW_TILT_MIN;
2471     static const int32_t RAW_TILT_MAX;
2472     static const int32_t RAW_ID_MIN;
2473     static const int32_t RAW_ID_MAX;
2474     static const int32_t RAW_SLOT_MIN;
2475     static const int32_t RAW_SLOT_MAX;
2476     static const float X_PRECISION;
2477     static const float Y_PRECISION;
2478 
2479     static const float GEOMETRIC_SCALE;
2480     static const TouchAffineTransformation AFFINE_TRANSFORM;
2481 
2482     static const VirtualKeyDefinition VIRTUAL_KEYS[2];
2483 
2484     enum Axes {
2485         POSITION = 1 << 0,
2486         TOUCH = 1 << 1,
2487         TOOL = 1 << 2,
2488         PRESSURE = 1 << 3,
2489         ORIENTATION = 1 << 4,
2490         MINOR = 1 << 5,
2491         ID = 1 << 6,
2492         DISTANCE = 1 << 7,
2493         TILT = 1 << 8,
2494         SLOT = 1 << 9,
2495         TOOL_TYPE = 1 << 10,
2496     };
2497 
2498     void prepareDisplay(int32_t orientation);
2499     void prepareVirtualKeys();
2500     void prepareLocationCalibration();
2501     int32_t toRawX(float displayX);
2502     int32_t toRawY(float displayY);
2503     float toCookedX(float rawX, float rawY);
2504     float toCookedY(float rawX, float rawY);
2505     float toDisplayX(int32_t rawX);
2506     float toDisplayY(int32_t rawY);
2507 };
2508 
2509 const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
2510 const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
2511 const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
2512 const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
2513 const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
2514 const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
2515 const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
2516 const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
2517 const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = RAW_TOUCH_MIN;
2518 const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = RAW_TOUCH_MAX;
2519 const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
2520 const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
2521 const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
2522 const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
2523 const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
2524 const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
2525 const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
2526 const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
2527 const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
2528 const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
2529 const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
2530 const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
2531 const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
2532         TouchAffineTransformation(1, -2, 3, -4, 5, -6);
2533 
2534 const float TouchInputMapperTest::GEOMETRIC_SCALE =
2535         avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
2536                 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
2537 
2538 const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
2539         { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
2540         { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
2541 };
2542 
prepareDisplay(int32_t orientation)2543 void TouchInputMapperTest::prepareDisplay(int32_t orientation) {
2544     setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation);
2545 }
2546 
prepareVirtualKeys()2547 void TouchInputMapperTest::prepareVirtualKeys() {
2548     mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
2549     mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
2550     mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2551     mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
2552 }
2553 
prepareLocationCalibration()2554 void TouchInputMapperTest::prepareLocationCalibration() {
2555     mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
2556 }
2557 
toRawX(float displayX)2558 int32_t TouchInputMapperTest::toRawX(float displayX) {
2559     return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
2560 }
2561 
toRawY(float displayY)2562 int32_t TouchInputMapperTest::toRawY(float displayY) {
2563     return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
2564 }
2565 
toCookedX(float rawX,float rawY)2566 float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
2567     AFFINE_TRANSFORM.applyTo(rawX, rawY);
2568     return rawX;
2569 }
2570 
toCookedY(float rawX,float rawY)2571 float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
2572     AFFINE_TRANSFORM.applyTo(rawX, rawY);
2573     return rawY;
2574 }
2575 
toDisplayX(int32_t rawX)2576 float TouchInputMapperTest::toDisplayX(int32_t rawX) {
2577     return float(rawX - RAW_X_MIN) * DISPLAY_WIDTH / (RAW_X_MAX - RAW_X_MIN + 1);
2578 }
2579 
toDisplayY(int32_t rawY)2580 float TouchInputMapperTest::toDisplayY(int32_t rawY) {
2581     return float(rawY - RAW_Y_MIN) * DISPLAY_HEIGHT / (RAW_Y_MAX - RAW_Y_MIN + 1);
2582 }
2583 
2584 
2585 // --- SingleTouchInputMapperTest ---
2586 
2587 class SingleTouchInputMapperTest : public TouchInputMapperTest {
2588 protected:
2589     void prepareButtons();
2590     void prepareAxes(int axes);
2591 
2592     void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
2593     void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
2594     void processUp(SingleTouchInputMapper* mappery);
2595     void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
2596     void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
2597     void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
2598     void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
2599     void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
2600     void processSync(SingleTouchInputMapper* mapper);
2601 };
2602 
prepareButtons()2603 void SingleTouchInputMapperTest::prepareButtons() {
2604     mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
2605 }
2606 
prepareAxes(int axes)2607 void SingleTouchInputMapperTest::prepareAxes(int axes) {
2608     if (axes & POSITION) {
2609         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
2610                 RAW_X_MIN, RAW_X_MAX, 0, 0);
2611         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
2612                 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
2613     }
2614     if (axes & PRESSURE) {
2615         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
2616                 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
2617     }
2618     if (axes & TOOL) {
2619         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
2620                 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
2621     }
2622     if (axes & DISTANCE) {
2623         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
2624                 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
2625     }
2626     if (axes & TILT) {
2627         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
2628                 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
2629         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
2630                 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
2631     }
2632 }
2633 
processDown(SingleTouchInputMapper * mapper,int32_t x,int32_t y)2634 void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
2635     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 1);
2636     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, x);
2637     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, y);
2638 }
2639 
processMove(SingleTouchInputMapper * mapper,int32_t x,int32_t y)2640 void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
2641     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, x);
2642     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, y);
2643 }
2644 
processUp(SingleTouchInputMapper * mapper)2645 void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
2646     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0);
2647 }
2648 
processPressure(SingleTouchInputMapper * mapper,int32_t pressure)2649 void SingleTouchInputMapperTest::processPressure(
2650         SingleTouchInputMapper* mapper, int32_t pressure) {
2651     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_PRESSURE, pressure);
2652 }
2653 
processToolMajor(SingleTouchInputMapper * mapper,int32_t toolMajor)2654 void SingleTouchInputMapperTest::processToolMajor(
2655         SingleTouchInputMapper* mapper, int32_t toolMajor) {
2656     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
2657 }
2658 
processDistance(SingleTouchInputMapper * mapper,int32_t distance)2659 void SingleTouchInputMapperTest::processDistance(
2660         SingleTouchInputMapper* mapper, int32_t distance) {
2661     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_DISTANCE, distance);
2662 }
2663 
processTilt(SingleTouchInputMapper * mapper,int32_t tiltX,int32_t tiltY)2664 void SingleTouchInputMapperTest::processTilt(
2665         SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
2666     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_X, tiltX);
2667     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_Y, tiltY);
2668 }
2669 
processKey(SingleTouchInputMapper * mapper,int32_t code,int32_t value)2670 void SingleTouchInputMapperTest::processKey(
2671         SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
2672     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value);
2673 }
2674 
processSync(SingleTouchInputMapper * mapper)2675 void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
2676     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2677 }
2678 
2679 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer)2680 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
2681     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2682     prepareButtons();
2683     prepareAxes(POSITION);
2684     addMapperAndConfigure(mapper);
2685 
2686     ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2687 }
2688 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad)2689 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
2690     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2691     mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
2692     mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
2693     prepareButtons();
2694     prepareAxes(POSITION);
2695     addMapperAndConfigure(mapper);
2696 
2697     ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2698 }
2699 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad)2700 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
2701     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2702     prepareButtons();
2703     prepareAxes(POSITION);
2704     addConfigurationProperty("touch.deviceType", "touchPad");
2705     addMapperAndConfigure(mapper);
2706 
2707     ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2708 }
2709 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen)2710 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
2711     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2712     prepareButtons();
2713     prepareAxes(POSITION);
2714     addConfigurationProperty("touch.deviceType", "touchScreen");
2715     addMapperAndConfigure(mapper);
2716 
2717     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
2718 }
2719 
TEST_F(SingleTouchInputMapperTest,GetKeyCodeState)2720 TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
2721     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2722     addConfigurationProperty("touch.deviceType", "touchScreen");
2723     prepareDisplay(DISPLAY_ORIENTATION_0);
2724     prepareButtons();
2725     prepareAxes(POSITION);
2726     prepareVirtualKeys();
2727     addMapperAndConfigure(mapper);
2728 
2729     // Unknown key.
2730     ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2731 
2732     // Virtual key is down.
2733     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2734     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2735     processDown(mapper, x, y);
2736     processSync(mapper);
2737     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
2738 
2739     ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2740 
2741     // Virtual key is up.
2742     processUp(mapper);
2743     processSync(mapper);
2744     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
2745 
2746     ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2747 }
2748 
TEST_F(SingleTouchInputMapperTest,GetScanCodeState)2749 TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
2750     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2751     addConfigurationProperty("touch.deviceType", "touchScreen");
2752     prepareDisplay(DISPLAY_ORIENTATION_0);
2753     prepareButtons();
2754     prepareAxes(POSITION);
2755     prepareVirtualKeys();
2756     addMapperAndConfigure(mapper);
2757 
2758     // Unknown key.
2759     ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2760 
2761     // Virtual key is down.
2762     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2763     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2764     processDown(mapper, x, y);
2765     processSync(mapper);
2766     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
2767 
2768     ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2769 
2770     // Virtual key is up.
2771     processUp(mapper);
2772     processSync(mapper);
2773     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
2774 
2775     ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2776 }
2777 
TEST_F(SingleTouchInputMapperTest,MarkSupportedKeyCodes)2778 TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
2779     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2780     addConfigurationProperty("touch.deviceType", "touchScreen");
2781     prepareDisplay(DISPLAY_ORIENTATION_0);
2782     prepareButtons();
2783     prepareAxes(POSITION);
2784     prepareVirtualKeys();
2785     addMapperAndConfigure(mapper);
2786 
2787     const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
2788     uint8_t flags[2] = { 0, 0 };
2789     ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
2790     ASSERT_TRUE(flags[0]);
2791     ASSERT_FALSE(flags[1]);
2792 }
2793 
TEST_F(SingleTouchInputMapperTest,Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp)2794 TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
2795     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2796     addConfigurationProperty("touch.deviceType", "touchScreen");
2797     prepareDisplay(DISPLAY_ORIENTATION_0);
2798     prepareButtons();
2799     prepareAxes(POSITION);
2800     prepareVirtualKeys();
2801     addMapperAndConfigure(mapper);
2802 
2803     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2804 
2805     NotifyKeyArgs args;
2806 
2807     // Press virtual key.
2808     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2809     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2810     processDown(mapper, x, y);
2811     processSync(mapper);
2812 
2813     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2814     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2815     ASSERT_EQ(DEVICE_ID, args.deviceId);
2816     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2817     ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2818     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2819     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2820     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2821     ASSERT_EQ(KEY_HOME, args.scanCode);
2822     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2823     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2824 
2825     // Release virtual key.
2826     processUp(mapper);
2827     processSync(mapper);
2828 
2829     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2830     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2831     ASSERT_EQ(DEVICE_ID, args.deviceId);
2832     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2833     ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2834     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2835     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2836     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2837     ASSERT_EQ(KEY_HOME, args.scanCode);
2838     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2839     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2840 
2841     // Should not have sent any motions.
2842     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
2843 }
2844 
TEST_F(SingleTouchInputMapperTest,Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel)2845 TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
2846     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2847     addConfigurationProperty("touch.deviceType", "touchScreen");
2848     prepareDisplay(DISPLAY_ORIENTATION_0);
2849     prepareButtons();
2850     prepareAxes(POSITION);
2851     prepareVirtualKeys();
2852     addMapperAndConfigure(mapper);
2853 
2854     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2855 
2856     NotifyKeyArgs keyArgs;
2857 
2858     // Press virtual key.
2859     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2860     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2861     processDown(mapper, x, y);
2862     processSync(mapper);
2863 
2864     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2865     ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2866     ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2867     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2868     ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2869     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2870     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
2871     ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2872     ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2873     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2874     ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2875 
2876     // Move out of bounds.  This should generate a cancel and a pointer down since we moved
2877     // into the display area.
2878     y -= 100;
2879     processMove(mapper, x, y);
2880     processSync(mapper);
2881 
2882     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2883     ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2884     ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2885     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2886     ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2887     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2888     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2889             | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
2890     ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2891     ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2892     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2893     ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2894 
2895     NotifyMotionArgs motionArgs;
2896     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2897     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2898     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2899     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2900     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2901     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2902     ASSERT_EQ(0, motionArgs.flags);
2903     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2904     ASSERT_EQ(0, motionArgs.buttonState);
2905     ASSERT_EQ(0, motionArgs.edgeFlags);
2906     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2907     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2908     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
2909     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2910             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
2911     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2912     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2913     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2914 
2915     // Keep moving out of bounds.  Should generate a pointer move.
2916     y -= 50;
2917     processMove(mapper, x, y);
2918     processSync(mapper);
2919 
2920     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2921     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2922     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2923     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2924     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2925     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2926     ASSERT_EQ(0, motionArgs.flags);
2927     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2928     ASSERT_EQ(0, motionArgs.buttonState);
2929     ASSERT_EQ(0, motionArgs.edgeFlags);
2930     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2931     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2932     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
2933     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2934             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
2935     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2936     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2937     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2938 
2939     // Release out of bounds.  Should generate a pointer up.
2940     processUp(mapper);
2941     processSync(mapper);
2942 
2943     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2944     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2945     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2946     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2947     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2948     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2949     ASSERT_EQ(0, motionArgs.flags);
2950     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2951     ASSERT_EQ(0, motionArgs.buttonState);
2952     ASSERT_EQ(0, motionArgs.edgeFlags);
2953     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2954     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2955     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
2956     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2957             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
2958     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2959     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2960     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2961 
2962     // Should not have sent any more keys or motions.
2963     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
2964     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
2965 }
2966 
TEST_F(SingleTouchInputMapperTest,Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay)2967 TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
2968     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2969     addConfigurationProperty("touch.deviceType", "touchScreen");
2970     prepareDisplay(DISPLAY_ORIENTATION_0);
2971     prepareButtons();
2972     prepareAxes(POSITION);
2973     prepareVirtualKeys();
2974     addMapperAndConfigure(mapper);
2975 
2976     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2977 
2978     NotifyMotionArgs motionArgs;
2979 
2980     // Initially go down out of bounds.
2981     int32_t x = -10;
2982     int32_t y = -10;
2983     processDown(mapper, x, y);
2984     processSync(mapper);
2985 
2986     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
2987 
2988     // Move into the display area.  Should generate a pointer down.
2989     x = 50;
2990     y = 75;
2991     processMove(mapper, x, y);
2992     processSync(mapper);
2993 
2994     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2995     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2996     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2997     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2998     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2999     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3000     ASSERT_EQ(0, motionArgs.flags);
3001     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3002     ASSERT_EQ(0, motionArgs.buttonState);
3003     ASSERT_EQ(0, motionArgs.edgeFlags);
3004     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3005     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3006     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3007     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3008             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3009     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3010     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3011     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3012 
3013     // Release.  Should generate a pointer up.
3014     processUp(mapper);
3015     processSync(mapper);
3016 
3017     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3018     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3019     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3020     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3021     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3022     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3023     ASSERT_EQ(0, motionArgs.flags);
3024     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3025     ASSERT_EQ(0, motionArgs.buttonState);
3026     ASSERT_EQ(0, motionArgs.edgeFlags);
3027     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3028     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3029     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3030     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3031             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3032     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3033     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3034     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3035 
3036     // Should not have sent any more keys or motions.
3037     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3038     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3039 }
3040 
TEST_F(SingleTouchInputMapperTest,Process_NormalSingleTouchGesture)3041 TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
3042     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3043     addConfigurationProperty("touch.deviceType", "touchScreen");
3044     prepareDisplay(DISPLAY_ORIENTATION_0);
3045     prepareButtons();
3046     prepareAxes(POSITION);
3047     prepareVirtualKeys();
3048     addMapperAndConfigure(mapper);
3049 
3050     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3051 
3052     NotifyMotionArgs motionArgs;
3053 
3054     // Down.
3055     int32_t x = 100;
3056     int32_t y = 125;
3057     processDown(mapper, x, y);
3058     processSync(mapper);
3059 
3060     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3061     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3062     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3063     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3064     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3065     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3066     ASSERT_EQ(0, motionArgs.flags);
3067     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3068     ASSERT_EQ(0, motionArgs.buttonState);
3069     ASSERT_EQ(0, motionArgs.edgeFlags);
3070     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3071     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3072     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3073     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3074             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3075     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3076     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3077     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3078 
3079     // Move.
3080     x += 50;
3081     y += 75;
3082     processMove(mapper, x, y);
3083     processSync(mapper);
3084 
3085     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3086     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3087     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3088     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3089     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3090     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3091     ASSERT_EQ(0, motionArgs.flags);
3092     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3093     ASSERT_EQ(0, motionArgs.buttonState);
3094     ASSERT_EQ(0, motionArgs.edgeFlags);
3095     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3096     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3097     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3098     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3099             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3100     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3101     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3102     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3103 
3104     // Up.
3105     processUp(mapper);
3106     processSync(mapper);
3107 
3108     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3109     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3110     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3111     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3112     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3113     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3114     ASSERT_EQ(0, motionArgs.flags);
3115     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3116     ASSERT_EQ(0, motionArgs.buttonState);
3117     ASSERT_EQ(0, motionArgs.edgeFlags);
3118     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3119     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3120     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3121     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3122             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3123     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3124     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3125     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3126 
3127     // Should not have sent any more keys or motions.
3128     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3129     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3130 }
3131 
TEST_F(SingleTouchInputMapperTest,Process_WhenNotOrientationAware_DoesNotRotateMotions)3132 TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3133     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3134     addConfigurationProperty("touch.deviceType", "touchScreen");
3135     prepareButtons();
3136     prepareAxes(POSITION);
3137     addConfigurationProperty("touch.orientationAware", "0");
3138     addMapperAndConfigure(mapper);
3139 
3140     NotifyMotionArgs args;
3141 
3142     // Rotation 90.
3143     prepareDisplay(DISPLAY_ORIENTATION_90);
3144     processDown(mapper, toRawX(50), toRawY(75));
3145     processSync(mapper);
3146 
3147     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3148     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3149     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3150 
3151     processUp(mapper);
3152     processSync(mapper);
3153     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3154 }
3155 
TEST_F(SingleTouchInputMapperTest,Process_WhenOrientationAware_RotatesMotions)3156 TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
3157     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3158     addConfigurationProperty("touch.deviceType", "touchScreen");
3159     prepareButtons();
3160     prepareAxes(POSITION);
3161     addMapperAndConfigure(mapper);
3162 
3163     NotifyMotionArgs args;
3164 
3165     // Rotation 0.
3166     prepareDisplay(DISPLAY_ORIENTATION_0);
3167     processDown(mapper, toRawX(50), toRawY(75));
3168     processSync(mapper);
3169 
3170     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3171     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3172     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3173 
3174     processUp(mapper);
3175     processSync(mapper);
3176     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3177 
3178     // Rotation 90.
3179     prepareDisplay(DISPLAY_ORIENTATION_90);
3180     processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
3181     processSync(mapper);
3182 
3183     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3184     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3185     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3186 
3187     processUp(mapper);
3188     processSync(mapper);
3189     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3190 
3191     // Rotation 180.
3192     prepareDisplay(DISPLAY_ORIENTATION_180);
3193     processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
3194     processSync(mapper);
3195 
3196     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3197     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3198     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3199 
3200     processUp(mapper);
3201     processSync(mapper);
3202     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3203 
3204     // Rotation 270.
3205     prepareDisplay(DISPLAY_ORIENTATION_270);
3206     processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
3207     processSync(mapper);
3208 
3209     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3210     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3211     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3212 
3213     processUp(mapper);
3214     processSync(mapper);
3215     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3216 }
3217 
TEST_F(SingleTouchInputMapperTest,Process_AllAxes_DefaultCalibration)3218 TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
3219     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3220     addConfigurationProperty("touch.deviceType", "touchScreen");
3221     prepareDisplay(DISPLAY_ORIENTATION_0);
3222     prepareButtons();
3223     prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
3224     addMapperAndConfigure(mapper);
3225 
3226     // These calculations are based on the input device calibration documentation.
3227     int32_t rawX = 100;
3228     int32_t rawY = 200;
3229     int32_t rawPressure = 10;
3230     int32_t rawToolMajor = 12;
3231     int32_t rawDistance = 2;
3232     int32_t rawTiltX = 30;
3233     int32_t rawTiltY = 110;
3234 
3235     float x = toDisplayX(rawX);
3236     float y = toDisplayY(rawY);
3237     float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3238     float size = float(rawToolMajor) / RAW_TOOL_MAX;
3239     float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
3240     float distance = float(rawDistance);
3241 
3242     float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
3243     float tiltScale = M_PI / 180;
3244     float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
3245     float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
3246     float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3247     float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3248 
3249     processDown(mapper, rawX, rawY);
3250     processPressure(mapper, rawPressure);
3251     processToolMajor(mapper, rawToolMajor);
3252     processDistance(mapper, rawDistance);
3253     processTilt(mapper, rawTiltX, rawTiltY);
3254     processSync(mapper);
3255 
3256     NotifyMotionArgs args;
3257     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3258     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3259             x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
3260     ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
3261 }
3262 
TEST_F(SingleTouchInputMapperTest,Process_XYAxes_AffineCalibration)3263 TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
3264     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3265     addConfigurationProperty("touch.deviceType", "touchScreen");
3266     prepareDisplay(DISPLAY_ORIENTATION_0);
3267     prepareLocationCalibration();
3268     prepareButtons();
3269     prepareAxes(POSITION);
3270     addMapperAndConfigure(mapper);
3271 
3272     int32_t rawX = 100;
3273     int32_t rawY = 200;
3274 
3275     float x = toDisplayX(toCookedX(rawX, rawY));
3276     float y = toDisplayY(toCookedY(rawX, rawY));
3277 
3278     processDown(mapper, rawX, rawY);
3279     processSync(mapper);
3280 
3281     NotifyMotionArgs args;
3282     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3283     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3284             x, y, 1, 0, 0, 0, 0, 0, 0, 0));
3285 }
3286 
TEST_F(SingleTouchInputMapperTest,Process_ShouldHandleAllButtons)3287 TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
3288     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3289     addConfigurationProperty("touch.deviceType", "touchScreen");
3290     prepareDisplay(DISPLAY_ORIENTATION_0);
3291     prepareButtons();
3292     prepareAxes(POSITION);
3293     addMapperAndConfigure(mapper);
3294 
3295     NotifyMotionArgs motionArgs;
3296     NotifyKeyArgs keyArgs;
3297 
3298     processDown(mapper, 100, 200);
3299     processSync(mapper);
3300     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3301     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3302     ASSERT_EQ(0, motionArgs.buttonState);
3303 
3304     // press BTN_LEFT, release BTN_LEFT
3305     processKey(mapper, BTN_LEFT, 1);
3306     processSync(mapper);
3307     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3308     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3309     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3310 
3311     processKey(mapper, BTN_LEFT, 0);
3312     processSync(mapper);
3313     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3314     ASSERT_EQ(0, motionArgs.buttonState);
3315     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3316 
3317     // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
3318     processKey(mapper, BTN_RIGHT, 1);
3319     processKey(mapper, BTN_MIDDLE, 1);
3320     processSync(mapper);
3321     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3322     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3323     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3324             motionArgs.buttonState);
3325 
3326     processKey(mapper, BTN_RIGHT, 0);
3327     processSync(mapper);
3328     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3329     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3330     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3331 
3332     processKey(mapper, BTN_MIDDLE, 0);
3333     processSync(mapper);
3334     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3335     ASSERT_EQ(0, motionArgs.buttonState);
3336     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3337 
3338     // press BTN_BACK, release BTN_BACK
3339     processKey(mapper, BTN_BACK, 1);
3340     processSync(mapper);
3341     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3342     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3343     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3344     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3345     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3346     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3347 
3348     processKey(mapper, BTN_BACK, 0);
3349     processSync(mapper);
3350     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3351     ASSERT_EQ(0, motionArgs.buttonState);
3352     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3353     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3354     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3355     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3356 
3357     // press BTN_SIDE, release BTN_SIDE
3358     processKey(mapper, BTN_SIDE, 1);
3359     processSync(mapper);
3360     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3361     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3362     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3363     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3364     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3365     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3366 
3367     processKey(mapper, BTN_SIDE, 0);
3368     processSync(mapper);
3369     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3370     ASSERT_EQ(0, motionArgs.buttonState);
3371     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3372     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3373     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3374     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3375 
3376     // press BTN_FORWARD, release BTN_FORWARD
3377     processKey(mapper, BTN_FORWARD, 1);
3378     processSync(mapper);
3379     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3380     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3381     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3382     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3383     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3384     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3385 
3386     processKey(mapper, BTN_FORWARD, 0);
3387     processSync(mapper);
3388     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3389     ASSERT_EQ(0, motionArgs.buttonState);
3390     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3391     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3392     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3393     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3394 
3395     // press BTN_EXTRA, release BTN_EXTRA
3396     processKey(mapper, BTN_EXTRA, 1);
3397     processSync(mapper);
3398     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3399     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3400     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3401     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3402     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3403     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3404 
3405     processKey(mapper, BTN_EXTRA, 0);
3406     processSync(mapper);
3407     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3408     ASSERT_EQ(0, motionArgs.buttonState);
3409     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3410     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3411     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3412     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3413 
3414     // press BTN_STYLUS, release BTN_STYLUS
3415     processKey(mapper, BTN_STYLUS, 1);
3416     processSync(mapper);
3417     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3418     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3419     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY, motionArgs.buttonState);
3420 
3421     processKey(mapper, BTN_STYLUS, 0);
3422     processSync(mapper);
3423     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3424     ASSERT_EQ(0, motionArgs.buttonState);
3425     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3426 
3427     // press BTN_STYLUS2, release BTN_STYLUS2
3428     processKey(mapper, BTN_STYLUS2, 1);
3429     processSync(mapper);
3430     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3431     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3432     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3433 
3434     processKey(mapper, BTN_STYLUS2, 0);
3435     processSync(mapper);
3436     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3437     ASSERT_EQ(0, motionArgs.buttonState);
3438     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3439 
3440     // release touch
3441     processUp(mapper);
3442     processSync(mapper);
3443     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3444     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3445     ASSERT_EQ(0, motionArgs.buttonState);
3446 }
3447 
TEST_F(SingleTouchInputMapperTest,Process_ShouldHandleAllToolTypes)3448 TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
3449     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3450     addConfigurationProperty("touch.deviceType", "touchScreen");
3451     prepareDisplay(DISPLAY_ORIENTATION_0);
3452     prepareButtons();
3453     prepareAxes(POSITION);
3454     addMapperAndConfigure(mapper);
3455 
3456     NotifyMotionArgs motionArgs;
3457 
3458     // default tool type is finger
3459     processDown(mapper, 100, 200);
3460     processSync(mapper);
3461     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3462     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3463     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3464 
3465     // eraser
3466     processKey(mapper, BTN_TOOL_RUBBER, 1);
3467     processSync(mapper);
3468     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3469     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3470     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
3471 
3472     // stylus
3473     processKey(mapper, BTN_TOOL_RUBBER, 0);
3474     processKey(mapper, BTN_TOOL_PEN, 1);
3475     processSync(mapper);
3476     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3477     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3478     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
3479 
3480     // brush
3481     processKey(mapper, BTN_TOOL_PEN, 0);
3482     processKey(mapper, BTN_TOOL_BRUSH, 1);
3483     processSync(mapper);
3484     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3485     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3486     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
3487 
3488     // pencil
3489     processKey(mapper, BTN_TOOL_BRUSH, 0);
3490     processKey(mapper, BTN_TOOL_PENCIL, 1);
3491     processSync(mapper);
3492     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3493     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3494     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
3495 
3496     // airbrush
3497     processKey(mapper, BTN_TOOL_PENCIL, 0);
3498     processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
3499     processSync(mapper);
3500     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3501     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3502     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
3503 
3504     // mouse
3505     processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
3506     processKey(mapper, BTN_TOOL_MOUSE, 1);
3507     processSync(mapper);
3508     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3509     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3510     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
3511 
3512     // lens
3513     processKey(mapper, BTN_TOOL_MOUSE, 0);
3514     processKey(mapper, BTN_TOOL_LENS, 1);
3515     processSync(mapper);
3516     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3517     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3518     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
3519 
3520     // double-tap
3521     processKey(mapper, BTN_TOOL_LENS, 0);
3522     processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
3523     processSync(mapper);
3524     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3525     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3526     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3527 
3528     // triple-tap
3529     processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
3530     processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
3531     processSync(mapper);
3532     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3533     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3534     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3535 
3536     // quad-tap
3537     processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
3538     processKey(mapper, BTN_TOOL_QUADTAP, 1);
3539     processSync(mapper);
3540     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3541     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3542     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3543 
3544     // finger
3545     processKey(mapper, BTN_TOOL_QUADTAP, 0);
3546     processKey(mapper, BTN_TOOL_FINGER, 1);
3547     processSync(mapper);
3548     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3549     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3550     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3551 
3552     // stylus trumps finger
3553     processKey(mapper, BTN_TOOL_PEN, 1);
3554     processSync(mapper);
3555     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3556     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3557     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
3558 
3559     // eraser trumps stylus
3560     processKey(mapper, BTN_TOOL_RUBBER, 1);
3561     processSync(mapper);
3562     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3563     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3564     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
3565 
3566     // mouse trumps eraser
3567     processKey(mapper, BTN_TOOL_MOUSE, 1);
3568     processSync(mapper);
3569     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3570     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3571     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
3572 
3573     // back to default tool type
3574     processKey(mapper, BTN_TOOL_MOUSE, 0);
3575     processKey(mapper, BTN_TOOL_RUBBER, 0);
3576     processKey(mapper, BTN_TOOL_PEN, 0);
3577     processKey(mapper, BTN_TOOL_FINGER, 0);
3578     processSync(mapper);
3579     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3580     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3581     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3582 }
3583 
TEST_F(SingleTouchInputMapperTest,Process_WhenBtnTouchPresent_HoversIfItsValueIsZero)3584 TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
3585     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3586     addConfigurationProperty("touch.deviceType", "touchScreen");
3587     prepareDisplay(DISPLAY_ORIENTATION_0);
3588     prepareButtons();
3589     prepareAxes(POSITION);
3590     mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
3591     addMapperAndConfigure(mapper);
3592 
3593     NotifyMotionArgs motionArgs;
3594 
3595     // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
3596     processKey(mapper, BTN_TOOL_FINGER, 1);
3597     processMove(mapper, 100, 200);
3598     processSync(mapper);
3599     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3600     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
3601     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3602             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
3603 
3604     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3605     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3606     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3607             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
3608 
3609     // move a little
3610     processMove(mapper, 150, 250);
3611     processSync(mapper);
3612     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3613     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3614     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3615             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3616 
3617     // down when BTN_TOUCH is pressed, pressure defaults to 1
3618     processKey(mapper, BTN_TOUCH, 1);
3619     processSync(mapper);
3620     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3621     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
3622     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3623             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3624 
3625     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3626     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3627     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3628             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
3629 
3630     // up when BTN_TOUCH is released, hover restored
3631     processKey(mapper, BTN_TOUCH, 0);
3632     processSync(mapper);
3633     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3634     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3635     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3636             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
3637 
3638     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3639     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
3640     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3641             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3642 
3643     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3644     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3645     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3646             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3647 
3648     // exit hover when pointer goes away
3649     processKey(mapper, BTN_TOOL_FINGER, 0);
3650     processSync(mapper);
3651     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3652     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
3653     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3654             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3655 }
3656 
TEST_F(SingleTouchInputMapperTest,Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero)3657 TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
3658     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3659     addConfigurationProperty("touch.deviceType", "touchScreen");
3660     prepareDisplay(DISPLAY_ORIENTATION_0);
3661     prepareButtons();
3662     prepareAxes(POSITION | PRESSURE);
3663     addMapperAndConfigure(mapper);
3664 
3665     NotifyMotionArgs motionArgs;
3666 
3667     // initially hovering because pressure is 0
3668     processDown(mapper, 100, 200);
3669     processPressure(mapper, 0);
3670     processSync(mapper);
3671     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3672     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
3673     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3674             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
3675 
3676     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3677     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3678     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3679             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
3680 
3681     // move a little
3682     processMove(mapper, 150, 250);
3683     processSync(mapper);
3684     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3685     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3686     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3687             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3688 
3689     // down when pressure is non-zero
3690     processPressure(mapper, RAW_PRESSURE_MAX);
3691     processSync(mapper);
3692     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3693     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
3694     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3695             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3696 
3697     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3698     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3699     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3700             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
3701 
3702     // up when pressure becomes 0, hover restored
3703     processPressure(mapper, 0);
3704     processSync(mapper);
3705     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3706     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3707     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3708             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
3709 
3710     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3711     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
3712     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3713             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3714 
3715     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3716     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
3717     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3718             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3719 
3720     // exit hover when pointer goes away
3721     processUp(mapper);
3722     processSync(mapper);
3723     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3724     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
3725     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3726             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
3727 }
3728 
3729 
3730 // --- MultiTouchInputMapperTest ---
3731 
3732 class MultiTouchInputMapperTest : public TouchInputMapperTest {
3733 protected:
3734     void prepareAxes(int axes);
3735 
3736     void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
3737     void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
3738     void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
3739     void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
3740     void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
3741     void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
3742     void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
3743     void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
3744     void processId(MultiTouchInputMapper* mapper, int32_t id);
3745     void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
3746     void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
3747     void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
3748     void processMTSync(MultiTouchInputMapper* mapper);
3749     void processSync(MultiTouchInputMapper* mapper);
3750 };
3751 
prepareAxes(int axes)3752 void MultiTouchInputMapperTest::prepareAxes(int axes) {
3753     if (axes & POSITION) {
3754         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
3755                 RAW_X_MIN, RAW_X_MAX, 0, 0);
3756         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
3757                 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3758     }
3759     if (axes & TOUCH) {
3760         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
3761                 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
3762         if (axes & MINOR) {
3763             mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
3764                     RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
3765         }
3766     }
3767     if (axes & TOOL) {
3768         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
3769                 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3770         if (axes & MINOR) {
3771             mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
3772                     RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
3773         }
3774     }
3775     if (axes & ORIENTATION) {
3776         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
3777                 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
3778     }
3779     if (axes & PRESSURE) {
3780         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
3781                 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3782     }
3783     if (axes & DISTANCE) {
3784         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
3785                 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3786     }
3787     if (axes & ID) {
3788         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
3789                 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
3790     }
3791     if (axes & SLOT) {
3792         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
3793                 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
3794         mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
3795     }
3796     if (axes & TOOL_TYPE) {
3797         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
3798                 0, MT_TOOL_MAX, 0, 0);
3799     }
3800 }
3801 
processPosition(MultiTouchInputMapper * mapper,int32_t x,int32_t y)3802 void MultiTouchInputMapperTest::processPosition(
3803         MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
3804     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_X, x);
3805     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_Y, y);
3806 }
3807 
processTouchMajor(MultiTouchInputMapper * mapper,int32_t touchMajor)3808 void MultiTouchInputMapperTest::processTouchMajor(
3809         MultiTouchInputMapper* mapper, int32_t touchMajor) {
3810     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
3811 }
3812 
processTouchMinor(MultiTouchInputMapper * mapper,int32_t touchMinor)3813 void MultiTouchInputMapperTest::processTouchMinor(
3814         MultiTouchInputMapper* mapper, int32_t touchMinor) {
3815     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
3816 }
3817 
processToolMajor(MultiTouchInputMapper * mapper,int32_t toolMajor)3818 void MultiTouchInputMapperTest::processToolMajor(
3819         MultiTouchInputMapper* mapper, int32_t toolMajor) {
3820     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
3821 }
3822 
processToolMinor(MultiTouchInputMapper * mapper,int32_t toolMinor)3823 void MultiTouchInputMapperTest::processToolMinor(
3824         MultiTouchInputMapper* mapper, int32_t toolMinor) {
3825     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
3826 }
3827 
processOrientation(MultiTouchInputMapper * mapper,int32_t orientation)3828 void MultiTouchInputMapperTest::processOrientation(
3829         MultiTouchInputMapper* mapper, int32_t orientation) {
3830     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_ORIENTATION, orientation);
3831 }
3832 
processPressure(MultiTouchInputMapper * mapper,int32_t pressure)3833 void MultiTouchInputMapperTest::processPressure(
3834         MultiTouchInputMapper* mapper, int32_t pressure) {
3835     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_PRESSURE, pressure);
3836 }
3837 
processDistance(MultiTouchInputMapper * mapper,int32_t distance)3838 void MultiTouchInputMapperTest::processDistance(
3839         MultiTouchInputMapper* mapper, int32_t distance) {
3840     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_DISTANCE, distance);
3841 }
3842 
processId(MultiTouchInputMapper * mapper,int32_t id)3843 void MultiTouchInputMapperTest::processId(
3844         MultiTouchInputMapper* mapper, int32_t id) {
3845     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TRACKING_ID, id);
3846 }
3847 
processSlot(MultiTouchInputMapper * mapper,int32_t slot)3848 void MultiTouchInputMapperTest::processSlot(
3849         MultiTouchInputMapper* mapper, int32_t slot) {
3850     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_SLOT, slot);
3851 }
3852 
processToolType(MultiTouchInputMapper * mapper,int32_t toolType)3853 void MultiTouchInputMapperTest::processToolType(
3854         MultiTouchInputMapper* mapper, int32_t toolType) {
3855     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
3856 }
3857 
processKey(MultiTouchInputMapper * mapper,int32_t code,int32_t value)3858 void MultiTouchInputMapperTest::processKey(
3859         MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
3860     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value);
3861 }
3862 
processMTSync(MultiTouchInputMapper * mapper)3863 void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
3864     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_MT_REPORT, 0);
3865 }
3866 
processSync(MultiTouchInputMapper * mapper)3867 void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
3868     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
3869 }
3870 
3871 
TEST_F(MultiTouchInputMapperTest,Process_NormalMultiTouchGesture_WithoutTrackingIds)3872 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
3873     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3874     addConfigurationProperty("touch.deviceType", "touchScreen");
3875     prepareDisplay(DISPLAY_ORIENTATION_0);
3876     prepareAxes(POSITION);
3877     prepareVirtualKeys();
3878     addMapperAndConfigure(mapper);
3879 
3880     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3881 
3882     NotifyMotionArgs motionArgs;
3883 
3884     // Two fingers down at once.
3885     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
3886     processPosition(mapper, x1, y1);
3887     processMTSync(mapper);
3888     processPosition(mapper, x2, y2);
3889     processMTSync(mapper);
3890     processSync(mapper);
3891 
3892     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3893     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3894     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3895     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3896     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3897     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3898     ASSERT_EQ(0, motionArgs.flags);
3899     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3900     ASSERT_EQ(0, motionArgs.buttonState);
3901     ASSERT_EQ(0, motionArgs.edgeFlags);
3902     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3903     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3904     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3905     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3906             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
3907     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3908     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3909     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3910 
3911     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3912     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3913     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3914     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3915     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3916     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3917             motionArgs.action);
3918     ASSERT_EQ(0, motionArgs.flags);
3919     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3920     ASSERT_EQ(0, motionArgs.buttonState);
3921     ASSERT_EQ(0, motionArgs.edgeFlags);
3922     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3923     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3924     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3925     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3926     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
3927     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3928             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
3929     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3930             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
3931     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3932     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3933     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3934 
3935     // Move.
3936     x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
3937     processPosition(mapper, x1, y1);
3938     processMTSync(mapper);
3939     processPosition(mapper, x2, y2);
3940     processMTSync(mapper);
3941     processSync(mapper);
3942 
3943     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3944     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3945     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3946     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3947     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3948     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3949     ASSERT_EQ(0, motionArgs.flags);
3950     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3951     ASSERT_EQ(0, motionArgs.buttonState);
3952     ASSERT_EQ(0, motionArgs.edgeFlags);
3953     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3954     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3955     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3956     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3957     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
3958     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3959             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
3960     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3961             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
3962     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3963     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3964     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3965 
3966     // First finger up.
3967     x2 += 15; y2 -= 20;
3968     processPosition(mapper, x2, y2);
3969     processMTSync(mapper);
3970     processSync(mapper);
3971 
3972     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3973     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3974     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3975     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3976     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3977     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3978             motionArgs.action);
3979     ASSERT_EQ(0, motionArgs.flags);
3980     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3981     ASSERT_EQ(0, motionArgs.buttonState);
3982     ASSERT_EQ(0, motionArgs.edgeFlags);
3983     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3984     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3985     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3986     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3987     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
3988     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3989             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
3990     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3991             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
3992     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3993     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3994     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3995 
3996     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3997     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3998     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3999     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4000     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4001     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4002     ASSERT_EQ(0, motionArgs.flags);
4003     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4004     ASSERT_EQ(0, motionArgs.buttonState);
4005     ASSERT_EQ(0, motionArgs.edgeFlags);
4006     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4007     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4008     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4009     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4010             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4011     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4012     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4013     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4014 
4015     // Move.
4016     x2 += 20; y2 -= 25;
4017     processPosition(mapper, x2, y2);
4018     processMTSync(mapper);
4019     processSync(mapper);
4020 
4021     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4022     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4023     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4024     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4025     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4026     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4027     ASSERT_EQ(0, motionArgs.flags);
4028     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4029     ASSERT_EQ(0, motionArgs.buttonState);
4030     ASSERT_EQ(0, motionArgs.edgeFlags);
4031     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4032     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4033     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4034     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4035             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4036     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4037     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4038     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4039 
4040     // New finger down.
4041     int32_t x3 = 700, y3 = 300;
4042     processPosition(mapper, x2, y2);
4043     processMTSync(mapper);
4044     processPosition(mapper, x3, y3);
4045     processMTSync(mapper);
4046     processSync(mapper);
4047 
4048     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4049     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4050     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4051     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4052     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4053     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4054             motionArgs.action);
4055     ASSERT_EQ(0, motionArgs.flags);
4056     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4057     ASSERT_EQ(0, motionArgs.buttonState);
4058     ASSERT_EQ(0, motionArgs.edgeFlags);
4059     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4060     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4061     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4062     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4063     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4064     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4065             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4066     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4067             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4068     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4069     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4070     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4071 
4072     // Second finger up.
4073     x3 += 30; y3 -= 20;
4074     processPosition(mapper, x3, y3);
4075     processMTSync(mapper);
4076     processSync(mapper);
4077 
4078     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4079     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4080     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4081     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4082     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4083     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4084             motionArgs.action);
4085     ASSERT_EQ(0, motionArgs.flags);
4086     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4087     ASSERT_EQ(0, motionArgs.buttonState);
4088     ASSERT_EQ(0, motionArgs.edgeFlags);
4089     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4090     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4091     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4092     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4093     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4094     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4095             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4096     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4097             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4098     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4099     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4100     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4101 
4102     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4103     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4104     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4105     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4106     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4107     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4108     ASSERT_EQ(0, motionArgs.flags);
4109     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4110     ASSERT_EQ(0, motionArgs.buttonState);
4111     ASSERT_EQ(0, motionArgs.edgeFlags);
4112     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4113     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4114     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4115     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4116             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4117     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4118     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4119     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4120 
4121     // Last finger up.
4122     processMTSync(mapper);
4123     processSync(mapper);
4124 
4125     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4126     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4127     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4128     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4129     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4130     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4131     ASSERT_EQ(0, motionArgs.flags);
4132     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4133     ASSERT_EQ(0, motionArgs.buttonState);
4134     ASSERT_EQ(0, motionArgs.edgeFlags);
4135     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4136     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4137     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4138     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4139             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4140     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4141     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4142     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4143 
4144     // Should not have sent any more keys or motions.
4145     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4146     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4147 }
4148 
TEST_F(MultiTouchInputMapperTest,Process_NormalMultiTouchGesture_WithTrackingIds)4149 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
4150     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4151     addConfigurationProperty("touch.deviceType", "touchScreen");
4152     prepareDisplay(DISPLAY_ORIENTATION_0);
4153     prepareAxes(POSITION | ID);
4154     prepareVirtualKeys();
4155     addMapperAndConfigure(mapper);
4156 
4157     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4158 
4159     NotifyMotionArgs motionArgs;
4160 
4161     // Two fingers down at once.
4162     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4163     processPosition(mapper, x1, y1);
4164     processId(mapper, 1);
4165     processMTSync(mapper);
4166     processPosition(mapper, x2, y2);
4167     processId(mapper, 2);
4168     processMTSync(mapper);
4169     processSync(mapper);
4170 
4171     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4172     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4173     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4174     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4175     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4176     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4177             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4178 
4179     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4180     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4181             motionArgs.action);
4182     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4183     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4184     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4185     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4186     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4187     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4188             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4189     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4190             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4191 
4192     // Move.
4193     x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4194     processPosition(mapper, x1, y1);
4195     processId(mapper, 1);
4196     processMTSync(mapper);
4197     processPosition(mapper, x2, y2);
4198     processId(mapper, 2);
4199     processMTSync(mapper);
4200     processSync(mapper);
4201 
4202     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4203     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4204     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4205     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4206     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4207     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4208     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4209     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4210             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4211     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4212             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4213 
4214     // First finger up.
4215     x2 += 15; y2 -= 20;
4216     processPosition(mapper, x2, y2);
4217     processId(mapper, 2);
4218     processMTSync(mapper);
4219     processSync(mapper);
4220 
4221     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4222     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4223             motionArgs.action);
4224     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4225     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4226     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4227     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4228     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4229     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4230             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4231     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4232             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4233 
4234     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4235     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4236     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4237     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4238     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4239     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4240             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4241 
4242     // Move.
4243     x2 += 20; y2 -= 25;
4244     processPosition(mapper, x2, y2);
4245     processId(mapper, 2);
4246     processMTSync(mapper);
4247     processSync(mapper);
4248 
4249     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4250     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4251     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4252     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4253     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4254     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4255             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4256 
4257     // New finger down.
4258     int32_t x3 = 700, y3 = 300;
4259     processPosition(mapper, x2, y2);
4260     processId(mapper, 2);
4261     processMTSync(mapper);
4262     processPosition(mapper, x3, y3);
4263     processId(mapper, 3);
4264     processMTSync(mapper);
4265     processSync(mapper);
4266 
4267     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4268     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4269             motionArgs.action);
4270     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4271     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4272     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4273     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4274     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4275     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4276             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4277     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4278             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4279 
4280     // Second finger up.
4281     x3 += 30; y3 -= 20;
4282     processPosition(mapper, x3, y3);
4283     processId(mapper, 3);
4284     processMTSync(mapper);
4285     processSync(mapper);
4286 
4287     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4288     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4289             motionArgs.action);
4290     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4291     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4292     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4293     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4294     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4295     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4296             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4297     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4298             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4299 
4300     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4301     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4302     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4303     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4304     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4305     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4306             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4307 
4308     // Last finger up.
4309     processMTSync(mapper);
4310     processSync(mapper);
4311 
4312     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4313     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4314     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4315     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4316     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4317     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4318             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4319 
4320     // Should not have sent any more keys or motions.
4321     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4322     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4323 }
4324 
TEST_F(MultiTouchInputMapperTest,Process_NormalMultiTouchGesture_WithSlots)4325 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
4326     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4327     addConfigurationProperty("touch.deviceType", "touchScreen");
4328     prepareDisplay(DISPLAY_ORIENTATION_0);
4329     prepareAxes(POSITION | ID | SLOT);
4330     prepareVirtualKeys();
4331     addMapperAndConfigure(mapper);
4332 
4333     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4334 
4335     NotifyMotionArgs motionArgs;
4336 
4337     // Two fingers down at once.
4338     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4339     processPosition(mapper, x1, y1);
4340     processId(mapper, 1);
4341     processSlot(mapper, 1);
4342     processPosition(mapper, x2, y2);
4343     processId(mapper, 2);
4344     processSync(mapper);
4345 
4346     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4347     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4348     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4349     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4350     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4351     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4352             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4353 
4354     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4355     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4356             motionArgs.action);
4357     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4358     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4359     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4360     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4361     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4362     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4363             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4364     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4365             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4366 
4367     // Move.
4368     x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4369     processSlot(mapper, 0);
4370     processPosition(mapper, x1, y1);
4371     processSlot(mapper, 1);
4372     processPosition(mapper, x2, y2);
4373     processSync(mapper);
4374 
4375     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4376     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4377     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4378     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4379     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4380     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4381     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4382     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4383             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4384     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4385             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4386 
4387     // First finger up.
4388     x2 += 15; y2 -= 20;
4389     processSlot(mapper, 0);
4390     processId(mapper, -1);
4391     processSlot(mapper, 1);
4392     processPosition(mapper, x2, y2);
4393     processSync(mapper);
4394 
4395     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4396     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4397             motionArgs.action);
4398     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4399     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4400     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4401     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4402     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4403     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4404             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4405     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4406             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4407 
4408     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4409     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4410     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4411     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4412     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4413     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4414             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4415 
4416     // Move.
4417     x2 += 20; y2 -= 25;
4418     processPosition(mapper, x2, y2);
4419     processSync(mapper);
4420 
4421     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4422     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4423     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4424     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4425     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4426     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4427             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4428 
4429     // New finger down.
4430     int32_t x3 = 700, y3 = 300;
4431     processPosition(mapper, x2, y2);
4432     processSlot(mapper, 0);
4433     processId(mapper, 3);
4434     processPosition(mapper, x3, y3);
4435     processSync(mapper);
4436 
4437     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4438     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4439             motionArgs.action);
4440     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4441     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4442     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4443     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4444     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4445     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4446             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4447     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4448             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4449 
4450     // Second finger up.
4451     x3 += 30; y3 -= 20;
4452     processSlot(mapper, 1);
4453     processId(mapper, -1);
4454     processSlot(mapper, 0);
4455     processPosition(mapper, x3, y3);
4456     processSync(mapper);
4457 
4458     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4459     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4460             motionArgs.action);
4461     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4462     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4463     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4464     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4465     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4466     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4467             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4468     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4469             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4470 
4471     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4472     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4473     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4474     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4475     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4476     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4477             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4478 
4479     // Last finger up.
4480     processId(mapper, -1);
4481     processSync(mapper);
4482 
4483     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4485     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4486     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4487     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4488     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4489             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4490 
4491     // Should not have sent any more keys or motions.
4492     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4493     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4494 }
4495 
TEST_F(MultiTouchInputMapperTest,Process_AllAxes_WithDefaultCalibration)4496 TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
4497     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4498     addConfigurationProperty("touch.deviceType", "touchScreen");
4499     prepareDisplay(DISPLAY_ORIENTATION_0);
4500     prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
4501     addMapperAndConfigure(mapper);
4502 
4503     // These calculations are based on the input device calibration documentation.
4504     int32_t rawX = 100;
4505     int32_t rawY = 200;
4506     int32_t rawTouchMajor = 7;
4507     int32_t rawTouchMinor = 6;
4508     int32_t rawToolMajor = 9;
4509     int32_t rawToolMinor = 8;
4510     int32_t rawPressure = 11;
4511     int32_t rawDistance = 0;
4512     int32_t rawOrientation = 3;
4513     int32_t id = 5;
4514 
4515     float x = toDisplayX(rawX);
4516     float y = toDisplayY(rawY);
4517     float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
4518     float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
4519     float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
4520     float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
4521     float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
4522     float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
4523     float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
4524     float distance = float(rawDistance);
4525 
4526     processPosition(mapper, rawX, rawY);
4527     processTouchMajor(mapper, rawTouchMajor);
4528     processTouchMinor(mapper, rawTouchMinor);
4529     processToolMajor(mapper, rawToolMajor);
4530     processToolMinor(mapper, rawToolMinor);
4531     processPressure(mapper, rawPressure);
4532     processOrientation(mapper, rawOrientation);
4533     processDistance(mapper, rawDistance);
4534     processId(mapper, id);
4535     processMTSync(mapper);
4536     processSync(mapper);
4537 
4538     NotifyMotionArgs args;
4539     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4540     ASSERT_EQ(0, args.pointerProperties[0].id);
4541     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4542             x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
4543             orientation, distance));
4544 }
4545 
TEST_F(MultiTouchInputMapperTest,Process_TouchAndToolAxes_GeometricCalibration)4546 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
4547     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4548     addConfigurationProperty("touch.deviceType", "touchScreen");
4549     prepareDisplay(DISPLAY_ORIENTATION_0);
4550     prepareAxes(POSITION | TOUCH | TOOL | MINOR);
4551     addConfigurationProperty("touch.size.calibration", "geometric");
4552     addMapperAndConfigure(mapper);
4553 
4554     // These calculations are based on the input device calibration documentation.
4555     int32_t rawX = 100;
4556     int32_t rawY = 200;
4557     int32_t rawTouchMajor = 140;
4558     int32_t rawTouchMinor = 120;
4559     int32_t rawToolMajor = 180;
4560     int32_t rawToolMinor = 160;
4561 
4562     float x = toDisplayX(rawX);
4563     float y = toDisplayY(rawY);
4564     float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
4565     float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
4566     float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
4567     float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
4568     float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
4569 
4570     processPosition(mapper, rawX, rawY);
4571     processTouchMajor(mapper, rawTouchMajor);
4572     processTouchMinor(mapper, rawTouchMinor);
4573     processToolMajor(mapper, rawToolMajor);
4574     processToolMinor(mapper, rawToolMinor);
4575     processMTSync(mapper);
4576     processSync(mapper);
4577 
4578     NotifyMotionArgs args;
4579     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4580     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4581             x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
4582 }
4583 
TEST_F(MultiTouchInputMapperTest,Process_TouchAndToolAxes_SummedLinearCalibration)4584 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
4585     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4586     addConfigurationProperty("touch.deviceType", "touchScreen");
4587     prepareDisplay(DISPLAY_ORIENTATION_0);
4588     prepareAxes(POSITION | TOUCH | TOOL);
4589     addConfigurationProperty("touch.size.calibration", "diameter");
4590     addConfigurationProperty("touch.size.scale", "10");
4591     addConfigurationProperty("touch.size.bias", "160");
4592     addConfigurationProperty("touch.size.isSummed", "1");
4593     addMapperAndConfigure(mapper);
4594 
4595     // These calculations are based on the input device calibration documentation.
4596     // Note: We only provide a single common touch/tool value because the device is assumed
4597     //       not to emit separate values for each pointer (isSummed = 1).
4598     int32_t rawX = 100;
4599     int32_t rawY = 200;
4600     int32_t rawX2 = 150;
4601     int32_t rawY2 = 250;
4602     int32_t rawTouchMajor = 5;
4603     int32_t rawToolMajor = 8;
4604 
4605     float x = toDisplayX(rawX);
4606     float y = toDisplayY(rawY);
4607     float x2 = toDisplayX(rawX2);
4608     float y2 = toDisplayY(rawY2);
4609     float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
4610     float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
4611     float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
4612 
4613     processPosition(mapper, rawX, rawY);
4614     processTouchMajor(mapper, rawTouchMajor);
4615     processToolMajor(mapper, rawToolMajor);
4616     processMTSync(mapper);
4617     processPosition(mapper, rawX2, rawY2);
4618     processTouchMajor(mapper, rawTouchMajor);
4619     processToolMajor(mapper, rawToolMajor);
4620     processMTSync(mapper);
4621     processSync(mapper);
4622 
4623     NotifyMotionArgs args;
4624     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4625     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4626 
4627     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4628     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4629             args.action);
4630     ASSERT_EQ(size_t(2), args.pointerCount);
4631     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4632             x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
4633     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
4634             x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
4635 }
4636 
TEST_F(MultiTouchInputMapperTest,Process_TouchAndToolAxes_AreaCalibration)4637 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
4638     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4639     addConfigurationProperty("touch.deviceType", "touchScreen");
4640     prepareDisplay(DISPLAY_ORIENTATION_0);
4641     prepareAxes(POSITION | TOUCH | TOOL);
4642     addConfigurationProperty("touch.size.calibration", "area");
4643     addConfigurationProperty("touch.size.scale", "43");
4644     addConfigurationProperty("touch.size.bias", "3");
4645     addMapperAndConfigure(mapper);
4646 
4647     // These calculations are based on the input device calibration documentation.
4648     int32_t rawX = 100;
4649     int32_t rawY = 200;
4650     int32_t rawTouchMajor = 5;
4651     int32_t rawToolMajor = 8;
4652 
4653     float x = toDisplayX(rawX);
4654     float y = toDisplayY(rawY);
4655     float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
4656     float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
4657     float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
4658 
4659     processPosition(mapper, rawX, rawY);
4660     processTouchMajor(mapper, rawTouchMajor);
4661     processToolMajor(mapper, rawToolMajor);
4662     processMTSync(mapper);
4663     processSync(mapper);
4664 
4665     NotifyMotionArgs args;
4666     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4667     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4668             x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
4669 }
4670 
TEST_F(MultiTouchInputMapperTest,Process_PressureAxis_AmplitudeCalibration)4671 TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
4672     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4673     addConfigurationProperty("touch.deviceType", "touchScreen");
4674     prepareDisplay(DISPLAY_ORIENTATION_0);
4675     prepareAxes(POSITION | PRESSURE);
4676     addConfigurationProperty("touch.pressure.calibration", "amplitude");
4677     addConfigurationProperty("touch.pressure.scale", "0.01");
4678     addMapperAndConfigure(mapper);
4679 
4680     // These calculations are based on the input device calibration documentation.
4681     int32_t rawX = 100;
4682     int32_t rawY = 200;
4683     int32_t rawPressure = 60;
4684 
4685     float x = toDisplayX(rawX);
4686     float y = toDisplayY(rawY);
4687     float pressure = float(rawPressure) * 0.01f;
4688 
4689     processPosition(mapper, rawX, rawY);
4690     processPressure(mapper, rawPressure);
4691     processMTSync(mapper);
4692     processSync(mapper);
4693 
4694     NotifyMotionArgs args;
4695     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4696     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4697             x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
4698 }
4699 
TEST_F(MultiTouchInputMapperTest,Process_ShouldHandleAllButtons)4700 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
4701     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4702     addConfigurationProperty("touch.deviceType", "touchScreen");
4703     prepareDisplay(DISPLAY_ORIENTATION_0);
4704     prepareAxes(POSITION | ID | SLOT);
4705     addMapperAndConfigure(mapper);
4706 
4707     NotifyMotionArgs motionArgs;
4708     NotifyKeyArgs keyArgs;
4709 
4710     processId(mapper, 1);
4711     processPosition(mapper, 100, 200);
4712     processSync(mapper);
4713     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4714     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4715     ASSERT_EQ(0, motionArgs.buttonState);
4716 
4717     // press BTN_LEFT, release BTN_LEFT
4718     processKey(mapper, BTN_LEFT, 1);
4719     processSync(mapper);
4720     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4721     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4722     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4723 
4724     processKey(mapper, BTN_LEFT, 0);
4725     processSync(mapper);
4726     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4727     ASSERT_EQ(0, motionArgs.buttonState);
4728     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4729 
4730     // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
4731     processKey(mapper, BTN_RIGHT, 1);
4732     processKey(mapper, BTN_MIDDLE, 1);
4733     processSync(mapper);
4734     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4735     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4736     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4737             motionArgs.buttonState);
4738 
4739     processKey(mapper, BTN_RIGHT, 0);
4740     processSync(mapper);
4741     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4742     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4743     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4744 
4745     processKey(mapper, BTN_MIDDLE, 0);
4746     processSync(mapper);
4747     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4748     ASSERT_EQ(0, motionArgs.buttonState);
4749     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4750 
4751     // press BTN_BACK, release BTN_BACK
4752     processKey(mapper, BTN_BACK, 1);
4753     processSync(mapper);
4754     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4755     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4756     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4757     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4758     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4759     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4760 
4761     processKey(mapper, BTN_BACK, 0);
4762     processSync(mapper);
4763     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4764     ASSERT_EQ(0, motionArgs.buttonState);
4765     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4766     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4767     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4768     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4769 
4770     // press BTN_SIDE, release BTN_SIDE
4771     processKey(mapper, BTN_SIDE, 1);
4772     processSync(mapper);
4773     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4774     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4775     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4776     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4777     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4778     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4779 
4780     processKey(mapper, BTN_SIDE, 0);
4781     processSync(mapper);
4782     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4783     ASSERT_EQ(0, motionArgs.buttonState);
4784     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4785     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4786     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4787     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4788 
4789     // press BTN_FORWARD, release BTN_FORWARD
4790     processKey(mapper, BTN_FORWARD, 1);
4791     processSync(mapper);
4792     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4793     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4794     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4795     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4796     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4797     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4798 
4799     processKey(mapper, BTN_FORWARD, 0);
4800     processSync(mapper);
4801     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4802     ASSERT_EQ(0, motionArgs.buttonState);
4803     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4804     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4805     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4806     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4807 
4808     // press BTN_EXTRA, release BTN_EXTRA
4809     processKey(mapper, BTN_EXTRA, 1);
4810     processSync(mapper);
4811     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4812     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4813     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4814     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4815     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4816     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4817 
4818     processKey(mapper, BTN_EXTRA, 0);
4819     processSync(mapper);
4820     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4821     ASSERT_EQ(0, motionArgs.buttonState);
4822     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4823     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4824     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4825     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4826 
4827     // press BTN_STYLUS, release BTN_STYLUS
4828     processKey(mapper, BTN_STYLUS, 1);
4829     processSync(mapper);
4830     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4831     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4832     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY, motionArgs.buttonState);
4833 
4834     processKey(mapper, BTN_STYLUS, 0);
4835     processSync(mapper);
4836     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4837     ASSERT_EQ(0, motionArgs.buttonState);
4838     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4839 
4840     // press BTN_STYLUS2, release BTN_STYLUS2
4841     processKey(mapper, BTN_STYLUS2, 1);
4842     processSync(mapper);
4843     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4844     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4845     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4846 
4847     processKey(mapper, BTN_STYLUS2, 0);
4848     processSync(mapper);
4849     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4850     ASSERT_EQ(0, motionArgs.buttonState);
4851     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4852 
4853     // release touch
4854     processId(mapper, -1);
4855     processSync(mapper);
4856     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4857     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4858     ASSERT_EQ(0, motionArgs.buttonState);
4859 }
4860 
TEST_F(MultiTouchInputMapperTest,Process_ShouldHandleAllToolTypes)4861 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4862     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4863     addConfigurationProperty("touch.deviceType", "touchScreen");
4864     prepareDisplay(DISPLAY_ORIENTATION_0);
4865     prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
4866     addMapperAndConfigure(mapper);
4867 
4868     NotifyMotionArgs motionArgs;
4869 
4870     // default tool type is finger
4871     processId(mapper, 1);
4872     processPosition(mapper, 100, 200);
4873     processSync(mapper);
4874     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4875     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4876     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4877 
4878     // eraser
4879     processKey(mapper, BTN_TOOL_RUBBER, 1);
4880     processSync(mapper);
4881     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4882     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4883     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4884 
4885     // stylus
4886     processKey(mapper, BTN_TOOL_RUBBER, 0);
4887     processKey(mapper, BTN_TOOL_PEN, 1);
4888     processSync(mapper);
4889     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4890     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4891     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4892 
4893     // brush
4894     processKey(mapper, BTN_TOOL_PEN, 0);
4895     processKey(mapper, BTN_TOOL_BRUSH, 1);
4896     processSync(mapper);
4897     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4898     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4899     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4900 
4901     // pencil
4902     processKey(mapper, BTN_TOOL_BRUSH, 0);
4903     processKey(mapper, BTN_TOOL_PENCIL, 1);
4904     processSync(mapper);
4905     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4906     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4907     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4908 
4909     // airbrush
4910     processKey(mapper, BTN_TOOL_PENCIL, 0);
4911     processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4912     processSync(mapper);
4913     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4914     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4915     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4916 
4917     // mouse
4918     processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4919     processKey(mapper, BTN_TOOL_MOUSE, 1);
4920     processSync(mapper);
4921     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4922     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4923     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4924 
4925     // lens
4926     processKey(mapper, BTN_TOOL_MOUSE, 0);
4927     processKey(mapper, BTN_TOOL_LENS, 1);
4928     processSync(mapper);
4929     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4930     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4931     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4932 
4933     // double-tap
4934     processKey(mapper, BTN_TOOL_LENS, 0);
4935     processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4936     processSync(mapper);
4937     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4938     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4939     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4940 
4941     // triple-tap
4942     processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4943     processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4944     processSync(mapper);
4945     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4946     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4947     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4948 
4949     // quad-tap
4950     processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4951     processKey(mapper, BTN_TOOL_QUADTAP, 1);
4952     processSync(mapper);
4953     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4954     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4955     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4956 
4957     // finger
4958     processKey(mapper, BTN_TOOL_QUADTAP, 0);
4959     processKey(mapper, BTN_TOOL_FINGER, 1);
4960     processSync(mapper);
4961     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4962     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4963     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4964 
4965     // stylus trumps finger
4966     processKey(mapper, BTN_TOOL_PEN, 1);
4967     processSync(mapper);
4968     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4969     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4970     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4971 
4972     // eraser trumps stylus
4973     processKey(mapper, BTN_TOOL_RUBBER, 1);
4974     processSync(mapper);
4975     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4976     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4977     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4978 
4979     // mouse trumps eraser
4980     processKey(mapper, BTN_TOOL_MOUSE, 1);
4981     processSync(mapper);
4982     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4983     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4984     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4985 
4986     // MT tool type trumps BTN tool types: MT_TOOL_FINGER
4987     processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
4988     processSync(mapper);
4989     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4990     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4991     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4992 
4993     // MT tool type trumps BTN tool types: MT_TOOL_PEN
4994     processToolType(mapper, MT_TOOL_PEN);
4995     processSync(mapper);
4996     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4997     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4998     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4999 
5000     // back to default tool type
5001     processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
5002     processKey(mapper, BTN_TOOL_MOUSE, 0);
5003     processKey(mapper, BTN_TOOL_RUBBER, 0);
5004     processKey(mapper, BTN_TOOL_PEN, 0);
5005     processKey(mapper, BTN_TOOL_FINGER, 0);
5006     processSync(mapper);
5007     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5008     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5009     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5010 }
5011 
TEST_F(MultiTouchInputMapperTest,Process_WhenBtnTouchPresent_HoversIfItsValueIsZero)5012 TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
5013     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5014     addConfigurationProperty("touch.deviceType", "touchScreen");
5015     prepareDisplay(DISPLAY_ORIENTATION_0);
5016     prepareAxes(POSITION | ID | SLOT);
5017     mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
5018     addMapperAndConfigure(mapper);
5019 
5020     NotifyMotionArgs motionArgs;
5021 
5022     // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5023     processId(mapper, 1);
5024     processPosition(mapper, 100, 200);
5025     processSync(mapper);
5026     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5027     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5028     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5029             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5030 
5031     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5032     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5033     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5034             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5035 
5036     // move a little
5037     processPosition(mapper, 150, 250);
5038     processSync(mapper);
5039     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5040     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5041     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5042             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5043 
5044     // down when BTN_TOUCH is pressed, pressure defaults to 1
5045     processKey(mapper, BTN_TOUCH, 1);
5046     processSync(mapper);
5047     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5048     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5049     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5050             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5051 
5052     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5053     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5054     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5055             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5056 
5057     // up when BTN_TOUCH is released, hover restored
5058     processKey(mapper, BTN_TOUCH, 0);
5059     processSync(mapper);
5060     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5061     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5062     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5063             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5064 
5065     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5067     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5068             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5069 
5070     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5071     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5072     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5073             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5074 
5075     // exit hover when pointer goes away
5076     processId(mapper, -1);
5077     processSync(mapper);
5078     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5079     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5080     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5081             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5082 }
5083 
TEST_F(MultiTouchInputMapperTest,Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero)5084 TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
5085     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5086     addConfigurationProperty("touch.deviceType", "touchScreen");
5087     prepareDisplay(DISPLAY_ORIENTATION_0);
5088     prepareAxes(POSITION | ID | SLOT | PRESSURE);
5089     addMapperAndConfigure(mapper);
5090 
5091     NotifyMotionArgs motionArgs;
5092 
5093     // initially hovering because pressure is 0
5094     processId(mapper, 1);
5095     processPosition(mapper, 100, 200);
5096     processPressure(mapper, 0);
5097     processSync(mapper);
5098     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5099     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5100     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5101             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5102 
5103     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5104     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5105     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5106             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5107 
5108     // move a little
5109     processPosition(mapper, 150, 250);
5110     processSync(mapper);
5111     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5112     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5113     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5114             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5115 
5116     // down when pressure becomes non-zero
5117     processPressure(mapper, RAW_PRESSURE_MAX);
5118     processSync(mapper);
5119     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5120     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5121     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5122             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5123 
5124     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5125     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5126     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5127             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5128 
5129     // up when pressure becomes 0, hover restored
5130     processPressure(mapper, 0);
5131     processSync(mapper);
5132     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5134     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5135             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5136 
5137     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5138     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5139     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5140             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5141 
5142     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5143     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5144     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5145             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5146 
5147     // exit hover when pointer goes away
5148     processId(mapper, -1);
5149     processSync(mapper);
5150     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5151     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5152     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5153             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5154 }
5155 
5156 
5157 } // namespace android
5158