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