InputReader_test.cpp revision efd3266b719eed5f1b217021c0a9e76e4b274b06
1//
2// Copyright 2010 The Android Open Source Project
3//
4
5#include "../InputReader.h"
6
7#include <utils/List.h>
8#include <gtest/gtest.h>
9#include <math.h>
10
11namespace android {
12
13// An arbitrary time value.
14static const nsecs_t ARBITRARY_TIME = 1234;
15
16// Arbitrary display properties.
17static const int32_t DISPLAY_ID = 0;
18static const int32_t DISPLAY_WIDTH = 480;
19static const int32_t DISPLAY_HEIGHT = 800;
20
21// Error tolerance for floating point assertions.
22static const float EPSILON = 0.001f;
23
24template<typename T>
25static inline T min(T a, T b) {
26    return a < b ? a : b;
27}
28
29static inline float avg(float x, float y) {
30    return (x + y) / 2;
31}
32
33
34// --- FakePointerController ---
35
36class FakePointerController : public PointerControllerInterface {
37    bool mHaveBounds;
38    float mMinX, mMinY, mMaxX, mMaxY;
39
40protected:
41    virtual ~FakePointerController() { }
42
43public:
44    FakePointerController() :
45        mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0) {
46    }
47
48    void setBounds(float minX, float minY, float maxX, float maxY) {
49        mHaveBounds = true;
50        mMinX = minX;
51        mMinY = minY;
52        mMaxX = maxX;
53        mMaxY = maxY;
54    }
55
56private:
57    virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
58        *outMinX = mMinX;
59        *outMinY = mMinY;
60        *outMaxX = mMaxX;
61        *outMaxY = mMaxY;
62        return mHaveBounds;
63    }
64
65    virtual void move(float deltaX, float deltaY) {
66    }
67
68    virtual void setButtonState(uint32_t buttonState) {
69    }
70
71    virtual uint32_t getButtonState() const {
72        return 0;
73    }
74
75    virtual void setPosition(float x, float y) {
76    }
77
78    virtual void getPosition(float* outX, float* outY) const {
79        *outX = 0;
80        *outY = 0;
81    }
82
83    virtual void fade() {
84    }
85
86    virtual void unfade() {
87    }
88};
89
90
91// --- FakeInputReaderPolicy ---
92
93class FakeInputReaderPolicy : public InputReaderPolicyInterface {
94    struct DisplayInfo {
95        int32_t width;
96        int32_t height;
97        int32_t orientation;
98    };
99
100    KeyedVector<int32_t, DisplayInfo> mDisplayInfos;
101    bool mFilterTouchEvents;
102    bool mFilterJumpyTouchEvents;
103    Vector<String8> mExcludedDeviceNames;
104    KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
105
106protected:
107    virtual ~FakeInputReaderPolicy() { }
108
109public:
110    FakeInputReaderPolicy() :
111            mFilterTouchEvents(false), mFilterJumpyTouchEvents(false) {
112    }
113
114    void removeDisplayInfo(int32_t displayId) {
115        mDisplayInfos.removeItem(displayId);
116    }
117
118    void setDisplayInfo(int32_t displayId, int32_t width, int32_t height, int32_t orientation) {
119        removeDisplayInfo(displayId);
120
121        DisplayInfo info;
122        info.width = width;
123        info.height = height;
124        info.orientation = orientation;
125        mDisplayInfos.add(displayId, info);
126    }
127
128    void setFilterTouchEvents(bool enabled) {
129        mFilterTouchEvents = enabled;
130    }
131
132    void setFilterJumpyTouchEvents(bool enabled) {
133        mFilterJumpyTouchEvents = enabled;
134    }
135
136    virtual nsecs_t getVirtualKeyQuietTime() {
137        return 0;
138    }
139
140    void addExcludedDeviceName(const String8& deviceName) {
141        mExcludedDeviceNames.push(deviceName);
142    }
143
144    void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
145        mPointerControllers.add(deviceId, controller);
146    }
147
148private:
149    virtual bool getDisplayInfo(int32_t displayId,
150            int32_t* width, int32_t* height, int32_t* orientation) {
151        ssize_t index = mDisplayInfos.indexOfKey(displayId);
152        if (index >= 0) {
153            const DisplayInfo& info = mDisplayInfos.valueAt(index);
154            if (width) {
155                *width = info.width;
156            }
157            if (height) {
158                *height = info.height;
159            }
160            if (orientation) {
161                *orientation = info.orientation;
162            }
163            return true;
164        }
165        return false;
166    }
167
168    virtual bool filterTouchEvents() {
169        return mFilterTouchEvents;
170    }
171
172    virtual bool filterJumpyTouchEvents() {
173        return mFilterJumpyTouchEvents;
174    }
175
176    virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) {
177        outExcludedDeviceNames.appendVector(mExcludedDeviceNames);
178    }
179
180    virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
181        return mPointerControllers.valueFor(deviceId);
182    }
183};
184
185
186// --- FakeInputDispatcher ---
187
188class FakeInputDispatcher : public InputDispatcherInterface {
189public:
190    struct NotifyConfigurationChangedArgs {
191        NotifyConfigurationChangedArgs() : eventTime(0) { }
192
193        nsecs_t eventTime;
194    };
195
196    struct NotifyKeyArgs {
197        nsecs_t eventTime;
198        int32_t deviceId;
199        uint32_t source;
200        uint32_t policyFlags;
201        int32_t action;
202        int32_t flags;
203        int32_t keyCode;
204        int32_t scanCode;
205        int32_t metaState;
206        nsecs_t downTime;
207    };
208
209    struct NotifyMotionArgs {
210        nsecs_t eventTime;
211        int32_t deviceId;
212        uint32_t source;
213        uint32_t policyFlags;
214        int32_t action;
215        int32_t flags;
216        int32_t metaState;
217        int32_t edgeFlags;
218        uint32_t pointerCount;
219        Vector<int32_t> pointerIds;
220        Vector<PointerCoords> pointerCoords;
221        float xPrecision;
222        float yPrecision;
223        nsecs_t downTime;
224    };
225
226    struct NotifySwitchArgs {
227        nsecs_t when;
228        int32_t switchCode;
229        int32_t switchValue;
230        uint32_t policyFlags;
231    };
232
233private:
234    List<NotifyConfigurationChangedArgs> mNotifyConfigurationChangedArgs;
235    List<NotifyKeyArgs> mNotifyKeyArgs;
236    List<NotifyMotionArgs> mNotifyMotionArgs;
237    List<NotifySwitchArgs> mNotifySwitchArgs;
238
239protected:
240    virtual ~FakeInputDispatcher() { }
241
242public:
243    FakeInputDispatcher() {
244    }
245
246    void assertNotifyConfigurationChangedWasCalled(NotifyConfigurationChangedArgs* outArgs = NULL) {
247        ASSERT_FALSE(mNotifyConfigurationChangedArgs.empty())
248                << "Expected notifyConfigurationChanged() to have been called.";
249        if (outArgs) {
250            *outArgs = *mNotifyConfigurationChangedArgs.begin();
251        }
252        mNotifyConfigurationChangedArgs.erase(mNotifyConfigurationChangedArgs.begin());
253    }
254
255    void assertNotifyKeyWasCalled(NotifyKeyArgs* outArgs = NULL) {
256        ASSERT_FALSE(mNotifyKeyArgs.empty())
257                << "Expected notifyKey() to have been called.";
258        if (outArgs) {
259            *outArgs = *mNotifyKeyArgs.begin();
260        }
261        mNotifyKeyArgs.erase(mNotifyKeyArgs.begin());
262    }
263
264    void assertNotifyKeyWasNotCalled() {
265        ASSERT_TRUE(mNotifyKeyArgs.empty())
266                << "Expected notifyKey() to not have been called.";
267    }
268
269    void assertNotifyMotionWasCalled(NotifyMotionArgs* outArgs = NULL) {
270        ASSERT_FALSE(mNotifyMotionArgs.empty())
271                << "Expected notifyMotion() to have been called.";
272        if (outArgs) {
273            *outArgs = *mNotifyMotionArgs.begin();
274        }
275        mNotifyMotionArgs.erase(mNotifyMotionArgs.begin());
276    }
277
278    void assertNotifyMotionWasNotCalled() {
279        ASSERT_TRUE(mNotifyMotionArgs.empty())
280                << "Expected notifyMotion() to not have been called.";
281    }
282
283    void assertNotifySwitchWasCalled(NotifySwitchArgs* outArgs = NULL) {
284        ASSERT_FALSE(mNotifySwitchArgs.empty())
285                << "Expected notifySwitch() to have been called.";
286        if (outArgs) {
287            *outArgs = *mNotifySwitchArgs.begin();
288        }
289        mNotifySwitchArgs.erase(mNotifySwitchArgs.begin());
290    }
291
292private:
293    virtual void notifyConfigurationChanged(nsecs_t eventTime) {
294        NotifyConfigurationChangedArgs args;
295        args.eventTime = eventTime;
296        mNotifyConfigurationChangedArgs.push_back(args);
297    }
298
299    virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
300            uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
301            int32_t scanCode, int32_t metaState, nsecs_t downTime) {
302        NotifyKeyArgs args;
303        args.eventTime = eventTime;
304        args.deviceId = deviceId;
305        args.source = source;
306        args.policyFlags = policyFlags;
307        args.action = action;
308        args.flags = flags;
309        args.keyCode = keyCode;
310        args.scanCode = scanCode;
311        args.metaState = metaState;
312        args.downTime = downTime;
313        mNotifyKeyArgs.push_back(args);
314    }
315
316    virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
317            uint32_t policyFlags, int32_t action, int32_t flags,
318            int32_t metaState, int32_t edgeFlags,
319            uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
320            float xPrecision, float yPrecision, nsecs_t downTime) {
321        NotifyMotionArgs args;
322        args.eventTime = eventTime;
323        args.deviceId = deviceId;
324        args.source = source;
325        args.policyFlags = policyFlags;
326        args.action = action;
327        args.flags = flags;
328        args.metaState = metaState;
329        args.edgeFlags = edgeFlags;
330        args.pointerCount = pointerCount;
331        args.pointerIds.clear();
332        args.pointerIds.appendArray(pointerIds, pointerCount);
333        args.pointerCoords.clear();
334        args.pointerCoords.appendArray(pointerCoords, pointerCount);
335        args.xPrecision = xPrecision;
336        args.yPrecision = yPrecision;
337        args.downTime = downTime;
338        mNotifyMotionArgs.push_back(args);
339    }
340
341    virtual void notifySwitch(nsecs_t when,
342            int32_t switchCode, int32_t switchValue, uint32_t policyFlags) {
343        NotifySwitchArgs args;
344        args.when = when;
345        args.switchCode = switchCode;
346        args.switchValue = switchValue;
347        args.policyFlags = policyFlags;
348        mNotifySwitchArgs.push_back(args);
349    }
350
351    virtual void dump(String8& dump) {
352        ADD_FAILURE() << "Should never be called by input reader.";
353    }
354
355    virtual void dispatchOnce() {
356        ADD_FAILURE() << "Should never be called by input reader.";
357    }
358
359    virtual int32_t injectInputEvent(const InputEvent* event,
360            int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) {
361        ADD_FAILURE() << "Should never be called by input reader.";
362        return INPUT_EVENT_INJECTION_FAILED;
363    }
364
365    virtual void setInputWindows(const Vector<InputWindow>& inputWindows) {
366        ADD_FAILURE() << "Should never be called by input reader.";
367    }
368
369    virtual void setFocusedApplication(const InputApplication* inputApplication) {
370        ADD_FAILURE() << "Should never be called by input reader.";
371    }
372
373    virtual void setInputDispatchMode(bool enabled, bool frozen) {
374        ADD_FAILURE() << "Should never be called by input reader.";
375    }
376
377    virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
378            const sp<InputChannel>& toChannel) {
379        ADD_FAILURE() << "Should never be called by input reader.";
380        return 0;
381    }
382
383    virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
384            const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
385        ADD_FAILURE() << "Should never be called by input reader.";
386        return 0;
387    }
388
389    virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) {
390        ADD_FAILURE() << "Should never be called by input reader.";
391        return 0;
392    }
393};
394
395
396// --- FakeEventHub ---
397
398class FakeEventHub : public EventHubInterface {
399    struct KeyInfo {
400        int32_t keyCode;
401        uint32_t flags;
402    };
403
404    struct Device {
405        String8 name;
406        uint32_t classes;
407        PropertyMap configuration;
408        KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
409        KeyedVector<int, bool> relativeAxes;
410        KeyedVector<int32_t, int32_t> keyCodeStates;
411        KeyedVector<int32_t, int32_t> scanCodeStates;
412        KeyedVector<int32_t, int32_t> switchStates;
413        KeyedVector<int32_t, KeyInfo> keys;
414        KeyedVector<int32_t, bool> leds;
415        Vector<VirtualKeyDefinition> virtualKeys;
416
417        Device(const String8& name, uint32_t classes) :
418                name(name), classes(classes) {
419        }
420    };
421
422    KeyedVector<int32_t, Device*> mDevices;
423    Vector<String8> mExcludedDevices;
424    List<RawEvent> mEvents;
425
426protected:
427    virtual ~FakeEventHub() {
428        for (size_t i = 0; i < mDevices.size(); i++) {
429            delete mDevices.valueAt(i);
430        }
431    }
432
433public:
434    FakeEventHub() { }
435
436    void addDevice(int32_t deviceId, const String8& name, uint32_t classes) {
437        Device* device = new Device(name, classes);
438        mDevices.add(deviceId, device);
439
440        enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0, 0, 0);
441    }
442
443    void removeDevice(int32_t deviceId) {
444        delete mDevices.valueFor(deviceId);
445        mDevices.removeItem(deviceId);
446
447        enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0, 0, 0);
448    }
449
450    void finishDeviceScan() {
451        enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0, 0, 0);
452    }
453
454    void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
455        Device* device = getDevice(deviceId);
456        device->configuration.addProperty(key, value);
457    }
458
459    void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
460        Device* device = getDevice(deviceId);
461        device->configuration.addAll(configuration);
462    }
463
464    void addAbsoluteAxis(int32_t deviceId, int axis,
465            int32_t minValue, int32_t maxValue, int flat, int fuzz) {
466        Device* device = getDevice(deviceId);
467
468        RawAbsoluteAxisInfo info;
469        info.valid = true;
470        info.minValue = minValue;
471        info.maxValue = maxValue;
472        info.flat = flat;
473        info.fuzz = fuzz;
474        device->absoluteAxes.add(axis, info);
475    }
476
477    void addRelativeAxis(int32_t deviceId, int32_t axis) {
478        Device* device = getDevice(deviceId);
479        device->relativeAxes.add(axis, true);
480    }
481
482    void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
483        Device* device = getDevice(deviceId);
484        device->keyCodeStates.replaceValueFor(keyCode, state);
485    }
486
487    void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
488        Device* device = getDevice(deviceId);
489        device->scanCodeStates.replaceValueFor(scanCode, state);
490    }
491
492    void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
493        Device* device = getDevice(deviceId);
494        device->switchStates.replaceValueFor(switchCode, state);
495    }
496
497    void addKey(int32_t deviceId, int32_t scanCode, int32_t keyCode, uint32_t flags) {
498        Device* device = getDevice(deviceId);
499        KeyInfo info;
500        info.keyCode = keyCode;
501        info.flags = flags;
502        device->keys.add(scanCode, info);
503    }
504
505    void addLed(int32_t deviceId, int32_t led, bool initialState) {
506        Device* device = getDevice(deviceId);
507        device->leds.add(led, initialState);
508    }
509
510    bool getLedState(int32_t deviceId, int32_t led) {
511        Device* device = getDevice(deviceId);
512        return device->leds.valueFor(led);
513    }
514
515    Vector<String8>& getExcludedDevices() {
516        return mExcludedDevices;
517    }
518
519    void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
520        Device* device = getDevice(deviceId);
521        device->virtualKeys.push(definition);
522    }
523
524    void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
525            int32_t scanCode, int32_t keyCode, int32_t value, uint32_t flags) {
526        RawEvent event;
527        event.when = when;
528        event.deviceId = deviceId;
529        event.type = type;
530        event.scanCode = scanCode;
531        event.keyCode = keyCode;
532        event.value = value;
533        event.flags = flags;
534        mEvents.push_back(event);
535    }
536
537    void assertQueueIsEmpty() {
538        ASSERT_EQ(size_t(0), mEvents.size())
539                << "Expected the event queue to be empty (fully consumed).";
540    }
541
542private:
543    Device* getDevice(int32_t deviceId) const {
544        ssize_t index = mDevices.indexOfKey(deviceId);
545        return index >= 0 ? mDevices.valueAt(index) : NULL;
546    }
547
548    virtual uint32_t getDeviceClasses(int32_t deviceId) const {
549        Device* device = getDevice(deviceId);
550        return device ? device->classes : 0;
551    }
552
553    virtual String8 getDeviceName(int32_t deviceId) const {
554        Device* device = getDevice(deviceId);
555        return device ? device->name : String8("unknown");
556    }
557
558    virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
559        Device* device = getDevice(deviceId);
560        if (device) {
561            *outConfiguration = device->configuration;
562        }
563    }
564
565    virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
566            RawAbsoluteAxisInfo* outAxisInfo) const {
567        Device* device = getDevice(deviceId);
568        if (device) {
569            ssize_t index = device->absoluteAxes.indexOfKey(axis);
570            if (index >= 0) {
571                *outAxisInfo = device->absoluteAxes.valueAt(index);
572                return OK;
573            }
574        }
575        return -1;
576    }
577
578    virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
579        Device* device = getDevice(deviceId);
580        if (device) {
581            return device->relativeAxes.indexOfKey(axis) >= 0;
582        }
583        return false;
584    }
585
586    virtual status_t mapKey(int32_t deviceId, int scancode,
587            int32_t* outKeycode, uint32_t* outFlags) const {
588        Device* device = getDevice(deviceId);
589        if (device) {
590            ssize_t index = device->keys.indexOfKey(scancode);
591            if (index >= 0) {
592                if (outKeycode) {
593                    *outKeycode = device->keys.valueAt(index).keyCode;
594                }
595                if (outFlags) {
596                    *outFlags = device->keys.valueAt(index).flags;
597                }
598                return OK;
599            }
600        }
601        return NAME_NOT_FOUND;
602    }
603
604    virtual status_t mapAxis(int32_t deviceId, int scancode,
605            AxisInfo* outAxisInfo) const {
606        return NAME_NOT_FOUND;
607    }
608
609    virtual void addExcludedDevice(const char* deviceName) {
610        mExcludedDevices.add(String8(deviceName));
611    }
612
613    virtual bool getEvent(RawEvent* outEvent) {
614        if (mEvents.empty()) {
615            return false;
616        }
617
618        *outEvent = *mEvents.begin();
619        mEvents.erase(mEvents.begin());
620        return true;
621    }
622
623    virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
624        Device* device = getDevice(deviceId);
625        if (device) {
626            ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
627            if (index >= 0) {
628                return device->scanCodeStates.valueAt(index);
629            }
630        }
631        return AKEY_STATE_UNKNOWN;
632    }
633
634    virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
635        Device* device = getDevice(deviceId);
636        if (device) {
637            ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
638            if (index >= 0) {
639                return device->keyCodeStates.valueAt(index);
640            }
641        }
642        return AKEY_STATE_UNKNOWN;
643    }
644
645    virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
646        Device* device = getDevice(deviceId);
647        if (device) {
648            ssize_t index = device->switchStates.indexOfKey(sw);
649            if (index >= 0) {
650                return device->switchStates.valueAt(index);
651            }
652        }
653        return AKEY_STATE_UNKNOWN;
654    }
655
656    virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
657            uint8_t* outFlags) const {
658        bool result = false;
659        Device* device = getDevice(deviceId);
660        if (device) {
661            for (size_t i = 0; i < numCodes; i++) {
662                for (size_t j = 0; j < device->keys.size(); j++) {
663                    if (keyCodes[i] == device->keys.valueAt(j).keyCode) {
664                        outFlags[i] = 1;
665                        result = true;
666                    }
667                }
668            }
669        }
670        return result;
671    }
672
673    virtual bool hasLed(int32_t deviceId, int32_t led) const {
674        Device* device = getDevice(deviceId);
675        return device && device->leds.indexOfKey(led) >= 0;
676    }
677
678    virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
679        Device* device = getDevice(deviceId);
680        if (device) {
681            ssize_t index = device->leds.indexOfKey(led);
682            if (index >= 0) {
683                device->leds.replaceValueAt(led, on);
684            } else {
685                ADD_FAILURE()
686                        << "Attempted to set the state of an LED that the EventHub declared "
687                        "was not present.  led=" << led;
688            }
689        }
690    }
691
692    virtual void getVirtualKeyDefinitions(int32_t deviceId,
693            Vector<VirtualKeyDefinition>& outVirtualKeys) const {
694        outVirtualKeys.clear();
695
696        Device* device = getDevice(deviceId);
697        if (device) {
698            outVirtualKeys.appendVector(device->virtualKeys);
699        }
700    }
701
702    virtual bool isExternal(int32_t deviceId) const {
703        return false;
704    }
705
706    virtual void dump(String8& dump) {
707    }
708};
709
710
711// --- FakeInputReaderContext ---
712
713class FakeInputReaderContext : public InputReaderContext {
714    sp<EventHubInterface> mEventHub;
715    sp<InputReaderPolicyInterface> mPolicy;
716    sp<InputDispatcherInterface> mDispatcher;
717    int32_t mGlobalMetaState;
718    bool mUpdateGlobalMetaStateWasCalled;
719
720public:
721    FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
722            const sp<InputReaderPolicyInterface>& policy,
723            const sp<InputDispatcherInterface>& dispatcher) :
724            mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
725            mGlobalMetaState(0) {
726    }
727
728    virtual ~FakeInputReaderContext() { }
729
730    void assertUpdateGlobalMetaStateWasCalled() {
731        ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
732                << "Expected updateGlobalMetaState() to have been called.";
733        mUpdateGlobalMetaStateWasCalled = false;
734    }
735
736    void setGlobalMetaState(int32_t state) {
737        mGlobalMetaState = state;
738    }
739
740private:
741    virtual void updateGlobalMetaState() {
742        mUpdateGlobalMetaStateWasCalled = true;
743    }
744
745    virtual int32_t getGlobalMetaState() {
746        return mGlobalMetaState;
747    }
748
749    virtual EventHubInterface* getEventHub() {
750        return mEventHub.get();
751    }
752
753    virtual InputReaderPolicyInterface* getPolicy() {
754        return mPolicy.get();
755    }
756
757    virtual InputDispatcherInterface* getDispatcher() {
758        return mDispatcher.get();
759    }
760
761    virtual void disableVirtualKeysUntil(nsecs_t time) {
762    }
763
764    virtual bool shouldDropVirtualKey(nsecs_t now,
765            InputDevice* device, int32_t keyCode, int32_t scanCode) {
766        return false;
767    }
768
769    virtual void fadePointer() {
770    }
771};
772
773
774// --- FakeInputMapper ---
775
776class FakeInputMapper : public InputMapper {
777    uint32_t mSources;
778    int32_t mKeyboardType;
779    int32_t mMetaState;
780    KeyedVector<int32_t, int32_t> mKeyCodeStates;
781    KeyedVector<int32_t, int32_t> mScanCodeStates;
782    KeyedVector<int32_t, int32_t> mSwitchStates;
783    Vector<int32_t> mSupportedKeyCodes;
784    RawEvent mLastEvent;
785
786    bool mConfigureWasCalled;
787    bool mResetWasCalled;
788    bool mProcessWasCalled;
789
790public:
791    FakeInputMapper(InputDevice* device, uint32_t sources) :
792            InputMapper(device),
793            mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
794            mMetaState(0),
795            mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
796    }
797
798    virtual ~FakeInputMapper() { }
799
800    void setKeyboardType(int32_t keyboardType) {
801        mKeyboardType = keyboardType;
802    }
803
804    void setMetaState(int32_t metaState) {
805        mMetaState = metaState;
806    }
807
808    void assertConfigureWasCalled() {
809        ASSERT_TRUE(mConfigureWasCalled)
810                << "Expected configure() to have been called.";
811        mConfigureWasCalled = false;
812    }
813
814    void assertResetWasCalled() {
815        ASSERT_TRUE(mResetWasCalled)
816                << "Expected reset() to have been called.";
817        mResetWasCalled = false;
818    }
819
820    void assertProcessWasCalled(RawEvent* outLastEvent = NULL) {
821        ASSERT_TRUE(mProcessWasCalled)
822                << "Expected process() to have been called.";
823        if (outLastEvent) {
824            *outLastEvent = mLastEvent;
825        }
826        mProcessWasCalled = false;
827    }
828
829    void setKeyCodeState(int32_t keyCode, int32_t state) {
830        mKeyCodeStates.replaceValueFor(keyCode, state);
831    }
832
833    void setScanCodeState(int32_t scanCode, int32_t state) {
834        mScanCodeStates.replaceValueFor(scanCode, state);
835    }
836
837    void setSwitchState(int32_t switchCode, int32_t state) {
838        mSwitchStates.replaceValueFor(switchCode, state);
839    }
840
841    void addSupportedKeyCode(int32_t keyCode) {
842        mSupportedKeyCodes.add(keyCode);
843    }
844
845private:
846    virtual uint32_t getSources() {
847        return mSources;
848    }
849
850    virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
851        InputMapper::populateDeviceInfo(deviceInfo);
852
853        if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
854            deviceInfo->setKeyboardType(mKeyboardType);
855        }
856    }
857
858    virtual void configure() {
859        mConfigureWasCalled = true;
860    }
861
862    virtual void reset() {
863        mResetWasCalled = true;
864    }
865
866    virtual void process(const RawEvent* rawEvent) {
867        mLastEvent = *rawEvent;
868        mProcessWasCalled = true;
869    }
870
871    virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
872        ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
873        return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
874    }
875
876    virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
877        ssize_t index = mScanCodeStates.indexOfKey(scanCode);
878        return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
879    }
880
881    virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode) {
882        ssize_t index = mSwitchStates.indexOfKey(switchCode);
883        return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
884    }
885
886    virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
887            const int32_t* keyCodes, uint8_t* outFlags) {
888        bool result = false;
889        for (size_t i = 0; i < numCodes; i++) {
890            for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
891                if (keyCodes[i] == mSupportedKeyCodes[j]) {
892                    outFlags[i] = 1;
893                    result = true;
894                }
895            }
896        }
897        return result;
898    }
899
900    virtual int32_t getMetaState() {
901        return mMetaState;
902    }
903
904    virtual void fadePointer() {
905    }
906};
907
908
909// --- InstrumentedInputReader ---
910
911class InstrumentedInputReader : public InputReader {
912    InputDevice* mNextDevice;
913
914public:
915    InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
916            const sp<InputReaderPolicyInterface>& policy,
917            const sp<InputDispatcherInterface>& dispatcher) :
918            InputReader(eventHub, policy, dispatcher),
919            mNextDevice(NULL) {
920    }
921
922    virtual ~InstrumentedInputReader() {
923        if (mNextDevice) {
924            delete mNextDevice;
925        }
926    }
927
928    void setNextDevice(InputDevice* device) {
929        mNextDevice = device;
930    }
931
932protected:
933    virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
934        if (mNextDevice) {
935            InputDevice* device = mNextDevice;
936            mNextDevice = NULL;
937            return device;
938        }
939        return InputReader::createDevice(deviceId, name, classes);
940    }
941
942    friend class InputReaderTest;
943};
944
945
946// --- InputReaderTest ---
947
948class InputReaderTest : public testing::Test {
949protected:
950    sp<FakeInputDispatcher> mFakeDispatcher;
951    sp<FakeInputReaderPolicy> mFakePolicy;
952    sp<FakeEventHub> mFakeEventHub;
953    sp<InstrumentedInputReader> mReader;
954
955    virtual void SetUp() {
956        mFakeEventHub = new FakeEventHub();
957        mFakePolicy = new FakeInputReaderPolicy();
958        mFakeDispatcher = new FakeInputDispatcher();
959
960        mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeDispatcher);
961    }
962
963    virtual void TearDown() {
964        mReader.clear();
965
966        mFakeDispatcher.clear();
967        mFakePolicy.clear();
968        mFakeEventHub.clear();
969    }
970
971    void addDevice(int32_t deviceId, const String8& name, uint32_t classes,
972            const PropertyMap* configuration) {
973        mFakeEventHub->addDevice(deviceId, name, classes);
974        if (configuration) {
975            mFakeEventHub->addConfigurationMap(deviceId, configuration);
976        }
977        mFakeEventHub->finishDeviceScan();
978        mReader->loopOnce();
979        mReader->loopOnce();
980        mFakeEventHub->assertQueueIsEmpty();
981    }
982
983    FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId,
984            const String8& name, uint32_t classes, uint32_t sources,
985            const PropertyMap* configuration) {
986        InputDevice* device = new InputDevice(mReader.get(), deviceId, name);
987        FakeInputMapper* mapper = new FakeInputMapper(device, sources);
988        device->addMapper(mapper);
989        mReader->setNextDevice(device);
990        addDevice(deviceId, name, classes, configuration);
991        return mapper;
992    }
993};
994
995TEST_F(InputReaderTest, GetInputConfiguration_WhenNoDevices_ReturnsDefaults) {
996    InputConfiguration config;
997    mReader->getInputConfiguration(&config);
998
999    ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1000    ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1001    ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1002}
1003
1004TEST_F(InputReaderTest, GetInputConfiguration_WhenAlphabeticKeyboardPresent_ReturnsQwertyKeyboard) {
1005    ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("keyboard"),
1006            INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_ALPHAKEY, NULL));
1007
1008    InputConfiguration config;
1009    mReader->getInputConfiguration(&config);
1010
1011    ASSERT_EQ(InputConfiguration::KEYBOARD_QWERTY, config.keyboard);
1012    ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1013    ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1014}
1015
1016TEST_F(InputReaderTest, GetInputConfiguration_WhenTouchScreenPresent_ReturnsFingerTouchScreen) {
1017    PropertyMap configuration;
1018    configuration.addProperty(String8("touch.deviceType"), String8("touchScreen"));
1019    ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("touchscreen"),
1020            INPUT_DEVICE_CLASS_TOUCH, &configuration));
1021
1022    InputConfiguration config;
1023    mReader->getInputConfiguration(&config);
1024
1025    ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1026    ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1027    ASSERT_EQ(InputConfiguration::TOUCHSCREEN_FINGER, config.touchScreen);
1028}
1029
1030TEST_F(InputReaderTest, GetInputConfiguration_WhenTouchPadPresent_ReturnsFingerNoTouch) {
1031    ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("touchpad"),
1032            INPUT_DEVICE_CLASS_TOUCH, NULL));
1033
1034    InputConfiguration config;
1035    mReader->getInputConfiguration(&config);
1036
1037    ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1038    ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1039    ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1040}
1041
1042TEST_F(InputReaderTest, GetInputConfiguration_WhenMousePresent_ReturnsNoNavigation) {
1043    sp<FakePointerController> controller = new FakePointerController();
1044    mFakePolicy->setPointerController(0, controller);
1045
1046    PropertyMap configuration;
1047    configuration.addProperty(String8("cursor.mode"), String8("pointer"));
1048    ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("mouse"),
1049            INPUT_DEVICE_CLASS_CURSOR, &configuration));
1050
1051    InputConfiguration config;
1052    mReader->getInputConfiguration(&config);
1053
1054    ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1055    ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1056    ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1057}
1058
1059TEST_F(InputReaderTest, GetInputConfiguration_WhenTrackballPresent_ReturnsTrackballNavigation) {
1060    PropertyMap configuration;
1061    configuration.addProperty(String8("cursor.mode"), String8("navigation"));
1062    ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("trackball"),
1063            INPUT_DEVICE_CLASS_CURSOR, &configuration));
1064
1065    InputConfiguration config;
1066    mReader->getInputConfiguration(&config);
1067
1068    ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1069    ASSERT_EQ(InputConfiguration::NAVIGATION_TRACKBALL, config.navigation);
1070    ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1071}
1072
1073TEST_F(InputReaderTest, GetInputConfiguration_WhenDPadPresent_ReturnsDPadNavigation) {
1074    ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("dpad"),
1075            INPUT_DEVICE_CLASS_DPAD, NULL));
1076
1077    InputConfiguration config;
1078    mReader->getInputConfiguration(&config);
1079
1080    ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1081    ASSERT_EQ(InputConfiguration::NAVIGATION_DPAD, config.navigation);
1082    ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1083}
1084
1085TEST_F(InputReaderTest, GetInputDeviceInfo_WhenDeviceIdIsValid) {
1086    ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),
1087            INPUT_DEVICE_CLASS_KEYBOARD, NULL));
1088
1089    InputDeviceInfo info;
1090    status_t result = mReader->getInputDeviceInfo(1, &info);
1091
1092    ASSERT_EQ(OK, result);
1093    ASSERT_EQ(1, info.getId());
1094    ASSERT_STREQ("keyboard", info.getName().string());
1095    ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, info.getKeyboardType());
1096    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, info.getSources());
1097    ASSERT_EQ(size_t(0), info.getMotionRanges().size());
1098}
1099
1100TEST_F(InputReaderTest, GetInputDeviceInfo_WhenDeviceIdIsInvalid) {
1101    InputDeviceInfo info;
1102    status_t result = mReader->getInputDeviceInfo(-1, &info);
1103
1104    ASSERT_EQ(NAME_NOT_FOUND, result);
1105}
1106
1107TEST_F(InputReaderTest, GetInputDeviceInfo_WhenDeviceIdIsIgnored) {
1108    addDevice(1, String8("ignored"), 0, NULL); // no classes so device will be ignored
1109
1110    InputDeviceInfo info;
1111    status_t result = mReader->getInputDeviceInfo(1, &info);
1112
1113    ASSERT_EQ(NAME_NOT_FOUND, result);
1114}
1115
1116TEST_F(InputReaderTest, GetInputDeviceIds) {
1117    sp<FakePointerController> controller = new FakePointerController();
1118    mFakePolicy->setPointerController(2, controller);
1119
1120    ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),
1121            INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_ALPHAKEY, NULL));
1122    ASSERT_NO_FATAL_FAILURE(addDevice(2, String8("mouse"),
1123            INPUT_DEVICE_CLASS_CURSOR, NULL));
1124
1125    Vector<int32_t> ids;
1126    mReader->getInputDeviceIds(ids);
1127
1128    ASSERT_EQ(size_t(2), ids.size());
1129    ASSERT_EQ(1, ids[0]);
1130    ASSERT_EQ(2, ids[1]);
1131}
1132
1133TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
1134    FakeInputMapper* mapper = NULL;
1135    ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
1136            INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1137    mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1138
1139    ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1140            AINPUT_SOURCE_ANY, AKEYCODE_A))
1141            << "Should return unknown when the device id is >= 0 but unknown.";
1142
1143    ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1144            AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1145            << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1146
1147    ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1148            AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1149            << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1150
1151    ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1152            AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1153            << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1154
1155    ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1156            AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1157            << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1158}
1159
1160TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
1161    FakeInputMapper* mapper = NULL;
1162    ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
1163            INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1164    mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1165
1166    ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1167            AINPUT_SOURCE_ANY, KEY_A))
1168            << "Should return unknown when the device id is >= 0 but unknown.";
1169
1170    ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1171            AINPUT_SOURCE_TRACKBALL, KEY_A))
1172            << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1173
1174    ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1175            AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1176            << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1177
1178    ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1179            AINPUT_SOURCE_TRACKBALL, KEY_A))
1180            << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1181
1182    ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1183            AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1184            << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1185}
1186
1187TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
1188    FakeInputMapper* mapper = NULL;
1189    ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
1190            INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1191    mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1192
1193    ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1194            AINPUT_SOURCE_ANY, SW_LID))
1195            << "Should return unknown when the device id is >= 0 but unknown.";
1196
1197    ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1198            AINPUT_SOURCE_TRACKBALL, SW_LID))
1199            << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1200
1201    ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1202            AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1203            << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1204
1205    ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1206            AINPUT_SOURCE_TRACKBALL, SW_LID))
1207            << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1208
1209    ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1210            AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1211            << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1212}
1213
1214TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
1215    FakeInputMapper* mapper = NULL;
1216    ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
1217            INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1218    mapper->addSupportedKeyCode(AKEYCODE_A);
1219    mapper->addSupportedKeyCode(AKEYCODE_B);
1220
1221    const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1222    uint8_t flags[4] = { 0, 0, 0, 1 };
1223
1224    ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1225            << "Should return false when device id is >= 0 but unknown.";
1226    ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1227
1228    flags[3] = 1;
1229    ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1230            << "Should return false when device id is valid but the sources are not supported by the device.";
1231    ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1232
1233    flags[3] = 1;
1234    ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1235            << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1236    ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1237
1238    flags[3] = 1;
1239    ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1240            << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1241    ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1242
1243    flags[3] = 1;
1244    ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1245            << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1246    ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1247}
1248
1249TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
1250    addDevice(1, String8("ignored"), INPUT_DEVICE_CLASS_KEYBOARD, NULL);
1251
1252    FakeInputDispatcher::NotifyConfigurationChangedArgs args;
1253    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyConfigurationChangedWasCalled(&args));
1254    ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1255}
1256
1257TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
1258    FakeInputMapper* mapper = NULL;
1259    ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
1260            INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1261
1262    mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, AKEYCODE_A, 1, POLICY_FLAG_WAKE);
1263    mReader->loopOnce();
1264    ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1265
1266    RawEvent event;
1267    ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1268    ASSERT_EQ(0, event.when);
1269    ASSERT_EQ(1, event.deviceId);
1270    ASSERT_EQ(EV_KEY, event.type);
1271    ASSERT_EQ(KEY_A, event.scanCode);
1272    ASSERT_EQ(AKEYCODE_A, event.keyCode);
1273    ASSERT_EQ(1, event.value);
1274    ASSERT_EQ(POLICY_FLAG_WAKE, event.flags);
1275}
1276
1277
1278// --- InputDeviceTest ---
1279
1280class InputDeviceTest : public testing::Test {
1281protected:
1282    static const char* DEVICE_NAME;
1283    static const int32_t DEVICE_ID;
1284
1285    sp<FakeEventHub> mFakeEventHub;
1286    sp<FakeInputReaderPolicy> mFakePolicy;
1287    sp<FakeInputDispatcher> mFakeDispatcher;
1288    FakeInputReaderContext* mFakeContext;
1289
1290    InputDevice* mDevice;
1291
1292    virtual void SetUp() {
1293        mFakeEventHub = new FakeEventHub();
1294        mFakePolicy = new FakeInputReaderPolicy();
1295        mFakeDispatcher = new FakeInputDispatcher();
1296        mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeDispatcher);
1297
1298        mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
1299        mDevice = new InputDevice(mFakeContext, DEVICE_ID, String8(DEVICE_NAME));
1300    }
1301
1302    virtual void TearDown() {
1303        delete mDevice;
1304
1305        delete mFakeContext;
1306        mFakeDispatcher.clear();
1307        mFakePolicy.clear();
1308        mFakeEventHub.clear();
1309    }
1310};
1311
1312const char* InputDeviceTest::DEVICE_NAME = "device";
1313const int32_t InputDeviceTest::DEVICE_ID = 1;
1314
1315TEST_F(InputDeviceTest, ImmutableProperties) {
1316    ASSERT_EQ(DEVICE_ID, mDevice->getId());
1317    ASSERT_STREQ(DEVICE_NAME, mDevice->getName());
1318}
1319
1320TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1321    // Configuration.
1322    mDevice->configure();
1323
1324    // Metadata.
1325    ASSERT_TRUE(mDevice->isIgnored());
1326    ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1327
1328    InputDeviceInfo info;
1329    mDevice->getDeviceInfo(&info);
1330    ASSERT_EQ(DEVICE_ID, info.getId());
1331    ASSERT_STREQ(DEVICE_NAME, info.getName().string());
1332    ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1333    ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1334
1335    // State queries.
1336    ASSERT_EQ(0, mDevice->getMetaState());
1337
1338    ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1339            << "Ignored device should return unknown key code state.";
1340    ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1341            << "Ignored device should return unknown scan code state.";
1342    ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1343            << "Ignored device should return unknown switch state.";
1344
1345    const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1346    uint8_t flags[2] = { 0, 1 };
1347    ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1348            << "Ignored device should never mark any key codes.";
1349    ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1350    ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1351
1352    // Reset.
1353    mDevice->reset();
1354}
1355
1356TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1357    // Configuration.
1358    mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1359
1360    FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1361    mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1362    mapper1->setMetaState(AMETA_ALT_ON);
1363    mapper1->addSupportedKeyCode(AKEYCODE_A);
1364    mapper1->addSupportedKeyCode(AKEYCODE_B);
1365    mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1366    mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1367    mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1368    mapper1->setScanCodeState(3, AKEY_STATE_UP);
1369    mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1370    mDevice->addMapper(mapper1);
1371
1372    FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1373    mapper2->setMetaState(AMETA_SHIFT_ON);
1374    mDevice->addMapper(mapper2);
1375
1376    mDevice->configure();
1377
1378    String8 propertyValue;
1379    ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1380            << "Device should have read configuration during configuration phase.";
1381    ASSERT_STREQ("value", propertyValue.string());
1382
1383    ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1384    ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1385
1386    // Metadata.
1387    ASSERT_FALSE(mDevice->isIgnored());
1388    ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1389
1390    InputDeviceInfo info;
1391    mDevice->getDeviceInfo(&info);
1392    ASSERT_EQ(DEVICE_ID, info.getId());
1393    ASSERT_STREQ(DEVICE_NAME, info.getName().string());
1394    ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1395    ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1396
1397    // State queries.
1398    ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1399            << "Should query mappers and combine meta states.";
1400
1401    ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1402            << "Should return unknown key code state when source not supported.";
1403    ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1404            << "Should return unknown scan code state when source not supported.";
1405    ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1406            << "Should return unknown switch state when source not supported.";
1407
1408    ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1409            << "Should query mapper when source is supported.";
1410    ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1411            << "Should query mapper when source is supported.";
1412    ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1413            << "Should query mapper when source is supported.";
1414
1415    const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1416    uint8_t flags[4] = { 0, 0, 0, 1 };
1417    ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1418            << "Should do nothing when source is unsupported.";
1419    ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1420    ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1421    ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1422    ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1423
1424    ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1425            << "Should query mapper when source is supported.";
1426    ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1427    ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1428    ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1429    ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1430
1431    // Event handling.
1432    RawEvent event;
1433    mDevice->process(&event);
1434
1435    ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1436    ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1437
1438    // Reset.
1439    mDevice->reset();
1440
1441    ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1442    ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1443}
1444
1445
1446// --- InputMapperTest ---
1447
1448class InputMapperTest : public testing::Test {
1449protected:
1450    static const char* DEVICE_NAME;
1451    static const int32_t DEVICE_ID;
1452
1453    sp<FakeEventHub> mFakeEventHub;
1454    sp<FakeInputReaderPolicy> mFakePolicy;
1455    sp<FakeInputDispatcher> mFakeDispatcher;
1456    FakeInputReaderContext* mFakeContext;
1457    InputDevice* mDevice;
1458
1459    virtual void SetUp() {
1460        mFakeEventHub = new FakeEventHub();
1461        mFakePolicy = new FakeInputReaderPolicy();
1462        mFakeDispatcher = new FakeInputDispatcher();
1463        mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeDispatcher);
1464        mDevice = new InputDevice(mFakeContext, DEVICE_ID, String8(DEVICE_NAME));
1465
1466        mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
1467    }
1468
1469    virtual void TearDown() {
1470        delete mDevice;
1471        delete mFakeContext;
1472        mFakeDispatcher.clear();
1473        mFakePolicy.clear();
1474        mFakeEventHub.clear();
1475    }
1476
1477    void addConfigurationProperty(const char* key, const char* value) {
1478        mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8(key), String8(value));
1479    }
1480
1481    void addMapperAndConfigure(InputMapper* mapper) {
1482        mDevice->addMapper(mapper);
1483        mDevice->configure();
1484    }
1485
1486    static void process(InputMapper* mapper, nsecs_t when, int32_t deviceId, int32_t type,
1487            int32_t scanCode, int32_t keyCode, int32_t value, uint32_t flags) {
1488        RawEvent event;
1489        event.when = when;
1490        event.deviceId = deviceId;
1491        event.type = type;
1492        event.scanCode = scanCode;
1493        event.keyCode = keyCode;
1494        event.value = value;
1495        event.flags = flags;
1496        mapper->process(&event);
1497    }
1498
1499    static void assertMotionRange(const InputDeviceInfo& info,
1500            int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1501        const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
1502        ASSERT_TRUE(range != NULL) << "Axis: " << axis << " Source: " << source;
1503        ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1504        ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1505        ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1506        ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1507        ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1508        ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1509    }
1510
1511    static void assertPointerCoords(const PointerCoords& coords,
1512            float x, float y, float pressure, float size,
1513            float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1514            float orientation) {
1515        ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1516        ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1517        ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1518        ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1519        ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1520        ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1521        ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1522        ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1523        ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1524    }
1525};
1526
1527const char* InputMapperTest::DEVICE_NAME = "device";
1528const int32_t InputMapperTest::DEVICE_ID = 1;
1529
1530
1531// --- SwitchInputMapperTest ---
1532
1533class SwitchInputMapperTest : public InputMapperTest {
1534protected:
1535};
1536
1537TEST_F(SwitchInputMapperTest, GetSources) {
1538    SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1539    addMapperAndConfigure(mapper);
1540
1541    ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1542}
1543
1544TEST_F(SwitchInputMapperTest, GetSwitchState) {
1545    SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1546    addMapperAndConfigure(mapper);
1547
1548    mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1549    ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1550
1551    mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1552    ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1553}
1554
1555TEST_F(SwitchInputMapperTest, Process) {
1556    SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1557    addMapperAndConfigure(mapper);
1558
1559    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_LID, 0, 1, 0);
1560
1561    FakeInputDispatcher::NotifySwitchArgs args;
1562    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifySwitchWasCalled(&args));
1563    ASSERT_EQ(ARBITRARY_TIME, args.when);
1564    ASSERT_EQ(SW_LID, args.switchCode);
1565    ASSERT_EQ(1, args.switchValue);
1566    ASSERT_EQ(uint32_t(0), args.policyFlags);
1567}
1568
1569
1570// --- KeyboardInputMapperTest ---
1571
1572class KeyboardInputMapperTest : public InputMapperTest {
1573protected:
1574    void testDPadKeyRotation(KeyboardInputMapper* mapper,
1575            int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1576};
1577
1578void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
1579        int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode) {
1580    FakeInputDispatcher::NotifyKeyArgs args;
1581
1582    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, originalKeyCode, 1, 0);
1583    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1584    ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1585    ASSERT_EQ(originalScanCode, args.scanCode);
1586    ASSERT_EQ(rotatedKeyCode, args.keyCode);
1587
1588    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, originalKeyCode, 0, 0);
1589    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1590    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1591    ASSERT_EQ(originalScanCode, args.scanCode);
1592    ASSERT_EQ(rotatedKeyCode, args.keyCode);
1593}
1594
1595
1596TEST_F(KeyboardInputMapperTest, GetSources) {
1597    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1598            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1599    addMapperAndConfigure(mapper);
1600
1601    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1602}
1603
1604TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
1605    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1606            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1607    addMapperAndConfigure(mapper);
1608
1609    // Key down.
1610    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1611            EV_KEY, KEY_HOME, AKEYCODE_HOME, 1, POLICY_FLAG_WAKE);
1612    FakeInputDispatcher::NotifyKeyArgs args;
1613    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1614    ASSERT_EQ(DEVICE_ID, args.deviceId);
1615    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1616    ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1617    ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1618    ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1619    ASSERT_EQ(KEY_HOME, args.scanCode);
1620    ASSERT_EQ(AMETA_NONE, args.metaState);
1621    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1622    ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1623    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1624
1625    // Key up.
1626    process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1627            EV_KEY, KEY_HOME, AKEYCODE_HOME, 0, POLICY_FLAG_WAKE);
1628    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1629    ASSERT_EQ(DEVICE_ID, args.deviceId);
1630    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1631    ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1632    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1633    ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1634    ASSERT_EQ(KEY_HOME, args.scanCode);
1635    ASSERT_EQ(AMETA_NONE, args.metaState);
1636    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1637    ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1638    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1639}
1640
1641TEST_F(KeyboardInputMapperTest, Reset_WhenKeysAreNotDown_DoesNotSynthesizeKeyUp) {
1642    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1643            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1644    addMapperAndConfigure(mapper);
1645
1646    // Key down.
1647    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1648            EV_KEY, KEY_HOME, AKEYCODE_HOME, 1, POLICY_FLAG_WAKE);
1649    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1650
1651    // Key up.
1652    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1653            EV_KEY, KEY_HOME, AKEYCODE_HOME, 0, POLICY_FLAG_WAKE);
1654    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1655
1656    // Reset.  Since no keys still down, should not synthesize any key ups.
1657    mapper->reset();
1658    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
1659}
1660
1661TEST_F(KeyboardInputMapperTest, Reset_WhenKeysAreDown_SynthesizesKeyUps) {
1662    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1663            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1664    addMapperAndConfigure(mapper);
1665
1666    // Metakey down.
1667    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1668            EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 1, 0);
1669    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1670
1671    // Key down.
1672    process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1673            EV_KEY, KEY_A, AKEYCODE_A, 1, 0);
1674    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1675
1676    // Reset.  Since two keys are still down, should synthesize two key ups in reverse order.
1677    mapper->reset();
1678
1679    FakeInputDispatcher::NotifyKeyArgs args;
1680    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1681    ASSERT_EQ(DEVICE_ID, args.deviceId);
1682    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1683    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1684    ASSERT_EQ(AKEYCODE_A, args.keyCode);
1685    ASSERT_EQ(KEY_A, args.scanCode);
1686    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1687    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1688    ASSERT_EQ(uint32_t(0), args.policyFlags);
1689    ASSERT_EQ(ARBITRARY_TIME + 1, args.downTime);
1690
1691    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1692    ASSERT_EQ(DEVICE_ID, args.deviceId);
1693    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1694    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1695    ASSERT_EQ(AKEYCODE_SHIFT_LEFT, args.keyCode);
1696    ASSERT_EQ(KEY_LEFTSHIFT, args.scanCode);
1697    ASSERT_EQ(AMETA_NONE, args.metaState);
1698    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1699    ASSERT_EQ(uint32_t(0), args.policyFlags);
1700    ASSERT_EQ(ARBITRARY_TIME + 1, args.downTime);
1701
1702    // And that's it.
1703    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
1704}
1705
1706TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
1707    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1708            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1709    addMapperAndConfigure(mapper);
1710
1711    // Initial metastate.
1712    ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1713
1714    // Metakey down.
1715    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1716            EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 1, 0);
1717    FakeInputDispatcher::NotifyKeyArgs args;
1718    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1719    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1720    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1721    ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1722
1723    // Key down.
1724    process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1725            EV_KEY, KEY_A, AKEYCODE_A, 1, 0);
1726    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1727    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1728    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1729
1730    // Key up.
1731    process(mapper, ARBITRARY_TIME + 2, DEVICE_ID,
1732            EV_KEY, KEY_A, AKEYCODE_A, 0, 0);
1733    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1734    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1735    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1736
1737    // Metakey up.
1738    process(mapper, ARBITRARY_TIME + 3, DEVICE_ID,
1739            EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 0, 0);
1740    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1741    ASSERT_EQ(AMETA_NONE, args.metaState);
1742    ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1743    ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1744}
1745
1746TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
1747    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1748            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1749    addMapperAndConfigure(mapper);
1750
1751    mFakePolicy->setDisplayInfo(DISPLAY_ID,
1752            DISPLAY_WIDTH, DISPLAY_HEIGHT,
1753            DISPLAY_ORIENTATION_90);
1754    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1755            KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1756    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1757            KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1758    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1759            KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1760    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1761            KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1762}
1763
1764TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
1765    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1766            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1767    addConfigurationProperty("keyboard.orientationAware", "1");
1768    addMapperAndConfigure(mapper);
1769
1770    mFakePolicy->setDisplayInfo(DISPLAY_ID,
1771            DISPLAY_WIDTH, DISPLAY_HEIGHT,
1772            DISPLAY_ORIENTATION_0);
1773    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1774            KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1775    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1776            KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1777    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1778            KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1779    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1780            KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1781
1782    mFakePolicy->setDisplayInfo(DISPLAY_ID,
1783            DISPLAY_WIDTH, DISPLAY_HEIGHT,
1784            DISPLAY_ORIENTATION_90);
1785    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1786            KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
1787    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1788            KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
1789    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1790            KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
1791    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1792            KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
1793
1794    mFakePolicy->setDisplayInfo(DISPLAY_ID,
1795            DISPLAY_WIDTH, DISPLAY_HEIGHT,
1796            DISPLAY_ORIENTATION_180);
1797    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1798            KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
1799    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1800            KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
1801    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1802            KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
1803    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1804            KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
1805
1806    mFakePolicy->setDisplayInfo(DISPLAY_ID,
1807            DISPLAY_WIDTH, DISPLAY_HEIGHT,
1808            DISPLAY_ORIENTATION_270);
1809    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1810            KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
1811    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1812            KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
1813    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1814            KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
1815    ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1816            KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
1817
1818    // Special case: if orientation changes while key is down, we still emit the same keycode
1819    // in the key up as we did in the key down.
1820    FakeInputDispatcher::NotifyKeyArgs args;
1821
1822    mFakePolicy->setDisplayInfo(DISPLAY_ID,
1823            DISPLAY_WIDTH, DISPLAY_HEIGHT,
1824            DISPLAY_ORIENTATION_270);
1825    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, AKEYCODE_DPAD_UP, 1, 0);
1826    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1827    ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1828    ASSERT_EQ(KEY_UP, args.scanCode);
1829    ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1830
1831    mFakePolicy->setDisplayInfo(DISPLAY_ID,
1832            DISPLAY_WIDTH, DISPLAY_HEIGHT,
1833            DISPLAY_ORIENTATION_180);
1834    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, AKEYCODE_DPAD_UP, 0, 0);
1835    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1836    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1837    ASSERT_EQ(KEY_UP, args.scanCode);
1838    ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1839}
1840
1841TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
1842    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1843            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1844    addMapperAndConfigure(mapper);
1845
1846    mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
1847    ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
1848
1849    mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
1850    ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
1851}
1852
1853TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
1854    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1855            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1856    addMapperAndConfigure(mapper);
1857
1858    mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
1859    ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
1860
1861    mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
1862    ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
1863}
1864
1865TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
1866    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1867            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1868    addMapperAndConfigure(mapper);
1869
1870    mFakeEventHub->addKey(DEVICE_ID, KEY_A, AKEYCODE_A, 0);
1871
1872    const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1873    uint8_t flags[2] = { 0, 0 };
1874    ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
1875    ASSERT_TRUE(flags[0]);
1876    ASSERT_FALSE(flags[1]);
1877}
1878
1879TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
1880    mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
1881    mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
1882    mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
1883
1884    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1885            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1886    addMapperAndConfigure(mapper);
1887
1888    // Initialization should have turned all of the lights off.
1889    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1890    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1891    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1892
1893    // Toggle caps lock on.
1894    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1895            EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 1, 0);
1896    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1897            EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0, 0);
1898    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1899    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1900    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1901    ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
1902
1903    // Toggle num lock on.
1904    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1905            EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 1, 0);
1906    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1907            EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0, 0);
1908    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1909    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1910    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1911    ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
1912
1913    // Toggle caps lock off.
1914    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1915            EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 1, 0);
1916    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1917            EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0, 0);
1918    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1919    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1920    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1921    ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
1922
1923    // Toggle scroll lock on.
1924    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1925            EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 1, 0);
1926    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1927            EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0, 0);
1928    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1929    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1930    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1931    ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
1932
1933    // Toggle num lock off.
1934    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1935            EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 1, 0);
1936    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1937            EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0, 0);
1938    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1939    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1940    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1941    ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
1942
1943    // Toggle scroll lock off.
1944    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1945            EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 1, 0);
1946    process(mapper, ARBITRARY_TIME, DEVICE_ID,
1947            EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0, 0);
1948    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1949    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1950    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1951    ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1952}
1953
1954
1955// --- CursorInputMapperTest ---
1956
1957class CursorInputMapperTest : public InputMapperTest {
1958protected:
1959    static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
1960
1961    sp<FakePointerController> mFakePointerController;
1962
1963    virtual void SetUp() {
1964        InputMapperTest::SetUp();
1965
1966        mFakePointerController = new FakePointerController();
1967        mFakePolicy->setPointerController(DEVICE_ID, mFakePointerController);
1968    }
1969
1970    void testMotionRotation(CursorInputMapper* mapper,
1971            int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
1972};
1973
1974const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
1975
1976void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
1977        int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
1978    FakeInputDispatcher::NotifyMotionArgs args;
1979
1980    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, originalX, 0);
1981    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, originalY, 0);
1982    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
1983    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
1984    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1985    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
1986            float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
1987            float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
1988            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
1989}
1990
1991TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
1992    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
1993    addConfigurationProperty("cursor.mode", "pointer");
1994    addMapperAndConfigure(mapper);
1995
1996    ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
1997}
1998
1999TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2000    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2001    addConfigurationProperty("cursor.mode", "navigation");
2002    addMapperAndConfigure(mapper);
2003
2004    ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2005}
2006
2007TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2008    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2009    addConfigurationProperty("cursor.mode", "pointer");
2010    addMapperAndConfigure(mapper);
2011
2012    InputDeviceInfo info;
2013    mapper->populateDeviceInfo(&info);
2014
2015    // Initially there may not be a valid motion range.
2016    ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2017    ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
2018    ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2019            AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2020
2021    // When the bounds are set, then there should be a valid motion range.
2022    mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2023
2024    InputDeviceInfo info2;
2025    mapper->populateDeviceInfo(&info2);
2026
2027    ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2028            AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2029            1, 800 - 1, 0.0f, 0.0f));
2030    ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2031            AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2032            2, 480 - 1, 0.0f, 0.0f));
2033    ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2034            AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2035            0.0f, 1.0f, 0.0f, 0.0f));
2036}
2037
2038TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2039    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2040    addConfigurationProperty("cursor.mode", "navigation");
2041    addMapperAndConfigure(mapper);
2042
2043    InputDeviceInfo info;
2044    mapper->populateDeviceInfo(&info);
2045
2046    ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2047            AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2048            -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2049    ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2050            AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2051            -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2052    ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2053            AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2054            0.0f, 1.0f, 0.0f, 0.0f));
2055}
2056
2057TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2058    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2059    addConfigurationProperty("cursor.mode", "navigation");
2060    addMapperAndConfigure(mapper);
2061
2062    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2063
2064    FakeInputDispatcher::NotifyMotionArgs args;
2065
2066    // Button press.
2067    // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
2068    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2069    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2070    ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2071    ASSERT_EQ(DEVICE_ID, args.deviceId);
2072    ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2073    ASSERT_EQ(uint32_t(0), args.policyFlags);
2074    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2075    ASSERT_EQ(0, args.flags);
2076    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2077    ASSERT_EQ(0, args.edgeFlags);
2078    ASSERT_EQ(uint32_t(1), args.pointerCount);
2079    ASSERT_EQ(0, args.pointerIds[0]);
2080    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2081            0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2082    ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2083    ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2084    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2085
2086    // Button release.  Should have same down time.
2087    process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
2088    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2089    ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2090    ASSERT_EQ(DEVICE_ID, args.deviceId);
2091    ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2092    ASSERT_EQ(uint32_t(0), args.policyFlags);
2093    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2094    ASSERT_EQ(0, args.flags);
2095    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2096    ASSERT_EQ(0, args.edgeFlags);
2097    ASSERT_EQ(uint32_t(1), args.pointerCount);
2098    ASSERT_EQ(0, args.pointerIds[0]);
2099    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2100            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2101    ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2102    ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2103    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2104}
2105
2106TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2107    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2108    addConfigurationProperty("cursor.mode", "navigation");
2109    addMapperAndConfigure(mapper);
2110
2111    FakeInputDispatcher::NotifyMotionArgs args;
2112
2113    // Motion in X but not Y.
2114    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, 1, 0);
2115    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2116    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2117    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2118    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2119            1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2120
2121    // Motion in Y but not X.
2122    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, -2, 0);
2123    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2124    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2125    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2126    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2127            0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2128}
2129
2130TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2131    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2132    addConfigurationProperty("cursor.mode", "navigation");
2133    addMapperAndConfigure(mapper);
2134
2135    FakeInputDispatcher::NotifyMotionArgs args;
2136
2137    // Button press without following sync.
2138    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2139    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2140    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2141    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2142            0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2143
2144    // Button release without following sync.
2145    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
2146    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2147    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2148    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2149            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2150}
2151
2152TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2153    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2154    addConfigurationProperty("cursor.mode", "navigation");
2155    addMapperAndConfigure(mapper);
2156
2157    FakeInputDispatcher::NotifyMotionArgs args;
2158
2159    // Combined X, Y and Button.
2160    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, 1, 0);
2161    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, -2, 0);
2162    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2163    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2164    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2165    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2166    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2167            1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2168            1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2169
2170    // Move X, Y a bit while pressed.
2171    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, 2, 0);
2172    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, 1, 0);
2173    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2174    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2175    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2176    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2177            2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2178            1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2179
2180    // Release Button.
2181    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
2182    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2183    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2184    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2185            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2186}
2187
2188TEST_F(CursorInputMapperTest, Reset_WhenButtonIsNotDown_ShouldNotSynthesizeButtonUp) {
2189    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2190    addConfigurationProperty("cursor.mode", "navigation");
2191    addMapperAndConfigure(mapper);
2192
2193    FakeInputDispatcher::NotifyMotionArgs args;
2194
2195    // Button press.
2196    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2197    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2198
2199    // Button release.
2200    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
2201    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2202
2203    // Reset.  Should not synthesize button up since button is not pressed.
2204    mapper->reset();
2205
2206    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2207}
2208
2209TEST_F(CursorInputMapperTest, Reset_WhenButtonIsDown_ShouldSynthesizeButtonUp) {
2210    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2211    addConfigurationProperty("cursor.mode", "navigation");
2212    addMapperAndConfigure(mapper);
2213
2214    FakeInputDispatcher::NotifyMotionArgs args;
2215
2216    // Button press.
2217    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2218    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2219
2220    // Reset.  Should synthesize button up.
2221    mapper->reset();
2222
2223    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2224    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2225    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2226            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2227}
2228
2229TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2230    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2231    addConfigurationProperty("cursor.mode", "navigation");
2232    addMapperAndConfigure(mapper);
2233
2234    mFakePolicy->setDisplayInfo(DISPLAY_ID,
2235            DISPLAY_WIDTH, DISPLAY_HEIGHT,
2236            DISPLAY_ORIENTATION_90);
2237    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
2238    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1,  1));
2239    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  1,  0));
2240    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1, -1));
2241    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0, -1));
2242    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2243    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0, -1,  0));
2244    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1,  1));
2245}
2246
2247TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2248    CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2249    addConfigurationProperty("cursor.mode", "navigation");
2250    addConfigurationProperty("cursor.orientationAware", "1");
2251    addMapperAndConfigure(mapper);
2252
2253    mFakePolicy->setDisplayInfo(DISPLAY_ID,
2254            DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0);
2255    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
2256    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1,  1));
2257    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  1,  0));
2258    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1, -1));
2259    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0, -1));
2260    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2261    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0, -1,  0));
2262    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1,  1));
2263
2264    mFakePolicy->setDisplayInfo(DISPLAY_ID,
2265            DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_90);
2266    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  1,  0));
2267    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1, -1));
2268    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  0, -1));
2269    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1, -1, -1));
2270    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1, -1,  0));
2271    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1,  1));
2272    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  0,  1));
2273    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1,  1));
2274
2275    mFakePolicy->setDisplayInfo(DISPLAY_ID,
2276            DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_180);
2277    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0, -1));
2278    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1, -1));
2279    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0, -1,  0));
2280    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1, -1,  1));
2281    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0,  1));
2282    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1,  1,  1));
2283    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  1,  0));
2284    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1, -1));
2285
2286    mFakePolicy->setDisplayInfo(DISPLAY_ID,
2287            DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_270);
2288    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1, -1,  0));
2289    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1,  1));
2290    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  0,  1));
2291    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1,  1));
2292    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  1,  0));
2293    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1,  1, -1));
2294    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  0, -1));
2295    ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1, -1));
2296}
2297
2298
2299// --- TouchInputMapperTest ---
2300
2301class TouchInputMapperTest : public InputMapperTest {
2302protected:
2303    static const int32_t RAW_X_MIN;
2304    static const int32_t RAW_X_MAX;
2305    static const int32_t RAW_Y_MIN;
2306    static const int32_t RAW_Y_MAX;
2307    static const int32_t RAW_TOUCH_MIN;
2308    static const int32_t RAW_TOUCH_MAX;
2309    static const int32_t RAW_TOOL_MIN;
2310    static const int32_t RAW_TOOL_MAX;
2311    static const int32_t RAW_PRESSURE_MIN;
2312    static const int32_t RAW_PRESSURE_MAX;
2313    static const int32_t RAW_ORIENTATION_MIN;
2314    static const int32_t RAW_ORIENTATION_MAX;
2315    static const int32_t RAW_ID_MIN;
2316    static const int32_t RAW_ID_MAX;
2317    static const float X_PRECISION;
2318    static const float Y_PRECISION;
2319
2320    static const VirtualKeyDefinition VIRTUAL_KEYS[2];
2321
2322    enum Axes {
2323        POSITION = 1 << 0,
2324        TOUCH = 1 << 1,
2325        TOOL = 1 << 2,
2326        PRESSURE = 1 << 3,
2327        ORIENTATION = 1 << 4,
2328        MINOR = 1 << 5,
2329        ID = 1 << 6,
2330    };
2331
2332    void prepareDisplay(int32_t orientation);
2333    void prepareVirtualKeys();
2334    int32_t toRawX(float displayX);
2335    int32_t toRawY(float displayY);
2336    float toDisplayX(int32_t rawX);
2337    float toDisplayY(int32_t rawY);
2338};
2339
2340const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
2341const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
2342const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
2343const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
2344const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
2345const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
2346const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
2347const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
2348const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = RAW_TOUCH_MIN;
2349const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = RAW_TOUCH_MAX;
2350const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
2351const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
2352const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
2353const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
2354const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
2355const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
2356
2357const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
2358        { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
2359        { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
2360};
2361
2362void TouchInputMapperTest::prepareDisplay(int32_t orientation) {
2363    mFakePolicy->setDisplayInfo(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation);
2364}
2365
2366void TouchInputMapperTest::prepareVirtualKeys() {
2367    mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
2368    mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
2369    mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2370    mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, AKEYCODE_MENU, POLICY_FLAG_WAKE);
2371}
2372
2373int32_t TouchInputMapperTest::toRawX(float displayX) {
2374    return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
2375}
2376
2377int32_t TouchInputMapperTest::toRawY(float displayY) {
2378    return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
2379}
2380
2381float TouchInputMapperTest::toDisplayX(int32_t rawX) {
2382    return float(rawX - RAW_X_MIN) * DISPLAY_WIDTH / (RAW_X_MAX - RAW_X_MIN + 1);
2383}
2384
2385float TouchInputMapperTest::toDisplayY(int32_t rawY) {
2386    return float(rawY - RAW_Y_MIN) * DISPLAY_HEIGHT / (RAW_Y_MAX - RAW_Y_MIN + 1);
2387}
2388
2389
2390// --- SingleTouchInputMapperTest ---
2391
2392class SingleTouchInputMapperTest : public TouchInputMapperTest {
2393protected:
2394    void prepareAxes(int axes);
2395
2396    void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
2397    void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
2398    void processUp(SingleTouchInputMapper* mappery);
2399    void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
2400    void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
2401    void processSync(SingleTouchInputMapper* mapper);
2402};
2403
2404void SingleTouchInputMapperTest::prepareAxes(int axes) {
2405    if (axes & POSITION) {
2406        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
2407                RAW_X_MIN, RAW_X_MAX, 0, 0);
2408        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
2409                RAW_Y_MIN, RAW_Y_MAX, 0, 0);
2410    }
2411    if (axes & PRESSURE) {
2412        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
2413                RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
2414    }
2415    if (axes & TOOL) {
2416        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
2417                RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
2418    }
2419}
2420
2421void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
2422    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0, 1, 0);
2423    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, 0, x, 0);
2424    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, 0, y, 0);
2425}
2426
2427void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
2428    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, 0, x, 0);
2429    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, 0, y, 0);
2430}
2431
2432void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
2433    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0, 0, 0);
2434}
2435
2436void SingleTouchInputMapperTest::processPressure(
2437        SingleTouchInputMapper* mapper, int32_t pressure) {
2438    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_PRESSURE, 0, pressure, 0);
2439}
2440
2441void SingleTouchInputMapperTest::processToolMajor(
2442        SingleTouchInputMapper* mapper, int32_t toolMajor) {
2443    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TOOL_WIDTH, 0, toolMajor, 0);
2444}
2445
2446void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
2447    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2448}
2449
2450
2451TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecified_ReturnsTouchPad) {
2452    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2453    prepareAxes(POSITION);
2454    addMapperAndConfigure(mapper);
2455
2456    ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2457}
2458
2459TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
2460    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2461    prepareAxes(POSITION);
2462    addConfigurationProperty("touch.deviceType", "touchPad");
2463    addMapperAndConfigure(mapper);
2464
2465    ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2466}
2467
2468TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
2469    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2470    prepareAxes(POSITION);
2471    addConfigurationProperty("touch.deviceType", "touchScreen");
2472    addMapperAndConfigure(mapper);
2473
2474    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
2475}
2476
2477TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
2478    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2479    addConfigurationProperty("touch.deviceType", "touchScreen");
2480    prepareDisplay(DISPLAY_ORIENTATION_0);
2481    prepareAxes(POSITION);
2482    prepareVirtualKeys();
2483    addMapperAndConfigure(mapper);
2484
2485    // Unknown key.
2486    ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2487
2488    // Virtual key is down.
2489    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2490    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2491    processDown(mapper, x, y);
2492    processSync(mapper);
2493    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2494
2495    ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2496
2497    // Virtual key is up.
2498    processUp(mapper);
2499    processSync(mapper);
2500    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2501
2502    ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2503}
2504
2505TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
2506    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2507    addConfigurationProperty("touch.deviceType", "touchScreen");
2508    prepareDisplay(DISPLAY_ORIENTATION_0);
2509    prepareAxes(POSITION);
2510    prepareVirtualKeys();
2511    addMapperAndConfigure(mapper);
2512
2513    // Unknown key.
2514    ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2515
2516    // Virtual key is down.
2517    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2518    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2519    processDown(mapper, x, y);
2520    processSync(mapper);
2521    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2522
2523    ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2524
2525    // Virtual key is up.
2526    processUp(mapper);
2527    processSync(mapper);
2528    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2529
2530    ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2531}
2532
2533TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
2534    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2535    addConfigurationProperty("touch.deviceType", "touchScreen");
2536    prepareDisplay(DISPLAY_ORIENTATION_0);
2537    prepareAxes(POSITION);
2538    prepareVirtualKeys();
2539    addMapperAndConfigure(mapper);
2540
2541    const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
2542    uint8_t flags[2] = { 0, 0 };
2543    ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
2544    ASSERT_TRUE(flags[0]);
2545    ASSERT_FALSE(flags[1]);
2546}
2547
2548TEST_F(SingleTouchInputMapperTest, Reset_WhenVirtualKeysAreDown_SendsUp) {
2549    // Note: Ideally we should send cancels but the implementation is more straightforward
2550    // with up and this will only happen if a device is forcibly removed.
2551    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2552    addConfigurationProperty("touch.deviceType", "touchScreen");
2553    prepareDisplay(DISPLAY_ORIENTATION_0);
2554    prepareAxes(POSITION);
2555    prepareVirtualKeys();
2556    addMapperAndConfigure(mapper);
2557
2558    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2559
2560    // Press virtual key.
2561    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2562    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2563    processDown(mapper, x, y);
2564    processSync(mapper);
2565    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2566
2567    // Reset.  Since key is down, synthesize key up.
2568    mapper->reset();
2569
2570    FakeInputDispatcher::NotifyKeyArgs args;
2571    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2572    //ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2573    ASSERT_EQ(DEVICE_ID, args.deviceId);
2574    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2575    ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2576    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2577    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2578    ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2579    ASSERT_EQ(KEY_HOME, args.scanCode);
2580    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2581    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2582}
2583
2584TEST_F(SingleTouchInputMapperTest, Reset_WhenNothingIsPressed_NothingMuchHappens) {
2585    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2586    addConfigurationProperty("touch.deviceType", "touchScreen");
2587    prepareDisplay(DISPLAY_ORIENTATION_0);
2588    prepareAxes(POSITION);
2589    prepareVirtualKeys();
2590    addMapperAndConfigure(mapper);
2591
2592    // Press virtual key.
2593    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2594    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2595    processDown(mapper, x, y);
2596    processSync(mapper);
2597    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2598
2599    // Release virtual key.
2600    processUp(mapper);
2601    processSync(mapper);
2602    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2603
2604    // Reset.  Since no key is down, nothing happens.
2605    mapper->reset();
2606
2607    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2608    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2609}
2610
2611TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
2612    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2613    addConfigurationProperty("touch.deviceType", "touchScreen");
2614    prepareDisplay(DISPLAY_ORIENTATION_0);
2615    prepareAxes(POSITION);
2616    prepareVirtualKeys();
2617    addMapperAndConfigure(mapper);
2618
2619    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2620
2621    FakeInputDispatcher::NotifyKeyArgs args;
2622
2623    // Press virtual key.
2624    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2625    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2626    processDown(mapper, x, y);
2627    processSync(mapper);
2628
2629    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2630    ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2631    ASSERT_EQ(DEVICE_ID, args.deviceId);
2632    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2633    ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2634    ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2635    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2636    ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2637    ASSERT_EQ(KEY_HOME, args.scanCode);
2638    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2639    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2640
2641    // Release virtual key.
2642    processUp(mapper);
2643    processSync(mapper);
2644
2645    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2646    ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2647    ASSERT_EQ(DEVICE_ID, args.deviceId);
2648    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2649    ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2650    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2651    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2652    ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2653    ASSERT_EQ(KEY_HOME, args.scanCode);
2654    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2655    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2656
2657    // Should not have sent any motions.
2658    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2659}
2660
2661TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
2662    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2663    addConfigurationProperty("touch.deviceType", "touchScreen");
2664    prepareDisplay(DISPLAY_ORIENTATION_0);
2665    prepareAxes(POSITION);
2666    prepareVirtualKeys();
2667    addMapperAndConfigure(mapper);
2668
2669    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2670
2671    FakeInputDispatcher::NotifyKeyArgs keyArgs;
2672
2673    // Press virtual key.
2674    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2675    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2676    processDown(mapper, x, y);
2677    processSync(mapper);
2678
2679    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&keyArgs));
2680    ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2681    ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2682    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2683    ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2684    ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2685    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
2686    ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2687    ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2688    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2689    ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2690
2691    // Move out of bounds.  This should generate a cancel and a pointer down since we moved
2692    // into the display area.
2693    y -= 100;
2694    processMove(mapper, x, y);
2695    processSync(mapper);
2696
2697    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&keyArgs));
2698    ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2699    ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2700    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2701    ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2702    ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2703    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2704            | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
2705    ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2706    ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2707    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2708    ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2709
2710    FakeInputDispatcher::NotifyMotionArgs motionArgs;
2711    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2712    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2713    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2714    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2715    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2716    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2717    ASSERT_EQ(0, motionArgs.flags);
2718    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2719    ASSERT_EQ(0, motionArgs.edgeFlags);
2720    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2721    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2722    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2723            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2724    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2725    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2726    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2727
2728    // Keep moving out of bounds.  Should generate a pointer move.
2729    y -= 50;
2730    processMove(mapper, x, y);
2731    processSync(mapper);
2732
2733    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2734    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2735    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2736    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2737    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2738    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2739    ASSERT_EQ(0, motionArgs.flags);
2740    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2741    ASSERT_EQ(0, motionArgs.edgeFlags);
2742    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2743    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2744    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2745            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2746    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2747    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2748    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2749
2750    // Release out of bounds.  Should generate a pointer up.
2751    processUp(mapper);
2752    processSync(mapper);
2753
2754    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2755    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2756    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2757    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2758    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2759    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2760    ASSERT_EQ(0, motionArgs.flags);
2761    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2762    ASSERT_EQ(0, motionArgs.edgeFlags);
2763    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2764    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2765    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2766            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2767    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2768    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2769    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2770
2771    // Should not have sent any more keys or motions.
2772    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2773    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2774}
2775
2776TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
2777    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2778    addConfigurationProperty("touch.deviceType", "touchScreen");
2779    prepareDisplay(DISPLAY_ORIENTATION_0);
2780    prepareAxes(POSITION);
2781    prepareVirtualKeys();
2782    addMapperAndConfigure(mapper);
2783
2784    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2785
2786    FakeInputDispatcher::NotifyMotionArgs motionArgs;
2787
2788    // Initially go down out of bounds.
2789    int32_t x = -10;
2790    int32_t y = -10;
2791    processDown(mapper, x, y);
2792    processSync(mapper);
2793
2794    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2795
2796    // Move into the display area.  Should generate a pointer down.
2797    x = 50;
2798    y = 75;
2799    processMove(mapper, x, y);
2800    processSync(mapper);
2801
2802    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2803    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2804    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2805    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2806    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2807    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2808    ASSERT_EQ(0, motionArgs.flags);
2809    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2810    ASSERT_EQ(0, motionArgs.edgeFlags);
2811    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2812    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2813    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2814            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2815    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2816    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2817    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2818
2819    // Release.  Should generate a pointer up.
2820    processUp(mapper);
2821    processSync(mapper);
2822
2823    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2824    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2825    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2826    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2827    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2828    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2829    ASSERT_EQ(0, motionArgs.flags);
2830    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2831    ASSERT_EQ(0, motionArgs.edgeFlags);
2832    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2833    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2834    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2835            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2836    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2837    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2838    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2839
2840    // Should not have sent any more keys or motions.
2841    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2842    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2843}
2844
2845TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
2846    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2847    addConfigurationProperty("touch.deviceType", "touchScreen");
2848    prepareDisplay(DISPLAY_ORIENTATION_0);
2849    prepareAxes(POSITION);
2850    prepareVirtualKeys();
2851    addMapperAndConfigure(mapper);
2852
2853    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2854
2855    FakeInputDispatcher::NotifyMotionArgs motionArgs;
2856
2857    // Down.
2858    int32_t x = 100;
2859    int32_t y = 125;
2860    processDown(mapper, x, y);
2861    processSync(mapper);
2862
2863    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2864    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2865    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2866    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2867    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2868    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2869    ASSERT_EQ(0, motionArgs.flags);
2870    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2871    ASSERT_EQ(0, motionArgs.edgeFlags);
2872    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2873    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2874    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2875            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2876    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2877    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2878    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2879
2880    // Move.
2881    x += 50;
2882    y += 75;
2883    processMove(mapper, x, y);
2884    processSync(mapper);
2885
2886    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2887    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2888    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2889    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2890    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2891    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2892    ASSERT_EQ(0, motionArgs.flags);
2893    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2894    ASSERT_EQ(0, motionArgs.edgeFlags);
2895    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2896    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2897    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2898            toDisplayX(x), toDisplayY(y), 1, 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    // Up.
2904    processUp(mapper);
2905    processSync(mapper);
2906
2907    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->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.edgeFlags);
2916    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2917    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2918    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2919            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2920    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2921    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2922    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2923
2924    // Should not have sent any more keys or motions.
2925    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2926    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2927}
2928
2929TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
2930    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2931    addConfigurationProperty("touch.deviceType", "touchScreen");
2932    prepareAxes(POSITION);
2933    addConfigurationProperty("touch.orientationAware", "0");
2934    addMapperAndConfigure(mapper);
2935
2936    FakeInputDispatcher::NotifyMotionArgs args;
2937
2938    // Rotation 90.
2939    prepareDisplay(DISPLAY_ORIENTATION_90);
2940    processDown(mapper, toRawX(50), toRawY(75));
2941    processSync(mapper);
2942
2943    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2944    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2945    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2946
2947    processUp(mapper);
2948    processSync(mapper);
2949    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
2950}
2951
2952TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
2953    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2954    addConfigurationProperty("touch.deviceType", "touchScreen");
2955    prepareAxes(POSITION);
2956    addMapperAndConfigure(mapper);
2957
2958    FakeInputDispatcher::NotifyMotionArgs args;
2959
2960    // Rotation 0.
2961    prepareDisplay(DISPLAY_ORIENTATION_0);
2962    processDown(mapper, toRawX(50), toRawY(75));
2963    processSync(mapper);
2964
2965    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2966    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2967    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2968
2969    processUp(mapper);
2970    processSync(mapper);
2971    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
2972
2973    // Rotation 90.
2974    prepareDisplay(DISPLAY_ORIENTATION_90);
2975    processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
2976    processSync(mapper);
2977
2978    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2979    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2980    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2981
2982    processUp(mapper);
2983    processSync(mapper);
2984    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
2985
2986    // Rotation 180.
2987    prepareDisplay(DISPLAY_ORIENTATION_180);
2988    processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
2989    processSync(mapper);
2990
2991    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2992    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2993    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2994
2995    processUp(mapper);
2996    processSync(mapper);
2997    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
2998
2999    // Rotation 270.
3000    prepareDisplay(DISPLAY_ORIENTATION_270);
3001    processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
3002    processSync(mapper);
3003
3004    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3005    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3006    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3007
3008    processUp(mapper);
3009    processSync(mapper);
3010    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3011}
3012
3013TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
3014    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3015    addConfigurationProperty("touch.deviceType", "touchScreen");
3016    prepareDisplay(DISPLAY_ORIENTATION_0);
3017    prepareAxes(POSITION | PRESSURE | TOOL);
3018    addMapperAndConfigure(mapper);
3019
3020    // These calculations are based on the input device calibration documentation.
3021    int32_t rawX = 100;
3022    int32_t rawY = 200;
3023    int32_t rawPressure = 10;
3024    int32_t rawToolMajor = 12;
3025
3026    float x = toDisplayX(rawX);
3027    float y = toDisplayY(rawY);
3028    float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3029    float size = float(rawToolMajor) / RAW_TOOL_MAX;
3030    float tool = min(DISPLAY_WIDTH, DISPLAY_HEIGHT) * size;
3031    float touch = min(tool * pressure, tool);
3032
3033    processDown(mapper, rawX, rawY);
3034    processPressure(mapper, rawPressure);
3035    processToolMajor(mapper, rawToolMajor);
3036    processSync(mapper);
3037
3038    FakeInputDispatcher::NotifyMotionArgs args;
3039    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3040    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3041            x, y, pressure, size, touch, touch, tool, tool, 0));
3042}
3043
3044
3045// --- MultiTouchInputMapperTest ---
3046
3047class MultiTouchInputMapperTest : public TouchInputMapperTest {
3048protected:
3049    void prepareAxes(int axes);
3050
3051    void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
3052    void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
3053    void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
3054    void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
3055    void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
3056    void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
3057    void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
3058    void processId(MultiTouchInputMapper* mapper, int32_t id);
3059    void processMTSync(MultiTouchInputMapper* mapper);
3060    void processSync(MultiTouchInputMapper* mapper);
3061};
3062
3063void MultiTouchInputMapperTest::prepareAxes(int axes) {
3064    if (axes & POSITION) {
3065        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
3066                RAW_X_MIN, RAW_X_MAX, 0, 0);
3067        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
3068                RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3069    }
3070    if (axes & TOUCH) {
3071        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
3072                RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
3073        if (axes & MINOR) {
3074            mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
3075                    RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
3076        }
3077    }
3078    if (axes & TOOL) {
3079        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
3080                RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3081        if (axes & MINOR) {
3082            mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
3083                    RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
3084        }
3085    }
3086    if (axes & ORIENTATION) {
3087        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
3088                RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
3089    }
3090    if (axes & PRESSURE) {
3091        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
3092                RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3093    }
3094    if (axes & ID) {
3095        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
3096                RAW_ID_MIN, RAW_ID_MAX, 0, 0);
3097    }
3098}
3099
3100void MultiTouchInputMapperTest::processPosition(
3101        MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
3102    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_X, 0, x, 0);
3103    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_Y, 0, y, 0);
3104}
3105
3106void MultiTouchInputMapperTest::processTouchMajor(
3107        MultiTouchInputMapper* mapper, int32_t touchMajor) {
3108    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MAJOR, 0, touchMajor, 0);
3109}
3110
3111void MultiTouchInputMapperTest::processTouchMinor(
3112        MultiTouchInputMapper* mapper, int32_t touchMinor) {
3113    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MINOR, 0, touchMinor, 0);
3114}
3115
3116void MultiTouchInputMapperTest::processToolMajor(
3117        MultiTouchInputMapper* mapper, int32_t toolMajor) {
3118    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MAJOR, 0, toolMajor, 0);
3119}
3120
3121void MultiTouchInputMapperTest::processToolMinor(
3122        MultiTouchInputMapper* mapper, int32_t toolMinor) {
3123    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MINOR, 0, toolMinor, 0);
3124}
3125
3126void MultiTouchInputMapperTest::processOrientation(
3127        MultiTouchInputMapper* mapper, int32_t orientation) {
3128    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_ORIENTATION, 0, orientation, 0);
3129}
3130
3131void MultiTouchInputMapperTest::processPressure(
3132        MultiTouchInputMapper* mapper, int32_t pressure) {
3133    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_PRESSURE, 0, pressure, 0);
3134}
3135
3136void MultiTouchInputMapperTest::processId(
3137        MultiTouchInputMapper* mapper, int32_t id) {
3138    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TRACKING_ID, 0, id, 0);
3139}
3140
3141void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
3142    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_MT_REPORT, 0, 0, 0);
3143}
3144
3145void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
3146    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
3147}
3148
3149
3150TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
3151    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3152    addConfigurationProperty("touch.deviceType", "touchScreen");
3153    prepareDisplay(DISPLAY_ORIENTATION_0);
3154    prepareAxes(POSITION);
3155    prepareVirtualKeys();
3156    addMapperAndConfigure(mapper);
3157
3158    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3159
3160    FakeInputDispatcher::NotifyMotionArgs motionArgs;
3161
3162    // Two fingers down at once.
3163    int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
3164    processPosition(mapper, x1, y1);
3165    processMTSync(mapper);
3166    processPosition(mapper, x2, y2);
3167    processMTSync(mapper);
3168    processSync(mapper);
3169
3170    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3171    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3172    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3173    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3174    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3175    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3176    ASSERT_EQ(0, motionArgs.flags);
3177    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3178    ASSERT_EQ(0, motionArgs.edgeFlags);
3179    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3180    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3181    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3182            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3183    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3184    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3185    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3186
3187    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3188    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3189    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3190    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3191    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3192    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3193            motionArgs.action);
3194    ASSERT_EQ(0, motionArgs.flags);
3195    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3196    ASSERT_EQ(0, motionArgs.edgeFlags);
3197    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3198    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3199    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3200    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3201            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3202    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3203            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3204    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3205    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3206    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3207
3208    // Move.
3209    x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
3210    processPosition(mapper, x1, y1);
3211    processMTSync(mapper);
3212    processPosition(mapper, x2, y2);
3213    processMTSync(mapper);
3214    processSync(mapper);
3215
3216    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3217    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3218    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3219    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3220    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3221    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3222    ASSERT_EQ(0, motionArgs.flags);
3223    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3224    ASSERT_EQ(0, motionArgs.edgeFlags);
3225    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3226    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3227    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3228    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3229            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3230    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3231            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3232    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3233    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3234    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3235
3236    // First finger up.
3237    x2 += 15; y2 -= 20;
3238    processPosition(mapper, x2, y2);
3239    processMTSync(mapper);
3240    processSync(mapper);
3241
3242    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3243    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3244    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3245    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3246    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3247    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3248            motionArgs.action);
3249    ASSERT_EQ(0, motionArgs.flags);
3250    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3251    ASSERT_EQ(0, motionArgs.edgeFlags);
3252    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3253    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3254    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3255    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3256            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3257    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3258            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3259    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3260    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3261    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3262
3263    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3264    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3265    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3266    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3267    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3268    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3269    ASSERT_EQ(0, motionArgs.flags);
3270    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3271    ASSERT_EQ(0, motionArgs.edgeFlags);
3272    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3273    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3274    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3275            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3276    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3277    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3278    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3279
3280    // Move.
3281    x2 += 20; y2 -= 25;
3282    processPosition(mapper, x2, y2);
3283    processMTSync(mapper);
3284    processSync(mapper);
3285
3286    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3287    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3288    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3289    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3290    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3291    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3292    ASSERT_EQ(0, motionArgs.flags);
3293    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3294    ASSERT_EQ(0, motionArgs.edgeFlags);
3295    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3296    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3297    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3298            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3299    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3300    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3301    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3302
3303    // New finger down.
3304    int32_t x3 = 700, y3 = 300;
3305    processPosition(mapper, x2, y2);
3306    processMTSync(mapper);
3307    processPosition(mapper, x3, y3);
3308    processMTSync(mapper);
3309    processSync(mapper);
3310
3311    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3312    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3313    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3314    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3315    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3316    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3317            motionArgs.action);
3318    ASSERT_EQ(0, motionArgs.flags);
3319    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3320    ASSERT_EQ(0, motionArgs.edgeFlags);
3321    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3322    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3323    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3324    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3325            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3326    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3327            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3328    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3329    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3330    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3331
3332    // Second finger up.
3333    x3 += 30; y3 -= 20;
3334    processPosition(mapper, x3, y3);
3335    processMTSync(mapper);
3336    processSync(mapper);
3337
3338    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3339    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3340    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3341    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3342    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3343    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3344            motionArgs.action);
3345    ASSERT_EQ(0, motionArgs.flags);
3346    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3347    ASSERT_EQ(0, motionArgs.edgeFlags);
3348    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3349    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3350    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3351    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3352            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3353    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3354            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3355    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3356    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3357    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3358
3359    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3360    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3361    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3362    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3363    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3364    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3365    ASSERT_EQ(0, motionArgs.flags);
3366    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3367    ASSERT_EQ(0, motionArgs.edgeFlags);
3368    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3369    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3370    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3371            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3372    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3373    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3374    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3375
3376    // Last finger up.
3377    processMTSync(mapper);
3378    processSync(mapper);
3379
3380    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3381    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3382    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3383    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3384    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3385    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3386    ASSERT_EQ(0, motionArgs.flags);
3387    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3388    ASSERT_EQ(0, motionArgs.edgeFlags);
3389    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3390    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3391    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3392            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3393    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3394    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3395    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3396
3397    // Should not have sent any more keys or motions.
3398    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3399    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3400}
3401
3402TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
3403    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3404    addConfigurationProperty("touch.deviceType", "touchScreen");
3405    prepareDisplay(DISPLAY_ORIENTATION_0);
3406    prepareAxes(POSITION | ID);
3407    prepareVirtualKeys();
3408    addMapperAndConfigure(mapper);
3409
3410    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3411
3412    FakeInputDispatcher::NotifyMotionArgs motionArgs;
3413
3414    // Two fingers down at once.
3415    int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
3416    processPosition(mapper, x1, y1);
3417    processId(mapper, 1);
3418    processMTSync(mapper);
3419    processPosition(mapper, x2, y2);
3420    processId(mapper, 2);
3421    processMTSync(mapper);
3422    processSync(mapper);
3423
3424    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3425    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3426    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3427    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3428    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3429            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3430
3431    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3432    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3433            motionArgs.action);
3434    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3435    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3436    ASSERT_EQ(2, motionArgs.pointerIds[1]);
3437    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3438            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3439    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3440            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3441
3442    // Move.
3443    x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
3444    processPosition(mapper, x1, y1);
3445    processId(mapper, 1);
3446    processMTSync(mapper);
3447    processPosition(mapper, x2, y2);
3448    processId(mapper, 2);
3449    processMTSync(mapper);
3450    processSync(mapper);
3451
3452    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3453    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3454    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3455    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3456    ASSERT_EQ(2, motionArgs.pointerIds[1]);
3457    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3458            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3459    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3460            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3461
3462    // First finger up.
3463    x2 += 15; y2 -= 20;
3464    processPosition(mapper, x2, y2);
3465    processId(mapper, 2);
3466    processMTSync(mapper);
3467    processSync(mapper);
3468
3469    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3470    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3471            motionArgs.action);
3472    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3473    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3474    ASSERT_EQ(2, motionArgs.pointerIds[1]);
3475    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3476            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3477    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3478            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3479
3480    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3481    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3482    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3483    ASSERT_EQ(2, motionArgs.pointerIds[0]);
3484    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3485            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3486
3487    // Move.
3488    x2 += 20; y2 -= 25;
3489    processPosition(mapper, x2, y2);
3490    processId(mapper, 2);
3491    processMTSync(mapper);
3492    processSync(mapper);
3493
3494    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3495    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3496    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3497    ASSERT_EQ(2, motionArgs.pointerIds[0]);
3498    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3499            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3500
3501    // New finger down.
3502    int32_t x3 = 700, y3 = 300;
3503    processPosition(mapper, x2, y2);
3504    processId(mapper, 2);
3505    processMTSync(mapper);
3506    processPosition(mapper, x3, y3);
3507    processId(mapper, 3);
3508    processMTSync(mapper);
3509    processSync(mapper);
3510
3511    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3512    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3513            motionArgs.action);
3514    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3515    ASSERT_EQ(2, motionArgs.pointerIds[0]);
3516    ASSERT_EQ(3, motionArgs.pointerIds[1]);
3517    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3518            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3519    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3520            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3521
3522    // Second finger up.
3523    x3 += 30; y3 -= 20;
3524    processPosition(mapper, x3, y3);
3525    processId(mapper, 3);
3526    processMTSync(mapper);
3527    processSync(mapper);
3528
3529    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3530    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3531            motionArgs.action);
3532    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3533    ASSERT_EQ(2, motionArgs.pointerIds[0]);
3534    ASSERT_EQ(3, motionArgs.pointerIds[1]);
3535    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3536            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3537    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3538            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3539
3540    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3541    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3542    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3543    ASSERT_EQ(3, motionArgs.pointerIds[0]);
3544    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3545            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3546
3547    // Last finger up.
3548    processMTSync(mapper);
3549    processSync(mapper);
3550
3551    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3552    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3553    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3554    ASSERT_EQ(3, motionArgs.pointerIds[0]);
3555    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3556            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3557
3558    // Should not have sent any more keys or motions.
3559    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3560    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3561}
3562
3563TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
3564    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3565    addConfigurationProperty("touch.deviceType", "touchScreen");
3566    prepareDisplay(DISPLAY_ORIENTATION_0);
3567    prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR);
3568    addMapperAndConfigure(mapper);
3569
3570    // These calculations are based on the input device calibration documentation.
3571    int32_t rawX = 100;
3572    int32_t rawY = 200;
3573    int32_t rawTouchMajor = 7;
3574    int32_t rawTouchMinor = 6;
3575    int32_t rawToolMajor = 9;
3576    int32_t rawToolMinor = 8;
3577    int32_t rawPressure = 11;
3578    int32_t rawOrientation = 3;
3579    int32_t id = 5;
3580
3581    float x = toDisplayX(rawX);
3582    float y = toDisplayY(rawY);
3583    float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3584    float size = avg(rawToolMajor, rawToolMinor) / RAW_TOOL_MAX;
3585    float toolMajor = float(min(DISPLAY_WIDTH, DISPLAY_HEIGHT)) * rawToolMajor / RAW_TOOL_MAX;
3586    float toolMinor = float(min(DISPLAY_WIDTH, DISPLAY_HEIGHT)) * rawToolMinor / RAW_TOOL_MAX;
3587    float touchMajor = min(toolMajor * pressure, toolMajor);
3588    float touchMinor = min(toolMinor * pressure, toolMinor);
3589    float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
3590
3591    processPosition(mapper, rawX, rawY);
3592    processTouchMajor(mapper, rawTouchMajor);
3593    processTouchMinor(mapper, rawTouchMinor);
3594    processToolMajor(mapper, rawToolMajor);
3595    processToolMinor(mapper, rawToolMinor);
3596    processPressure(mapper, rawPressure);
3597    processOrientation(mapper, rawOrientation);
3598    processId(mapper, id);
3599    processMTSync(mapper);
3600    processSync(mapper);
3601
3602    FakeInputDispatcher::NotifyMotionArgs args;
3603    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3604    ASSERT_EQ(id, args.pointerIds[0]);
3605    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3606            x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor, orientation));
3607}
3608
3609TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
3610    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3611    addConfigurationProperty("touch.deviceType", "touchScreen");
3612    prepareDisplay(DISPLAY_ORIENTATION_0);
3613    prepareAxes(POSITION | TOUCH | TOOL | MINOR);
3614    addConfigurationProperty("touch.touchSize.calibration", "geometric");
3615    addConfigurationProperty("touch.toolSize.calibration", "geometric");
3616    addMapperAndConfigure(mapper);
3617
3618    // These calculations are based on the input device calibration documentation.
3619    int32_t rawX = 100;
3620    int32_t rawY = 200;
3621    int32_t rawTouchMajor = 140;
3622    int32_t rawTouchMinor = 120;
3623    int32_t rawToolMajor = 180;
3624    int32_t rawToolMinor = 160;
3625
3626    float x = toDisplayX(rawX);
3627    float y = toDisplayY(rawY);
3628    float pressure = float(rawTouchMajor) / RAW_TOUCH_MAX;
3629    float size = avg(rawToolMajor, rawToolMinor) / RAW_TOOL_MAX;
3630    float scale = avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3631            float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3632    float toolMajor = float(rawToolMajor) * scale;
3633    float toolMinor = float(rawToolMinor) * scale;
3634    float touchMajor = min(float(rawTouchMajor) * scale, toolMajor);
3635    float touchMinor = min(float(rawTouchMinor) * scale, toolMinor);
3636
3637    processPosition(mapper, rawX, rawY);
3638    processTouchMajor(mapper, rawTouchMajor);
3639    processTouchMinor(mapper, rawTouchMinor);
3640    processToolMajor(mapper, rawToolMajor);
3641    processToolMinor(mapper, rawToolMinor);
3642    processMTSync(mapper);
3643    processSync(mapper);
3644
3645    FakeInputDispatcher::NotifyMotionArgs args;
3646    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3647    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3648            x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor, 0));
3649}
3650
3651TEST_F(MultiTouchInputMapperTest, Process_TouchToolPressureSizeAxes_SummedLinearCalibration) {
3652    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3653    addConfigurationProperty("touch.deviceType", "touchScreen");
3654    prepareDisplay(DISPLAY_ORIENTATION_0);
3655    prepareAxes(POSITION | TOUCH | TOOL);
3656    addConfigurationProperty("touch.touchSize.calibration", "pressure");
3657    addConfigurationProperty("touch.toolSize.calibration", "linear");
3658    addConfigurationProperty("touch.toolSize.linearScale", "10");
3659    addConfigurationProperty("touch.toolSize.linearBias", "160");
3660    addConfigurationProperty("touch.toolSize.isSummed", "1");
3661    addConfigurationProperty("touch.pressure.calibration", "amplitude");
3662    addConfigurationProperty("touch.pressure.source", "touch");
3663    addConfigurationProperty("touch.pressure.scale", "0.01");
3664    addMapperAndConfigure(mapper);
3665
3666    // These calculations are based on the input device calibration documentation.
3667    // Note: We only provide a single common touch/tool value because the device is assumed
3668    //       not to emit separate values for each pointer (isSummed = 1).
3669    int32_t rawX = 100;
3670    int32_t rawY = 200;
3671    int32_t rawX2 = 150;
3672    int32_t rawY2 = 250;
3673    int32_t rawTouchMajor = 60;
3674    int32_t rawToolMajor = 5;
3675
3676    float x = toDisplayX(rawX);
3677    float y = toDisplayY(rawY);
3678    float x2 = toDisplayX(rawX2);
3679    float y2 = toDisplayY(rawY2);
3680    float pressure = float(rawTouchMajor) * 0.01f;
3681    float size = float(rawToolMajor) / RAW_TOOL_MAX;
3682    float tool = (float(rawToolMajor) * 10.0f + 160.0f) / 2;
3683    float touch = min(tool * pressure, tool);
3684
3685    processPosition(mapper, rawX, rawY);
3686    processTouchMajor(mapper, rawTouchMajor);
3687    processToolMajor(mapper, rawToolMajor);
3688    processMTSync(mapper);
3689    processPosition(mapper, rawX2, rawY2);
3690    processTouchMajor(mapper, rawTouchMajor);
3691    processToolMajor(mapper, rawToolMajor);
3692    processMTSync(mapper);
3693    processSync(mapper);
3694
3695    FakeInputDispatcher::NotifyMotionArgs args;
3696    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3697    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3698    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3699    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3700            args.action);
3701    ASSERT_EQ(size_t(2), args.pointerCount);
3702    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3703            x, y, pressure, size, touch, touch, tool, tool, 0));
3704    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
3705            x2, y2, pressure, size, touch, touch, tool, tool, 0));
3706}
3707
3708TEST_F(MultiTouchInputMapperTest, Process_TouchToolPressureSizeAxes_AreaCalibration) {
3709    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3710    addConfigurationProperty("touch.deviceType", "touchScreen");
3711    prepareDisplay(DISPLAY_ORIENTATION_0);
3712    prepareAxes(POSITION | TOUCH | TOOL);
3713    addConfigurationProperty("touch.touchSize.calibration", "pressure");
3714    addConfigurationProperty("touch.toolSize.calibration", "area");
3715    addConfigurationProperty("touch.toolSize.areaScale", "22");
3716    addConfigurationProperty("touch.toolSize.areaBias", "1");
3717    addConfigurationProperty("touch.toolSize.linearScale", "9.2");
3718    addConfigurationProperty("touch.toolSize.linearBias", "3");
3719    addConfigurationProperty("touch.pressure.calibration", "amplitude");
3720    addConfigurationProperty("touch.pressure.source", "touch");
3721    addConfigurationProperty("touch.pressure.scale", "0.01");
3722    addMapperAndConfigure(mapper);
3723
3724    // These calculations are based on the input device calibration documentation.
3725    int32_t rawX = 100;
3726    int32_t rawY = 200;
3727    int32_t rawTouchMajor = 60;
3728    int32_t rawToolMajor = 5;
3729
3730    float x = toDisplayX(rawX);
3731    float y = toDisplayY(rawY);
3732    float pressure = float(rawTouchMajor) * 0.01f;
3733    float size = float(rawToolMajor) / RAW_TOOL_MAX;
3734    float tool = sqrtf(float(rawToolMajor) * 22.0f + 1.0f) * 9.2f + 3.0f;
3735    float touch = min(tool * pressure, tool);
3736
3737    processPosition(mapper, rawX, rawY);
3738    processTouchMajor(mapper, rawTouchMajor);
3739    processToolMajor(mapper, rawToolMajor);
3740    processMTSync(mapper);
3741    processSync(mapper);
3742
3743    FakeInputDispatcher::NotifyMotionArgs args;
3744    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3745    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3746            x, y, pressure, size, touch, touch, tool, tool, 0));
3747}
3748
3749} // namespace android
3750