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