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