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