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