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