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