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