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