1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <atomic> 17 #include <chrono> 18 #include <thread> 19 20 #include <gmock/gmock.h> 21 #include <gtest/gtest.h> 22 23 #include "input_manager.h" 24 #include "test_device.h" 25 26 using namespace std::chrono_literals; 27 28 using ::testing::ext::TestSize; 29 30 namespace { 31 constexpr auto DEVICE_MAX_DELAY = 100ms; 32 constexpr auto DEVICE_DELAY_STEP = 10ms; 33 } 34 35 class TestDeviceListener : public OHOS::MMI::IInputDeviceListener { 36 public: 37 ~TestDeviceListener() override = default; OnDeviceAdded(int32_t deviceId,const std::string & type)38 void OnDeviceAdded(int32_t deviceId, const std::string& type) 39 { 40 added_ = true; 41 deviceId_ = deviceId; 42 } OnDeviceRemoved(int32_t deviceId,const std::string & type)43 void OnDeviceRemoved(int32_t deviceId, const std::string& type) 44 { 45 removed_ = true; 46 deviceId_ = deviceId; 47 } 48 Clear()49 void Clear() 50 { 51 added_ = false; 52 removed_ = false; 53 deviceId_ = -1; 54 } 55 56 std::atomic<bool> added_ = false; 57 std::atomic<bool> removed_ = false; 58 std::atomic<int32_t> deviceId_ = -1; 59 }; 60 61 class E2eUdevTest : public ::testing::Test { 62 public: SetUpTestSuite()63 static void SetUpTestSuite() 64 { 65 inputManager_->RegisterDevListener("change", listener_); 66 } 67 TearDownTestSuite()68 static void TearDownTestSuite() 69 { 70 inputManager_->UnregisterDevListener("change", listener_); 71 } 72 WaitAdded()73 bool WaitAdded() 74 { 75 auto till = std::chrono::steady_clock::now() + DEVICE_MAX_DELAY; 76 while (!listener_->added_ && std::chrono::steady_clock::now() < till) { 77 std::this_thread::sleep_for(DEVICE_DELAY_STEP); 78 } 79 return listener_->added_; 80 } 81 SetUp()82 void SetUp() override 83 { 84 listener_->Clear(); 85 } 86 TearDown()87 void TearDown() override 88 { 89 if (!listener_->added_) { 90 return; 91 } 92 testDevice_.Destroy(); 93 auto till = std::chrono::steady_clock::now() + DEVICE_MAX_DELAY; 94 while (!listener_->removed_ && std::chrono::steady_clock::now() < till) { 95 std::this_thread::sleep_for(DEVICE_DELAY_STEP); 96 } 97 } 98 99 inline static OHOS::MMI::InputManager* inputManager_ = OHOS::MMI::InputManager::GetInstance(); 100 inline static std::shared_ptr<TestDeviceListener> listener_ = std::make_shared<TestDeviceListener>(); 101 TestDevice testDevice_; 102 }; 103 104 HWTEST_F(E2eUdevTest, TestUdevPropsDefault, TestSize.Level1) 105 { 106 ASSERT_NO_FATAL_FAILURE(testDevice_.Init()); 107 ASSERT_TRUE(WaitAdded()); 108 EXPECT_GE(listener_->deviceId_, 0); 109 __anona78aa62c0202(std::shared_ptr<OHOS::MMI::InputDevice> dev) 110 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 111 EXPECT_EQ(dev->GetName(), TestDevice::TEST_NAME); 112 EXPECT_EQ(dev->GetBus(), TestDevice::TEST_BUS); 113 EXPECT_EQ(dev->GetVendor(), TestDevice::TEST_VENDOR); 114 EXPECT_EQ(dev->GetProduct(), TestDevice::TEST_PRODUCT); 115 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_POINTER); 116 }); 117 EXPECT_EQ(res, 0); 118 } 119 120 HWTEST_F(E2eUdevTest, TestUdevPropsKey, TestSize.Level1) 121 { 122 testDevice_.KeyboardSetup(); 123 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 124 ASSERT_TRUE(WaitAdded()); 125 EXPECT_GE(listener_->deviceId_, 0); 126 __anona78aa62c0302(std::shared_ptr<OHOS::MMI::InputDevice> dev) 127 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 128 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_KEYBOARD); 129 }); 130 EXPECT_EQ(res, 0); 131 } 132 133 HWTEST_F(E2eUdevTest, TestUdevPropsSwitch, TestSize.Level1) 134 { 135 testDevice_.SwitchSetup(); 136 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 137 ASSERT_TRUE(WaitAdded()); 138 EXPECT_GE(listener_->deviceId_, 0); 139 __anona78aa62c0402(std::shared_ptr<OHOS::MMI::InputDevice> dev) 140 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 141 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_SWITCH); 142 }); 143 EXPECT_EQ(res, 0); 144 } 145 146 HWTEST_F(E2eUdevTest, TestUdevPropsAccel, TestSize.Level1) 147 { 148 testDevice_.AccelerometerSetup(); 149 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 150 ASSERT_FALSE(WaitAdded()); 151 } 152 153 HWTEST_F(E2eUdevTest, TestUdevPropsStick, TestSize.Level1) 154 { 155 testDevice_.StickSetup(); 156 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 157 ASSERT_TRUE(WaitAdded()); 158 EXPECT_GE(listener_->deviceId_, 0); 159 __anona78aa62c0502(std::shared_ptr<OHOS::MMI::InputDevice> dev) 160 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 161 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_POINTER); 162 }); 163 EXPECT_EQ(res, 0); 164 } 165 166 HWTEST_F(E2eUdevTest, TestUdevPropsTouchpad, TestSize.Level1) 167 { 168 testDevice_.TouchpadSetup(); 169 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 170 ASSERT_TRUE(WaitAdded()); 171 EXPECT_GE(listener_->deviceId_, 0); 172 __anona78aa62c0602(std::shared_ptr<OHOS::MMI::InputDevice> dev) 173 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 174 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_POINTER); 175 }); 176 EXPECT_EQ(res, 0); 177 } 178 179 HWTEST_F(E2eUdevTest, TestUdevPropsTouchscreen, TestSize.Level1) 180 { 181 testDevice_.TouchscreenSetup(); 182 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 183 ASSERT_TRUE(WaitAdded()); 184 EXPECT_GE(listener_->deviceId_, 0); 185 __anona78aa62c0702(std::shared_ptr<OHOS::MMI::InputDevice> dev) 186 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 187 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_TOUCH); 188 }); 189 EXPECT_EQ(res, 0); 190 } 191 192 HWTEST_F(E2eUdevTest, TestUdevPropsJoystick, TestSize.Level1) 193 { 194 testDevice_.JoystickSetup(); 195 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 196 ASSERT_TRUE(WaitAdded()); 197 EXPECT_GE(listener_->deviceId_, 0); 198 __anona78aa62c0802(std::shared_ptr<OHOS::MMI::InputDevice> dev) 199 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 200 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_JOYSTICK); 201 }); 202 EXPECT_EQ(res, 0); 203 } 204 205 HWTEST_F(E2eUdevTest, TestUdevPropsTablet, TestSize.Level1) 206 { 207 testDevice_.TabletSetup(); 208 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 209 ASSERT_TRUE(WaitAdded()); 210 EXPECT_GE(listener_->deviceId_, 0); 211 __anona78aa62c0902(std::shared_ptr<OHOS::MMI::InputDevice> dev) 212 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 213 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_TABLET_TOOL); 214 }); 215 EXPECT_EQ(res, 0); 216 } 217