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