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