Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 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 _UI_TEST_INPUT_UINPUT_INJECTOR_H |
| 18 | #define _UI_TEST_INPUT_UINPUT_INJECTOR_H |
| 19 | |
| 20 | #include <android-base/unique_fd.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <linux/uinput.h> |
| 24 | #include <log/log.h> |
| 25 | |
| 26 | #include <memory> |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | // This is the factory method that must be used to create a UinputDevice. |
| 31 | template <class D, class... Ts> |
| 32 | std::unique_ptr<D> createUinputDevice(Ts... args) { |
| 33 | // Using `new` to access non-public constructors. |
| 34 | std::unique_ptr<D> dev(new D(&args...)); |
| 35 | EXPECT_NO_FATAL_FAILURE(dev->init()); |
| 36 | return dev; |
| 37 | } |
| 38 | |
| 39 | // --- UinputDevice --- |
| 40 | |
| 41 | class UinputDevice { |
| 42 | public: |
| 43 | virtual ~UinputDevice(); |
| 44 | |
| 45 | inline const char* getName() const { return mName; } |
| 46 | |
| 47 | // Subclasses must either provide a public constructor or must be-friend the factory method. |
| 48 | template <class D, class... Ts> |
| 49 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 50 | |
| 51 | protected: |
| 52 | const char* mName; |
| 53 | |
| 54 | UinputDevice(const char* name); |
| 55 | |
| 56 | // Signals which types of events this device supports before it is created. |
| 57 | // This must be overridden by subclasses. |
| 58 | virtual void configureDevice(int fd, uinput_user_dev* device) = 0; |
| 59 | |
| 60 | void injectEvent(uint16_t type, uint16_t code, int32_t value); |
| 61 | |
| 62 | private: |
| 63 | base::unique_fd mDeviceFd; |
| 64 | |
| 65 | // This is called once by the factory method createUinputDevice(). |
| 66 | void init(); |
| 67 | }; |
| 68 | |
| 69 | // --- UinputKeyboard --- |
| 70 | |
| 71 | class UinputKeyboard : public UinputDevice { |
| 72 | public: |
| 73 | static constexpr const char* KEYBOARD_NAME = "Test Keyboard Device"; |
| 74 | |
| 75 | // Injects key press and sync. |
| 76 | void pressKey(int key); |
| 77 | // Injects key release and sync. |
| 78 | void releaseKey(int key); |
| 79 | // Injects 4 events: key press, sync, key release, and sync. |
| 80 | void pressAndReleaseKey(int key); |
| 81 | |
| 82 | template <class D, class... Ts> |
| 83 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 84 | |
| 85 | protected: |
| 86 | UinputKeyboard(std::initializer_list<int> keys = {}); |
| 87 | |
| 88 | private: |
| 89 | void configureDevice(int fd, uinput_user_dev* device) override; |
| 90 | |
| 91 | std::set<int> mKeys; |
| 92 | }; |
| 93 | |
| 94 | // --- UinputHomeKey--- |
| 95 | |
| 96 | // A keyboard device that has a single HOME key. |
| 97 | class UinputHomeKey : public UinputKeyboard { |
| 98 | public: |
| 99 | // Injects 4 events: key press, sync, key release, and sync. |
| 100 | void pressAndReleaseHomeKey(); |
| 101 | |
| 102 | template <class D, class... Ts> |
| 103 | friend std::unique_ptr<D> createUinputDevice(Ts... args); |
| 104 | |
| 105 | private: |
| 106 | UinputHomeKey(); |
| 107 | }; |
| 108 | |
| 109 | } // namespace android |
| 110 | |
| 111 | #endif // _UI_TEST_INPUT_UINPUT_INJECTOR_H |