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