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