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