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