1 /*
2  * Copyright (c) 2024 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 <cinttypes>
17 
18 #include "event_util_test.h"
19 #include "input_manager.h"
20 #include "input_manager_util.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "VirtualInputDeviceTest"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 constexpr int32_t TIME_WAIT_FOR_OP = 100;
29 
30 } // namespace
31 
32 class VirtualInputDeviceTest : public testing::Test {
33 public:
34     void SetUp();
35     void TearDown();
36     static void SetUpTestCase();
37     std::string GetEventDump();
38     static bool CmpInputDevice(std::shared_ptr<InputDevice> one, std::shared_ptr<InputDevice> other);
39 };
40 
41 class VirtualInputDeviceListener : public IInputDeviceListener {
42 public:
43     ~VirtualInputDeviceListener() override = default;
OnDeviceAdded(int32_t deviceId,const std::string & type)44     void OnDeviceAdded(int32_t deviceId, const std::string &type) override
45     {
46         MMI_HILOGI("deviceId:%{public}d added, type:%{public}s", deviceId, type.c_str());
47         KeyboardType keyType { KeyboardType::KEYBOARD_TYPE_NONE };
48         auto keyboardCallback = [&keyType] (int32_t keyboardType) {
49             MMI_HILOGI("KeyboardType:%{public}d", keyboardType);
50         };
51         int32_t ret = InputManager::GetInstance()->GetKeyboardType(deviceId, keyboardCallback);
52         ASSERT_EQ(ret, RET_OK);
53     }
54 
OnDeviceRemoved(int32_t deviceId,const std::string & type)55     void OnDeviceRemoved(int32_t deviceId, const std::string &type) override
56     {
57         MMI_HILOGI("DeviceId:%{public}d removed, type:%{public}s", deviceId, type.c_str());
58     }
59 };
60 
SetUpTestCase()61 void VirtualInputDeviceTest::SetUpTestCase()
62 {
63     ASSERT_TRUE(TestUtil->Init());
64 }
65 
SetUp()66 void VirtualInputDeviceTest::SetUp()
67 {
68     TestUtil->SetRecvFlag(RECV_FLAG::RECV_FOCUS);
69 }
70 
TearDown()71 void VirtualInputDeviceTest::TearDown()
72 {
73     TestUtil->AddEventDump("");
74     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
75 }
76 
GetEventDump()77 std::string VirtualInputDeviceTest::GetEventDump()
78 {
79     return TestUtil->GetEventDump();
80 }
81 
CmpInputDevice(std::shared_ptr<InputDevice> one,std::shared_ptr<InputDevice> other)82 bool VirtualInputDeviceTest::CmpInputDevice(std::shared_ptr<InputDevice> one, std::shared_ptr<InputDevice> other)
83 {
84     if (one == nullptr && other == nullptr) {
85         return true;
86     }
87     if (one == nullptr || other == nullptr) {
88         return false;
89     }
90     return one->GetName() == other->GetName() && one->GetType() == other->GetType() &&
91         one->GetBus() == other->GetBus() && one->GetVersion() == other->GetVersion() &&
92         one->GetProduct() == other->GetProduct() && one->GetVendor() == other->GetVendor() &&
93         one->GetPhys() == other->GetPhys() && one->GetUniq() == other->GetUniq() &&
94         one->HasCapability(InputDeviceCapability::INPUT_DEV_CAP_KEYBOARD) ==
95         other->HasCapability(InputDeviceCapability::INPUT_DEV_CAP_KEYBOARD) &&
96         one->HasCapability(InputDeviceCapability::INPUT_DEV_CAP_POINTER) ==
97         other->HasCapability(InputDeviceCapability::INPUT_DEV_CAP_POINTER);
98 };
99 
100 /**
101  * @tc.name: VirtualInputDeviceTest_AddVirtualInputDevice_001
102  * @tc.desc: Add virtual input device
103  * @tc.type: FUNC
104  * @tc.require:
105  */
106 HWTEST_F(VirtualInputDeviceTest, VirtualInputDeviceTest_AddVirtualInputDevice_001, TestSize.Level1)
107 {
108     CALL_TEST_DEBUG;
109     auto inputDeviceAdd = std::make_shared<InputDevice>();
110     inputDeviceAdd->SetName("VirtualDeviceName");
111     inputDeviceAdd->SetType(-1);
112     inputDeviceAdd->SetBus(-1);
113     inputDeviceAdd->SetVersion(-1);
114     inputDeviceAdd->SetProduct(-1);
115     inputDeviceAdd->SetVendor(-1);
116     inputDeviceAdd->SetPhys("Phys");
117     inputDeviceAdd->SetUniq("unique");
118     inputDeviceAdd->AddCapability(InputDeviceCapability::INPUT_DEV_CAP_KEYBOARD);
119     int32_t deviceId { -1 };
120     int32_t ret = InputManager::GetInstance()->AddVirtualInputDevice(inputDeviceAdd, deviceId);
121     ASSERT_EQ(ret, RET_OK);
122     auto inputDeviceGet = std::make_shared<InputDevice>();
__anonae97a7060302(std::shared_ptr<InputDevice> dev) 123     auto inputDevFun = [&inputDeviceGet] (std::shared_ptr<InputDevice> dev) {
124         inputDeviceGet = dev;
125     };
126     ASSERT_TRUE(InputManager::GetInstance()->GetDevice(deviceId, inputDevFun) == RET_OK);
127     ASSERT_TRUE(CmpInputDevice(inputDeviceAdd, inputDeviceGet));
128     ret = InputManager::GetInstance()->RemoveVirtualInputDevice(deviceId);
129     ASSERT_EQ(ret, RET_OK);
130 }
131 
132 /**
133  * @tc.name: VirtualInputDeviceTest_RemoveVirtualInputDevice_001
134  * @tc.desc: Remove virtual input device
135  * @tc.type: FUNC
136  * @tc.require:
137  */
138 HWTEST_F(VirtualInputDeviceTest, VirtualInputDeviceTest_RemoveVirtualInputDevice_001, TestSize.Level1)
139 {
140     auto inputDeviceAdd = std::make_shared<InputDevice>();
141     int32_t deviceId { -1 };
142     int32_t ret = InputManager::GetInstance()->AddVirtualInputDevice(inputDeviceAdd, deviceId);
143     ASSERT_EQ(ret, RET_OK);
144     auto inputDeviceGet = std::make_shared<InputDevice>();
__anonae97a7060402(std::shared_ptr<InputDevice> dev) 145     auto inputDevFun = [&inputDeviceGet] (std::shared_ptr<InputDevice> dev) {
146         inputDeviceGet = dev;
147     };
148     ret = InputManager::GetInstance()->GetDevice(deviceId, inputDevFun);
149     ASSERT_EQ(ret, RET_OK);
150     ret = InputManager::GetInstance()->RemoveVirtualInputDevice(deviceId);
151     ASSERT_EQ(ret, RET_OK);
152     ret = InputManager::GetInstance()->RemoveVirtualInputDevice(deviceId);
153     ASSERT_NE(ret, RET_OK);
154     ret = InputManager::GetInstance()->GetDevice(deviceId, inputDevFun);
155     ASSERT_NE(ret, RET_OK);
156 }
157 /**
158  * @tc.name: VirtualInputDeviceTest_TestHotPlug
159  * @tc.desc: Test virtual input hot plug
160  * @tc.type: FUNC
161  * @tc.require:
162  */
163 HWTEST_F(VirtualInputDeviceTest, VirtualInputDeviceTest_TestHotPlug, TestSize.Level1)
164 {
165     auto listener = std::make_shared<VirtualInputDeviceListener>();
166     int32_t ret = InputManager::GetInstance()->RegisterDevListener("change", listener);
167     ASSERT_EQ(ret, RET_OK);
168 }
169 } // namespace MMI
170 } // namespace OHOS
171