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