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