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