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> |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame] | 21 | #include <set> |
| 22 | #include <string> |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame] | 23 | |
| 24 | #include <linux/input.h> |
| 25 | |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame] | 26 | #include "InputHub.h" |
| 27 | |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame] | 30 | class MockInputDeviceNode : public InputDeviceNode { |
| 31 | public: |
| 32 | MockInputDeviceNode() = default; |
| 33 | virtual ~MockInputDeviceNode() = default; |
| 34 | |
| 35 | virtual const std::string& getPath() const override { return mPath; } |
| 36 | virtual const std::string& getName() const override { return mName; } |
| 37 | virtual const std::string& getLocation() const override { return mLocation; } |
| 38 | virtual const std::string& getUniqueId() const override { return mUniqueId; } |
| 39 | |
| 40 | void setPath(const std::string& path) { mPath = path; } |
| 41 | void setName(const std::string& name) { mName = name; } |
| 42 | void setLocation(const std::string& location) { mLocation = location; } |
| 43 | void setUniqueId(const std::string& uniqueId) { mUniqueId = uniqueId; } |
| 44 | |
| 45 | virtual uint16_t getBusType() const override { return mBusType; } |
| 46 | virtual uint16_t getVendorId() const override { return mVendorId; } |
| 47 | virtual uint16_t getProductId() const override { return mProductId; } |
| 48 | virtual uint16_t getVersion() const override { return mVersion; } |
| 49 | |
| 50 | void setBusType(uint16_t busType) { mBusType = busType; } |
| 51 | void setVendorId(uint16_t vendorId) { mVendorId = vendorId; } |
| 52 | void setProductId(uint16_t productId) { mProductId = productId; } |
| 53 | void setVersion(uint16_t version) { mVersion = version; } |
| 54 | |
| 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; |
| 57 | virtual bool hasRelativeAxis(int axis) const override { return mRelAxes.count(axis); } |
| 58 | virtual bool hasAbsoluteAxis(int32_t axis) const override { return mAbsAxes.count(axis); } |
| 59 | virtual bool hasSwitch(int32_t sw) const override { return mSwitches.count(sw); } |
| 60 | virtual bool hasForceFeedback(int32_t ff) const override { return mForceFeedbacks.count(ff); } |
| 61 | virtual bool hasInputProperty(int32_t property) const override { |
| 62 | return mInputProperties.count(property); |
| 63 | } |
| 64 | |
| 65 | // base case |
| 66 | void addKeys() {} |
| 67 | // inductive case |
| 68 | template<typename I, typename... Is> |
| 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 | |
| 76 | void addRelAxis(int32_t axis) { mRelAxes.insert(axis); } |
| 77 | void addAbsAxis(int32_t axis, AbsoluteAxisInfo* info) { mAbsAxes[axis] = info; } |
| 78 | void addSwitch(int32_t sw) { mSwitches.insert(sw); } |
| 79 | void addForceFeedback(int32_t ff) { mForceFeedbacks.insert(ff); } |
| 80 | void addInputProperty(int32_t property) { mInputProperties.insert(property); } |
| 81 | |
| 82 | virtual int32_t getKeyState(int32_t key) const override { return 0; } |
| 83 | virtual int32_t getSwitchState(int32_t sw) const override { return 0; } |
| 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 | } |
| 91 | virtual status_t getAbsoluteAxisValue(int32_t axis, int32_t* outValue) const override { |
| 92 | // TODO |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | virtual void vibrate(nsecs_t duration) override {} |
| 97 | virtual void cancelVibrate() override {} |
| 98 | |
| 99 | virtual void disableDriverKeyRepeat() override { mKeyRepeatDisabled = true; } |
| 100 | |
| 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 | |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame] | 140 | } // namespace android |
| 141 | |
| 142 | #endif // ANDROID_INPUT_MOCKS_H_ |