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