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