1 /*
2  * Copyright (C) 2015 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 #ifndef ANDROID_INPUT_MOCKS_H_
18 #define ANDROID_INPUT_MOCKS_H_
19 
20 #include <map>
21 #include <set>
22 #include <string>
23 
24 #include <linux/input.h>
25 
26 #include "InputHub.h"
27 
28 namespace android {
29 
30 class MockInputDeviceNode : public InputDeviceNode {
31 public:
32     MockInputDeviceNode() = default;
33     virtual ~MockInputDeviceNode() = default;
34 
getPath()35     virtual const std::string& getPath() const override { return mPath; }
getName()36     virtual const std::string& getName() const override { return mName; }
getLocation()37     virtual const std::string& getLocation() const override { return mLocation; }
getUniqueId()38     virtual const std::string& getUniqueId() const override { return mUniqueId; }
39 
setPath(const std::string & path)40     void setPath(const std::string& path) { mPath = path; }
setName(const std::string & name)41     void setName(const std::string& name) { mName = name; }
setLocation(const std::string & location)42     void setLocation(const std::string& location) { mLocation = location; }
setUniqueId(const std::string & uniqueId)43     void setUniqueId(const std::string& uniqueId) { mUniqueId = uniqueId; }
44 
getBusType()45     virtual uint16_t getBusType() const override { return mBusType; }
getVendorId()46     virtual uint16_t getVendorId() const override { return mVendorId; }
getProductId()47     virtual uint16_t getProductId() const override { return mProductId; }
getVersion()48     virtual uint16_t getVersion() const override { return mVersion; }
49 
setBusType(uint16_t busType)50     void setBusType(uint16_t busType) { mBusType = busType; }
setVendorId(uint16_t vendorId)51     void setVendorId(uint16_t vendorId) { mVendorId = vendorId; }
setProductId(uint16_t productId)52     void setProductId(uint16_t productId) { mProductId = productId; }
setVersion(uint16_t version)53     void setVersion(uint16_t version) { mVersion = version; }
54 
hasKey(int32_t key)55     virtual bool hasKey(int32_t key) const override { return mKeys.count(key); }
56     virtual bool hasKeyInRange(int32_t startKey, int32_t endKey) const override;
hasRelativeAxis(int axis)57     virtual bool hasRelativeAxis(int axis) const override { return mRelAxes.count(axis); }
hasAbsoluteAxis(int32_t axis)58     virtual bool hasAbsoluteAxis(int32_t axis) const override { return mAbsAxes.count(axis); }
hasSwitch(int32_t sw)59     virtual bool hasSwitch(int32_t sw) const override { return mSwitches.count(sw); }
hasForceFeedback(int32_t ff)60     virtual bool hasForceFeedback(int32_t ff) const override { return mForceFeedbacks.count(ff); }
hasInputProperty(int32_t property)61     virtual bool hasInputProperty(int32_t property) const override {
62         return mInputProperties.count(property);
63     }
64 
65     // base case
addKeys()66     void addKeys() {}
67     // inductive case
68     template<typename I, typename... Is>
addKeys(I key,Is...keys)69     void addKeys(I key, Is... keys) {
70         // Add the first key
71         mKeys.insert(key);
72         // Recursively add the remaining keys
73         addKeys(keys...);
74     }
75 
addRelAxis(int32_t axis)76     void addRelAxis(int32_t axis) { mRelAxes.insert(axis); }
addAbsAxis(int32_t axis,AbsoluteAxisInfo * info)77     void addAbsAxis(int32_t axis, AbsoluteAxisInfo* info) { mAbsAxes[axis] = info; }
addSwitch(int32_t sw)78     void addSwitch(int32_t sw) { mSwitches.insert(sw); }
addForceFeedback(int32_t ff)79     void addForceFeedback(int32_t ff) { mForceFeedbacks.insert(ff); }
addInputProperty(int32_t property)80     void addInputProperty(int32_t property) { mInputProperties.insert(property); }
81 
getKeyState(int32_t key)82     virtual int32_t getKeyState(int32_t key) const override { return 0; }
getSwitchState(int32_t sw)83     virtual int32_t getSwitchState(int32_t sw) const override { return 0; }
getAbsoluteAxisInfo(int32_t axis)84     virtual const AbsoluteAxisInfo* getAbsoluteAxisInfo(int32_t axis) const override {
85         auto iter = mAbsAxes.find(axis);
86         if (iter != mAbsAxes.end()) {
87             return iter->second;
88         }
89         return nullptr;
90     }
getAbsoluteAxisValue(int32_t axis,int32_t * outValue)91     virtual status_t getAbsoluteAxisValue(int32_t axis, int32_t* outValue) const override {
92         // TODO
93         return 0;
94     }
95 
vibrate(nsecs_t duration)96     virtual void vibrate(nsecs_t duration) override {}
cancelVibrate()97     virtual void cancelVibrate() override {}
98 
disableDriverKeyRepeat()99     virtual void disableDriverKeyRepeat() override { mKeyRepeatDisabled = true; }
100 
isDriverKeyRepeatEnabled()101     bool isDriverKeyRepeatEnabled() { return mKeyRepeatDisabled; }
102 
103 private:
104     std::string mPath = "/test";
105     std::string mName = "Test Device";
106     std::string mLocation = "test/0";
107     std::string mUniqueId = "test-id";
108 
109     uint16_t mBusType = 0;
110     uint16_t mVendorId = 0;
111     uint16_t mProductId = 0;
112     uint16_t mVersion = 0;
113 
114     std::set<int32_t> mKeys;
115     std::set<int32_t> mRelAxes;
116     std::map<int32_t, AbsoluteAxisInfo*> mAbsAxes;
117     std::set<int32_t> mSwitches;
118     std::set<int32_t> mForceFeedbacks;
119     std::set<int32_t> mInputProperties;
120 
121     bool mKeyRepeatDisabled = false;
122 };
123 
124 namespace MockNexus7v2 {
125 MockInputDeviceNode* getElanTouchscreen();
126 MockInputDeviceNode* getLidInput();
127 MockInputDeviceNode* getButtonJack();
128 MockInputDeviceNode* getHeadsetJack();
129 MockInputDeviceNode* getH2wButton();
130 MockInputDeviceNode* getGpioKeys();
131 }  // namespace MockNexus7v2
132 
133 namespace MockNexusPlayer {
134 MockInputDeviceNode* getGpioKeys();
135 MockInputDeviceNode* getMidPowerBtn();
136 MockInputDeviceNode* getNexusRemote();
137 MockInputDeviceNode* getAsusGamepad();
138 }  // namespace MockNexusPlayer
139 
140 }  // namespace android
141 
142 #endif  // ANDROID_INPUT_MOCKS_H_
143