Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame^] | 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 <memory> |
| 22 | #include <set> |
| 23 | #include <string> |
| 24 | #include <unordered_map> |
| 25 | |
| 26 | #include <linux/input.h> |
| 27 | |
| 28 | #include <hardware/input.h> |
| 29 | #include <utils/PropertyMap.h> |
| 30 | |
| 31 | #include "InputHub.h" |
| 32 | |
| 33 | // Test definitions of opaque HAL structs |
| 34 | struct input_host {}; |
| 35 | struct input_device_identifier { |
| 36 | const char* name; |
| 37 | const char* uniqueId; |
| 38 | input_bus_t bus; |
| 39 | int32_t vendorId; |
| 40 | int32_t productId; |
| 41 | int32_t version; |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | namespace android { |
| 46 | |
| 47 | extern input_host_callbacks_t kTestCallbacks; |
| 48 | |
| 49 | class MockInputHost : public ::input_host_t { |
| 50 | public: |
| 51 | virtual ~MockInputHost() = default; |
| 52 | |
| 53 | void addDeviceProperty(const std::string& key, const std::string& value) { |
| 54 | mDevicePropertyMap.addProperty(String8(key.c_str()), String8(value.c_str())); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Call this at the end of a test to verify that any allocations made |
| 59 | * during the test were freed. |
| 60 | */ |
| 61 | bool checkAllocations() const; |
| 62 | |
| 63 | // Callbacks |
| 64 | input_device_identifier_t* createDeviceIdentifier( |
| 65 | const char* name, int32_t product_id, int32_t vendor_id, |
| 66 | input_bus_t bus, const char* unique_id); |
| 67 | |
| 68 | input_property_map_t* getDevicePropertyMap(input_device_identifier_t* id); |
| 69 | |
| 70 | input_property_t* getDeviceProperty(input_property_map_t* map, const char* key); |
| 71 | |
| 72 | const char* getPropertyKey(input_property_t* property); |
| 73 | |
| 74 | const char* getPropertyValue(input_property_t* property); |
| 75 | |
| 76 | void freeDeviceProperty(input_property_t* property); |
| 77 | |
| 78 | void freeDevicePropertyMap(input_property_map_t* map); |
| 79 | |
| 80 | private: |
| 81 | PropertyMap mDevicePropertyMap; |
| 82 | std::unique_ptr<input_device_identifier_t> mDeviceId; |
| 83 | int32_t mMapAllocations = 0; |
| 84 | std::unordered_map<std::string, int32_t> mPropertyAllocations; |
| 85 | }; |
| 86 | |
| 87 | class MockInputDeviceNode : public InputDeviceNode { |
| 88 | public: |
| 89 | MockInputDeviceNode() = default; |
| 90 | virtual ~MockInputDeviceNode() = default; |
| 91 | |
| 92 | virtual const std::string& getPath() const override { return mPath; } |
| 93 | virtual const std::string& getName() const override { return mName; } |
| 94 | virtual const std::string& getLocation() const override { return mLocation; } |
| 95 | virtual const std::string& getUniqueId() const override { return mUniqueId; } |
| 96 | |
| 97 | void setPath(const std::string& path) { mPath = path; } |
| 98 | void setName(const std::string& name) { mName = name; } |
| 99 | void setLocation(const std::string& location) { mLocation = location; } |
| 100 | void setUniqueId(const std::string& uniqueId) { mUniqueId = uniqueId; } |
| 101 | |
| 102 | virtual uint16_t getBusType() const override { return mBusType; } |
| 103 | virtual uint16_t getVendorId() const override { return mVendorId; } |
| 104 | virtual uint16_t getProductId() const override { return mProductId; } |
| 105 | virtual uint16_t getVersion() const override { return mVersion; } |
| 106 | |
| 107 | void setBusType(uint16_t busType) { mBusType = busType; } |
| 108 | void setVendorId(uint16_t vendorId) { mVendorId = vendorId; } |
| 109 | void setProductId(uint16_t productId) { mProductId = productId; } |
| 110 | void setVersion(uint16_t version) { mVersion = version; } |
| 111 | |
| 112 | virtual bool hasKey(int32_t key) const override { return mKeys.count(key); } |
| 113 | virtual bool hasKeyInRange(int32_t startKey, int32_t endKey) const override; |
| 114 | virtual bool hasRelativeAxis(int axis) const override { return mRelAxes.count(axis); } |
| 115 | virtual bool hasAbsoluteAxis(int32_t axis) const override { return mAbsAxes.count(axis); } |
| 116 | virtual bool hasSwitch(int32_t sw) const override { return mSwitches.count(sw); } |
| 117 | virtual bool hasForceFeedback(int32_t ff) const override { return mForceFeedbacks.count(ff); } |
| 118 | virtual bool hasInputProperty(int32_t property) const override { |
| 119 | return mInputProperties.count(property); |
| 120 | } |
| 121 | |
| 122 | // base case |
| 123 | void addKeys() {} |
| 124 | // inductive case |
| 125 | template<typename I, typename... Is> |
| 126 | void addKeys(I key, Is... keys) { |
| 127 | // Add the first key |
| 128 | mKeys.insert(key); |
| 129 | // Recursively add the remaining keys |
| 130 | addKeys(keys...); |
| 131 | } |
| 132 | |
| 133 | void addRelAxis(int32_t axis) { mRelAxes.insert(axis); } |
| 134 | void addAbsAxis(int32_t axis, AbsoluteAxisInfo* info) { mAbsAxes[axis] = info; } |
| 135 | void addSwitch(int32_t sw) { mSwitches.insert(sw); } |
| 136 | void addForceFeedback(int32_t ff) { mForceFeedbacks.insert(ff); } |
| 137 | void addInputProperty(int32_t property) { mInputProperties.insert(property); } |
| 138 | |
| 139 | virtual int32_t getKeyState(int32_t key) const override { return 0; } |
| 140 | virtual int32_t getSwitchState(int32_t sw) const override { return 0; } |
| 141 | virtual const AbsoluteAxisInfo* getAbsoluteAxisInfo(int32_t axis) const override { |
| 142 | auto iter = mAbsAxes.find(axis); |
| 143 | if (iter != mAbsAxes.end()) { |
| 144 | return iter->second; |
| 145 | } |
| 146 | return nullptr; |
| 147 | } |
| 148 | virtual status_t getAbsoluteAxisValue(int32_t axis, int32_t* outValue) const override { |
| 149 | // TODO |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | virtual void vibrate(nsecs_t duration) override {} |
| 154 | virtual void cancelVibrate() override {} |
| 155 | |
| 156 | virtual void disableDriverKeyRepeat() override { mKeyRepeatDisabled = true; } |
| 157 | |
| 158 | bool isDriverKeyRepeatEnabled() { return mKeyRepeatDisabled; } |
| 159 | |
| 160 | private: |
| 161 | std::string mPath = "/test"; |
| 162 | std::string mName = "Test Device"; |
| 163 | std::string mLocation = "test/0"; |
| 164 | std::string mUniqueId = "test-id"; |
| 165 | |
| 166 | uint16_t mBusType = 0; |
| 167 | uint16_t mVendorId = 0; |
| 168 | uint16_t mProductId = 0; |
| 169 | uint16_t mVersion = 0; |
| 170 | |
| 171 | std::set<int32_t> mKeys; |
| 172 | std::set<int32_t> mRelAxes; |
| 173 | std::map<int32_t, AbsoluteAxisInfo*> mAbsAxes; |
| 174 | std::set<int32_t> mSwitches; |
| 175 | std::set<int32_t> mForceFeedbacks; |
| 176 | std::set<int32_t> mInputProperties; |
| 177 | |
| 178 | bool mKeyRepeatDisabled = false; |
| 179 | }; |
| 180 | |
| 181 | namespace MockNexus7v2 { |
| 182 | MockInputDeviceNode* getElanTouchscreen(); |
| 183 | MockInputDeviceNode* getLidInput(); |
| 184 | MockInputDeviceNode* getButtonJack(); |
| 185 | MockInputDeviceNode* getHeadsetJack(); |
| 186 | MockInputDeviceNode* getH2wButton(); |
| 187 | MockInputDeviceNode* getGpioKeys(); |
| 188 | } // namespace MockNexus7v2 |
| 189 | |
| 190 | namespace MockNexusPlayer { |
| 191 | MockInputDeviceNode* getGpioKeys(); |
| 192 | MockInputDeviceNode* getMidPowerBtn(); |
| 193 | MockInputDeviceNode* getNexusRemote(); |
| 194 | MockInputDeviceNode* getAsusGamepad(); |
| 195 | } // namespace MockNexusPlayer |
| 196 | |
| 197 | // HAL method prototypes used in mock callbacks |
| 198 | extern "C" { |
| 199 | input_device_identifier_t* create_device_identifier(input_host_t* host, |
| 200 | const char* name, int32_t product_id, int32_t vendor_id, |
| 201 | input_bus_t bus, const char* unique_id); |
| 202 | |
| 203 | input_device_definition_t* create_device_definition(input_host_t* host); |
| 204 | |
| 205 | input_report_definition_t* create_input_report_definition(input_host_t* host); |
| 206 | |
| 207 | input_report_definition_t* create_output_report_definition(input_host_t* host); |
| 208 | |
| 209 | void input_device_definition_add_report(input_host_t* host, |
| 210 | input_device_definition_t* d, input_report_definition_t* r); |
| 211 | |
| 212 | void input_report_definition_add_collection(input_host_t* host, |
| 213 | input_report_definition_t* report, input_collection_id_t id, int32_t arity); |
| 214 | |
| 215 | void input_report_definition_declare_usage_int(input_host_t* host, |
| 216 | input_report_definition_t* report, input_collection_id_t id, |
| 217 | input_usage_t usage, int32_t min, int32_t max, float resolution); |
| 218 | |
| 219 | void input_report_definition_declare_usages_bool(input_host_t* host, |
| 220 | input_report_definition_t* report, input_collection_id_t id, |
| 221 | input_usage_t* usage, size_t usage_count); |
| 222 | |
| 223 | |
| 224 | input_device_handle_t* register_device(input_host_t* host, |
| 225 | input_device_identifier_t* id, input_device_definition_t* d); |
| 226 | |
| 227 | void unregister_device(input_host_t* host, input_device_handle_t* handle); |
| 228 | |
| 229 | input_report_t* input_allocate_report(input_host_t* host, input_report_definition_t* r); |
| 230 | |
| 231 | void input_report_set_usage_int(input_host_t* host, input_report_t* r, |
| 232 | input_collection_id_t id, input_usage_t usage, int32_t value, int32_t arity_index); |
| 233 | |
| 234 | void input_report_set_usage_bool(input_host_t* host, input_report_t* r, |
| 235 | input_collection_id_t id, input_usage_t usage, bool value, int32_t arity_index); |
| 236 | |
| 237 | void report_event(input_host_t* host, input_device_handle_t* d, input_report_t* report); |
| 238 | |
| 239 | input_property_map_t* input_get_device_property_map(input_host_t* host, |
| 240 | input_device_identifier_t* id); |
| 241 | |
| 242 | input_property_t* input_get_device_property(input_host_t* host, input_property_map_t* map, |
| 243 | const char* key); |
| 244 | |
| 245 | const char* input_get_property_key(input_host_t* host, input_property_t* property); |
| 246 | |
| 247 | const char* input_get_property_value(input_host_t* host, input_property_t* property); |
| 248 | |
| 249 | void input_free_device_property(input_host_t* host, input_property_t* property); |
| 250 | |
| 251 | void input_free_device_property_map(input_host_t* host, input_property_map_t* map); |
| 252 | } // extern "C" |
| 253 | |
| 254 | } // namespace android |
| 255 | |
| 256 | #endif // ANDROID_INPUT_MOCKS_H_ |