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