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 26using namespace std::chrono_literals; 27using ::testing::ext::TestSize; 28namespace { 29constexpr auto DEVICE_MAX_DELAY = 100ms; 30constexpr auto DEVICE_DELAY_STEP = 10ms; 31} // namespace 32 33class TestDeviceListener : public OHOS::MMI::IInputDeviceListener { 34public: 35 ~TestDeviceListener() override = default; 36 void OnDeviceAdded(int32_t deviceId, const std::string& type) override 37 { 38 added_ = true; 39 deviceId_ = deviceId; 40 } 41 void OnDeviceRemoved(int32_t deviceId, const std::string& type) override 42 { 43 removed_ = true; 44 deviceId_ = deviceId; 45 } 46 47 void Clear() 48 { 49 added_ = false; 50 removed_ = false; 51 deviceId_ = -1; 52 } 53 54 std::atomic<bool> added_ = false; 55 std::atomic<bool> removed_ = false; 56 std::atomic<int32_t> deviceId_ = -1; 57}; 58 59class E2eUdevTest : public ::testing::Test { 60public: 61 static void SetUpTestSuite() 62 { 63 inputManager_->RegisterDevListener("change", listener_); 64 } 65 66 static void TearDownTestSuite() 67 { 68 inputManager_->UnregisterDevListener("change", listener_); 69 } 70 71 bool WaitAdded() 72 { 73 auto till = std::chrono::steady_clock::now() + DEVICE_MAX_DELAY; 74 while (!listener_->added_ && std::chrono::steady_clock::now() < till) { 75 std::this_thread::sleep_for(DEVICE_DELAY_STEP); 76 } 77 return listener_->added_; 78 } 79 80 void SetUp() override 81 { 82 listener_->Clear(); 83 } 84 85 void TearDown() override 86 { 87 if (!listener_->added_) { 88 return; 89 } 90 testDevice_.Destroy(); 91 auto till = std::chrono::steady_clock::now() + DEVICE_MAX_DELAY; 92 while (!listener_->removed_ && std::chrono::steady_clock::now() < till) { 93 std::this_thread::sleep_for(DEVICE_DELAY_STEP); 94 } 95 } 96 97 inline static OHOS::MMI::InputManager* inputManager_ = OHOS::MMI::InputManager::GetInstance(); 98 inline static std::shared_ptr<TestDeviceListener> listener_ = std::make_shared<TestDeviceListener>(); 99 TestDevice testDevice_; 100}; 101 102HWTEST_F(E2eUdevTest, TestUdevPropsDefault, TestSize.Level1) 103{ 104 ASSERT_NO_FATAL_FAILURE(testDevice_.Init()); 105 ASSERT_TRUE(WaitAdded()); 106 EXPECT_GE(listener_->deviceId_, 0); 107 108 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 109 EXPECT_EQ(dev->GetName(), TestDevice::TEST_NAME); 110 EXPECT_EQ(dev->GetBus(), TestDevice::TEST_BUS); 111 EXPECT_EQ(dev->GetVendor(), TestDevice::TEST_VENDOR); 112 EXPECT_EQ(dev->GetProduct(), TestDevice::TEST_PRODUCT); 113 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_POINTER); 114 }); 115 EXPECT_EQ(res, 0); 116} 117 118HWTEST_F(E2eUdevTest, TestUdevPropsKey, TestSize.Level1) 119{ 120 testDevice_.KeyboardSetup(); 121 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 122 ASSERT_TRUE(WaitAdded()); 123 EXPECT_GE(listener_->deviceId_, 0); 124 125 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 126 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_KEYBOARD); 127 }); 128 EXPECT_EQ(res, 0); 129} 130 131HWTEST_F(E2eUdevTest, TestUdevPropsSwitch, TestSize.Level1) 132{ 133 testDevice_.SwitchSetup(); 134 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 135 ASSERT_TRUE(WaitAdded()); 136 EXPECT_GE(listener_->deviceId_, 0); 137 138 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 139 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_SWITCH); 140 }); 141 EXPECT_EQ(res, 0); 142} 143 144HWTEST_F(E2eUdevTest, TestUdevPropsAccel, TestSize.Level1) 145{ 146 testDevice_.AccelerometerSetup(); 147 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 148 ASSERT_FALSE(WaitAdded()); 149} 150 151HWTEST_F(E2eUdevTest, TestUdevPropsStick, TestSize.Level1) 152{ 153 testDevice_.StickSetup(); 154 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 155 ASSERT_TRUE(WaitAdded()); 156 EXPECT_GE(listener_->deviceId_, 0); 157 158 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 159 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_POINTER); 160 }); 161 EXPECT_EQ(res, 0); 162} 163 164HWTEST_F(E2eUdevTest, TestUdevPropsTouchpad, TestSize.Level1) 165{ 166 testDevice_.TouchpadSetup(); 167 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 168 ASSERT_TRUE(WaitAdded()); 169 EXPECT_GE(listener_->deviceId_, 0); 170 171 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 172 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_POINTER); 173 }); 174 EXPECT_EQ(res, 0); 175} 176 177HWTEST_F(E2eUdevTest, TestUdevPropsTouchscreen, TestSize.Level1) 178{ 179 testDevice_.TouchscreenSetup(); 180 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 181 ASSERT_TRUE(WaitAdded()); 182 EXPECT_GE(listener_->deviceId_, 0); 183 184 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 185 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_TOUCH); 186 }); 187 EXPECT_EQ(res, 0); 188} 189 190HWTEST_F(E2eUdevTest, TestUdevPropsJoystick, TestSize.Level1) 191{ 192 testDevice_.JoystickSetup(); 193 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 194 ASSERT_TRUE(WaitAdded()); 195 EXPECT_GE(listener_->deviceId_, 0); 196 197 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 198 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_JOYSTICK); 199 }); 200 EXPECT_EQ(res, 0); 201} 202 203HWTEST_F(E2eUdevTest, TestUdevPropsTablet, TestSize.Level1) 204{ 205 testDevice_.TabletSetup(); 206 ASSERT_NO_FATAL_FAILURE(testDevice_.Init(false)); 207 ASSERT_TRUE(WaitAdded()); 208 EXPECT_GE(listener_->deviceId_, 0); 209 210 auto res = inputManager_->GetDevice(listener_->deviceId_, [](std::shared_ptr<OHOS::MMI::InputDevice> dev) { 211 EXPECT_EQ(dev->GetCapabilities(), 1ULL << OHOS::MMI::INPUT_DEV_CAP_TABLET_TOOL); 212 }); 213 EXPECT_EQ(res, 0); 214} 215