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