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