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