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 #include <gtest/gtest.h>
16 #include <vector>
17
18 #include <system_ability_definition.h>
19 #include <thread>
20 #include <linux/uinput.h>
21 #include "driver_ext_mgr_client.h"
22 #include "hilog_wrapper.h"
23 #include "iservice_registry.h"
24 #include "system_ability_load_callback_stub.h"
25
26 namespace OHOS {
27 namespace ExternalDeviceManager {
28 using namespace testing::ext;
29 class EmitEventTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
SetUp()33 void SetUp() override {};
TearDown()34 void TearDown() override {};
35
36 private:
37 class LoadCallback : public SystemAbilityLoadCallbackStub {
38 public:
39 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
40 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
41 };
42 };
43
44 static DriverExtMgrClient &edmClient = DriverExtMgrClient::GetInstance();
45 static int32_t deviceId = -1;
46 enum class LoadStatus {
47 LOAD_SUCCESS,
48 LOAD_FAILED,
49 ALREADY_EXISTS,
50 };
51
52 static LoadStatus g_loadStatus_ = LoadStatus::LOAD_FAILED;
53 static sptr<IRemoteObject> g_saObject = nullptr;
54 static constexpr uint64_t START_SA_SERVICE_WAIT_TIME = 3;
55
SetUpTestCase()56 void EmitEventTest::SetUpTestCase()
57 {
58 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59 if (samgr == nullptr) {
60 EDM_LOGE(EDM_MODULE_TEST, "%{public}s get samgr failed", __func__);
61 g_loadStatus_ = LoadStatus::LOAD_FAILED;
62 return;
63 }
64
65 auto saObj = samgr->CheckSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
66 if (saObj != nullptr) {
67 g_saObject = saObj;
68 g_loadStatus_ = LoadStatus::ALREADY_EXISTS;
69 EDM_LOGE(EDM_MODULE_TEST, "%{public}s external device SA exist", __func__);
70 return;
71 }
72
73 sptr<LoadCallback> loadCallback_ = new LoadCallback();
74 int32_t ret = samgr->LoadSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID, loadCallback_);
75 if (ret != UsbErrCode::EDM_OK) {
76 g_loadStatus_ = LoadStatus::LOAD_FAILED;
77 }
78 EDM_LOGE(EDM_MODULE_TEST, "%{public}s load hdf_ext_devmgr, ret:%{public}d", __func__, ret);
79 }
80
TearDownTestCase()81 void EmitEventTest::TearDownTestCase()
82 {
83 if (g_loadStatus_ == LoadStatus::LOAD_FAILED || g_loadStatus_ == LoadStatus::ALREADY_EXISTS) {
84 return;
85 }
86
87 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
88 if (samgr == nullptr) {
89 EDM_LOGE(EDM_MODULE_TEST, "%{public}s get samgr failed", __func__);
90 return;
91 }
92
93 int32_t ret = samgr->UnloadSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
94 EDM_LOGE(EDM_MODULE_TEST, "%{public}s unload hdf_ext_devmgr, ret:%{public}d", __func__, ret);
95 }
96
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)97 void EmitEventTest::LoadCallback::OnLoadSystemAbilitySuccess(
98 int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
99 {
100 std::cout << "load success: systemAbilityId:" << systemAbilityId
101 << " IRemoteObject result:" << ((remoteObject != nullptr) ? "succeed" : "failed") << std::endl;
102 g_loadStatus_ = LoadStatus::LOAD_SUCCESS;
103 g_saObject = remoteObject;
104 }
105
OnLoadSystemAbilityFail(int32_t systemAbilityId)106 void EmitEventTest::LoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
107 {
108 std::cout << "load failed: systemAbilityId:" << systemAbilityId << std::endl;
109 g_loadStatus_ = LoadStatus::LOAD_FAILED;
110 g_saObject = nullptr;
111 }
112
113 HWTEST_F(EmitEventTest, CheckSAServiceLoad001, TestSize.Level1)
114 {
115 if (g_loadStatus_ != LoadStatus::ALREADY_EXISTS) {
116 std::this_thread::sleep_for(std::chrono::seconds(START_SA_SERVICE_WAIT_TIME));
117 }
118
119 ASSERT_NE(g_saObject, nullptr);
120 }
121
122 HWTEST_F(EmitEventTest, CreateDevice001, TestSize.Level1)
123 {
124 Hid_Device hidDevice = {
125 .deviceName = "VSoC keyboard",
126 .vendorId = 0x6006,
127 .productId = 0x6008,
128 .version = 1,
129 .bustype = BUS_USB
130 };
131 std::vector<Hid_EventType> eventType = {HID_EV_KEY};
132 Hid_EventTypeArray eventTypeArray = {.hidEventType = eventType.data(), .length = (uint16_t)eventType.size()};
133 std::vector<Hid_KeyCode> keyCode = {HID_KEY_1, HID_KEY_SPACE, HID_KEY_BACKSPACE, HID_KEY_ENTER};
134 Hid_KeyCodeArray keyCodeArray = {.hidKeyCode = keyCode.data(), .length = (uint16_t)keyCode.size()};
135 Hid_EventProperties hidEventProp = {.hidEventTypes = eventTypeArray, .hidKeys = keyCodeArray};
136 auto ret = edmClient.CreateDevice(&hidDevice, &hidEventProp);
137 deviceId = ret;
138 std::cout << "create device: deviceId:" << deviceId << std::endl;
139 ASSERT_GE(ret, 0);
140 }
141
142 HWTEST_F(EmitEventTest, EmitEvent001, TestSize.Level1)
143 {
144 std::vector<Hid_EmitItem> items = {
145 {1, 0x14a, 108},
146 {3, 0, 50 },
147 {3, 1, 50 }
148 };
149 auto ret = edmClient.EmitEvent(0, items);
150 ASSERT_EQ(ret, 0);
151 }
152
153 HWTEST_F(EmitEventTest, EmitEvent002, TestSize.Level1)
154 {
155 const uint16_t len = 21;
156 std::vector<Hid_EmitItem> items;
157 for (uint16_t i = 0; i < len; ++i) {
158 Hid_EmitItem item = {1, 0x14a, 108};
159 items.push_back(item);
160 }
161 auto ret = edmClient.EmitEvent(deviceId, items);
162 ASSERT_NE(ret, 0);
163 }
164
165 HWTEST_F(EmitEventTest, EmitEvent003, TestSize.Level1)
166 {
167 const uint16_t len = 20;
168 std::vector<Hid_EmitItem> items;
169 for (uint16_t i = 0; i < len; ++i) {
170 Hid_EmitItem item = {1, 0x14a, 108};
171 items.push_back(item);
172 }
173 auto ret = edmClient.EmitEvent(deviceId, items);
174 ASSERT_EQ(ret, 0);
175 }
176
177 HWTEST_F(EmitEventTest, DestroyDevice001, TestSize.Level1)
178 {
179 std::cout << "destroy device: deviceId:" << deviceId << std::endl;
180 int32_t ret = edmClient.DestroyDevice(deviceId);
181 ASSERT_EQ(ret, 0);
182 }
183
184 HWTEST_F(EmitEventTest, DestroyDevice002, TestSize.Level1)
185 {
186 int32_t ret = edmClient.DestroyDevice(-1);
187 ASSERT_NE(ret, 0);
188 }
189
190 HWTEST_F(EmitEventTest, DestroyDevice003, TestSize.Level1)
191 {
192 const int16_t devId = 200;
193 int32_t ret = edmClient.DestroyDevice(devId);
194 ASSERT_NE(ret, 0);
195 }
196 } // namespace ExternalDeviceManager
197 } // namespace OHOS