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