1 /*
2 * Copyright (c) 2022-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 "distributed_ability_manager_service_test.h"
16
17 #include <thread>
18 #include "distributed_sched_util.h"
19 #include "dtbschedmgr_log.h"
20 #define private public
21 #include "continuation_extra_params.h"
22 #include "continuation_manager/app_connection_stub.h"
23 #include "continuation_manager/notifier_info.h"
24 #include "mission/distributed_sched_mission_manager.h"
25 #undef private
26 #include "test_log.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30
31 namespace OHOS {
32 namespace DistributedSchedule {
33 namespace {
34 constexpr int32_t REGISTER = 500;
35 constexpr int32_t UPDATE_CONNECT_STATUS = 504;
36 constexpr int32_t INVALID_CODE = -1;
37 constexpr int32_t MAX_REGISTER_NUM = 600;
38 constexpr int32_t MAX_WAIT_TIME = 10000;
39 constexpr uint32_t TEST_UINT32_T = 0;
40 const std::u16string DMS_STUB_INTERFACE_TOKEN = u"OHOS.DistributedSchedule.IDistributedAbilityManager";
41 const std::u16string TEST_U16STRING = u"test u16string";
42 const std::string DEVICE_ID = "testDeviceId";
43 }
44
45 sptr<DistributedAbilityManagerService> DistributedAbilityManagerServiceTest::dtbabilitymgrService_;
46 int32_t DistributedAbilityManagerServiceTest::startTaskNum_ = 1;
47 std::mutex DistributedAbilityManagerServiceTest::caseDoneLock_;
48 std::condition_variable DistributedAbilityManagerServiceTest::caseDoneCondition_;
49
OnDeviceConnect(const std::vector<ContinuationResult> & continuationResults)50 void DeviceSelectionNotifierTest::OnDeviceConnect(const std::vector<ContinuationResult>& continuationResults)
51 {
52 }
53
OnDeviceDisconnect(const std::vector<ContinuationResult> & continuationResults)54 void DeviceSelectionNotifierTest::OnDeviceDisconnect(const std::vector<ContinuationResult>& continuationResults)
55 {
56 }
57
SetUpTestCase()58 void DistributedAbilityManagerServiceTest::SetUpTestCase()
59 {
60 dtbabilitymgrService_ = new DistributedAbilityManagerService();
61 DTEST_LOG << "DistributedAbilityManagerServiceTest::SetUpTestCase" << std::endl;
62 }
63
TearDownTestCase()64 void DistributedAbilityManagerServiceTest::TearDownTestCase()
65 {
66 // Wait until all async tasks are completed before exiting the test suite
67 auto caseDoneNotifyTask = []() {
68 std::lock_guard<std::mutex> autoLock(caseDoneLock_);
69 --startTaskNum_;
70 caseDoneCondition_.notify_one();
71 };
72 if (dtbabilitymgrService_->continuationHandler_ != nullptr) {
73 dtbabilitymgrService_->continuationHandler_->submit(caseDoneNotifyTask);
74 }
75 std::unique_lock<std::mutex> lock(caseDoneLock_);
76 caseDoneCondition_.wait_for(lock, std::chrono::milliseconds(MAX_WAIT_TIME),
77 [&] () { return startTaskNum_ == 0; });
78 DTEST_LOG << "DistributedAbilityManagerServiceTest::TearDownTestCase" << std::endl;
79 }
80
SetUp()81 void DistributedAbilityManagerServiceTest::SetUp()
82 {
83 DTEST_LOG << "DistributedAbilityManagerServiceTest::SetUp" << std::endl;
84 }
85
TearDown()86 void DistributedAbilityManagerServiceTest::TearDown()
87 {
88 DTEST_LOG << "DistributedAbilityManagerServiceTest::TearDown" << std::endl;
89 }
90
91 /**
92 * @tc.name: Dump_001
93 * @tc.desc: test Dump when args is not empty.
94 * @tc.type: FUNC
95 * @tc.require: I6NMR8
96 */
97 HWTEST_F(DistributedAbilityManagerServiceTest, Dump_001, TestSize.Level3)
98 {
99 DTEST_LOG << "DistributedAbilityManagerServiceTest Dump_001 start" << std::endl;
100 if (dtbabilitymgrService_ == nullptr) {
101 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
102 return;
103 }
104 /**
105 * @tc.steps: step1. test Dump when args is not empty.
106 */
107 std::vector<std::u16string> args;
108 args.push_back(TEST_U16STRING);
109 int32_t result = dtbabilitymgrService_->Dump(INVALID_CODE, args);
110 EXPECT_EQ(result, DMS_WRITE_FILE_FAILED_ERR);
111 /**
112 * @tc.steps: step2. test DumpAppRegisterInfo when tokenMap_ is not empty.
113 */
114 std::string info;
115 std::vector<int32_t> tokenMapValue;
116 {
117 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->tokenMapMutex_);
118 dtbabilitymgrService_->tokenMap_.clear();
119 tokenMapValue.push_back(INVALID_CODE);
120 dtbabilitymgrService_->tokenMap_[TEST_UINT32_T] = tokenMapValue;
121 }
122 dtbabilitymgrService_->DumpAppRegisterInfo(info);
123 {
124 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->tokenMapMutex_);
125 dtbabilitymgrService_->tokenMap_.clear();
126 }
127 DTEST_LOG << "DistributedAbilityManagerServiceTest Dump_001 end" << std::endl;
128 }
129
130 /**
131 * @tc.name: OnRemoteRequest_001
132 * @tc.desc: test OnRemoteRequest to start Register
133 * @tc.type: FUNC
134 * @tc.require: I5NOA1
135 */
136 HWTEST_F(DistributedAbilityManagerServiceTest, OnRemoteRequest_001, TestSize.Level3)
137 {
138 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_001 start" << std::endl;
139 if (dtbabilitymgrService_ == nullptr) {
140 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
141 return;
142 }
143 MessageParcel data;
144 MessageParcel reply;
145 MessageOption option;
146 data.WriteInterfaceToken(DMS_STUB_INTERFACE_TOKEN);
147 int32_t result = dtbabilitymgrService_->OnRemoteRequest(REGISTER, data, reply, option);
148 EXPECT_EQ(DMS_PERMISSION_DENIED, result);
149 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_001 end" << std::endl;
150 }
151
152 /**
153 * @tc.name: OnRemoteRequest_002
154 * @tc.desc: test OnRemoteRequest with invalid code
155 * @tc.type: FUNC
156 * @tc.require: I5NOA1
157 */
158 HWTEST_F(DistributedAbilityManagerServiceTest, OnRemoteRequest_002, TestSize.Level3)
159 {
160 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_002 start" << std::endl;
161 if (dtbabilitymgrService_ == nullptr) {
162 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
163 return;
164 }
165 MessageParcel data;
166 MessageParcel reply;
167 MessageOption option;
168 data.WriteInterfaceToken(DMS_STUB_INTERFACE_TOKEN);
169 int32_t result = dtbabilitymgrService_->OnRemoteRequest(INVALID_CODE, data, reply, option);
170 EXPECT_NE(ERR_NONE, result);
171 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_002 end" << std::endl;
172 }
173
174 /**
175 * @tc.name: OnRemoteRequest_003
176 * @tc.desc: test OnRemoteRequest to start funcsMap_ without token
177 * @tc.type: FUNC
178 * @tc.require: I5NOA1
179 */
180 HWTEST_F(DistributedAbilityManagerServiceTest, OnRemoteRequest_003, TestSize.Level3)
181 {
182 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_003 start" << std::endl;
183 if (dtbabilitymgrService_ == nullptr) {
184 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
185 return;
186 }
187 MessageParcel data;
188 MessageParcel reply;
189 MessageOption option;
190 int32_t result = dtbabilitymgrService_->OnRemoteRequest(UPDATE_CONNECT_STATUS, data, reply, option);
191 EXPECT_EQ(DMS_PERMISSION_DENIED, result);
192 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_003 end" << std::endl;
193 }
194
195 /**
196 * @tc.name: OnRemoteRequest_004
197 * @tc.desc: test OnRemoteRequest to start funcsMap_ without DATASYNC permission
198 * @tc.type: FUNC
199 * @tc.require: I5NOA1
200 */
201 HWTEST_F(DistributedAbilityManagerServiceTest, OnRemoteRequest_004, TestSize.Level3)
202 {
203 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_004 start" << std::endl;
204 if (dtbabilitymgrService_ == nullptr) {
205 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
206 return;
207 }
208 MessageParcel data;
209 MessageParcel reply;
210 MessageOption option;
211 data.WriteInterfaceToken(DMS_STUB_INTERFACE_TOKEN);
212 int32_t result = dtbabilitymgrService_->OnRemoteRequest(UPDATE_CONNECT_STATUS, data, reply, option);
213 EXPECT_EQ(DMS_PERMISSION_DENIED, result);
214 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_004 end" << std::endl;
215 }
216
217 /**
218 * @tc.name: OnRemoteRequest_005
219 * @tc.desc: test OnRemoteRequest to start funcsMap_ with DATASYNC permission
220 * @tc.type: FUNC
221 */
222 HWTEST_F(DistributedAbilityManagerServiceTest, OnRemoteRequest_005, TestSize.Level3)
223 {
224 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_005 start" << std::endl;
225 if (dtbabilitymgrService_ == nullptr) {
226 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
227 return;
228 }
229 DistributedSchedUtil::MockPermission();
230 MessageParcel data;
231 MessageParcel reply;
232 MessageOption option;
233 data.WriteInterfaceToken(DMS_STUB_INTERFACE_TOKEN);
234 int32_t result = dtbabilitymgrService_->OnRemoteRequest(UPDATE_CONNECT_STATUS, data, reply, option);
235 EXPECT_NE(ERR_NONE, result);
236 DTEST_LOG << "DistributedAbilityManagerServiceTest OnRemoteRequest_005 end" << std::endl;
237 }
238
239 /**
240 * @tc.name: Register_001
241 * @tc.desc: test Register when continuationExtraParams != nullptr.
242 * @tc.type: FUNC
243 * @tc.require: I6NMR8
244 */
245 HWTEST_F(DistributedAbilityManagerServiceTest, Register_001, TestSize.Level3)
246 {
247 DTEST_LOG << "DistributedAbilityManagerServiceTest Register_001 start" << std::endl;
248 if (dtbabilitymgrService_ == nullptr) {
249 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
250 return;
251 }
252 /**
253 * @tc.steps: step1. test DumpNotifierLocked when callbackMap_ is not empty.
254 */
255 std::vector<int32_t> tokenVec;
256 std::string info;
257 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
258 {
259 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
260 dtbabilitymgrService_->callbackMap_.clear();
261 dtbabilitymgrService_->callbackMap_[INVALID_CODE] = std::move(notifierInfo);;
262 }
263 tokenVec.push_back(INVALID_CODE);
264 dtbabilitymgrService_->DumpNotifierLocked(tokenVec, info);
265 /**
266 * @tc.steps: step2. test Register when register max num.
267 */
268 int32_t token = 0;
269 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
270 for (int32_t i = 0; i < MAX_REGISTER_NUM; i++) {
271 dtbabilitymgrService_->Register(continuationExtraParams, token);
272 }
273 int32_t ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
274 EXPECT_EQ(ret, REGISTER_EXCEED_MAX_TIMES);
275 {
276 std::lock_guard<std::mutex> tokenLock(dtbabilitymgrService_->tokenMutex_);
277 dtbabilitymgrService_->tokenMap_.clear();
278 }
279 DTEST_LOG << "DistributedAbilityManagerServiceTest Register_001 end" << std::endl;
280 }
281
282 /**
283 * @tc.name: Unregister_001
284 * @tc.desc: test Unregister
285 * @tc.type: FUNC
286 * @tc.require: I5NOA1
287 */
288 HWTEST_F(DistributedAbilityManagerServiceTest, Unregister_001, TestSize.Level3)
289 {
290 DTEST_LOG << "DistributedAbilityManagerServiceTest Unregister_001 start" << std::endl;
291 if (dtbabilitymgrService_ == nullptr) {
292 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
293 return;
294 }
295 /**
296 * @tc.steps: step1. test Unregister when token is not registered.
297 */
298 int32_t ret = dtbabilitymgrService_->Unregister(INVALID_CODE);
299 EXPECT_EQ(ret, TOKEN_HAS_NOT_REGISTERED);
300 /**
301 * @tc.steps: step2. test Unregister when notifier is not registered.
302 */
303 {
304 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
305 dtbabilitymgrService_->callbackMap_.clear();
306 }
307 int32_t token = 0;
308 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
309 ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
310 EXPECT_EQ(ret, ERR_OK);
311 ret = dtbabilitymgrService_->Unregister(token);
312 EXPECT_EQ(ret, ERR_OK);
313 /**
314 * @tc.steps: step3. test Unregister when notifier is registered.
315 */
316 ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
317 EXPECT_EQ(ret, ERR_OK);
318 {
319 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
320 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
321 dtbabilitymgrService_->callbackMap_[token] = std::move(notifierInfo);;
322 }
323 ret = dtbabilitymgrService_->Unregister(token);
324 EXPECT_EQ(ret, ERR_OK);
325 {
326 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
327 dtbabilitymgrService_->callbackMap_.clear();
328 }
329 DTEST_LOG << "DistributedAbilityManagerServiceTest Unregister_001 end" << std::endl;
330 }
331
332 /**
333 * @tc.name: Unregister_002
334 * @tc.desc: test Unregister when iter->second is not empty.
335 * @tc.type: FUNC
336 * @tc.require: I5NOA1
337 */
338 HWTEST_F(DistributedAbilityManagerServiceTest, Unregister_002, TestSize.Level3)
339 {
340 DTEST_LOG << "DistributedAbilityManagerServiceTest Unregister_002 start" << std::endl;
341 if (dtbabilitymgrService_ == nullptr) {
342 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
343 return;
344 }
345 /**
346 * @tc.steps: step1. test Unregister when iter->second is not empty.
347 */
348 int32_t token = 0;
349 int32_t tokenBackup = 0;
350 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
351 int32_t ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
352 EXPECT_EQ(ret, ERR_OK);
353 ret = dtbabilitymgrService_->Register(continuationExtraParams, tokenBackup);
354 EXPECT_EQ(ret, ERR_OK);
355 {
356 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
357 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
358 dtbabilitymgrService_->callbackMap_[token] = std::move(notifierInfo);;
359 }
360 ret = dtbabilitymgrService_->Unregister(token);
361 EXPECT_EQ(ret, ERR_OK);
362 ret = dtbabilitymgrService_->Unregister(tokenBackup);
363 EXPECT_EQ(ret, ERR_OK);
364 {
365 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
366 dtbabilitymgrService_->callbackMap_.clear();
367 }
368 DTEST_LOG << "DistributedAbilityManagerServiceTest Unregister_002 end" << std::endl;
369 }
370
371 /**
372 * @tc.name: RegisterDeviceSelectionCallback_001
373 * @tc.desc: test RegisterDeviceSelectionCallback
374 * @tc.type: FUNC
375 * @tc.require: I5NOA1
376 */
377 HWTEST_F(DistributedAbilityManagerServiceTest, RegisterDeviceSelectionCallback_001, TestSize.Level3)
378 {
379 DTEST_LOG << "DistributedAbilityManagerServiceTest RegisterDeviceSelectionCallback_001 start" << std::endl;
380 if (dtbabilitymgrService_ == nullptr) {
381 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
382 return;
383 }
384 /**
385 * @tc.steps: step1. test RegisterDeviceSelectionCallback when cbType is invalid.
386 */
387 int32_t ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(INVALID_CODE, "", nullptr);
388 EXPECT_EQ(ret, UNKNOWN_CALLBACK_TYPE);
389 /**
390 * @tc.steps: step2. test RegisterDeviceSelectionCallback when cbType has registered.
391 */
392 int32_t token = 0;
393 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
394 ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
395 EXPECT_EQ(ret, ERR_OK);
396 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
397 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_CONNECT, notifier);
398 EXPECT_EQ(ret, ERR_OK);
399 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_CONNECT, notifier);
400 EXPECT_EQ(ret, CALLBACK_HAS_REGISTERED);
401
402 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_CONNECT);
403 EXPECT_EQ(ret, ERR_OK);
404 ret = dtbabilitymgrService_->Unregister(token);
405 EXPECT_EQ(ret, ERR_OK);
406 DTEST_LOG << "DistributedAbilityManagerServiceTest RegisterDeviceSelectionCallback_001 end" << std::endl;
407 }
408
409 /**
410 * @tc.name: RegisterDeviceSelectionCallback_002
411 * @tc.desc: test RegisterDeviceSelectionCallback
412 * @tc.type: FUNC
413 * @tc.require: I5NOA1
414 */
415 HWTEST_F(DistributedAbilityManagerServiceTest, RegisterDeviceSelectionCallback_002, TestSize.Level3)
416 {
417 DTEST_LOG << "DistributedAbilityManagerServiceTest RegisterDeviceSelectionCallback_002 start" << std::endl;
418 if (dtbabilitymgrService_ == nullptr) {
419 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
420 return;
421 }
422 /**
423 * @tc.steps: step1. test RegisterDeviceSelectionCallback when iter->second != nullptr.
424 */
425 int32_t token = 0;
426 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
427 int32_t ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
428 EXPECT_EQ(ret, ERR_OK);
429 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
430 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_CONNECT, notifier);
431 EXPECT_EQ(ret, ERR_OK);
432 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_DISCONNECT, notifier);
433 EXPECT_EQ(ret, ERR_OK);
434
435 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_CONNECT);
436 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_DISCONNECT);
437 EXPECT_EQ(ret, ERR_OK);
438 ret = dtbabilitymgrService_->Unregister(token);
439 EXPECT_EQ(ret, ERR_OK);
440 DTEST_LOG << "DistributedAbilityManagerServiceTest RegisterDeviceSelectionCallback_002 end" << std::endl;
441 }
442
443 /**
444 * @tc.name: RegisterDeviceSelectionCallback_003
445 * @tc.desc: test RegisterDeviceSelectionCallback
446 * @tc.type: FUNC
447 * @tc.require: I5NOA1
448 */
449 HWTEST_F(DistributedAbilityManagerServiceTest, RegisterDeviceSelectionCallback_003, TestSize.Level3)
450 {
451 DTEST_LOG << "DistributedAbilityManagerServiceTest RegisterDeviceSelectionCallback_002 start" << std::endl;
452 if (dtbabilitymgrService_ == nullptr) {
453 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
454 return;
455 }
456 /**
457 * @tc.steps: step1. test RegisterDeviceSelectionCallback when iter->second != nullptr.
458 */
459 int32_t token = 0;
460 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
461 int32_t ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
462 EXPECT_EQ(ret, ERR_OK);
463 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
464 {
465 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
466 dtbabilitymgrService_->callbackMap_[token] = nullptr;
467 }
468 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_CONNECT, notifier);
469 EXPECT_EQ(ret, ERR_NULL_OBJECT);
470 DTEST_LOG << "DistributedAbilityManagerServiceTest RegisterDeviceSelectionCallback_003 end" << std::endl;
471 }
472
473 /**
474 * @tc.name: IsTokenRegistered_001
475 * @tc.desc: test IsTokenRegistered
476 * @tc.type: FUNC
477 * @tc.require: I5NOA1
478 */
479 HWTEST_F(DistributedAbilityManagerServiceTest, IsTokenRegistered_001, TestSize.Level3)
480 {
481 DTEST_LOG << "DistributedAbilityManagerServiceTest IsTokenRegistered_001 start" << std::endl;
482 if (dtbabilitymgrService_ == nullptr) {
483 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
484 return;
485 }
486 /**
487 * @tc.steps: step1. test RegisterDeviceSelectionCallback when iter->second != nullptr.
488 */
489 int32_t token = 0;
490 {
491 std::lock_guard<std::mutex> tokenMapLock(dtbabilitymgrService_->tokenMapMutex_);
492 dtbabilitymgrService_->tokenMap_.clear();
493 std::vector<int32_t> vec;
494 for (int32_t i = 0; i < MAX_REGISTER_NUM; ++i) {
495 vec.push_back(INVALID_CODE);
496 }
497 dtbabilitymgrService_->tokenMap_[TEST_UINT32_T] = vec;
498 }
499 int32_t ret = dtbabilitymgrService_->IsTokenRegistered(TEST_UINT32_T, token);
500 EXPECT_EQ(ret, ERR_OK);
501 DTEST_LOG << "DistributedAbilityManagerServiceTest IsTokenRegistered_001 end" << std::endl;
502 }
503
504 /**
505 * @tc.name: UnregisterDeviceSelectionCallback_001
506 * @tc.desc: test UnregisterDeviceSelectionCallback
507 * @tc.type: FUNC
508 * @tc.require: I5NOA1
509 */
510 HWTEST_F(DistributedAbilityManagerServiceTest, UnregisterDeviceSelectionCallback_001, TestSize.Level3)
511 {
512 DTEST_LOG << "DistributedAbilityManagerServiceTest UnregisterDeviceSelectionCallback_001 start" << std::endl;
513 if (dtbabilitymgrService_ == nullptr) {
514 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
515 return;
516 }
517 /**
518 * @tc.steps: step1. test UnregisterDeviceSelectionCallback when cbType is invalid.
519 */
520 int32_t ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(INVALID_CODE, "");
521 EXPECT_EQ(ret, UNKNOWN_CALLBACK_TYPE);
522 /**
523 * @tc.steps: step2. test UnregisterDeviceSelectionCallback when cbType == EVENT_CONNECT.
524 */
525 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(INVALID_CODE, EVENT_CONNECT);
526 EXPECT_EQ(ret, TOKEN_HAS_NOT_REGISTERED);
527 /**
528 * @tc.steps: step3. test UnregisterDeviceSelectionCallback when cbType == EVENT_DISCONNECT.
529 */
530 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(INVALID_CODE, EVENT_DISCONNECT);
531 EXPECT_EQ(ret, TOKEN_HAS_NOT_REGISTERED);
532 /**
533 * @tc.steps: step4. test UnregisterDeviceSelectionCallback when token is registered.
534 */
535 int32_t token = 0;
536 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
537 ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
538 EXPECT_EQ(ret, ERR_OK);
539 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_CONNECT);
540 EXPECT_EQ(ret, CALLBACK_HAS_NOT_REGISTERED);
541 /**
542 * @tc.steps: step5. test UnregisterDeviceSelectionCallback when callback is registered.
543 */
544 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
545 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_CONNECT, notifier);
546 EXPECT_EQ(ret, ERR_OK);
547
548 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_CONNECT);
549 EXPECT_EQ(ret, ERR_OK);
550 ret = dtbabilitymgrService_->Unregister(token);
551 EXPECT_EQ(ret, ERR_OK);
552 DTEST_LOG << "DistributedAbilityManagerServiceTest UnregisterDeviceSelectionCallback_001 end" << std::endl;
553 }
554
555 /**
556 * @tc.name: UnregisterDeviceSelectionCallback_002
557 * @tc.desc: test UnregisterDeviceSelectionCallback when callback is registered.
558 * @tc.type: FUNC
559 * @tc.require: I5NOA1
560 */
561 HWTEST_F(DistributedAbilityManagerServiceTest, UnregisterDeviceSelectionCallback_002, TestSize.Level3)
562 {
563 DTEST_LOG << "DistributedAbilityManagerServiceTest UnregisterDeviceSelectionCallback_002 start" << std::endl;
564 if (dtbabilitymgrService_ == nullptr) {
565 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
566 return;
567 }
568 /**
569 * @tc.steps: step1. test UnregisterDeviceSelectionCallback when callback is registered.
570 */
571 int32_t token = 0;
572 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
573 int32_t ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
574 EXPECT_EQ(ret, ERR_OK);
575 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
576 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_CONNECT, notifier);
577 EXPECT_EQ(ret, ERR_OK);
578 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_DISCONNECT, notifier);
579 EXPECT_EQ(ret, ERR_OK);
580
581 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_CONNECT);
582 EXPECT_EQ(ret, ERR_OK);
583 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_DISCONNECT);
584 EXPECT_EQ(ret, ERR_OK);
585 ret = dtbabilitymgrService_->Unregister(token);
586 EXPECT_EQ(ret, ERR_OK);
587 DTEST_LOG << "DistributedAbilityManagerServiceTest UnregisterDeviceSelectionCallback_002 end" << std::endl;
588 }
589
590 /**
591 * @tc.name: UpdateConnectStatus_001
592 * @tc.desc: test UpdateConnectStatus
593 * @tc.type: FUNC
594 * @tc.require: I5NOA1
595 */
596 HWTEST_F(DistributedAbilityManagerServiceTest, UpdateConnectStatus_001, TestSize.Level3)
597 {
598 DTEST_LOG << "DistributedAbilityManagerServiceTest UpdateConnectStatus_001 start" << std::endl;
599 if (dtbabilitymgrService_ == nullptr) {
600 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
601 return;
602 }
603 /**
604 * @tc.steps: step1. test UpdateConnectStatus when deviceId is empty.
605 */
606 int32_t ret = dtbabilitymgrService_->UpdateConnectStatus(INVALID_CODE, "", DeviceConnectStatus::IDLE);
607 EXPECT_EQ(ret, ERR_NULL_OBJECT);
608 /**
609 * @tc.steps: step2. test UpdateConnectStatus when token is not registered.
610 */
611 ret = dtbabilitymgrService_->UpdateConnectStatus(INVALID_CODE, DEVICE_ID, DeviceConnectStatus::IDLE);
612 EXPECT_EQ(ret, TOKEN_HAS_NOT_REGISTERED);
613 /**
614 * @tc.steps: step3. test UpdateConnectStatus when callback is not registered.
615 */
616 int32_t token = 0;
617 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
618 ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
619 EXPECT_EQ(ret, ERR_OK);
620 ret = dtbabilitymgrService_->UpdateConnectStatus(token, DEVICE_ID, DeviceConnectStatus::IDLE);
621 EXPECT_EQ(ret, CALLBACK_HAS_NOT_REGISTERED);
622 /**
623 * @tc.steps: step4. test UpdateConnectStatus when callback is registered.
624 */
625 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
626 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_CONNECT, notifier);
627 EXPECT_EQ(ret, ERR_OK);
628 ret = dtbabilitymgrService_->UpdateConnectStatus(token, DEVICE_ID, DeviceConnectStatus::IDLE);
629 EXPECT_EQ(ret, ERR_OK);
630 /**
631 * @tc.steps: step5. test UpdateConnectStatus when appProxy_ is nullptr.
632 */
633 {
634 std::lock_guard<std::mutex> appProxyLock(dtbabilitymgrService_->appProxyMutex_);
635 dtbabilitymgrService_->appProxy_ = nullptr;
636 }
637 ret = dtbabilitymgrService_->UpdateConnectStatus(token, DEVICE_ID, DeviceConnectStatus::IDLE);
638 EXPECT_EQ(ret, ERR_OK);
639
640 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_CONNECT);
641 EXPECT_EQ(ret, ERR_OK);
642 ret = dtbabilitymgrService_->Unregister(token);
643 EXPECT_EQ(ret, ERR_OK);
644 DTEST_LOG << "DistributedAbilityManagerServiceTest UpdateConnectStatus_001 end" << std::endl;
645 }
646
647 /**
648 * @tc.name: ConnectAbility
649 * @tc.desc: test ConnectAbility
650 * @tc.type: FUNC
651 */
652 HWTEST_F(DistributedAbilityManagerServiceTest, ConnectAbility_001, TestSize.Level3)
653 {
654 DTEST_LOG << "DistributedAbilityManagerServiceTest ConnectAbility_001 start" << std::endl;
655 if (dtbabilitymgrService_ == nullptr) {
656 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
657 return;
658 }
659 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
660 int32_t ret = dtbabilitymgrService_->ConnectAbility(1, continuationExtraParams);
661 EXPECT_NE(ret, true);
662 DTEST_LOG << "DistributedAbilityManagerServiceTest ConnectAbility_001 end" << std::endl;
663 }
664
665 /**
666 * @tc.name: OnDeviceDisconnect_001
667 * @tc.desc: test OnDeviceDisconnect
668 * @tc.type: FUNC
669 * @tc.require: I5NOA1
670 */
671 HWTEST_F(DistributedAbilityManagerServiceTest, OnDeviceDisconnect_001, TestSize.Level3)
672 {
673 DTEST_LOG << "DistributedAbilityManagerServiceTest OnDeviceDisconnect_001 start" << std::endl;
674 if (dtbabilitymgrService_ == nullptr) {
675 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
676 return;
677 }
678 /**
679 * @tc.steps: step1. test OnDeviceDisconnect when callback has not registered.
680 */
681 if (dtbabilitymgrService_->continuationHandler_ == nullptr) {
682 dtbabilitymgrService_->continuationHandler_ = std::make_shared<ffrt::queue>("ContinuationMgr");
683 }
684 std::vector<ContinuationResult> continuationResults;
685 int32_t ret = dtbabilitymgrService_->OnDeviceDisconnect(INVALID_CODE, continuationResults);
686 EXPECT_EQ(ret, CALLBACK_HAS_NOT_REGISTERED);
687 /**
688 * @tc.steps: step2. test OnDeviceDisconnect when callback has registered.
689 */
690 int32_t token = 0;
691 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
692 ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
693 EXPECT_EQ(ret, ERR_OK);
694 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
695 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_DISCONNECT, notifier);
696 EXPECT_EQ(ret, ERR_OK);
697 ret = dtbabilitymgrService_->OnDeviceDisconnect(token, continuationResults);
698 EXPECT_EQ(ret, ERR_OK);
699
700 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_DISCONNECT);
701 EXPECT_EQ(ret, ERR_OK);
702 ret = dtbabilitymgrService_->Unregister(token);
703 EXPECT_EQ(ret, ERR_OK);
704 DTEST_LOG << "DistributedAbilityManagerServiceTest OnDeviceDisconnect_001 end" << std::endl;
705 }
706
707 /**
708 * @tc.name: IsExceededRegisterMaxNum_001
709 * @tc.desc: test IsExceededRegisterMaxNum when IsExceededRegisterMaxNum returns true.
710 * @tc.type: FUNC
711 * @tc.require: I5NOA1
712 */
713 HWTEST_F(DistributedAbilityManagerServiceTest, IsExceededRegisterMaxNum_001, TestSize.Level3)
714 {
715 DTEST_LOG << "DistributedAbilityManagerServiceTest IsExceededRegisterMaxNum_001 start" << std::endl;
716 if (dtbabilitymgrService_ == nullptr) {
717 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
718 return;
719 }
720 {
721 std::lock_guard<std::mutex> tokenMapLock(dtbabilitymgrService_->tokenMapMutex_);
722 dtbabilitymgrService_->tokenMap_.clear();
723 std::vector<int32_t> vec;
724 for (int32_t i = 0; i < MAX_REGISTER_NUM; ++i) {
725 vec.push_back(INVALID_CODE);
726 }
727 dtbabilitymgrService_->tokenMap_[TEST_UINT32_T] = vec;
728 }
729 bool ret = dtbabilitymgrService_->IsExceededRegisterMaxNum(TEST_UINT32_T);
730 EXPECT_EQ(ret, true);
731 DTEST_LOG << "DistributedAbilityManagerServiceTest IsExceededRegisterMaxNum_001 end" << std::endl;
732 }
733
734 /**
735 * @tc.name: IsNotifierRegistered_001
736 * @tc.desc: test IsNotifierRegistered when callbackMap_[token] = nullptr.
737 * @tc.type: FUNC
738 * @tc.require: I5NOA1
739 */
740 HWTEST_F(DistributedAbilityManagerServiceTest, IsNotifierRegistered_001, TestSize.Level3)
741 {
742 DTEST_LOG << "DistributedAbilityManagerServiceTest IsNotifierRegistered_001 start" << std::endl;
743 if (dtbabilitymgrService_ == nullptr) {
744 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
745 return;
746 }
747 {
748 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
749 dtbabilitymgrService_->callbackMap_[INVALID_CODE] = nullptr;
750 }
751 bool ret = dtbabilitymgrService_->IsNotifierRegistered(INVALID_CODE);
752 EXPECT_EQ(ret, false);
753 DTEST_LOG << "DistributedAbilityManagerServiceTest IsNotifierRegistered_001 end" << std::endl;
754 }
755
756 /**
757 * @tc.name: HandleDeviceConnect_002
758 * @tc.desc: test HandleDeviceConnect when continuationHandler_ == nullptr.
759 * @tc.type: FUNC
760 * @tc.require: I5NOA1
761 */
762 HWTEST_F(DistributedAbilityManagerServiceTest, HandleDeviceConnect_002, TestSize.Level3)
763 {
764 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleDeviceConnect_002 start" << std::endl;
765 if (dtbabilitymgrService_ == nullptr) {
766 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
767 return;
768 }
769 dtbabilitymgrService_->continuationHandler_ = nullptr;
770 std::vector<ContinuationResult> continuationResults;
771 bool ret = dtbabilitymgrService_->HandleDeviceConnect(nullptr, continuationResults);
772 EXPECT_EQ(ret, false);
773 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleDeviceConnect_002 end" << std::endl;
774 }
775
776 /**
777 * @tc.name: HandleDeviceDisconnect
778 * @tc.desc: test HandleDeviceDisconnect
779 * @tc.type: FUNC
780 */
781 HWTEST_F(DistributedAbilityManagerServiceTest, HandleDeviceDisconnect_001, TestSize.Level3)
782 {
783 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleDeviceDisconnect_001 start" << std::endl;
784 if (dtbabilitymgrService_ == nullptr) {
785 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
786 return;
787 }
788 std::vector<ContinuationResult> continuationResults;
789 bool ret = dtbabilitymgrService_->HandleDeviceDisconnect(dtbabilitymgrService_, continuationResults);
790 EXPECT_NE(ret, true);
791 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
792 dtbabilitymgrService_->ScheduleStartDeviceManager(nullptr, 1, continuationExtraParams);
793 dtbabilitymgrService_->ScheduleStartDeviceManager(dtbabilitymgrService_, 1, continuationExtraParams);
794 dtbabilitymgrService_->HandleStartDeviceManager(1, continuationExtraParams);
795 dtbabilitymgrService_->HandleStartDeviceManager(1, nullptr);
796 if (dtbabilitymgrService_->continuationHandler_ == nullptr) {
797 dtbabilitymgrService_->continuationHandler_ = std::make_shared<ffrt::queue>("ContinuationMgr");
798 }
799 bool result = dtbabilitymgrService_->HandleDeviceConnect(dtbabilitymgrService_, continuationResults);
800 dtbabilitymgrService_->HandleStartDeviceManager(1, continuationExtraParams);
801 dtbabilitymgrService_->HandleStartDeviceManager(1, nullptr);
802 EXPECT_EQ(result, true);
803 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleDeviceDisconnect_001 end" << std::endl;
804 }
805
806 /**
807 * @tc.name: HandleDeviceDisconnect_002
808 * @tc.desc: test HandleDeviceDisconnect when continuationHandler_ != nullptr.
809 * @tc.type: FUNC
810 * @tc.require: I5NOA1
811 */
812 HWTEST_F(DistributedAbilityManagerServiceTest, HandleDeviceDisconnect_002, TestSize.Level3)
813 {
814 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleDeviceDisconnect_002 start" << std::endl;
815 if (dtbabilitymgrService_ == nullptr) {
816 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
817 return;
818 }
819 /**
820 * @tc.steps: step1. test HandleUpdateConnectStatus when continuationHandler_ is nullptr.
821 */
822 dtbabilitymgrService_->continuationHandler_ = nullptr;
823 dtbabilitymgrService_->HandleUpdateConnectStatus(INVALID_CODE, DEVICE_ID, DeviceConnectStatus::CONNECTED);
824 /**
825 * @tc.steps: step2. test HandleDeviceConnect when continuationHandler_ is not nullptr.
826 */
827 dtbabilitymgrService_->continuationHandler_ = std::make_shared<ffrt::queue>("ContinuationMgr");
828 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
829 std::vector<ContinuationResult> continuationResults;
830 bool ret = dtbabilitymgrService_->HandleDeviceDisconnect(notifier, continuationResults);
831 EXPECT_EQ(ret, true);
832 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleDeviceDisconnect_002 end" << std::endl;
833 }
834
835 /**
836 * @tc.name: HandleDeviceConnect
837 * @tc.desc: test HandleDeviceConnect
838 * @tc.type: FUNC
839 */
840 HWTEST_F(DistributedAbilityManagerServiceTest, HandleDeviceConnect_001, TestSize.Level3)
841 {
842 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleDeviceConnect_001 start" << std::endl;
843 if (dtbabilitymgrService_ == nullptr) {
844 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
845 return;
846 }
847 std::vector<ContinuationResult> continuationResults;
848 int32_t ret = dtbabilitymgrService_->HandleDeviceConnect(dtbabilitymgrService_, continuationResults);
849 EXPECT_EQ(ret, true);
850 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleDeviceConnect_001 end" << std::endl;
851 }
852
853 /**
854 * @tc.name: QueryTokenByNotifier_001
855 * @tc.desc: test QueryTokenByNotifier when iter->second == nullptr.
856 * @tc.type: FUNC
857 * @tc.require: I5NOA1
858 */
859 HWTEST_F(DistributedAbilityManagerServiceTest, QueryTokenByNotifier_001, TestSize.Level3)
860 {
861 DTEST_LOG << "DistributedAbilityManagerServiceTest QueryTokenByNotifier_001 start" << std::endl;
862 if (dtbabilitymgrService_ == nullptr) {
863 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
864 return;
865 }
866 /**
867 * @tc.steps: step1. test QueryTokenByNotifier when iter->second == nullptr.
868 */
869 {
870 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
871 dtbabilitymgrService_->callbackMap_.clear();
872 dtbabilitymgrService_->callbackMap_[INVALID_CODE] = nullptr;
873 }
874 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
875 int32_t token;
876 bool ret = dtbabilitymgrService_->QueryTokenByNotifier(notifier, token);
877 EXPECT_FALSE(ret);
878 {
879 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
880 dtbabilitymgrService_->callbackMap_.clear();
881 }
882 /**
883 * @tc.steps: step2. test ProcessNotifierDied when notifier != nullptr.
884 */
885 dtbabilitymgrService_->ProcessNotifierDied(notifier);
886 /**
887 * @tc.steps: step3. test HandleNotifierDied when continuationHandler_ == nullptr.
888 */
889 dtbabilitymgrService_->continuationHandler_ = nullptr;
890 dtbabilitymgrService_->HandleNotifierDied(nullptr);
891 /**
892 * @tc.steps: step4. test HandleNotifierDied when QueryTokenByNotifier returns true.
893 */
894 dtbabilitymgrService_->continuationHandler_ = std::make_shared<ffrt::queue>("ContinuationMgr");
895 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
896 notifierInfo->SetNotifier(EVENT_CONNECT, notifier);
897 {
898 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
899 dtbabilitymgrService_->callbackMap_[INVALID_CODE] = std::move(notifierInfo);;
900 }
901 dtbabilitymgrService_->HandleNotifierDied(notifier);
902 DTEST_LOG << "DistributedAbilityManagerServiceTest QueryTokenByNotifier_001 end" << std::endl;
903 }
904
905 /**
906 * @tc.name: HandleNotifierDied_001
907 * @tc.desc: test HandleNotifierDied
908 * @tc.type: FUNC
909 */
910 HWTEST_F(DistributedAbilityManagerServiceTest, HandleNotifierDied_001, TestSize.Level3)
911 {
912 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleNotifierDied_001 start" << std::endl;
913 if (dtbabilitymgrService_ == nullptr) {
914 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
915 return;
916 }
917 /**
918 * @tc.steps: step1. test HandleNotifierDied.
919 */
920 std::vector<ContinuationResult> continuationResults;
921 dtbabilitymgrService_->HandleNotifierDied(dtbabilitymgrService_);
922 /**
923 * @tc.steps: step2. test HandleNotifierDied when IsNotifierRegistered returns true.
924 */
925 int32_t token = 0;
926 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
927 int32_t res = dtbabilitymgrService_->Register(continuationExtraParams, token);
928 EXPECT_EQ(res, ERR_OK);
929 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
930 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
931 notifierInfo->SetNotifier(EVENT_CONNECT, notifier);
932 {
933 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
934 dtbabilitymgrService_->callbackMap_.clear();
935 dtbabilitymgrService_->callbackMap_[token] = std::move(notifierInfo);;
936 }
937 dtbabilitymgrService_->HandleNotifierDied(notifier);
938 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleNotifierDied_001 end" << std::endl;
939 }
940
941 /**
942 * @tc.name: OnDeviceCancel_001
943 * @tc.desc: test OnDeviceCancel
944 * @tc.type: FUNC
945 */
946 HWTEST_F(DistributedAbilityManagerServiceTest, OnDeviceCancel_001, TestSize.Level3)
947 {
948 DTEST_LOG << "DistributedAbilityManagerServiceTest OnDeviceCancel_001 start" << std::endl;
949 if (dtbabilitymgrService_ == nullptr) {
950 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
951 return;
952 }
953 int32_t ret = dtbabilitymgrService_->OnDeviceCancel();
954 EXPECT_NE(ret, true);
955 DTEST_LOG << "DistributedAbilityManagerServiceTest OnDeviceCancel_001 end" << std::endl;
956 }
957
958 /**
959 * @tc.name: HandleUpdateConnectStatus
960 * @tc.desc: test HandleUpdateConnectStatus
961 * @tc.type: FUNC
962 */
963 HWTEST_F(DistributedAbilityManagerServiceTest, HandleUpdateConnectStatus_001, TestSize.Level3)
964 {
965 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleUpdateConnectStatus_001 start" << std::endl;
966 if (dtbabilitymgrService_ == nullptr) {
967 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
968 return;
969 }
970 DeviceConnectStatus deviceConnectStatus = DeviceConnectStatus::CONNECTING;
971 dtbabilitymgrService_->HandleUpdateConnectStatus(1, DEVICE_ID, deviceConnectStatus);
972 if (dtbabilitymgrService_->continuationHandler_ == nullptr) {
973 dtbabilitymgrService_->continuationHandler_ = std::make_shared<ffrt::queue>("ContinuationMgr");
974 }
975 dtbabilitymgrService_->HandleUpdateConnectStatus(1, DEVICE_ID, deviceConnectStatus);
976 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleUpdateConnectStatus_001 end" << std::endl;
977 }
978
979 /**
980 * @tc.name: DumpNotifierLocked_002
981 * @tc.desc: test DumpNotifierLocked
982 * @tc.type: FUNC
983 * @tc.require: I76U22
984 */
985 HWTEST_F(DistributedAbilityManagerServiceTest, DumpNotifierLocked_002, TestSize.Level3)
986 {
987 DTEST_LOG << "DistributedAbilityManagerServiceTest DumpNotifierLocked_002 start" << std::endl;
988 if (dtbabilitymgrService_ == nullptr) {
989 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
990 return;
991 }
992 /**
993 * @tc.steps: step1. test DumpNotifierLocked when callbackMap_ is not empty.
994 */
995 std::vector<int32_t> tokenVec;
996 std::string info;
997 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
998 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
999 notifierInfo->SetNotifier(EVENT_CONNECT, notifier);
1000 {
1001 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
1002 dtbabilitymgrService_->callbackMap_.clear();
1003 dtbabilitymgrService_->callbackMap_[INVALID_CODE] = std::move(notifierInfo);;
1004 }
1005 tokenVec.push_back(INVALID_CODE);
1006 dtbabilitymgrService_->DumpNotifierLocked(tokenVec, info);
1007 EXPECT_NE(dtbabilitymgrService_, nullptr);
1008 {
1009 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
1010 dtbabilitymgrService_->callbackMap_.clear();
1011 }
1012 DTEST_LOG << "DistributedAbilityManagerServiceTest DumpNotifierLocked_002 end" << std::endl;
1013 }
1014
1015 /**
1016 * @tc.name: Register_002
1017 * @tc.desc: test Register when continuationExtraParams != nullptr.
1018 * @tc.type: FUNC
1019 * @tc.require: I76U22
1020 */
1021 HWTEST_F(DistributedAbilityManagerServiceTest, Register_002, TestSize.Level3)
1022 {
1023 DTEST_LOG << "DistributedAbilityManagerServiceTest Register_002 start" << std::endl;
1024 if (dtbabilitymgrService_ == nullptr) {
1025 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1026 return;
1027 }
1028
1029 int32_t token = 0;
1030 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
1031 ContinuationMode continuationMode = static_cast<ContinuationMode>(-1);
1032 continuationExtraParams->SetContinuationMode(continuationMode);
1033 int32_t ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
1034 EXPECT_EQ(ret, INVALID_CONTINUATION_MODE);
1035 {
1036 std::lock_guard<std::mutex> tokenLock(dtbabilitymgrService_->tokenMutex_);
1037 dtbabilitymgrService_->tokenMap_.clear();
1038 }
1039 DTEST_LOG << "DistributedAbilityManagerServiceTest Register_002 end" << std::endl;
1040 }
1041
1042 /**
1043 * @tc.name: UpdateConnectStatus_002
1044 * @tc.desc: test UpdateConnectStatus
1045 * @tc.type: FUNC
1046 * @tc.require: I76U22
1047 */
1048 HWTEST_F(DistributedAbilityManagerServiceTest, UpdateConnectStatus_002, TestSize.Level3)
1049 {
1050 DTEST_LOG << "DistributedAbilityManagerServiceTest UpdateConnectStatus_002 start" << std::endl;
1051 if (dtbabilitymgrService_ == nullptr) {
1052 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1053 return;
1054 }
1055
1056 int32_t token = 0;
1057 DeviceConnectStatus deviceConnectStatus = static_cast<DeviceConnectStatus>(-1);
1058 int32_t ret = dtbabilitymgrService_->UpdateConnectStatus(token, DEVICE_ID, deviceConnectStatus);
1059 EXPECT_EQ(ret, INVALID_CONNECT_STATUS);
1060 DTEST_LOG << "DistributedAbilityManagerServiceTest UpdateConnectStatus_002 end" << std::endl;
1061 }
1062
1063 /**
1064 * @tc.name: UpdateConnectStatus_003
1065 * @tc.desc: test UpdateConnectStatus
1066 * @tc.type: FUNC
1067 * @tc.require: I5NOA1
1068 */
1069 HWTEST_F(DistributedAbilityManagerServiceTest, UpdateConnectStatus_003, TestSize.Level3)
1070 {
1071 DTEST_LOG << "DistributedAbilityManagerServiceTest UpdateConnectStatus_003 start" << std::endl;
1072 if (dtbabilitymgrService_ == nullptr) {
1073 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1074 return;
1075 }
1076 int32_t token = 0;
1077 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
1078 int32_t ret = dtbabilitymgrService_->Register(continuationExtraParams, token);
1079 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
1080 ret = dtbabilitymgrService_->RegisterDeviceSelectionCallback(token, EVENT_CONNECT, notifier);
1081 EXPECT_EQ(ret, ERR_OK);
1082 dtbabilitymgrService_->ScheduleStartDeviceManager(dtbabilitymgrService_, 1, continuationExtraParams);
1083 ret = dtbabilitymgrService_->UpdateConnectStatus(token, DEVICE_ID, DeviceConnectStatus::IDLE);
1084 EXPECT_EQ(ret, ERR_OK);
1085 ret = dtbabilitymgrService_->UnregisterDeviceSelectionCallback(token, EVENT_CONNECT);
1086 EXPECT_EQ(ret, ERR_OK);
1087 ret = dtbabilitymgrService_->Unregister(token);
1088 EXPECT_EQ(ret, ERR_OK);
1089 DTEST_LOG << "DistributedAbilityManagerServiceTest UpdateConnectStatus_003 end" << std::endl;
1090 }
1091
1092 /**
1093 * @tc.name: StartDeviceManager_001
1094 * @tc.desc: test StartDeviceManager when continuationExtraParams != nullptr.
1095 * @tc.type: FUNC
1096 * @tc.require: I76U22
1097 */
1098 HWTEST_F(DistributedAbilityManagerServiceTest, StartDeviceManager_001, TestSize.Level3)
1099 {
1100 DTEST_LOG << "DistributedAbilityManagerServiceTest StartDeviceManager_001 start" << std::endl;
1101 if (dtbabilitymgrService_ == nullptr) {
1102 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1103 return;
1104 }
1105
1106 int32_t token = 0;
1107 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
1108 ContinuationMode continuationMode = static_cast<ContinuationMode>(-1);
1109 continuationExtraParams->SetContinuationMode(continuationMode);
1110 int32_t ret = dtbabilitymgrService_->StartDeviceManager(token, continuationExtraParams);
1111 EXPECT_EQ(ret, INVALID_CONTINUATION_MODE);
1112 DTEST_LOG << "DistributedAbilityManagerServiceTest StartDeviceManager_001 end" << std::endl;
1113 }
1114
1115 /**
1116 * @tc.name: StartDeviceManager_002
1117 * @tc.desc: test StartDeviceManager when continuationExtraParams != nullptr.
1118 * @tc.type: FUNC
1119 * @tc.require: I76U22
1120 */
1121 HWTEST_F(DistributedAbilityManagerServiceTest, StartDeviceManager_002, TestSize.Level3)
1122 {
1123 DTEST_LOG << "DistributedAbilityManagerServiceTest StartDeviceManager_002 start" << std::endl;
1124 if (dtbabilitymgrService_ == nullptr) {
1125 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1126 return;
1127 }
1128
1129 int32_t token = 0;
1130 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
1131 int32_t ret = dtbabilitymgrService_->StartDeviceManager(token, continuationExtraParams);
1132 EXPECT_EQ(ret, TOKEN_HAS_NOT_REGISTERED);
1133 DTEST_LOG << "DistributedAbilityManagerServiceTest StartDeviceManager_002 end" << std::endl;
1134 }
1135
1136 /**
1137 * @tc.name: StartDeviceManager_003
1138 * @tc.desc: test StartDeviceManager when continuationExtraParams != nullptr.
1139 * @tc.type: FUNC
1140 * @tc.require: I76U22
1141 */
1142 HWTEST_F(DistributedAbilityManagerServiceTest, StartDeviceManager_003, TestSize.Level3)
1143 {
1144 DTEST_LOG << "DistributedAbilityManagerServiceTest StartDeviceManager_003 start" << std::endl;
1145 if (dtbabilitymgrService_ == nullptr) {
1146 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1147 return;
1148 }
1149
1150 int32_t token = 0;
1151 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
1152 int32_t res = dtbabilitymgrService_->Register(continuationExtraParams, token);
1153 EXPECT_EQ(res, ERR_OK);
1154
1155 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
1156 {
1157 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
1158 dtbabilitymgrService_->callbackMap_.clear();
1159 dtbabilitymgrService_->callbackMap_[token] = std::move(notifierInfo);
1160 }
1161 int32_t ret = dtbabilitymgrService_->StartDeviceManager(token, continuationExtraParams);
1162 EXPECT_EQ(ret, ERR_OK);
1163 {
1164 std::lock_guard<std::mutex> tokenLock(dtbabilitymgrService_->tokenMutex_);
1165 dtbabilitymgrService_->tokenMap_.clear();
1166 }
1167 {
1168 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
1169 dtbabilitymgrService_->callbackMap_.clear();
1170 }
1171 DTEST_LOG << "DistributedAbilityManagerServiceTest StartDeviceManager_003 end" << std::endl;
1172 }
1173
1174 /**
1175 * @tc.name: DisconnectAbility_001
1176 * @tc.desc: test DisconnectAbility when continuationExtraParams != nullptr.
1177 * @tc.type: FUNC
1178 * @tc.require: I76U22
1179 */
1180 HWTEST_F(DistributedAbilityManagerServiceTest, DisconnectAbility_001, TestSize.Level3)
1181 {
1182 DTEST_LOG << "DistributedAbilityManagerServiceTest DisconnectAbility_001 start" << std::endl;
1183 if (dtbabilitymgrService_ == nullptr) {
1184 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1185 return;
1186 }
1187 int32_t token = 0;
1188 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
1189 dtbabilitymgrService_->connect_ = new AppConnectionStub(token, continuationExtraParams);
1190 int32_t ret = dtbabilitymgrService_->DisconnectAbility();
1191 EXPECT_EQ(ret, ERR_OK);
1192 DTEST_LOG << "DistributedAbilityManagerServiceTest DisconnectAbility_001 end" << std::endl;
1193 }
1194
1195 /**
1196 * @tc.name: OnDeviceConnect_001
1197 * @tc.desc: test OnDeviceConnect when continuationHandler_ != nullptr.
1198 * @tc.type: FUNC
1199 * @tc.require: I76U22
1200 */
1201 HWTEST_F(DistributedAbilityManagerServiceTest, OnDeviceConnect_001, TestSize.Level3)
1202 {
1203 DTEST_LOG << "DistributedAbilityManagerServiceTest OnDeviceConnect_001 start" << std::endl;
1204 if (dtbabilitymgrService_ == nullptr) {
1205 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1206 return;
1207 }
1208 if (dtbabilitymgrService_->continuationHandler_ == nullptr) {
1209 dtbabilitymgrService_->continuationHandler_ = std::make_shared<ffrt::queue>("ContinuationMgr");
1210 }
1211 int32_t token = 0;
1212 std::vector<ContinuationResult> continuationResults;
1213 int32_t ret = dtbabilitymgrService_->OnDeviceConnect(token, continuationResults);
1214 EXPECT_EQ(ret, CALLBACK_HAS_NOT_REGISTERED);
1215 DTEST_LOG << "DistributedAbilityManagerServiceTest DisconnectAbility_001 end" << std::endl;
1216 }
1217
1218 /**
1219 * @tc.name: OnDeviceConnect_002
1220 * @tc.desc: test OnDeviceConnect when continuationHandler_ != nullptr.
1221 * @tc.type: FUNC
1222 * @tc.require: I76U22
1223 */
1224 HWTEST_F(DistributedAbilityManagerServiceTest, OnDeviceConnect_002, TestSize.Level3)
1225 {
1226 DTEST_LOG << "DistributedAbilityManagerServiceTest OnDeviceConnect_002 start" << std::endl;
1227 if (dtbabilitymgrService_ == nullptr) {
1228 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1229 return;
1230 }
1231 if (dtbabilitymgrService_->continuationHandler_ == nullptr) {
1232 dtbabilitymgrService_->continuationHandler_ = std::make_shared<ffrt::queue>("ContinuationMgr");
1233 }
1234 int32_t token = 0;
1235 std::vector<ContinuationResult> continuationResults;
1236 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
1237 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
1238 notifierInfo->SetNotifier(EVENT_CONNECT, notifier);
1239 {
1240 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
1241 dtbabilitymgrService_->callbackMap_.clear();
1242 dtbabilitymgrService_->callbackMap_[token] = std::move(notifierInfo);
1243 }
1244 int32_t ret = dtbabilitymgrService_->OnDeviceConnect(token, continuationResults);
1245 {
1246 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
1247 dtbabilitymgrService_->callbackMap_.clear();
1248 }
1249 EXPECT_EQ(ret, ERR_OK);
1250 DTEST_LOG << "DistributedAbilityManagerServiceTest OnDeviceConnect_002 end" << std::endl;
1251 }
1252
1253 /**
1254 * @tc.name: HandleStartDeviceManager_001
1255 * @tc.desc: test HandleStartDeviceManager
1256 * @tc.type: FUNC
1257 * @tc.require: I76U22
1258 */
1259 HWTEST_F(DistributedAbilityManagerServiceTest, HandleStartDeviceManager_001, TestSize.Level3)
1260 {
1261 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleStartDeviceManager_001 start" << std::endl;
1262 if (dtbabilitymgrService_ == nullptr) {
1263 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1264 return;
1265 }
1266 if (dtbabilitymgrService_->continuationHandler_ == nullptr) {
1267 dtbabilitymgrService_->continuationHandler_ = std::make_shared<ffrt::queue>("ContinuationMgr");
1268 }
1269 int32_t token = 0;
1270 dtbabilitymgrService_->HandleStartDeviceManager(token, nullptr);
1271 EXPECT_NE(dtbabilitymgrService_, nullptr);
1272 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleStartDeviceManager_001 end" << std::endl;
1273 }
1274
1275 /**
1276 * @tc.name: HandleStartDeviceManager_002
1277 * @tc.desc: test HandleStartDeviceManager
1278 * @tc.type: FUNC
1279 * @tc.require: I76U22
1280 */
1281 HWTEST_F(DistributedAbilityManagerServiceTest, HandleStartDeviceManager_002, TestSize.Level3)
1282 {
1283 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleStartDeviceManager_002 start" << std::endl;
1284 if (dtbabilitymgrService_ == nullptr) {
1285 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1286 return;
1287 }
1288 if (dtbabilitymgrService_->continuationHandler_ == nullptr) {
1289 dtbabilitymgrService_->continuationHandler_ = std::make_shared<ffrt::queue>("ContinuationMgr");
1290 }
1291 int32_t token = 0;
1292 std::shared_ptr<ContinuationExtraParams> continuationExtraParams = std::make_shared<ContinuationExtraParams>();
1293 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
1294 {
1295 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
1296 dtbabilitymgrService_->callbackMap_.clear();
1297 dtbabilitymgrService_->callbackMap_[token] = std::move(notifierInfo);
1298 }
1299 dtbabilitymgrService_->HandleStartDeviceManager(token, continuationExtraParams);
1300 {
1301 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
1302 dtbabilitymgrService_->callbackMap_.clear();
1303 }
1304 EXPECT_NE(dtbabilitymgrService_, nullptr);
1305 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleStartDeviceManager_002 end" << std::endl;
1306 }
1307
1308 /**
1309 * @tc.name: HandleNotifierDied_002
1310 * @tc.desc: test HandleNotifierDied
1311 * @tc.type: FUNC
1312 * @tc.require: I76U22
1313 */
1314 HWTEST_F(DistributedAbilityManagerServiceTest, HandleNotifierDied_002, TestSize.Level3)
1315 {
1316 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleNotifierDied_002 start" << std::endl;
1317 if (dtbabilitymgrService_ == nullptr) {
1318 DTEST_LOG << "dtbabilitymgrService_ is nullptr" << std::endl;
1319 return;
1320 }
1321 int32_t token = 0;
1322 sptr<DeviceSelectionNotifierTest> notifier = new DeviceSelectionNotifierTest();
1323 std::unique_ptr<NotifierInfo> notifierInfo = std::make_unique<NotifierInfo>();
1324 notifierInfo->SetNotifier(EVENT_CONNECT, notifier);
1325 {
1326 std::lock_guard<std::mutex> callbackMapLock(dtbabilitymgrService_->callbackMapMutex_);
1327 dtbabilitymgrService_->callbackMap_.clear();
1328 dtbabilitymgrService_->callbackMap_[token] = std::move(notifierInfo);;
1329 }
1330 dtbabilitymgrService_->HandleNotifierDied(dtbabilitymgrService_);
1331 {
1332 std::lock_guard<std::mutex> autoLock(dtbabilitymgrService_->callbackMapMutex_);
1333 dtbabilitymgrService_->callbackMap_.clear();
1334 }
1335 EXPECT_NE(dtbabilitymgrService_, nullptr);
1336 DTEST_LOG << "DistributedAbilityManagerServiceTest HandleNotifierDied_002 end" << std::endl;
1337 }
1338 }
1339 }