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