InputReader_test.cpp revision ace13b17866dc9136aeecf6dfaf7077f37434469
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_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
2452    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2453    prepareAxes(POSITION);
2454    addMapperAndConfigure(mapper);
2455
2456    ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2457}
2458
2459TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
2460    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2461    mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
2462    mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
2463    prepareAxes(POSITION);
2464    addMapperAndConfigure(mapper);
2465
2466    ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2467}
2468
2469TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
2470    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2471    prepareAxes(POSITION);
2472    addConfigurationProperty("touch.deviceType", "touchPad");
2473    addMapperAndConfigure(mapper);
2474
2475    ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2476}
2477
2478TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
2479    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2480    prepareAxes(POSITION);
2481    addConfigurationProperty("touch.deviceType", "touchScreen");
2482    addMapperAndConfigure(mapper);
2483
2484    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
2485}
2486
2487TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
2488    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2489    addConfigurationProperty("touch.deviceType", "touchScreen");
2490    prepareDisplay(DISPLAY_ORIENTATION_0);
2491    prepareAxes(POSITION);
2492    prepareVirtualKeys();
2493    addMapperAndConfigure(mapper);
2494
2495    // Unknown key.
2496    ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2497
2498    // Virtual key is down.
2499    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2500    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2501    processDown(mapper, x, y);
2502    processSync(mapper);
2503    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2504
2505    ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2506
2507    // Virtual key is up.
2508    processUp(mapper);
2509    processSync(mapper);
2510    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2511
2512    ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2513}
2514
2515TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
2516    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2517    addConfigurationProperty("touch.deviceType", "touchScreen");
2518    prepareDisplay(DISPLAY_ORIENTATION_0);
2519    prepareAxes(POSITION);
2520    prepareVirtualKeys();
2521    addMapperAndConfigure(mapper);
2522
2523    // Unknown key.
2524    ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2525
2526    // Virtual key is down.
2527    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2528    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2529    processDown(mapper, x, y);
2530    processSync(mapper);
2531    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2532
2533    ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2534
2535    // Virtual key is up.
2536    processUp(mapper);
2537    processSync(mapper);
2538    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2539
2540    ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2541}
2542
2543TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
2544    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2545    addConfigurationProperty("touch.deviceType", "touchScreen");
2546    prepareDisplay(DISPLAY_ORIENTATION_0);
2547    prepareAxes(POSITION);
2548    prepareVirtualKeys();
2549    addMapperAndConfigure(mapper);
2550
2551    const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
2552    uint8_t flags[2] = { 0, 0 };
2553    ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
2554    ASSERT_TRUE(flags[0]);
2555    ASSERT_FALSE(flags[1]);
2556}
2557
2558TEST_F(SingleTouchInputMapperTest, Reset_WhenVirtualKeysAreDown_SendsUp) {
2559    // Note: Ideally we should send cancels but the implementation is more straightforward
2560    // with up and this will only happen if a device is forcibly removed.
2561    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2562    addConfigurationProperty("touch.deviceType", "touchScreen");
2563    prepareDisplay(DISPLAY_ORIENTATION_0);
2564    prepareAxes(POSITION);
2565    prepareVirtualKeys();
2566    addMapperAndConfigure(mapper);
2567
2568    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2569
2570    // Press virtual key.
2571    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2572    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2573    processDown(mapper, x, y);
2574    processSync(mapper);
2575    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2576
2577    // Reset.  Since key is down, synthesize key up.
2578    mapper->reset();
2579
2580    FakeInputDispatcher::NotifyKeyArgs args;
2581    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2582    //ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2583    ASSERT_EQ(DEVICE_ID, args.deviceId);
2584    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2585    ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2586    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2587    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2588    ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2589    ASSERT_EQ(KEY_HOME, args.scanCode);
2590    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2591    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2592}
2593
2594TEST_F(SingleTouchInputMapperTest, Reset_WhenNothingIsPressed_NothingMuchHappens) {
2595    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2596    addConfigurationProperty("touch.deviceType", "touchScreen");
2597    prepareDisplay(DISPLAY_ORIENTATION_0);
2598    prepareAxes(POSITION);
2599    prepareVirtualKeys();
2600    addMapperAndConfigure(mapper);
2601
2602    // Press virtual key.
2603    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2604    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2605    processDown(mapper, x, y);
2606    processSync(mapper);
2607    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2608
2609    // Release virtual key.
2610    processUp(mapper);
2611    processSync(mapper);
2612    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2613
2614    // Reset.  Since no key is down, nothing happens.
2615    mapper->reset();
2616
2617    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2618    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2619}
2620
2621TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
2622    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2623    addConfigurationProperty("touch.deviceType", "touchScreen");
2624    prepareDisplay(DISPLAY_ORIENTATION_0);
2625    prepareAxes(POSITION);
2626    prepareVirtualKeys();
2627    addMapperAndConfigure(mapper);
2628
2629    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2630
2631    FakeInputDispatcher::NotifyKeyArgs args;
2632
2633    // Press virtual key.
2634    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2635    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2636    processDown(mapper, x, y);
2637    processSync(mapper);
2638
2639    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2640    ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2641    ASSERT_EQ(DEVICE_ID, args.deviceId);
2642    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2643    ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2644    ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2645    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2646    ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2647    ASSERT_EQ(KEY_HOME, args.scanCode);
2648    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2649    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2650
2651    // Release virtual key.
2652    processUp(mapper);
2653    processSync(mapper);
2654
2655    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2656    ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2657    ASSERT_EQ(DEVICE_ID, args.deviceId);
2658    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2659    ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2660    ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2661    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2662    ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2663    ASSERT_EQ(KEY_HOME, args.scanCode);
2664    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2665    ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2666
2667    // Should not have sent any motions.
2668    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2669}
2670
2671TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
2672    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2673    addConfigurationProperty("touch.deviceType", "touchScreen");
2674    prepareDisplay(DISPLAY_ORIENTATION_0);
2675    prepareAxes(POSITION);
2676    prepareVirtualKeys();
2677    addMapperAndConfigure(mapper);
2678
2679    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2680
2681    FakeInputDispatcher::NotifyKeyArgs keyArgs;
2682
2683    // Press virtual key.
2684    int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2685    int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2686    processDown(mapper, x, y);
2687    processSync(mapper);
2688
2689    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&keyArgs));
2690    ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2691    ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2692    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2693    ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2694    ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2695    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
2696    ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2697    ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2698    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2699    ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2700
2701    // Move out of bounds.  This should generate a cancel and a pointer down since we moved
2702    // into the display area.
2703    y -= 100;
2704    processMove(mapper, x, y);
2705    processSync(mapper);
2706
2707    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&keyArgs));
2708    ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2709    ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2710    ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2711    ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2712    ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2713    ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2714            | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
2715    ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2716    ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2717    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2718    ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2719
2720    FakeInputDispatcher::NotifyMotionArgs motionArgs;
2721    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2722    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2723    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2724    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2725    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2726    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2727    ASSERT_EQ(0, motionArgs.flags);
2728    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2729    ASSERT_EQ(0, motionArgs.edgeFlags);
2730    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2731    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2732    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2733            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2734    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2735    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2736    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2737
2738    // Keep moving out of bounds.  Should generate a pointer move.
2739    y -= 50;
2740    processMove(mapper, x, y);
2741    processSync(mapper);
2742
2743    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2744    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2745    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2746    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2747    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2748    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2749    ASSERT_EQ(0, motionArgs.flags);
2750    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2751    ASSERT_EQ(0, motionArgs.edgeFlags);
2752    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2753    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2754    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2755            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2756    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2757    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2758    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2759
2760    // Release out of bounds.  Should generate a pointer up.
2761    processUp(mapper);
2762    processSync(mapper);
2763
2764    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2765    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2766    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2767    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2768    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2769    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2770    ASSERT_EQ(0, motionArgs.flags);
2771    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2772    ASSERT_EQ(0, motionArgs.edgeFlags);
2773    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2774    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2775    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2776            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2777    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2778    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2779    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2780
2781    // Should not have sent any more keys or motions.
2782    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2783    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2784}
2785
2786TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
2787    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2788    addConfigurationProperty("touch.deviceType", "touchScreen");
2789    prepareDisplay(DISPLAY_ORIENTATION_0);
2790    prepareAxes(POSITION);
2791    prepareVirtualKeys();
2792    addMapperAndConfigure(mapper);
2793
2794    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2795
2796    FakeInputDispatcher::NotifyMotionArgs motionArgs;
2797
2798    // Initially go down out of bounds.
2799    int32_t x = -10;
2800    int32_t y = -10;
2801    processDown(mapper, x, y);
2802    processSync(mapper);
2803
2804    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2805
2806    // Move into the display area.  Should generate a pointer down.
2807    x = 50;
2808    y = 75;
2809    processMove(mapper, x, y);
2810    processSync(mapper);
2811
2812    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2813    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2814    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2815    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2816    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2817    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2818    ASSERT_EQ(0, motionArgs.flags);
2819    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2820    ASSERT_EQ(0, motionArgs.edgeFlags);
2821    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2822    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2823    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2824            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2825    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2826    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2827    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2828
2829    // Release.  Should generate a pointer up.
2830    processUp(mapper);
2831    processSync(mapper);
2832
2833    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2834    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2835    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2836    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2837    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2838    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2839    ASSERT_EQ(0, motionArgs.flags);
2840    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2841    ASSERT_EQ(0, motionArgs.edgeFlags);
2842    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2843    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2844    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2845            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2846    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2847    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2848    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2849
2850    // Should not have sent any more keys or motions.
2851    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2852    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2853}
2854
2855TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
2856    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2857    addConfigurationProperty("touch.deviceType", "touchScreen");
2858    prepareDisplay(DISPLAY_ORIENTATION_0);
2859    prepareAxes(POSITION);
2860    prepareVirtualKeys();
2861    addMapperAndConfigure(mapper);
2862
2863    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2864
2865    FakeInputDispatcher::NotifyMotionArgs motionArgs;
2866
2867    // Down.
2868    int32_t x = 100;
2869    int32_t y = 125;
2870    processDown(mapper, x, y);
2871    processSync(mapper);
2872
2873    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2874    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2875    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2876    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2877    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2878    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2879    ASSERT_EQ(0, motionArgs.flags);
2880    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2881    ASSERT_EQ(0, motionArgs.edgeFlags);
2882    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2883    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2884    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2885            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2886    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2887    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2888    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2889
2890    // Move.
2891    x += 50;
2892    y += 75;
2893    processMove(mapper, x, y);
2894    processSync(mapper);
2895
2896    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2897    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2898    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2899    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2900    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2901    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2902    ASSERT_EQ(0, motionArgs.flags);
2903    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2904    ASSERT_EQ(0, motionArgs.edgeFlags);
2905    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2906    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2907    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2908            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2909    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2910    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2911    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2912
2913    // Up.
2914    processUp(mapper);
2915    processSync(mapper);
2916
2917    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2918    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2919    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2920    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2921    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2922    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2923    ASSERT_EQ(0, motionArgs.flags);
2924    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
2925    ASSERT_EQ(0, motionArgs.edgeFlags);
2926    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
2927    ASSERT_EQ(0, motionArgs.pointerIds[0]);
2928    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2929            toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2930    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2931    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2932    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2933
2934    // Should not have sent any more keys or motions.
2935    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2936    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2937}
2938
2939TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
2940    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2941    addConfigurationProperty("touch.deviceType", "touchScreen");
2942    prepareAxes(POSITION);
2943    addConfigurationProperty("touch.orientationAware", "0");
2944    addMapperAndConfigure(mapper);
2945
2946    FakeInputDispatcher::NotifyMotionArgs args;
2947
2948    // Rotation 90.
2949    prepareDisplay(DISPLAY_ORIENTATION_90);
2950    processDown(mapper, toRawX(50), toRawY(75));
2951    processSync(mapper);
2952
2953    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2954    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2955    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2956
2957    processUp(mapper);
2958    processSync(mapper);
2959    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
2960}
2961
2962TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
2963    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2964    addConfigurationProperty("touch.deviceType", "touchScreen");
2965    prepareAxes(POSITION);
2966    addMapperAndConfigure(mapper);
2967
2968    FakeInputDispatcher::NotifyMotionArgs args;
2969
2970    // Rotation 0.
2971    prepareDisplay(DISPLAY_ORIENTATION_0);
2972    processDown(mapper, toRawX(50), toRawY(75));
2973    processSync(mapper);
2974
2975    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2976    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2977    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2978
2979    processUp(mapper);
2980    processSync(mapper);
2981    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
2982
2983    // Rotation 90.
2984    prepareDisplay(DISPLAY_ORIENTATION_90);
2985    processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
2986    processSync(mapper);
2987
2988    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2989    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
2990    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
2991
2992    processUp(mapper);
2993    processSync(mapper);
2994    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
2995
2996    // Rotation 180.
2997    prepareDisplay(DISPLAY_ORIENTATION_180);
2998    processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
2999    processSync(mapper);
3000
3001    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3002    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3003    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3004
3005    processUp(mapper);
3006    processSync(mapper);
3007    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3008
3009    // Rotation 270.
3010    prepareDisplay(DISPLAY_ORIENTATION_270);
3011    processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
3012    processSync(mapper);
3013
3014    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3015    ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3016    ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3017
3018    processUp(mapper);
3019    processSync(mapper);
3020    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3021}
3022
3023TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
3024    SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3025    addConfigurationProperty("touch.deviceType", "touchScreen");
3026    prepareDisplay(DISPLAY_ORIENTATION_0);
3027    prepareAxes(POSITION | PRESSURE | TOOL);
3028    addMapperAndConfigure(mapper);
3029
3030    // These calculations are based on the input device calibration documentation.
3031    int32_t rawX = 100;
3032    int32_t rawY = 200;
3033    int32_t rawPressure = 10;
3034    int32_t rawToolMajor = 12;
3035
3036    float x = toDisplayX(rawX);
3037    float y = toDisplayY(rawY);
3038    float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3039    float size = float(rawToolMajor) / RAW_TOOL_MAX;
3040    float tool = min(DISPLAY_WIDTH, DISPLAY_HEIGHT) * size;
3041    float touch = min(tool * pressure, tool);
3042
3043    processDown(mapper, rawX, rawY);
3044    processPressure(mapper, rawPressure);
3045    processToolMajor(mapper, rawToolMajor);
3046    processSync(mapper);
3047
3048    FakeInputDispatcher::NotifyMotionArgs args;
3049    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3050    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3051            x, y, pressure, size, touch, touch, tool, tool, 0));
3052}
3053
3054
3055// --- MultiTouchInputMapperTest ---
3056
3057class MultiTouchInputMapperTest : public TouchInputMapperTest {
3058protected:
3059    void prepareAxes(int axes);
3060
3061    void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
3062    void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
3063    void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
3064    void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
3065    void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
3066    void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
3067    void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
3068    void processId(MultiTouchInputMapper* mapper, int32_t id);
3069    void processMTSync(MultiTouchInputMapper* mapper);
3070    void processSync(MultiTouchInputMapper* mapper);
3071};
3072
3073void MultiTouchInputMapperTest::prepareAxes(int axes) {
3074    if (axes & POSITION) {
3075        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
3076                RAW_X_MIN, RAW_X_MAX, 0, 0);
3077        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
3078                RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3079    }
3080    if (axes & TOUCH) {
3081        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
3082                RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
3083        if (axes & MINOR) {
3084            mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
3085                    RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
3086        }
3087    }
3088    if (axes & TOOL) {
3089        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
3090                RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3091        if (axes & MINOR) {
3092            mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
3093                    RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
3094        }
3095    }
3096    if (axes & ORIENTATION) {
3097        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
3098                RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
3099    }
3100    if (axes & PRESSURE) {
3101        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
3102                RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3103    }
3104    if (axes & ID) {
3105        mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
3106                RAW_ID_MIN, RAW_ID_MAX, 0, 0);
3107    }
3108}
3109
3110void MultiTouchInputMapperTest::processPosition(
3111        MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
3112    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_X, 0, x, 0);
3113    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_Y, 0, y, 0);
3114}
3115
3116void MultiTouchInputMapperTest::processTouchMajor(
3117        MultiTouchInputMapper* mapper, int32_t touchMajor) {
3118    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MAJOR, 0, touchMajor, 0);
3119}
3120
3121void MultiTouchInputMapperTest::processTouchMinor(
3122        MultiTouchInputMapper* mapper, int32_t touchMinor) {
3123    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MINOR, 0, touchMinor, 0);
3124}
3125
3126void MultiTouchInputMapperTest::processToolMajor(
3127        MultiTouchInputMapper* mapper, int32_t toolMajor) {
3128    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MAJOR, 0, toolMajor, 0);
3129}
3130
3131void MultiTouchInputMapperTest::processToolMinor(
3132        MultiTouchInputMapper* mapper, int32_t toolMinor) {
3133    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MINOR, 0, toolMinor, 0);
3134}
3135
3136void MultiTouchInputMapperTest::processOrientation(
3137        MultiTouchInputMapper* mapper, int32_t orientation) {
3138    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_ORIENTATION, 0, orientation, 0);
3139}
3140
3141void MultiTouchInputMapperTest::processPressure(
3142        MultiTouchInputMapper* mapper, int32_t pressure) {
3143    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_PRESSURE, 0, pressure, 0);
3144}
3145
3146void MultiTouchInputMapperTest::processId(
3147        MultiTouchInputMapper* mapper, int32_t id) {
3148    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TRACKING_ID, 0, id, 0);
3149}
3150
3151void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
3152    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_MT_REPORT, 0, 0, 0);
3153}
3154
3155void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
3156    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
3157}
3158
3159
3160TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
3161    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3162    addConfigurationProperty("touch.deviceType", "touchScreen");
3163    prepareDisplay(DISPLAY_ORIENTATION_0);
3164    prepareAxes(POSITION);
3165    prepareVirtualKeys();
3166    addMapperAndConfigure(mapper);
3167
3168    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3169
3170    FakeInputDispatcher::NotifyMotionArgs motionArgs;
3171
3172    // Two fingers down at once.
3173    int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
3174    processPosition(mapper, x1, y1);
3175    processMTSync(mapper);
3176    processPosition(mapper, x2, y2);
3177    processMTSync(mapper);
3178    processSync(mapper);
3179
3180    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3181    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3182    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3183    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3184    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3185    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3186    ASSERT_EQ(0, motionArgs.flags);
3187    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3188    ASSERT_EQ(0, motionArgs.edgeFlags);
3189    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3190    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3191    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3192            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3193    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3194    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3195    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3196
3197    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3198    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3199    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3200    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3201    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3202    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3203            motionArgs.action);
3204    ASSERT_EQ(0, motionArgs.flags);
3205    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3206    ASSERT_EQ(0, motionArgs.edgeFlags);
3207    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3208    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3209    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3210    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3211            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3212    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3213            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3214    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3215    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3216    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3217
3218    // Move.
3219    x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
3220    processPosition(mapper, x1, y1);
3221    processMTSync(mapper);
3222    processPosition(mapper, x2, y2);
3223    processMTSync(mapper);
3224    processSync(mapper);
3225
3226    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3227    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3228    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3229    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3230    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3231    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3232    ASSERT_EQ(0, motionArgs.flags);
3233    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3234    ASSERT_EQ(0, motionArgs.edgeFlags);
3235    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3236    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3237    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3238    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3239            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3240    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3241            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3242    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3243    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3244    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3245
3246    // First finger up.
3247    x2 += 15; y2 -= 20;
3248    processPosition(mapper, x2, y2);
3249    processMTSync(mapper);
3250    processSync(mapper);
3251
3252    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3253    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3254    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3255    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3256    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3257    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3258            motionArgs.action);
3259    ASSERT_EQ(0, motionArgs.flags);
3260    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3261    ASSERT_EQ(0, motionArgs.edgeFlags);
3262    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3263    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3264    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3265    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3266            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3267    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3268            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3269    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3270    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3271    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3272
3273    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3274    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3275    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3276    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3277    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3278    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3279    ASSERT_EQ(0, motionArgs.flags);
3280    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3281    ASSERT_EQ(0, motionArgs.edgeFlags);
3282    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3283    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3284    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3285            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3286    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3287    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3288    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3289
3290    // Move.
3291    x2 += 20; y2 -= 25;
3292    processPosition(mapper, x2, y2);
3293    processMTSync(mapper);
3294    processSync(mapper);
3295
3296    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3297    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3298    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3299    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3300    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3301    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3302    ASSERT_EQ(0, motionArgs.flags);
3303    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3304    ASSERT_EQ(0, motionArgs.edgeFlags);
3305    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3306    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3307    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3308            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3309    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3310    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3311    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3312
3313    // New finger down.
3314    int32_t x3 = 700, y3 = 300;
3315    processPosition(mapper, x2, y2);
3316    processMTSync(mapper);
3317    processPosition(mapper, x3, y3);
3318    processMTSync(mapper);
3319    processSync(mapper);
3320
3321    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3322    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3323    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3324    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3325    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3326    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3327            motionArgs.action);
3328    ASSERT_EQ(0, motionArgs.flags);
3329    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3330    ASSERT_EQ(0, motionArgs.edgeFlags);
3331    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3332    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3333    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3334    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3335            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3336    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3337            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3338    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3339    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3340    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3341
3342    // Second finger up.
3343    x3 += 30; y3 -= 20;
3344    processPosition(mapper, x3, y3);
3345    processMTSync(mapper);
3346    processSync(mapper);
3347
3348    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3349    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3350    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3351    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3352    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3353    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3354            motionArgs.action);
3355    ASSERT_EQ(0, motionArgs.flags);
3356    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3357    ASSERT_EQ(0, motionArgs.edgeFlags);
3358    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3359    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3360    ASSERT_EQ(1, motionArgs.pointerIds[1]);
3361    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3362            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3363    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3364            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3365    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3366    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3367    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3368
3369    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3370    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3371    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3372    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3373    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3374    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3375    ASSERT_EQ(0, motionArgs.flags);
3376    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3377    ASSERT_EQ(0, motionArgs.edgeFlags);
3378    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3379    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3380    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3381            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3382    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3383    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3384    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3385
3386    // Last finger up.
3387    processMTSync(mapper);
3388    processSync(mapper);
3389
3390    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3391    ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3392    ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3393    ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3394    ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3395    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3396    ASSERT_EQ(0, motionArgs.flags);
3397    ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3398    ASSERT_EQ(0, motionArgs.edgeFlags);
3399    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3400    ASSERT_EQ(0, motionArgs.pointerIds[0]);
3401    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3402            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3403    ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3404    ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3405    ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3406
3407    // Should not have sent any more keys or motions.
3408    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3409    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3410}
3411
3412TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
3413    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3414    addConfigurationProperty("touch.deviceType", "touchScreen");
3415    prepareDisplay(DISPLAY_ORIENTATION_0);
3416    prepareAxes(POSITION | ID);
3417    prepareVirtualKeys();
3418    addMapperAndConfigure(mapper);
3419
3420    mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3421
3422    FakeInputDispatcher::NotifyMotionArgs motionArgs;
3423
3424    // Two fingers down at once.
3425    int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
3426    processPosition(mapper, x1, y1);
3427    processId(mapper, 1);
3428    processMTSync(mapper);
3429    processPosition(mapper, x2, y2);
3430    processId(mapper, 2);
3431    processMTSync(mapper);
3432    processSync(mapper);
3433
3434    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3435    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3436    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3437    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3438    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3439            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3440
3441    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3442    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3443            motionArgs.action);
3444    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3445    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3446    ASSERT_EQ(2, motionArgs.pointerIds[1]);
3447    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3448            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3449    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3450            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3451
3452    // Move.
3453    x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
3454    processPosition(mapper, x1, y1);
3455    processId(mapper, 1);
3456    processMTSync(mapper);
3457    processPosition(mapper, x2, y2);
3458    processId(mapper, 2);
3459    processMTSync(mapper);
3460    processSync(mapper);
3461
3462    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3463    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3464    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3465    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3466    ASSERT_EQ(2, motionArgs.pointerIds[1]);
3467    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3468            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3469    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3470            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3471
3472    // First finger up.
3473    x2 += 15; y2 -= 20;
3474    processPosition(mapper, x2, y2);
3475    processId(mapper, 2);
3476    processMTSync(mapper);
3477    processSync(mapper);
3478
3479    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3480    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3481            motionArgs.action);
3482    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3483    ASSERT_EQ(1, motionArgs.pointerIds[0]);
3484    ASSERT_EQ(2, motionArgs.pointerIds[1]);
3485    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3486            toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3487    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3488            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3489
3490    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3491    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3492    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3493    ASSERT_EQ(2, motionArgs.pointerIds[0]);
3494    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3495            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3496
3497    // Move.
3498    x2 += 20; y2 -= 25;
3499    processPosition(mapper, x2, y2);
3500    processId(mapper, 2);
3501    processMTSync(mapper);
3502    processSync(mapper);
3503
3504    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3505    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3506    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3507    ASSERT_EQ(2, motionArgs.pointerIds[0]);
3508    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3509            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3510
3511    // New finger down.
3512    int32_t x3 = 700, y3 = 300;
3513    processPosition(mapper, x2, y2);
3514    processId(mapper, 2);
3515    processMTSync(mapper);
3516    processPosition(mapper, x3, y3);
3517    processId(mapper, 3);
3518    processMTSync(mapper);
3519    processSync(mapper);
3520
3521    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3522    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3523            motionArgs.action);
3524    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3525    ASSERT_EQ(2, motionArgs.pointerIds[0]);
3526    ASSERT_EQ(3, motionArgs.pointerIds[1]);
3527    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3528            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3529    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3530            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3531
3532    // Second finger up.
3533    x3 += 30; y3 -= 20;
3534    processPosition(mapper, x3, y3);
3535    processId(mapper, 3);
3536    processMTSync(mapper);
3537    processSync(mapper);
3538
3539    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3540    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3541            motionArgs.action);
3542    ASSERT_EQ(size_t(2), motionArgs.pointerCount);
3543    ASSERT_EQ(2, motionArgs.pointerIds[0]);
3544    ASSERT_EQ(3, motionArgs.pointerIds[1]);
3545    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3546            toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3547    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3548            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3549
3550    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3551    ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3552    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3553    ASSERT_EQ(3, motionArgs.pointerIds[0]);
3554    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3555            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3556
3557    // Last finger up.
3558    processMTSync(mapper);
3559    processSync(mapper);
3560
3561    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3562    ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3563    ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3564    ASSERT_EQ(3, motionArgs.pointerIds[0]);
3565    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3566            toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3567
3568    // Should not have sent any more keys or motions.
3569    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3570    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3571}
3572
3573TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
3574    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3575    addConfigurationProperty("touch.deviceType", "touchScreen");
3576    prepareDisplay(DISPLAY_ORIENTATION_0);
3577    prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR);
3578    addMapperAndConfigure(mapper);
3579
3580    // These calculations are based on the input device calibration documentation.
3581    int32_t rawX = 100;
3582    int32_t rawY = 200;
3583    int32_t rawTouchMajor = 7;
3584    int32_t rawTouchMinor = 6;
3585    int32_t rawToolMajor = 9;
3586    int32_t rawToolMinor = 8;
3587    int32_t rawPressure = 11;
3588    int32_t rawOrientation = 3;
3589    int32_t id = 5;
3590
3591    float x = toDisplayX(rawX);
3592    float y = toDisplayY(rawY);
3593    float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3594    float size = avg(rawToolMajor, rawToolMinor) / RAW_TOOL_MAX;
3595    float toolMajor = float(min(DISPLAY_WIDTH, DISPLAY_HEIGHT)) * rawToolMajor / RAW_TOOL_MAX;
3596    float toolMinor = float(min(DISPLAY_WIDTH, DISPLAY_HEIGHT)) * rawToolMinor / RAW_TOOL_MAX;
3597    float touchMajor = min(toolMajor * pressure, toolMajor);
3598    float touchMinor = min(toolMinor * pressure, toolMinor);
3599    float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
3600
3601    processPosition(mapper, rawX, rawY);
3602    processTouchMajor(mapper, rawTouchMajor);
3603    processTouchMinor(mapper, rawTouchMinor);
3604    processToolMajor(mapper, rawToolMajor);
3605    processToolMinor(mapper, rawToolMinor);
3606    processPressure(mapper, rawPressure);
3607    processOrientation(mapper, rawOrientation);
3608    processId(mapper, id);
3609    processMTSync(mapper);
3610    processSync(mapper);
3611
3612    FakeInputDispatcher::NotifyMotionArgs args;
3613    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3614    ASSERT_EQ(id, args.pointerIds[0]);
3615    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3616            x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor, orientation));
3617}
3618
3619TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
3620    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3621    addConfigurationProperty("touch.deviceType", "touchScreen");
3622    prepareDisplay(DISPLAY_ORIENTATION_0);
3623    prepareAxes(POSITION | TOUCH | TOOL | MINOR);
3624    addConfigurationProperty("touch.touchSize.calibration", "geometric");
3625    addConfigurationProperty("touch.toolSize.calibration", "geometric");
3626    addMapperAndConfigure(mapper);
3627
3628    // These calculations are based on the input device calibration documentation.
3629    int32_t rawX = 100;
3630    int32_t rawY = 200;
3631    int32_t rawTouchMajor = 140;
3632    int32_t rawTouchMinor = 120;
3633    int32_t rawToolMajor = 180;
3634    int32_t rawToolMinor = 160;
3635
3636    float x = toDisplayX(rawX);
3637    float y = toDisplayY(rawY);
3638    float pressure = float(rawTouchMajor) / RAW_TOUCH_MAX;
3639    float size = avg(rawToolMajor, rawToolMinor) / RAW_TOOL_MAX;
3640    float scale = avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3641            float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
3642    float toolMajor = float(rawToolMajor) * scale;
3643    float toolMinor = float(rawToolMinor) * scale;
3644    float touchMajor = min(float(rawTouchMajor) * scale, toolMajor);
3645    float touchMinor = min(float(rawTouchMinor) * scale, toolMinor);
3646
3647    processPosition(mapper, rawX, rawY);
3648    processTouchMajor(mapper, rawTouchMajor);
3649    processTouchMinor(mapper, rawTouchMinor);
3650    processToolMajor(mapper, rawToolMajor);
3651    processToolMinor(mapper, rawToolMinor);
3652    processMTSync(mapper);
3653    processSync(mapper);
3654
3655    FakeInputDispatcher::NotifyMotionArgs args;
3656    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3657    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3658            x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor, 0));
3659}
3660
3661TEST_F(MultiTouchInputMapperTest, Process_TouchToolPressureSizeAxes_SummedLinearCalibration) {
3662    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3663    addConfigurationProperty("touch.deviceType", "touchScreen");
3664    prepareDisplay(DISPLAY_ORIENTATION_0);
3665    prepareAxes(POSITION | TOUCH | TOOL);
3666    addConfigurationProperty("touch.touchSize.calibration", "pressure");
3667    addConfigurationProperty("touch.toolSize.calibration", "linear");
3668    addConfigurationProperty("touch.toolSize.linearScale", "10");
3669    addConfigurationProperty("touch.toolSize.linearBias", "160");
3670    addConfigurationProperty("touch.toolSize.isSummed", "1");
3671    addConfigurationProperty("touch.pressure.calibration", "amplitude");
3672    addConfigurationProperty("touch.pressure.source", "touch");
3673    addConfigurationProperty("touch.pressure.scale", "0.01");
3674    addMapperAndConfigure(mapper);
3675
3676    // These calculations are based on the input device calibration documentation.
3677    // Note: We only provide a single common touch/tool value because the device is assumed
3678    //       not to emit separate values for each pointer (isSummed = 1).
3679    int32_t rawX = 100;
3680    int32_t rawY = 200;
3681    int32_t rawX2 = 150;
3682    int32_t rawY2 = 250;
3683    int32_t rawTouchMajor = 60;
3684    int32_t rawToolMajor = 5;
3685
3686    float x = toDisplayX(rawX);
3687    float y = toDisplayY(rawY);
3688    float x2 = toDisplayX(rawX2);
3689    float y2 = toDisplayY(rawY2);
3690    float pressure = float(rawTouchMajor) * 0.01f;
3691    float size = float(rawToolMajor) / RAW_TOOL_MAX;
3692    float tool = (float(rawToolMajor) * 10.0f + 160.0f) / 2;
3693    float touch = min(tool * pressure, tool);
3694
3695    processPosition(mapper, rawX, rawY);
3696    processTouchMajor(mapper, rawTouchMajor);
3697    processToolMajor(mapper, rawToolMajor);
3698    processMTSync(mapper);
3699    processPosition(mapper, rawX2, rawY2);
3700    processTouchMajor(mapper, rawTouchMajor);
3701    processToolMajor(mapper, rawToolMajor);
3702    processMTSync(mapper);
3703    processSync(mapper);
3704
3705    FakeInputDispatcher::NotifyMotionArgs args;
3706    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3707    ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3708    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3709    ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3710            args.action);
3711    ASSERT_EQ(size_t(2), args.pointerCount);
3712    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3713            x, y, pressure, size, touch, touch, tool, tool, 0));
3714    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
3715            x2, y2, pressure, size, touch, touch, tool, tool, 0));
3716}
3717
3718TEST_F(MultiTouchInputMapperTest, Process_TouchToolPressureSizeAxes_AreaCalibration) {
3719    MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
3720    addConfigurationProperty("touch.deviceType", "touchScreen");
3721    prepareDisplay(DISPLAY_ORIENTATION_0);
3722    prepareAxes(POSITION | TOUCH | TOOL);
3723    addConfigurationProperty("touch.touchSize.calibration", "pressure");
3724    addConfigurationProperty("touch.toolSize.calibration", "area");
3725    addConfigurationProperty("touch.toolSize.areaScale", "22");
3726    addConfigurationProperty("touch.toolSize.areaBias", "1");
3727    addConfigurationProperty("touch.toolSize.linearScale", "9.2");
3728    addConfigurationProperty("touch.toolSize.linearBias", "3");
3729    addConfigurationProperty("touch.pressure.calibration", "amplitude");
3730    addConfigurationProperty("touch.pressure.source", "touch");
3731    addConfigurationProperty("touch.pressure.scale", "0.01");
3732    addMapperAndConfigure(mapper);
3733
3734    // These calculations are based on the input device calibration documentation.
3735    int32_t rawX = 100;
3736    int32_t rawY = 200;
3737    int32_t rawTouchMajor = 60;
3738    int32_t rawToolMajor = 5;
3739
3740    float x = toDisplayX(rawX);
3741    float y = toDisplayY(rawY);
3742    float pressure = float(rawTouchMajor) * 0.01f;
3743    float size = float(rawToolMajor) / RAW_TOOL_MAX;
3744    float tool = sqrtf(float(rawToolMajor) * 22.0f + 1.0f) * 9.2f + 3.0f;
3745    float touch = min(tool * pressure, tool);
3746
3747    processPosition(mapper, rawX, rawY);
3748    processTouchMajor(mapper, rawTouchMajor);
3749    processToolMajor(mapper, rawToolMajor);
3750    processMTSync(mapper);
3751    processSync(mapper);
3752
3753    FakeInputDispatcher::NotifyMotionArgs args;
3754    ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3755    ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3756            x, y, pressure, size, touch, touch, tool, tool, 0));
3757}
3758
3759} // namespace android
3760