1 /*
2 * Copyright (c) 2022 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 "UTTest_dm_device_state_manager.h"
17
18 #include <iostream>
19
20 #include "dm_log.h"
21 #include "dm_constants.h"
22 #include "dm_adapter_manager.h"
23 #include "ipc_notify_device_state_req.h"
24 #include "ipc_notify_auth_result_req.h"
25 #include "ipc_notify_verify_auth_result_req.h"
26 #include "dm_device_state_manager.h"
27 #include "ipc_notify_device_found_req.h"
28 #include "ipc_notify_discover_result_req.h"
29 #include "hichain_connector.h"
30 #include "device_manager_service_listener.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
SetUp()34 void DmDeviceStateManagerTest::SetUp()
35 {
36 }
37
TearDown()38 void DmDeviceStateManagerTest::TearDown()
39 {
40 }
41
SetUpTestCase()42 void DmDeviceStateManagerTest::SetUpTestCase()
43 {
44 }
45
TearDownTestCase()46 void DmDeviceStateManagerTest::TearDownTestCase()
47 {
48 }
49 namespace {
50 std::shared_ptr<HiChainConnector> hiChainConnector_ = std::make_shared<HiChainConnector>();
51 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
52 std::shared_ptr<DeviceManagerServiceListener> listener_ = std::make_shared<DeviceManagerServiceListener>();
53 std::shared_ptr<DmDeviceStateManager> dmDeviceStateManager =
54 std::make_shared<DmDeviceStateManager>(softbusConnector, listener_, hiChainConnector_);
55
56 /**
57 * @tc.name: DmDeviceStateManager_001
58 * @tc.desc: set DmDeviceStateManager to tne new pointer, and it's not nullptr
59 * @tc.type: FUNC
60 * @tc.require: AR000GHSJK
61 */
62 HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_001, testing::ext::TestSize.Level0)
63 {
64 std::shared_ptr<DmDeviceStateManager> p = std::make_shared<DmDeviceStateManager>(softbusConnector, listener_,
65 hiChainConnector_);
66 ASSERT_NE(p, nullptr);
67 }
68
69 /**
70 * @tc.name: DmDeviceStateManager_002
71 * @tc.desc: set DmDeviceStateManager to tne new pointer,it's not nullptr and delete it
72 * @tc.type: FUNC
73 * @tc.require: AR000GHSJK
74 */
75 HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_002, testing::ext::TestSize.Level0)
76 {
77 std::shared_ptr<DmDeviceStateManager> p = std::make_shared<DmDeviceStateManager>(softbusConnector, listener_,
78 hiChainConnector_);
79 p.reset();
80 EXPECT_EQ(p, nullptr);
81 }
82
83 /**
84 * @tc.name: OnDeviceOnline_001
85 * @tc.desc: set info.deviceId to some para, and return it
86 * @tc.type: FUNC
87 * @tc.require: AR000GHSJK
88 */
89 HWTEST_F(DmDeviceStateManagerTest, OnDeviceOnline_001, testing::ext::TestSize.Level0)
90 {
91 std::string pkgName = "123";
92 DmDeviceInfo info;
93 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
94 dmDeviceStateManager->OnDeviceOnline(pkgName, info);
95 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
96 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
97 DmDeviceInfo dminfo;
98 if (pReq == nullptr) {
99 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
100 } else {
101 dminfo = pReq->GetDeviceInfo();
102 }
103 int result = strcmp(info.deviceId, dminfo.deviceId);
104 EXPECT_EQ(result, 0);
105 }
106
107 /**
108 * @tc.name: OnDeviceChanged_001
109 * @tc.desc: set info.deviceId to some para, and return it
110 * @tc.type: FUNC
111 * @tc.require: AR000GHSJK
112 */
113 HWTEST_F(DmDeviceStateManagerTest, OnDeviceChanged_001, testing::ext::TestSize.Level0)
114 {
115 std::string pkgName = "111";
116 DmDeviceInfo info;
117 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
118 dmDeviceStateManager->OnDeviceChanged(pkgName, info);
119 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
120 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
121 DmDeviceInfo dminfo;
122 if (pReq == nullptr) {
123 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
124 } else {
125 dminfo = pReq->GetDeviceInfo();
126 }
127 int result = strcmp(info.deviceId, dminfo.deviceId);
128 EXPECT_EQ(result, 0);
129 }
130
131 /**
132 * @tc.name: OnDeviceReady_001
133 * @tc.desc: set info.deviceId to some para, and return it
134 * @tc.type: FUNC
135 * @tc.require: AR000GHSJK
136 */
137 HWTEST_F(DmDeviceStateManagerTest, OnDbReady_001, testing::ext::TestSize.Level0)
138 {
139 std::string pkgName;
140 std::string deviceId;
141 DmDeviceInfo info;
142 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
143 dmDeviceStateManager->OnDbReady(pkgName, deviceId);
144 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
145 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
146 DmDeviceInfo dminfo;
147 if (pReq == nullptr) {
148 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
149 } else {
150 dminfo = pReq->GetDeviceInfo();
151 }
152 int result = strcmp(info.deviceId, dminfo.deviceId);
153 EXPECT_EQ(result, 0);
154 }
155
156 /**
157 * @tc.name: OnDeviceReady_001
158 * @tc.desc: set info.deviceId to 123, and call OnDeviceReady ,change info.deviceId to 4
159 * @tc.type: FUNC
160 * @tc.require: AR000GHSJK
161 */
162 HWTEST_F(DmDeviceStateManagerTest, OnDeviceReady_001, testing::ext::TestSize.Level0)
163 {
164 std::string pkgName;
165 DmDeviceInfo info;
166 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
167 dmDeviceStateManager->OnDeviceReady(pkgName, info);
168 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
169 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
170 DmDeviceInfo dminfo;
171 if (pReq == nullptr) {
172 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
173 } else {
174 dminfo = pReq->GetDeviceInfo();
175 }
176 int result = strcmp(info.deviceId, dminfo.deviceId);
177 EXPECT_EQ(result, 0);
178 }
179
180 /**
181 * @tc.name: OnDeviceChanged_002
182 * @tc.desc: set info.deviceId to 123, and call OnDeviceChanged ,change info.deviceId to 4
183 * @tc.type: FUNC
184 * @tc.require: AR000GHSJK
185 */
186 HWTEST_F(DmDeviceStateManagerTest, OnDeviceChanged_002, testing::ext::TestSize.Level0)
187 {
188 std::string pkgName;
189 DmDeviceInfo info;
190 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
191 dmDeviceStateManager->OnDeviceChanged(pkgName, info);
192 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
193 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
194 DmDeviceInfo dminfo;
195 if (pReq == nullptr) {
196 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
197 } else {
198 dminfo = pReq->GetDeviceInfo();
199 }
200 int result = strcmp(info.deviceId, dminfo.deviceId);
201 EXPECT_EQ(result, 0);
202 }
203
204 /**
205 * @tc.name: RegisterSoftbusStateCallback_001
206 * @tc.desc: call RegisterSoftbusStateCallback and return DM_OK
207 * @tc.type: FUNC
208 * @tc.require: AR000GHSJK
209 */
210 HWTEST_F(DmDeviceStateManagerTest, RegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0)
211 {
212 int ret = dmDeviceStateManager->RegisterSoftbusStateCallback();
213 EXPECT_EQ(ret, DM_OK);
214 }
215
216 /**
217 * @tc.name: PostDeviceOnline_001
218 * @tc.desc: call PostDeviceOnline
219 * @tc.type: FUNC
220 * @tc.require: AR000GHSJK
221 */
222 HWTEST_F(DmDeviceStateManagerTest, PostDeviceOnline_001, testing::ext::TestSize.Level0)
223 {
224 std::string pkgName = "123";
225 DmDeviceInfo info = {
226 .deviceId = "123",
227 .deviceName = "asda",
228 .deviceTypeId = 1,
229 };
230 dmDeviceStateManager->PostDeviceOnline(pkgName, info);
231 }
232
233 /**
234 * @tc.name: PostDeviceOffline_001
235 * @tc.desc: call PostDeviceOffline
236 * @tc.type: FUNC
237 * @tc.require: AR000GHSJK
238 */
239 HWTEST_F(DmDeviceStateManagerTest, PostDeviceOffline_001, testing::ext::TestSize.Level0)
240 {
241 std::string pkgName = "123";
242 DmDeviceInfo info = {
243 .deviceId = "123",
244 .deviceName = "asda",
245 .deviceTypeId = 1,
246 };
247 dmDeviceStateManager->PostDeviceOffline(pkgName, info);
248 }
249
250 /**
251 * @tc.name: RegisterDevStateCallback_001
252 * @tc.desc: call RegisterDevStateCallback
253 * @tc.type: FUNC
254 * @tc.require: AR000GHSJK
255 */
256 HWTEST_F(DmDeviceStateManagerTest, RegisterDevStateCallback_001, testing::ext::TestSize.Level0)
257 {
258 std::string pkgName = "123";
259 std::string extra = "ahaha";
260 dmDeviceStateManager->RegisterDevStateCallback(pkgName, extra);
261 }
262
263 /**
264 * @tc.name: UnRegisterDevStateCallback_001
265 * @tc.desc: call UnRegisterDevStateCallback
266 * @tc.type: FUNC
267 * @tc.require: AR000GHSJK
268 */
269 HWTEST_F(DmDeviceStateManagerTest, UnRegisterDevStateCallback_001, testing::ext::TestSize.Level0)
270 {
271 std::string pkgName = "123";
272 std::string extra;
273 dmDeviceStateManager->UnRegisterDevStateCallback(pkgName, extra);
274 }
275
276 /**
277 * @tc.name: RegisterOffLineTimer_001
278 * @tc.desc: call RegisterOffLineTimer
279 * @tc.type: FUNC
280 * @tc.require: AR000GHSJK
281 */
282 HWTEST_F(DmDeviceStateManagerTest, RegisterOffLineTimer_001, testing::ext::TestSize.Level0)
283 {
284 DmDeviceInfo info = {
285 .deviceId = "123",
286 .deviceName = "asda",
287 .deviceTypeId = 1,
288 };
289 dmDeviceStateManager->RegisterOffLineTimer(info);
290 }
291
292 /**
293 * @tc.name: SaveOnlineDeviceInfo_001
294 * @tc.desc: call SaveOnlineDeviceInfo
295 * @tc.type: FUNC
296 */
297 HWTEST_F(DmDeviceStateManagerTest, SaveOnlineDeviceInfo_001, testing::ext::TestSize.Level0)
298 {
299 std::string pkgName = "123";
300 DmDeviceInfo info = {
301 .deviceId = "123",
302 .deviceName = "asda",
303 .deviceTypeId = 1,
304 };
305 dmDeviceStateManager->SaveOnlineDeviceInfo(pkgName, info);
306 }
307
308 /**
309 * @tc.name: DeleteOfflineDeviceInfo_001
310 * @tc.desc: call DeleteOfflineDeviceInfo
311 * @tc.type: FUNC
312 */
313 HWTEST_F(DmDeviceStateManagerTest, DeleteOfflineDeviceInfo_001, testing::ext::TestSize.Level0)
314 {
315 std::string pkgName = "123";
316 DmDeviceInfo info = {
317 .deviceId = "123",
318 .deviceName = "asda",
319 .deviceTypeId = 1,
320 };
321 dmDeviceStateManager->DeleteOfflineDeviceInfo(pkgName, info);
322 }
323 } // namespace
324 } // namespace DistributedHardware
325 } // namespace OHOS
326