• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <gtest/gtest.h>
17 
18 #define private public
19 #define protected public
20 #include "ability_connect_manager.h"
21 #undef private
22 #undef protected
23 
24 #include "ability_config.h"
25 #include "ability_manager_errors.h"
26 #include "ability_scheduler.h"
27 #include "event_handler.h"
28 #include "mock_ability_connect_callback.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS::AppExecFwk;
32 
33 namespace OHOS {
34 namespace AAFwk {
35 template<typename F>
WaitUntilTaskCalled(const F & f,const std::shared_ptr<EventHandler> & handler,std::atomic<bool> & taskCalled)36 static void WaitUntilTaskCalled(const F &f, const std::shared_ptr<EventHandler> &handler, std::atomic<bool> &taskCalled)
37 {
38     const uint32_t maxRetryCount = 1000;
39     const uint32_t sleepTime = 1000;
40     uint32_t count = 0;
41     if (handler->PostTask(f)) {
42         while (!taskCalled.load()) {
43             ++count;
44             // if delay more than 1 second, break
45             if (count >= maxRetryCount) {
46                 break;
47             }
48             usleep(sleepTime);
49         }
50     }
51 }
52 
WaitUntilTaskDone(const std::shared_ptr<EventHandler> & handler)53 static void WaitUntilTaskDone(const std::shared_ptr<EventHandler> &handler)
54 {
55     std::atomic<bool> taskCalled(false);
56     auto f = [&taskCalled]() { taskCalled.store(true); };
57     WaitUntilTaskCalled(f, handler, taskCalled);
58 }
59 
60 class AbilityConnectManagerTest : public testing::Test {
61 public:
62     static void SetUpTestCase(void);
63     static void TearDownTestCase(void);
64     void SetUp();
65     void TearDown();
66 
67     AbilityConnectManager *ConnectManager() const;
68 
69     AbilityRequest GenerateAbilityRequest(const std::string &deviceName, const std::string &abilityName,
70         const std::string &appName, const std::string &bundleName, const std::string &moduleName);
71 
72     static constexpr int TEST_WAIT_TIME = 1000000;
73 
74 protected:
75     AbilityRequest abilityRequest_ {};
76     AbilityRequest abilityRequest1_ {};
77     std::shared_ptr<AbilityRecord> serviceRecord_ {nullptr};
78     std::shared_ptr<AbilityRecord> serviceRecord1_ {nullptr};
79     OHOS::sptr<Token> serviceToken_ {nullptr};
80     OHOS::sptr<Token> serviceToken1_ {nullptr};
81     OHOS::sptr<IAbilityConnection> callbackA_ {nullptr};
82     OHOS::sptr<IAbilityConnection> callbackB_ {nullptr};
83 
84 private:
85     std::shared_ptr<AbilityConnectManager> connectManager_;
86 };
87 
GenerateAbilityRequest(const std::string & deviceName,const std::string & abilityName,const std::string & appName,const std::string & bundleName,const std::string & moduleName)88 AbilityRequest AbilityConnectManagerTest::GenerateAbilityRequest(const std::string& deviceName,
89     const std::string &abilityName, const std::string &appName, const std::string &bundleName,
90     const std::string &moduleName)
91 {
92     ElementName element(deviceName, bundleName, abilityName, moduleName);
93     Want want;
94     want.SetElement(element);
95 
96     AbilityInfo abilityInfo;
97     abilityInfo.visible = true;
98     abilityInfo.applicationName = appName;
99     abilityInfo.type = AbilityType::SERVICE;
100     abilityInfo.name = abilityName;
101     abilityInfo.bundleName = bundleName;
102     abilityInfo.moduleName = moduleName;
103     abilityInfo.deviceId = deviceName;
104     ApplicationInfo appinfo;
105     appinfo.name = appName;
106     abilityInfo.applicationInfo = appinfo;
107     AbilityRequest abilityRequest;
108     abilityRequest.want = want;
109     abilityRequest.abilityInfo = abilityInfo;
110     abilityRequest.appInfo = appinfo;
111 
112     return abilityRequest;
113 }
114 
SetUpTestCase(void)115 void AbilityConnectManagerTest::SetUpTestCase(void)
116 {}
TearDownTestCase(void)117 void AbilityConnectManagerTest::TearDownTestCase(void)
118 {}
119 
SetUp(void)120 void AbilityConnectManagerTest::SetUp(void)
121 {
122     connectManager_ = std::make_unique<AbilityConnectManager>(0);
123     // generate ability request
124     std::string deviceName = "device";
125     std::string abilityName = "ServiceAbility";
126     std::string appName = "hiservcie";
127     std::string bundleName = "com.ix.hiservcie";
128     std::string moduleName = "entry";
129     abilityRequest_ = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName);
130     serviceRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_);
131     serviceToken_ = serviceRecord_->GetToken();
132     std::string deviceName1 = "device";
133     std::string abilityName1 = "musicServiceAbility";
134     std::string appName1 = "musicservcie";
135     std::string bundleName1 = "com.ix.musicservcie";
136     std::string moduleName1 = "entry";
137     abilityRequest1_ = GenerateAbilityRequest(deviceName1, abilityName1, appName1, bundleName1, moduleName1);
138     serviceRecord1_ = AbilityRecord::CreateAbilityRecord(abilityRequest1_);
139     serviceToken1_ = serviceRecord_->GetToken();
140     callbackA_ = new AbilityConnectCallback();
141     callbackB_ = new AbilityConnectCallback();
142 }
143 
TearDown(void)144 void AbilityConnectManagerTest::TearDown(void)
145 {
146     // reset the callback count
147     AbilityConnectCallback::onAbilityConnectDoneCount = 0;
148     AbilityConnectCallback::onAbilityDisconnectDoneCount = 0;
149     serviceRecord_ = nullptr;
150 }
151 
ConnectManager() const152 AbilityConnectManager* AbilityConnectManagerTest::ConnectManager() const
153 {
154     return connectManager_.get();
155 }
156 
157 /*
158  * Feature: AbilityConnectManager
159  * Function: StartAbility
160  * SubFunction: NA
161  * FunctionPoints: StartAbility
162  * EnvConditions:NA
163  * CaseDescription: Verify the normal process of startability
164  */
165 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_001, TestSize.Level1)
166 {
167     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
168     ConnectManager()->SetEventHandler(handler);
169 
170     auto result = ConnectManager()->StartAbility(abilityRequest_);
171     EXPECT_EQ(OHOS::ERR_OK, result);
172     WaitUntilTaskDone(handler);
173 
174     auto elementName = abilityRequest_.want.GetElement().GetURI();
175     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
176     EXPECT_NE(service, nullptr);
177     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
178 
179     service->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
180 
181     auto result1 = ConnectManager()->StartAbility(abilityRequest_);
182     WaitUntilTaskDone(handler);
183     EXPECT_EQ(OHOS::ERR_OK, result1);
184     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
185 
186     service->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVATING);
187     auto result2 = ConnectManager()->StartAbility(abilityRequest_);
188     WaitUntilTaskDone(handler);
189     EXPECT_EQ(OHOS::AAFwk::START_SERVICE_ABILITY_ACTIVATING, result2);
190     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
191 }
192 
193 /*
194  * Feature: AbilityConnectManager
195  * Function: TerminateAbility
196  * SubFunction: NA
197  * FunctionPoints: StartAbility and TerminateAbility
198  * EnvConditions:NA
199  * CaseDescription: Verify the following:
200  * 1.token is nullptr, terminate ability failed
201  * 2.token is not nullptr, terminate ability success, and verify the status
202  */
203 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_002, TestSize.Level1)
204 {
205     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
206     ConnectManager()->SetEventHandler(handler);
207 
208     auto result = ConnectManager()->StartAbility(abilityRequest_);
209     EXPECT_EQ(OHOS::ERR_OK, result);
210     WaitUntilTaskDone(handler);
211 
212     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
213     auto result1 = ConnectManager()->TerminateAbility(nullToken);
214     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result1);
215 
216     auto elementName = abilityRequest_.want.GetElement().GetURI();
217     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
218     EXPECT_NE(service, nullptr);
219 
220     auto result2 = ConnectManager()->TerminateAbility(service->GetToken());
221     WaitUntilTaskDone(handler);
222     EXPECT_EQ(OHOS::ERR_OK, result2);
223     EXPECT_EQ(service->GetAbilityState(), TERMINATING);
224 }
225 
226 /*
227  * Feature: AbilityConnectManager
228  * Function: TerminateAbility
229  * SubFunction: NA
230  * FunctionPoints: StartAbility and TerminateAbility
231  * EnvConditions:NA
232  * CaseDescription: Verify ability is terminating, terminate ability success
233  */
234 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_003, TestSize.Level1)
235 {
236     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
237     ConnectManager()->SetEventHandler(handler);
238 
239     auto result = ConnectManager()->StartAbility(abilityRequest_);
240     EXPECT_EQ(OHOS::ERR_OK, result);
241     WaitUntilTaskDone(handler);
242 
243     auto elementName = abilityRequest_.want.GetElement().GetURI();
244     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
245     EXPECT_NE(service, nullptr);
246 
247     service->SetTerminatingState();
248     auto result1 = ConnectManager()->TerminateAbility(service->GetToken());
249     WaitUntilTaskDone(handler);
250     EXPECT_EQ(OHOS::ERR_OK, result1);
251     EXPECT_NE(service->GetAbilityState(), TERMINATING);
252 }
253 
254 /*
255  * Feature: AbilityConnectManager
256  * Function: TerminateAbility
257  * SubFunction: NA
258  * FunctionPoints: StartAbility and TerminateAbility
259  * EnvConditions: NA
260  * CaseDescription: Verify service is connected, terminate ability failed
261  */
262 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_004, TestSize.Level1)
263 {
264     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
265     ConnectManager()->SetEventHandler(handler);
266 
267     auto result = ConnectManager()->StartAbility(abilityRequest_);
268     EXPECT_EQ(OHOS::ERR_OK, result);
269     WaitUntilTaskDone(handler);
270 
271     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
272     EXPECT_EQ(0, result1);
273 
274     auto elementName = abilityRequest_.want.GetElement().GetURI();
275     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
276     EXPECT_NE(service, nullptr);
277 
278     auto result2 = ConnectManager()->TerminateAbility(service->GetToken());
279     WaitUntilTaskDone(handler);
280     EXPECT_EQ(0, result2);
281     EXPECT_EQ(service->GetAbilityState(), TERMINATING);
282 }
283 
284 /*
285  * Feature: AbilityConnectManager
286  * Function: StopServiceAbility
287  * SubFunction: NA
288  * FunctionPoints: StartAbility and StopServiceAbility
289  * EnvConditions: NA
290  * CaseDescription: Verify the following:
291  * 1.token is nullptr, stop service ability failed
292  * 2.token is not nullptr, stop service ability success, and verify the status
293  */
294 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_005, TestSize.Level1)
295 {
296     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
297     ConnectManager()->SetEventHandler(handler);
298 
299     auto result = ConnectManager()->StartAbility(abilityRequest_);
300     EXPECT_EQ(OHOS::ERR_OK, result);
301     WaitUntilTaskDone(handler);
302 
303     auto elementName = abilityRequest_.want.GetElement().GetURI();
304     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
305     EXPECT_NE(service, nullptr);
306 
307     AbilityRequest otherRequest;
308     auto result1 = ConnectManager()->StopServiceAbility(otherRequest);
309     WaitUntilTaskDone(handler);
310     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result1);
311 
312     auto result2 = ConnectManager()->StopServiceAbility(abilityRequest_);
313     WaitUntilTaskDone(handler);
314     EXPECT_EQ(OHOS::ERR_OK, result2);
315     EXPECT_EQ(service->GetAbilityState(), TERMINATING);
316 }
317 
318 /*
319  * Feature: AbilityConnectManager
320  * Function: StopServiceAbility
321  * SubFunction: NA
322  * FunctionPoints: StartAbility and StopServiceAbility
323  * EnvConditions:NA
324  * CaseDescription: Verify ability is terminating, stop service ability success
325  */
326 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_006, TestSize.Level1)
327 {
328     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
329     ConnectManager()->SetEventHandler(handler);
330 
331     auto result = ConnectManager()->StartAbility(abilityRequest_);
332     EXPECT_EQ(OHOS::ERR_OK, result);
333     WaitUntilTaskDone(handler);
334 
335     auto elementName = abilityRequest_.want.GetElement().GetURI();
336     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
337     EXPECT_NE(service, nullptr);
338 
339     service->SetTerminatingState();
340     auto result1 = ConnectManager()->StopServiceAbility(abilityRequest_);
341     WaitUntilTaskDone(handler);
342     EXPECT_EQ(OHOS::ERR_OK, result1);
343     EXPECT_NE(service->GetAbilityState(), TERMINATING);
344 }
345 
346 /*
347  * Feature: AbilityConnectManager
348  * Function: StopServiceAbility
349  * SubFunction: NA
350  * FunctionPoints: StartAbility and StopServiceAbility
351  * EnvConditions: NA
352  * CaseDescription: Verify service is connected, stop service ability failed
353  */
354 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_007, TestSize.Level1)
355 {
356     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
357     ConnectManager()->SetEventHandler(handler);
358 
359     auto result = ConnectManager()->StartAbility(abilityRequest_);
360     EXPECT_EQ(OHOS::ERR_OK, result);
361     WaitUntilTaskDone(handler);
362 
363     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
364     EXPECT_EQ(0, result1);
365 
366     auto elementName = abilityRequest_.want.GetElement().GetURI();
367     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
368     EXPECT_NE(service, nullptr);
369 
370     auto result2 = ConnectManager()->StopServiceAbility(abilityRequest_);
371     WaitUntilTaskDone(handler);
372     EXPECT_EQ(0, result2);
373     EXPECT_EQ(service->GetAbilityState(), TERMINATING);
374 }
375 
376 /*
377  * Feature: AbilityConnectManager
378  * Function: ConnectAbilityLocked
379  * SubFunction: NA
380  * FunctionPoints: NA
381  * EnvConditions:NA
382  * CaseDescription: verify the scene of service not loaded and callback not bound.
383  */
384 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_008, TestSize.Level1)
385 {
386     int result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
387     EXPECT_EQ(0, result);
388 
389     auto connectMap = ConnectManager()->GetConnectMap();
390     auto connectRecordList = connectMap.at(callbackA_->AsObject());
391     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
392 
393     auto elementName = abilityRequest_.want.GetElement();
394     auto elementNameUri = elementName.GetURI();
395     auto serviceMap = ConnectManager()->GetServiceMap();
396     auto abilityRecord = serviceMap.at(elementNameUri);
397     connectRecordList = abilityRecord->GetConnectRecordList();
398     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
399 }
400 
401 /*
402  * Feature: AbilityConnectManager
403  * Function: ConnectAbilityLocked
404  * SubFunction: NA
405  * FunctionPoints: NA
406  * EnvConditions:NA
407  * CaseDescription: verify the scene of service load ability's timeout.
408  */
409 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_009, TestSize.Level1)
410 {
411     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
412     ConnectManager()->SetEventHandler(handler);
413     int result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
414     EXPECT_EQ(0, result);
415 
416     auto connectMap = ConnectManager()->GetConnectMap();
417     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
418     WaitUntilTaskDone(handler);
419     usleep(TEST_WAIT_TIME);
420 
421     connectMap = ConnectManager()->GetConnectMap();
422     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
423 }
424 
425 /*
426  * Feature: AbilityConnectManager
427  * Function: ConnectAbilityLocked
428  * SubFunction: NA
429  * FunctionPoints: NA
430  * EnvConditions:NA
431  * CaseDescription: verify the scene of service loaded and callback not bound.
432  */
433 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_010, TestSize.Level1)
434 {
435     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
436     EXPECT_EQ(0, result);
437 
438     result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr);
439     EXPECT_EQ(0, result);
440 
441     auto connectMap = ConnectManager()->GetConnectMap();
442     auto connectRecordList = connectMap.at(callbackA_->AsObject());
443     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
444 
445     connectRecordList = connectMap.at(callbackB_->AsObject());
446     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
447 
448     auto elementName = abilityRequest_.want.GetElement();
449     std::string elementNameUri = elementName.GetURI();
450     auto serviceMap = ConnectManager()->GetServiceMap();
451     auto abilityRecord = serviceMap.at(elementNameUri);
452     connectRecordList = abilityRecord->GetConnectRecordList();
453     EXPECT_EQ(2, static_cast<int>(connectRecordList.size()));
454 }
455 
456 /*
457  * Feature: AbilityConnectManager
458  * Function: ConnectAbilityLocked
459  * SubFunction: NA
460  * FunctionPoints: NA
461  * EnvConditions:NA
462  * CaseDescription: verify the scene of service connect ability's timeout.
463  */
464 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_011, TestSize.Level1)
465 {
466     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
467     ConnectManager()->SetEventHandler(handler);
468 
469     int result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
470     auto elementName = abilityRequest_.want.GetElement();
471     std::string elementNameUri = elementName.GetURI();
472     auto serviceMap = ConnectManager()->GetServiceMap();
473     auto abilityRecord = serviceMap.at(elementNameUri);
474     auto token = abilityRecord->GetToken();
475 
476     auto connectMap = ConnectManager()->GetConnectMap();
477     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
478 
479     auto scheduler = new AbilityScheduler();
480     ConnectManager()->AttachAbilityThreadLocked(scheduler, token->AsObject());
481     ConnectManager()->AbilityTransitionDone(token->AsObject(), OHOS::AAFwk::AbilityState::INACTIVE);
482 
483     WaitUntilTaskDone(handler);
484     usleep(TEST_WAIT_TIME);
485     connectMap = ConnectManager()->GetConnectMap();
486     EXPECT_EQ(0, result);
487     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
488 }
489 
490 /*
491  * Feature: AbilityConnectManager
492  * Function: ConnectAbilityLocked
493  * SubFunction: NA
494  * FunctionPoints: NA
495  * EnvConditions:NA
496  * CaseDescription: verify the scene of service loaded and callback bound.
497  */
498 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_012, TestSize.Level1)
499 {
500     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
501     EXPECT_EQ(0, result);
502 
503     result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
504     EXPECT_EQ(0, result);
505 
506     auto connectMap = ConnectManager()->GetConnectMap();
507     auto connectRecordList = connectMap.at(callbackA_->AsObject());
508     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
509 
510     auto elementName = abilityRequest_.want.GetElement();
511     std::string elementNameUri = elementName.GetURI();
512     auto serviceMap = ConnectManager()->GetServiceMap();
513     auto abilityRecord = serviceMap.at(elementNameUri);
514     connectRecordList = abilityRecord->GetConnectRecordList();
515     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
516 }
517 
518 /*
519  * Feature: AbilityConnectManager
520  * Function: ConnectAbilityLocked
521  * SubFunction: NA
522  * FunctionPoints: NA
523  * EnvConditions:NA
524  * CaseDescription: verify the scene of service not loaded and callback bound.
525  */
526 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_013, TestSize.Level1)
527 {
528     int result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
529     EXPECT_EQ(0, result);
530 
531     std::string deviceNameB = "device";
532     std::string abilityNameB = "ServiceAbilityB";
533     std::string appNameB = "hiservcieB";
534     std::string bundleNameB = "com.ix.hiservcieB";
535     std::string moduleNameB = "entry";
536     auto abilityRequestB = GenerateAbilityRequest(deviceNameB, abilityNameB, appNameB, bundleNameB, moduleNameB);
537     result = ConnectManager()->ConnectAbilityLocked(abilityRequestB, callbackA_, nullptr);
538     EXPECT_EQ(0, result);
539 
540     auto connectMap = ConnectManager()->GetConnectMap();
541     auto connectRecordList = connectMap.at(callbackA_->AsObject());
542     EXPECT_EQ(2, static_cast<int>(connectRecordList.size()));
543 
544     auto elementName = abilityRequest_.want.GetElement();
545     std::string elementNameUri = elementName.GetURI();
546     auto serviceMap = ConnectManager()->GetServiceMap();
547     auto abilityRecord = serviceMap.at(elementNameUri);
548     connectRecordList = abilityRecord->GetConnectRecordList();
549     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
550 
551     auto elementNameB = abilityRequest_.want.GetElement();
552     std::string elementNameUriB = elementNameB.GetURI();
553     abilityRecord = serviceMap.at(elementNameUriB);
554     connectRecordList = abilityRecord->GetConnectRecordList();
555     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
556 }
557 
558 /*
559  * Feature: AbilityConnectManager
560  * Function: ConnectAbilityLocked
561  * SubFunction: NA
562  * FunctionPoints: NA
563  * EnvConditions:NA
564  * CaseDescription: verify the scene of service loaded and callback bound, but service and callback was not associated.
565  */
566 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_014, TestSize.Level1)
567 {
568     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
569     EXPECT_EQ(0, result);
570 
571     std::string deviceNameB = "device";
572     std::string abilityNameB = "ServiceAbilityB";
573     std::string appNameB = "hiservcieB";
574     std::string bundleNameB = "com.ix.hiservcieB";
575     std::string moduleNameB = "entry";
576     auto abilityRequestB = GenerateAbilityRequest(deviceNameB, abilityNameB, appNameB, bundleNameB, moduleNameB);
577     result = ConnectManager()->ConnectAbilityLocked(abilityRequestB, callbackB_, nullptr);
578     EXPECT_EQ(0, result);
579 
580     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr);
581     auto connectMap = ConnectManager()->GetConnectMap();
582     auto connectRecordList = connectMap.at(callbackB_->AsObject());
583     EXPECT_EQ(2, static_cast<int>(connectRecordList.size()));
584 
585     connectRecordList = connectMap.at(callbackA_->AsObject());
586     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
587 
588     auto elementName = abilityRequest_.want.GetElement();
589     std::string elementNameUri = elementName.GetURI();
590     auto serviceMap = ConnectManager()->GetServiceMap();
591     auto abilityRecord = serviceMap.at(elementNameUri);
592     connectRecordList = abilityRecord->GetConnectRecordList();
593     EXPECT_EQ(2, static_cast<int>(connectRecordList.size()));
594 }
595 
596 /*
597  * Feature: AbilityConnectManager
598  * Function: AttachAbilityThreadLocked
599  * SubFunction: NA
600  * FunctionPoints: NA
601  * EnvConditions:NA
602  * CaseDescription: verify the AttachAbilityThreadLocked function when the parameter is null.
603  */
604 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_015, TestSize.Level1)
605 {
606     auto result = ConnectManager()->AttachAbilityThreadLocked(nullptr, nullptr);
607     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result);
608 }
609 
610 /*
611  * Feature: AbilityConnectManager
612  * Function: ScheduleConnectAbilityDoneLocked
613  * SubFunction: NA
614  * FunctionPoints: NA
615  * EnvConditions:NA
616  * CaseDescription: verify the ScheduleConnectAbilityDoneLocked function when the parameter is null.
617  */
618 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_016, TestSize.Level1)
619 {
620     auto callback = new AbilityConnectCallback();
621     auto result = ConnectManager()->ScheduleConnectAbilityDoneLocked(nullptr, callback);
622     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result);
623 }
624 
625 /*
626  * Feature: AbilityConnectManager
627  * Function: ScheduleConnectAbilityDoneLocked
628  * SubFunction: NA
629  * FunctionPoints: NA
630  * EnvConditions:NA
631  * CaseDescription: verify the ScheduleConnectAbilityDoneLocked function when the state is CONNECTED.
632  */
633 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_017, TestSize.Level1)
634 {
635     auto callback = new AbilityConnectCallback();
636     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callback, nullptr);
637 
638     auto elementName = abilityRequest_.want.GetElement();
639     std::string elementNameUri = elementName.GetURI();
640     auto serviceMap = ConnectManager()->GetServiceMap();
641     auto abilityRecord = serviceMap.at(elementNameUri);
642     auto token = abilityRecord->GetToken();
643 
644     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
645     ConnectManager()->ScheduleConnectAbilityDoneLocked(token, callback);
646     auto abilityRecordB = Token::GetAbilityRecordByToken(token);
647     EXPECT_TRUE(abilityRecordB);
648     auto connectRecordList = abilityRecordB->GetConnectRecordList();
649     int size = connectRecordList.size();
650     EXPECT_EQ(1, size);
651     if (size != 0) {
652         auto connState = (*(connectRecordList.begin()))->GetConnectState();
653         EXPECT_EQ(ConnectionState::CONNECTED, connState);
654     }
655 }
656 
657 /*
658  * Feature: AbilityConnectManager
659  * Function: ScheduleConnectAbilityDoneLocked
660  * SubFunction: NA
661  * FunctionPoints: NA
662  * EnvConditions:NA
663  * CaseDescription: verify the input parameters.
664  */
665 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_001, TestSize.Level1)
666 {
667     // start test
668     // test1 for serviceToken is null but remoteObject is valid
669     OHOS::sptr<OHOS::IRemoteObject> object = new AbilityConnectCallback();
670     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(nullptr, object);
671     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, ret);
672 
673     // test2 for both of serviceToken and remoteObject are null
674     ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(nullptr, nullptr);
675     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, ret);
676 }
677 
678 /*
679  * Feature: AbilityConnectManager
680  * Function: ScheduleConnectAbilityDoneLocked
681  * SubFunction: NA
682  * FunctionPoints: NA
683  * EnvConditions:NA
684  * CaseDescription: verify the input serviceToken which corresponding ability record doesn't exist.
685  */
686 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_002, TestSize.Level1)
687 {
688     // test for serviceToken's abilityRecord is null
689     serviceRecord_ = nullptr;
690     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, nullptr);
691     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, ret);
692 }
693 
694 /*
695  * Feature: AbilityConnectManager
696  * Function: ScheduleConnectAbilityDoneLocked
697  * SubFunction: OnAbilityConnectDone
698  * FunctionPoints: NA
699  * EnvConditions:NA
700  * CaseDescription: verify the input serviceToken which corresponding connection list is empty.
701  */
702 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_003, TestSize.Level1)
703 {
704     // test for serviceToken's connection list is null
705     // start test
706     auto callback = new AbilityConnectCallback();
707     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
708     EXPECT_EQ(OHOS::AAFwk::INVALID_CONNECTION_STATE, ret);
709     auto connList = serviceRecord_->GetConnectRecordList();
710     EXPECT_EQ(true, connList.empty());  // the connection list size should be empty
711 }
712 
713 /*
714  * Feature: AbilityConnectManager
715  * Function: ScheduleConnectAbilityDoneLocked
716  * SubFunction: OnAbilityConnectDone
717  * FunctionPoints: NA
718  * EnvConditions:NA
719  * CaseDescription: verify the input serviceToken which corresponding connection list members' state
720  * is not CONNECTING or CONNECTED.
721  */
722 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_004, TestSize.Level1)
723 {
724     // test for schedule the service connected done but the corresponding connection state is not CONNECTING
725     // generate the first connection record of callbackA_
726     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
727         serviceToken_, serviceRecord_, callbackA_);  // newConnRecord1's default state is INIT
728     serviceRecord_->AddConnectRecordToList(newConnRecord1);
729     // generate the second connection record of callbackB_
730     auto newConnRecord2 = ConnectionRecord::CreateConnectionRecord(serviceToken_, serviceRecord_, callbackB_);
731     newConnRecord2->SetConnectState(ConnectionState::DISCONNECTING);  // newConnRecord2's state is DISCONNECTING
732     serviceRecord_->AddConnectRecordToList(newConnRecord2);
733     auto connList = serviceRecord_->GetConnectRecordList();
734     EXPECT_EQ(2, static_cast<int>(connList.size()));  // the connection list members should be two
735     // start test
736     auto callback = new AbilityConnectCallback();
737     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
738     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
739     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
740     // connection callback should not be called, so check the count
741     EXPECT_EQ(0, AbilityConnectCallback::onAbilityConnectDoneCount);
742     EXPECT_EQ(0, AbilityConnectCallback::onAbilityDisconnectDoneCount);
743 }
744 
745 /*
746  * Feature: AbilityConnectManager
747  * Function: ScheduleConnectAbilityDoneLocked
748  * SubFunction: OnAbilityConnectDone
749  * FunctionPoints: NA
750  * EnvConditions:NA
751  * CaseDescription: verify the scene : 1.serviceToken's corresponding connection list member's state is CONNECTING.
752  * 2.But the connection callback is null.
753  */
754 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_005, TestSize.Level1)
755 {
756     // test for schedule the service connected done but the corresponding connection state is not CONNECTING
757     // generate the first connection record of null
758     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
759         serviceToken_, serviceRecord_, nullptr);  // newConnRecord1's default state is INIT
760     serviceRecord_->AddConnectRecordToList(newConnRecord1);
761     newConnRecord1->SetConnectState(ConnectionState::CONNECTING);  // newConnRecord1's state is CONNECTING
762     auto connList = serviceRecord_->GetConnectRecordList();
763     EXPECT_EQ(1, static_cast<int>(connList.size()));  // the connection list members should be zero
764     // start test
765     auto callback = new AbilityConnectCallback();
766     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
767     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
768     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
769     // connection callback should not be called, so check the count
770     EXPECT_EQ(0, AbilityConnectCallback::onAbilityConnectDoneCount);
771 }
772 
773 /*
774  * Feature: AbilityConnectManager
775  * Function: ScheduleConnectAbilityDoneLocked
776  * SubFunction: OnAbilityConnectDone
777  * FunctionPoints: NA
778  * EnvConditions:NA
779  * CaseDescription: verify the scene : 1.serviceToken's corresponding connection list member's state is CONNECTED.
780  * 2.But the connection callback is null.
781  */
782 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_006, TestSize.Level1)
783 {
784     // test for schedule the service connected done but the corresponding connection state is not CONNECTING
785     // generate the first connection record of null
786     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
787         serviceToken_, serviceRecord_, nullptr);  // newConnRecord1's default state is INIT
788     serviceRecord_->AddConnectRecordToList(newConnRecord1);
789     newConnRecord1->SetConnectState(ConnectionState::CONNECTED);  // newConnRecord1's state is CONNECTED
790     auto connList = serviceRecord_->GetConnectRecordList();
791     EXPECT_EQ(1, static_cast<int>(connList.size()));  // the connection list members should be zero
792     // start test
793     auto callback = new AbilityConnectCallback();
794     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
795     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
796     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
797     // connection callback should not be called, so check the count
798     EXPECT_EQ(0, AbilityConnectCallback::onAbilityConnectDoneCount);
799 }
800 
801 /*
802  * Feature: AbilityConnectManager
803  * Function: ScheduleConnectAbilityDoneLocked
804  * SubFunction: OnAbilityConnectDone
805  * FunctionPoints: NA
806  * EnvConditions:NA
807  * CaseDescription: verify the scene : 1.serviceToken's corresponding connection list member's state is CONNECTING.
808  * 2.But the connection callback is valid.
809  */
810 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_007, TestSize.Level1)
811 {
812     // test for schedule the service connected done but the corresponding connection state is not CONNECTING
813     // generate the first connection record of callbackA_
814     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
815         serviceToken_, serviceRecord_, callbackA_);  // newConnRecord1's default state is INIT
816     serviceRecord_->AddConnectRecordToList(newConnRecord1);
817     // generate the second connection record of callbackB_
818     auto newConnRecord2 = ConnectionRecord::CreateConnectionRecord(serviceToken_, serviceRecord_, callbackB_);
819     newConnRecord2->SetConnectState(ConnectionState::CONNECTING);  // newConnRecord2's state is CONNECTING
820     serviceRecord_->AddConnectRecordToList(newConnRecord2);
821     auto connList = serviceRecord_->GetConnectRecordList();
822     EXPECT_EQ(2, static_cast<int>(connList.size()));  // the connection list members should be two
823     // start test
824     auto callback = new AbilityConnectCallback();
825     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
826     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
827     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
828     // connection callback should not be called, so check the count
829     EXPECT_EQ(1, AbilityConnectCallback::onAbilityConnectDoneCount);
830     EXPECT_EQ(0, AbilityConnectCallback::onAbilityDisconnectDoneCount);
831 }
832 
833 /*
834  * Feature: AbilityConnectManager
835  * Function: ScheduleConnectAbilityDoneLocked
836  * SubFunction: OnAbilityConnectDone
837  * FunctionPoints: NA
838  * EnvConditions:NA
839  * CaseDescription: verify the scene : 1.serviceToken's corresponding connection list member's state is CONNECTED.
840  * 2.But the connection callback is valid.
841  */
842 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_008, TestSize.Level1)
843 {
844     // test for schedule the service connected done but the corresponding connection state is not CONNECTING
845     // generate the first connection record of callbackA_
846     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
847         serviceToken_, serviceRecord_, callbackA_);               // newConnRecord1's default state is INIT
848     newConnRecord1->SetConnectState(ConnectionState::CONNECTED);  // newConnRecord1's state is CONNECTED
849     serviceRecord_->AddConnectRecordToList(newConnRecord1);
850     // generate the second connection record of callbackB_
851     auto newConnRecord2 = ConnectionRecord::CreateConnectionRecord(serviceToken_, serviceRecord_, callbackB_);
852     newConnRecord2->SetConnectState(ConnectionState::CONNECTING);  // newConnRecord2's state is CONNECTING
853     serviceRecord_->AddConnectRecordToList(newConnRecord2);
854     // generate the third connection record of callbackC
855     auto callbackC = new AbilityConnectCallback();
856     auto newConnRecord3 = ConnectionRecord::CreateConnectionRecord(serviceToken_, serviceRecord_, callbackC);
857     newConnRecord3->SetConnectState(ConnectionState::CONNECTING);  // newConnRecord3's state is CONNECTING
858     serviceRecord_->AddConnectRecordToList(newConnRecord3);
859     auto connList = serviceRecord_->GetConnectRecordList();
860     EXPECT_EQ(3, static_cast<int>(connList.size()));  // the connection list members should be three
861     // start test
862     auto callback = new AbilityConnectCallback();
863     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
864     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
865     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
866     // connection callback should not be called, so check the count
867     EXPECT_EQ(2, AbilityConnectCallback::onAbilityConnectDoneCount);
868     EXPECT_EQ(0, AbilityConnectCallback::onAbilityDisconnectDoneCount);
869 }
870 
871 /*
872  * Feature: AbilityConnectManager
873  * Function: DisconnectAbilityLocked
874  * SubFunction:
875  * FunctionPoints: DisconnectAbilityLocked and ConnectAbilityLocked
876  * EnvConditions:NA
877  * CaseDescription:Verify the following:
878  * 1. Disconnect ability a nonexistent connect, disconnect failed
879  * 2. If the current connect ability state is not connected, disconnect fails
880  * 3. Verify the success of disconnect ability
881  */
882 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Disconnect_001, TestSize.Level1)
883 {
884     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
885     ConnectManager()->SetEventHandler(handler);
886 
887     auto callback = new AbilityConnectCallback();
888     auto result = ConnectManager()->DisconnectAbilityLocked(callback);
889     EXPECT_EQ(result, OHOS::AAFwk::CONNECTION_NOT_EXIST);
890 
891     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
892     EXPECT_EQ(0, result1);
893 
894     auto result2 = ConnectManager()->DisconnectAbilityLocked(callbackA_);
895     EXPECT_EQ(result2, OHOS::AAFwk::INVALID_CONNECTION_STATE);
896 
897     auto list = ConnectManager()->GetConnectRecordListByCallback(callbackA_);
898     EXPECT_EQ(static_cast<int>(list.size()), 1);
899 
900     for (auto &it : list) {
901         it->SetConnectState(ConnectionState::CONNECTED);
902     }
903 
904     auto result3 = ConnectManager()->DisconnectAbilityLocked(callbackA_);
905     EXPECT_EQ(result3, OHOS::ERR_OK);
906 }
907 
908 /*
909  * Feature: AbilityConnectManager
910  * Function: DisconnectAbilityLocked
911  * SubFunction:
912  * FunctionPoints: DisconnectAbilityLocked and ConnectAbilityLocked
913  * EnvConditions:NA
914  * CaseDescription: Results after verifying the disconnect ability
915  */
916 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Disconnect_002, TestSize.Level1)
917 {
918     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
919     ConnectManager()->SetEventHandler(handler);
920 
921     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
922     EXPECT_EQ(0, result);
923 
924     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr);
925     EXPECT_EQ(0, result1);
926 
927     auto result2 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackA_, nullptr);
928     EXPECT_EQ(0, result2);
929 
930     auto result3 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackB_, nullptr);
931     EXPECT_EQ(0, result3);
932 
933     auto listA = ConnectManager()->GetConnectRecordListByCallback(callbackA_);
934     EXPECT_EQ(static_cast<int>(listA.size()), 2);
935 
936     for (auto &it : listA) {
937         it->SetConnectState(ConnectionState::CONNECTED);
938     }
939 
940     auto listB = ConnectManager()->GetConnectRecordListByCallback(callbackB_);
941     EXPECT_EQ(static_cast<int>(listB.size()), 2);
942 
943     for (auto &it : listB) {
944         it->SetConnectState(ConnectionState::CONNECTED);
945     }
946 
947     auto result5 = ConnectManager()->DisconnectAbilityLocked(callbackA_);
948     WaitUntilTaskDone(handler);
949     EXPECT_EQ(result5, OHOS::ERR_OK);
950     auto serviceMap = ConnectManager()->GetServiceMap();
951     EXPECT_EQ(static_cast<int>(serviceMap.size()), 2);
952 
953     auto connectMap = ConnectManager()->GetConnectMap();
954     EXPECT_EQ(static_cast<int>(connectMap.size()), 1);
955     for (auto &it : connectMap) {
956         EXPECT_EQ(static_cast<int>(it.second.size()), 2);
957     }
958 }
959 
960 /*
961  * Feature: AbilityConnectManager
962  * Function: AbilityTransitionDone
963  * SubFunction: NA
964  * FunctionPoints: AbilityTransitionDone
965  * EnvConditions:NA
966  * CaseDescription: Verify the abilitytransitiondone process
967  */
968 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_019, TestSize.Level1)
969 {
970     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
971     auto result = ConnectManager()->AbilityTransitionDone(nullToken, OHOS::AAFwk::AbilityState::INACTIVE);
972     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
973 
974     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
975     auto elementName = abilityRequest_.want.GetElement();
976     std::string elementNameUri = elementName.GetURI();
977     auto serviceMap = ConnectManager()->GetServiceMap();
978     auto abilityRecord = serviceMap.at(elementNameUri);
979     auto token = abilityRecord->GetToken();
980 
981     auto result1 = ConnectManager()->AbilityTransitionDone(token, OHOS::AAFwk::AbilityState::INACTIVE);
982     EXPECT_EQ(result1, OHOS::ERR_INVALID_VALUE);
983 
984     ConnectManager()->MoveToTerminatingMap(abilityRecord);
985     auto result2 = ConnectManager()->AbilityTransitionDone(token, OHOS::AAFwk::AbilityState::INITIAL);
986     EXPECT_EQ(result2, OHOS::ERR_OK);
987 
988     auto result3 = ConnectManager()->AbilityTransitionDone(token, OHOS::AAFwk::AbilityState::TERMINATING);
989     EXPECT_EQ(result3, OHOS::ERR_INVALID_VALUE);
990 }
991 
992 /*
993  * Feature: AbilityConnectManager
994  * Function: ScheduleDisconnectAbilityDoneLocked
995  * SubFunction: NA
996  * FunctionPoints: ScheduleDisconnectAbilityDoneLocked
997  * EnvConditions:NA
998  * CaseDescription: Verify the ScheduleDisconnectAbilityDoneLocked process
999  */
1000 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_020, TestSize.Level1)
1001 {
1002     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1003     ConnectManager()->SetEventHandler(handler);
1004 
1005     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
1006     auto result = ConnectManager()->ScheduleDisconnectAbilityDoneLocked(nullToken);
1007     EXPECT_EQ(result, OHOS::AAFwk::CONNECTION_NOT_EXIST);
1008 
1009     std::shared_ptr<AbilityRecord> ability = nullptr;
1010     OHOS::sptr<OHOS::IRemoteObject> token1 = new OHOS::AAFwk::Token(ability);
1011     auto result1 = ConnectManager()->ScheduleDisconnectAbilityDoneLocked(token1);
1012     EXPECT_EQ(result1, OHOS::AAFwk::CONNECTION_NOT_EXIST);
1013 
1014     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1015     auto elementName = abilityRequest_.want.GetElement();
1016     std::string elementNameUri = elementName.GetURI();
1017     auto serviceMap = ConnectManager()->GetServiceMap();
1018     auto abilityRecord = serviceMap.at(elementNameUri);
1019     auto token = abilityRecord->GetToken();
1020 
1021     auto listA = ConnectManager()->GetConnectRecordListByCallback(callbackA_);
1022     for (auto &it : listA) {
1023         it->SetConnectState(ConnectionState::CONNECTED);
1024     }
1025 
1026     auto result2 = ConnectManager()->DisconnectAbilityLocked(callbackA_);
1027     WaitUntilTaskDone(handler);
1028     EXPECT_EQ(result2, OHOS::ERR_OK);
1029 
1030     auto result3 = ConnectManager()->ScheduleDisconnectAbilityDoneLocked(token);
1031     EXPECT_EQ(result3, OHOS::AAFwk::INVALID_CONNECTION_STATE);
1032 
1033     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
1034 
1035     auto result4 = ConnectManager()->ScheduleDisconnectAbilityDoneLocked(token);
1036     EXPECT_EQ(result4, OHOS::ERR_OK);
1037 }
1038 
1039 /*
1040  * Feature: AbilityConnectManager
1041  * Function: ScheduleCommandAbilityDoneLocked
1042  * SubFunction: NA
1043  * FunctionPoints: ScheduleCommandAbilityDoneLocked
1044  * EnvConditions:NA
1045  * CaseDescription: Verify the ScheduleCommandAbilityDoneLocked process
1046  */
1047 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_021, TestSize.Level1)
1048 {
1049     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1050     ConnectManager()->SetEventHandler(handler);
1051 
1052     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
1053     auto result = ConnectManager()->ScheduleCommandAbilityDoneLocked(nullToken);
1054     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
1055 
1056     std::shared_ptr<AbilityRecord> ability = nullptr;
1057     OHOS::sptr<OHOS::IRemoteObject> token1 = new OHOS::AAFwk::Token(ability);
1058     auto result1 = ConnectManager()->ScheduleCommandAbilityDoneLocked(token1);
1059     EXPECT_EQ(result1, OHOS::ERR_INVALID_VALUE);
1060 
1061     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1062     auto elementName = abilityRequest_.want.GetElement();
1063     std::string elementNameUri = elementName.GetURI();
1064     auto serviceMap = ConnectManager()->GetServiceMap();
1065     auto abilityRecord = serviceMap.at(elementNameUri);
1066     auto token = abilityRecord->GetToken();
1067 
1068     auto result2 = ConnectManager()->ScheduleCommandAbilityDoneLocked(token);
1069     EXPECT_EQ(result2, OHOS::AAFwk::INVALID_CONNECTION_STATE);
1070 
1071     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
1072     auto result3 = ConnectManager()->ScheduleCommandAbilityDoneLocked(token);
1073     EXPECT_EQ(result3, OHOS::ERR_OK);
1074 }
1075 
1076 /*
1077  * Feature: AbilityConnectManager
1078  * Function: GetExtensionByTokenFromSeriveMap
1079  * SubFunction: NA
1080  * FunctionPoints: GetExtensionByTokenFromSeriveMap
1081  * EnvConditions:NA
1082  * CaseDescription: Verify the GetExtensionByTokenFromSeriveMap process
1083  */
1084 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_022, TestSize.Level1)
1085 {
1086     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1087     auto elementName = abilityRequest_.want.GetElement();
1088     std::string elementNameUri = elementName.GetURI();
1089     auto serviceMap = ConnectManager()->GetServiceMap();
1090     auto abilityRecord = serviceMap.at(elementNameUri);
1091     auto token = abilityRecord->GetToken();
1092 
1093     auto ability = ConnectManager()->GetExtensionByTokenFromSeriveMap(token);
1094     EXPECT_EQ(abilityRecord, ability);
1095 
1096     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
1097     auto ability1 = ConnectManager()->GetExtensionByTokenFromSeriveMap(nullToken);
1098     EXPECT_EQ(nullptr, ability1);
1099 }
1100 
1101 /*
1102  * Feature: AbilityConnectManager
1103  * Function: OnAbilityDied
1104  * SubFunction:
1105  * FunctionPoints: OnAbilityDied
1106  * EnvConditions:NA
1107  * CaseDescription: Verify the OnAbilityDied process
1108  */
1109 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_024, TestSize.Level1)
1110 {
1111     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1112     ConnectManager()->SetEventHandler(handler);
1113 
1114     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1115     EXPECT_EQ(0, result);
1116 
1117     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr);
1118     EXPECT_EQ(0, result1);
1119 
1120     auto result2 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackA_, nullptr);
1121     EXPECT_EQ(0, result2);
1122 
1123     auto result3 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackB_, nullptr);
1124     EXPECT_EQ(0, result3);
1125 
1126     auto listA = ConnectManager()->GetConnectRecordListByCallback(callbackA_);
1127     EXPECT_EQ(static_cast<int>(listA.size()), 2);
1128 
1129     for (auto &it : listA) {
1130         it->SetConnectState(ConnectionState::CONNECTED);
1131     }
1132 
1133     auto listB = ConnectManager()->GetConnectRecordListByCallback(callbackB_);
1134     EXPECT_EQ(static_cast<int>(listB.size()), 2);
1135 
1136     for (auto &it : listB) {
1137         it->SetConnectState(ConnectionState::CONNECTED);
1138     }
1139 
1140     auto elementName = abilityRequest_.want.GetElement();
1141     std::string elementNameUri = elementName.GetURI();
1142     auto serviceMap = ConnectManager()->GetServiceMap();
1143     auto abilityRecord = serviceMap.at(elementNameUri);
1144     auto token = abilityRecord->GetToken();
1145 
1146     ConnectManager()->OnAbilityDied(abilityRecord, 0);
1147     WaitUntilTaskDone(handler);
1148     auto list = abilityRecord->GetConnectRecordList();
1149     EXPECT_EQ(static_cast<int>(list.size()), 0);
1150 
1151     auto elementName1 = abilityRequest1_.want.GetElement();
1152     std::string elementNameUri1 = elementName1.GetURI();
1153     serviceMap = ConnectManager()->GetServiceMap();
1154     auto abilityRecord1 = serviceMap.at(elementNameUri1);
1155     auto token1 = abilityRecord1->GetToken();
1156 
1157     ConnectManager()->OnAbilityDied(abilityRecord1, 0);
1158     WaitUntilTaskDone(handler);
1159     auto list1 = abilityRecord1->GetConnectRecordList();
1160     EXPECT_EQ(static_cast<int>(list1.size()), 0);
1161 }
1162 
1163 /*
1164  * Feature: AbilityConnectManager
1165  * Function: DispatchInactive
1166  * SubFunction:
1167  * FunctionPoints: DispatchInactive
1168  * EnvConditions:NA
1169  * CaseDescription: Verify the DispatchInactive process
1170  */
1171 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_025, TestSize.Level1)
1172 {
1173     std::shared_ptr<AbilityRecord> ability = nullptr;
1174     auto result = ConnectManager()->DispatchInactive(ability, OHOS::AAFwk::AbilityState::INACTIVATING);
1175     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
1176 
1177     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1178     ConnectManager()->SetEventHandler(handler);
1179 
1180     auto result3 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1181     EXPECT_EQ(0, result3);
1182 
1183     auto elementName = abilityRequest_.want.GetElement();
1184     std::string elementNameUri = elementName.GetURI();
1185     auto serviceMap = ConnectManager()->GetServiceMap();
1186     auto abilityRecord = serviceMap.at(elementNameUri);
1187     auto token = abilityRecord->GetToken();
1188 
1189     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
1190     auto result1 = ConnectManager()->DispatchInactive(abilityRecord, OHOS::AAFwk::AbilityState::INACTIVATING);
1191     EXPECT_EQ(result1, OHOS::ERR_INVALID_VALUE);
1192 
1193     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
1194     auto result2 = ConnectManager()->DispatchInactive(abilityRecord, OHOS::AAFwk::AbilityState::INACTIVATING);
1195     EXPECT_EQ(result2, OHOS::ERR_OK);
1196     EXPECT_EQ(abilityRecord->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVE);
1197 }
1198 
1199 /*
1200  * Feature: AbilityConnectManager
1201  * Function: DispatchInactive
1202  * SubFunction:
1203  * FunctionPoints: DispatchInactive
1204  * EnvConditions:NA
1205  * CaseDescription: Verify the DispatchInactive process
1206  */
1207 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_026, TestSize.Level1)
1208 {
1209     std::shared_ptr<AbilityRecord> ability = nullptr;
1210     auto result = ConnectManager()->DispatchInactive(ability, OHOS::AAFwk::AbilityState::INACTIVATING);
1211     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
1212 
1213     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1214     ConnectManager()->SetEventHandler(handler);
1215 
1216     auto result3 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1217     EXPECT_EQ(0, result3);
1218 
1219     auto elementName = abilityRequest_.want.GetElement();
1220     std::string elementNameUri = elementName.GetURI();
1221     auto serviceMap = ConnectManager()->GetServiceMap();
1222     auto abilityRecord = serviceMap.at(elementNameUri);
1223     auto token = abilityRecord->GetToken();
1224 
1225     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
1226     auto result1 = ConnectManager()->DispatchInactive(abilityRecord, OHOS::AAFwk::AbilityState::INACTIVATING);
1227     EXPECT_EQ(result1, OHOS::ERR_INVALID_VALUE);
1228 
1229     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
1230     auto result2 = ConnectManager()->DispatchInactive(abilityRecord, OHOS::AAFwk::AbilityState::INACTIVATING);
1231     EXPECT_EQ(result2, OHOS::ERR_OK);
1232     EXPECT_EQ(abilityRecord->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVE);
1233 }
1234 
1235 /*
1236  * Feature: AbilityConnectManager
1237  * Function: AddConnectDeathRecipient
1238  * SubFunction:
1239  * FunctionPoints: AddConnectDeathRecipient
1240  * EnvConditions:NA
1241  * CaseDescription: Verify the AddConnectDeathRecipient process
1242  */
1243 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_027, TestSize.Level1)
1244 {
1245     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1246     ConnectManager()->SetEventHandler(handler);
1247 
1248     ConnectManager()->AddConnectDeathRecipient(nullptr);
1249     EXPECT_TRUE(ConnectManager()->recipientMap_.empty());
1250 
1251     ConnectManager()->AddConnectDeathRecipient(callbackA_);
1252     EXPECT_EQ(static_cast<int>(ConnectManager()->recipientMap_.size()), 1);
1253 
1254     // Add twice, do not add repeatedly
1255     ConnectManager()->AddConnectDeathRecipient(callbackA_);
1256     EXPECT_EQ(static_cast<int>(ConnectManager()->recipientMap_.size()), 1);
1257 }
1258 
1259 /*
1260  * Feature: AbilityConnectManager
1261  * Function: RemoveConnectDeathRecipient
1262  * SubFunction:
1263  * FunctionPoints: RemoveConnectDeathRecipient
1264  * EnvConditions:NA
1265  * CaseDescription: Verify the RemoveConnectDeathRecipient process
1266  */
1267 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_028, TestSize.Level1)
1268 {
1269     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1270     ConnectManager()->SetEventHandler(handler);
1271 
1272     ConnectManager()->AddConnectDeathRecipient(callbackA_);
1273     EXPECT_EQ(static_cast<int>(ConnectManager()->recipientMap_.size()), 1);
1274 
1275     ConnectManager()->RemoveConnectDeathRecipient(nullptr);
1276     EXPECT_FALSE(ConnectManager()->recipientMap_.empty());
1277 
1278     ConnectManager()->RemoveConnectDeathRecipient(callbackA_);
1279     EXPECT_TRUE(ConnectManager()->recipientMap_.empty());
1280 }
1281 
1282 /*
1283  * Feature: AbilityConnectManager
1284  * Function: OnCallBackDied
1285  * SubFunction:
1286  * FunctionPoints: OnCallBackDied
1287  * EnvConditions:NA
1288  * CaseDescription: Verify the OnCallBackDied process
1289  */
1290 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_029, TestSize.Level1)
1291 {
1292     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1293     ConnectManager()->SetEventHandler(handler);
1294 
1295     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1296     WaitUntilTaskDone(handler);
1297     EXPECT_EQ(0, result);
1298 
1299     ConnectManager()->OnCallBackDied(nullptr);
1300     WaitUntilTaskDone(handler);
1301     auto connectMap = ConnectManager()->GetConnectMap();
1302     auto connectRecordList = connectMap.at(callbackA_->AsObject());
1303     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
1304     for (auto &it : connectRecordList) {
1305         EXPECT_NE(it->GetAbilityConnectCallback(), nullptr);
1306     }
1307 
1308     ConnectManager()->OnCallBackDied(callbackA_->AsObject());
1309     WaitUntilTaskDone(handler);
1310     auto cMap = ConnectManager()->GetConnectMap();
1311     connectRecordList = connectMap.at(callbackA_->AsObject());
1312     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
1313     for (auto &it : connectRecordList) {
1314         EXPECT_EQ(it->GetAbilityConnectCallback(), nullptr);
1315     }
1316 }
1317 
1318 /*
1319  * Feature: AbilityConnectManager
1320  * Function: TerminateAbility
1321  * SubFunction: TerminateAbility
1322  * FunctionPoints: NA
1323  * EnvConditions: NA
1324  * CaseDescription: Verify AbilityConnectManager TerminateAbility
1325  */
1326 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_TerminateAbility_001, TestSize.Level1)
1327 {
1328     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1329     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1330     int requestCode = 0;
1331     std::shared_ptr<CallerRecord> caller = std::make_shared<CallerRecord>(requestCode, abilityRecord);
1332     abilityRecord->callerList_.emplace_back(caller);
1333     abilityRecord->abilityInfo_.visible = false;
1334     connectManager->serviceMap_.emplace("first", abilityRecord);
1335     int res = connectManager->TerminateAbility(abilityRecord, requestCode);
1336     EXPECT_EQ(res, ABILITY_VISIBLE_FALSE_DENY_REQUEST);
1337 }
1338 
1339 /*
1340  * Feature: AbilityConnectManager
1341  * Function: TerminateAbility
1342  * SubFunction: TerminateAbility
1343  * FunctionPoints: NA
1344  * EnvConditions: NA
1345  * CaseDescription: Verify AbilityConnectManager TerminateAbility
1346  */
1347 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_TerminateAbility_002, TestSize.Level1)
1348 {
1349     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1350     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1351     int requestCode = 0;
1352     std::shared_ptr<CallerRecord> caller = std::make_shared<CallerRecord>(0, abilityRecord);
1353     abilityRecord->callerList_.emplace_back(caller);
1354     abilityRecord->abilityInfo_.visible = true;
1355     connectManager->serviceMap_.emplace("first", abilityRecord);
1356     int res = connectManager->TerminateAbility(abilityRecord, requestCode);
1357     EXPECT_EQ(res, ERR_OK);
1358 }
1359 
1360 /*
1361  * Feature: AbilityConnectManager
1362  * Function: TerminateAbility
1363  * SubFunction: TerminateAbility
1364  * FunctionPoints: NA
1365  * EnvConditions: NA
1366  * CaseDescription: Verify AbilityConnectManager TerminateAbility
1367  */
1368 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_TerminateAbility_003, TestSize.Level1)
1369 {
1370     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1371     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1372     int requestCode = 0;
1373     std::shared_ptr<CallerRecord> caller = std::make_shared<CallerRecord>(1, abilityRecord);
1374     abilityRecord->callerList_.emplace_back(caller);
1375     connectManager->serviceMap_.emplace("first", abilityRecord);
1376     int res = connectManager->TerminateAbility(abilityRecord, requestCode);
1377     EXPECT_EQ(res, NO_FOUND_ABILITY_BY_CALLER);
1378 }
1379 
1380 /*
1381  * Feature: AbilityConnectManager
1382  * Function: TerminateAbility
1383  * SubFunction: TerminateAbility
1384  * FunctionPoints: NA
1385  * EnvConditions: NA
1386  * CaseDescription: Verify AbilityConnectManager TerminateAbility
1387  */
1388 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_TerminateAbility_004, TestSize.Level1)
1389 {
1390     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1391     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1392     int requestCode = 0;
1393     std::shared_ptr<CallerRecord> caller = std::make_shared<CallerRecord>(1, serviceRecord1_);
1394     abilityRecord->callerList_.emplace_back(caller);
1395     connectManager->serviceMap_.emplace("first", abilityRecord);
1396     int res = connectManager->TerminateAbility(abilityRecord, requestCode);
1397     EXPECT_EQ(res, NO_FOUND_ABILITY_BY_CALLER);
1398 }
1399 
1400 /*
1401  * Feature: AbilityConnectManager
1402  * Function: StartAbilityLocked
1403  * SubFunction: StartAbilityLocked
1404  * FunctionPoints: NA
1405  * EnvConditions: NA
1406  * CaseDescription: Verify AbilityConnectManager StartAbilityLocked
1407  */
1408 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_StartAbilityLocked_001, TestSize.Level1)
1409 {
1410     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1411     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1412     AbilityRequest abilityRequest;
1413     abilityRequest.abilityInfo.deviceId = "id";
1414     abilityRequest.abilityInfo.bundleName = "bundle";
1415     abilityRequest.abilityInfo.name = "name";
1416     abilityRequest.abilityInfo.moduleName = "module";
1417     std::string stringUri = "id/bundle/module/name";
1418     AppExecFwk::ElementName element(abilityRequest.abilityInfo.deviceId, abilityRequest.abilityInfo.bundleName,
1419         abilityRequest.abilityInfo.name, abilityRequest.abilityInfo.moduleName);
1420     EXPECT_EQ(element.GetURI(), stringUri);
1421     abilityRecord->currentState_ = AbilityState::ACTIVE;
1422     abilityRecord->SetPreAbilityRecord(serviceRecord1_);
1423     connectManager->serviceMap_.emplace(stringUri, abilityRecord);
1424     int res = connectManager->StartAbilityLocked(abilityRequest);
1425     EXPECT_EQ(res, ERR_OK);
1426 }
1427 
1428 /*
1429  * Feature: AbilityConnectManager
1430  * Function: TerminateAbilityResultLocked
1431  * SubFunction: TerminateAbilityResultLocked
1432  * FunctionPoints: NA
1433  * EnvConditions: NA
1434  * CaseDescription: Verify AbilityConnectManager TerminateAbilityResultLocked
1435  */
1436 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_TerminateAbilityResultLocked_001, TestSize.Level1)
1437 {
1438     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1439     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1440     int startId = 1;
1441     abilityRecord->startId_ = startId;
1442     int res1 = connectManager->TerminateAbilityResultLocked(abilityRecord->GetToken(), startId);
1443     EXPECT_NE(res1, TERMINATE_ABILITY_RESULT_FAILED);
1444     abilityRecord->AddStartId();
1445     std::string stringUri = "id/bundle/module/name";
1446     connectManager->serviceMap_.emplace(stringUri, abilityRecord);
1447     int res2 = connectManager->TerminateAbilityResultLocked(abilityRecord->GetToken(), startId);
1448     EXPECT_EQ(res2, TERMINATE_ABILITY_RESULT_FAILED);
1449 }
1450 
1451 /*
1452  * Feature: AbilityConnectManager
1453  * Function: GetOrCreateServiceRecord
1454  * SubFunction: GetOrCreateServiceRecord
1455  * FunctionPoints: NA
1456  * EnvConditions: NA
1457  * CaseDescription: Verify AbilityConnectManager GetOrCreateServiceRecord
1458  */
1459 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetOrCreateServiceRecord_001, TestSize.Level1)
1460 {
1461     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1462     AbilityRequest abilityRequest;
1463     bool isCreatedByConnect = false;
1464     std::shared_ptr<AbilityRecord> targetService = nullptr;
1465     bool isLoadedAbility = false;
1466     abilityRequest.abilityInfo.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
1467     connectManager->serviceMap_.clear();
1468     connectManager->GetOrCreateServiceRecord(abilityRequest, isCreatedByConnect, targetService, isLoadedAbility);
1469 }
1470 
1471 /*
1472  * Feature: AbilityConnectManager
1473  * Function: ConnectAbilityLocked
1474  * SubFunction: ConnectAbilityLocked
1475  * FunctionPoints: NA
1476  * EnvConditions: NA
1477  * CaseDescription: Verify AbilityConnectManager ConnectAbilityLocked
1478  */
1479 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_ConnectAbilityLocked_001, TestSize.Level1)
1480 {
1481     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1482     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1483     connectManager->SetEventHandler(handler);
1484     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1485     AbilityRequest abilityRequest;
1486     sptr<IRemoteObject> callerToken = abilityRecord->GetToken();
1487     OHOS::sptr<IAbilityConnection> connect = new AbilityConnectCallback();
1488     OHOS::sptr<IAbilityConnection> callback1 = new AbilityConnectCallback();
1489     OHOS::sptr<IAbilityConnection> callback2 = new AbilityConnectCallback();
1490     std::shared_ptr<ConnectionRecord> connection1 =
1491         std::make_shared<ConnectionRecord>(callerToken, abilityRecord, callback1);
1492     std::shared_ptr<ConnectionRecord> connection2 =
1493         std::make_shared<ConnectionRecord>(callerToken, abilityRecord, callback2);
1494     abilityRequest.abilityInfo.deviceId = "id";
1495     abilityRequest.abilityInfo.bundleName = "bundle";
1496     abilityRequest.abilityInfo.name = "name";
1497     abilityRequest.abilityInfo.moduleName = "module";
1498     std::string stringUri = "id/bundle/name/module";
1499     abilityRecord->currentState_ = AbilityState::ACTIVE;
1500     abilityRecord->AddConnectRecordToList(connection1);
1501     connectManager->serviceMap_.emplace(stringUri, abilityRecord);
1502     connectManager->connectMap_.clear();
1503     connectManager->ConnectAbilityLocked(abilityRequest, connect, callerToken);
1504     abilityRecord->AddConnectRecordToList(connection2);
1505     connectManager->ConnectAbilityLocked(abilityRequest, connect, callerToken);
1506     connectManager->SetEventHandler(nullptr);
1507     connectManager->ConnectAbilityLocked(abilityRequest, connect, callerToken);
1508 }
1509 
1510 /*
1511  * Feature: AbilityConnectManager
1512  * Function: AttachAbilityThreadLocked
1513  * SubFunction: AttachAbilityThreadLocked
1514  * FunctionPoints: NA
1515  * EnvConditions: NA
1516  * CaseDescription: Verify AbilityConnectManager AttachAbilityThreadLocked
1517  */
1518 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_AttachAbilityThreadLocked_001, TestSize.Level1)
1519 {
1520     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1521     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1522     sptr<IAbilityScheduler> scheduler = nullptr;
1523     sptr<IRemoteObject> token = abilityRecord->GetToken();
1524     connectManager->serviceMap_.emplace("first", abilityRecord);
1525     connectManager->eventHandler_ = nullptr;
1526     int res = connectManager->AttachAbilityThreadLocked(scheduler, token);
1527     EXPECT_EQ(res, ERR_OK);
1528 }
1529 
1530 /*
1531  * Feature: AbilityConnectManager
1532  * Function: OnAppStateChanged
1533  * SubFunction: OnAppStateChanged
1534  * FunctionPoints: NA
1535  * EnvConditions: NA
1536  * CaseDescription: Verify AbilityConnectManager OnAppStateChanged
1537  */
1538 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAppStateChanged_001, TestSize.Level1)
1539 {
1540     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1541     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1542     AppInfo info;
1543     std::string bundleName = "bundleName";
1544     std::string name = "name";
1545     int32_t uid = 0;
1546     info.processName = bundleName;
1547     abilityRecord->applicationInfo_.bundleName = bundleName;
1548     abilityRecord->applicationInfo_.name = name;
1549     abilityRecord->abilityInfo_.uid = uid;
1550     info.appData.push_back({name, uid});
1551     connectManager->serviceMap_.emplace("first", abilityRecord);
1552     connectManager->OnAppStateChanged(info);
1553 }
1554 
1555 /*
1556  * Feature: AbilityConnectManager
1557  * Function: OnAppStateChanged
1558  * SubFunction: OnAppStateChanged
1559  * FunctionPoints: NA
1560  * EnvConditions: NA
1561  * CaseDescription: Verify AbilityConnectManager OnAppStateChanged
1562  */
1563 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAppStateChanged_002, TestSize.Level1)
1564 {
1565     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1566     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1567     AppInfo info;
1568     std::string bundleName = "bundleName";
1569     std::string name = "name";
1570     int32_t uid = 0;
1571     info.processName = "";
1572     abilityRecord->applicationInfo_.bundleName = bundleName;
1573     abilityRecord->applicationInfo_.name = name;
1574     abilityRecord->abilityInfo_.uid = uid;
1575     info.appData.push_back({name, uid});
1576     connectManager->serviceMap_.emplace("first", abilityRecord);
1577     connectManager->serviceMap_.emplace("first", nullptr);
1578     connectManager->OnAppStateChanged(info);
1579 }
1580 
1581 /*
1582  * Feature: AbilityConnectManager
1583  * Function: AbilityTransitionDone
1584  * SubFunction: AbilityTransitionDone
1585  * FunctionPoints: NA
1586  * EnvConditions: NA
1587  * CaseDescription: Verify AbilityConnectManager AbilityTransitionDone
1588  */
1589 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_AbilityTransitionDone_001, TestSize.Level1)
1590 {
1591     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1592     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1593     sptr<IRemoteObject> token = abilityRecord->GetToken();
1594     int state = AbilityState::INACTIVE;
1595     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
1596     abilityRecord->SetAbilityState(AbilityState::ACTIVE);
1597     connectManager->serviceMap_.emplace("first", abilityRecord);
1598     int res1 = connectManager->AbilityTransitionDone(token, state);
1599     EXPECT_EQ(res1, ERR_INVALID_VALUE);
1600     state = AbilityState::INITIAL;
1601     connectManager->MoveToTerminatingMap(abilityRecord);
1602     int res2 = connectManager->AbilityTransitionDone(token, state);
1603     EXPECT_EQ(res2, ERR_OK);
1604 }
1605 
1606 /*
1607  * Feature: AbilityConnectManager
1608  * Function: ScheduleConnectAbilityDoneLocked
1609  * SubFunction: ScheduleConnectAbilityDoneLocked
1610  * FunctionPoints: NA
1611  * EnvConditions: NA
1612  * CaseDescription: Verify AbilityConnectManager ScheduleConnectAbilityDoneLocked
1613  */
1614 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_ScheduleConnectAbilityDoneLocked_001, TestSize.Level1)
1615 {
1616     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1617     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1618     sptr<IRemoteObject> token = abilityRecord->GetToken();
1619     sptr<IRemoteObject> remoteObject = nullptr;
1620     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
1621     abilityRecord->SetAbilityState(AbilityState::INACTIVE);
1622     connectManager->serviceMap_.emplace("first", abilityRecord);
1623     int res = connectManager->ScheduleConnectAbilityDoneLocked(token, remoteObject);
1624     EXPECT_EQ(res, ERR_OK);
1625 }
1626 
1627 /*
1628  * Feature: AbilityConnectManager
1629  * Function: ScheduleDisconnectAbilityDoneLocked
1630  * SubFunction: ScheduleDisconnectAbilityDoneLocked
1631  * FunctionPoints: NA
1632  * EnvConditions: NA
1633  * CaseDescription: Verify AbilityConnectManager ScheduleDisconnectAbilityDoneLocked
1634  */
1635 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_ScheduleDisconnectAbilityDoneLocked_001, TestSize.Level1)
1636 {
1637     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1638     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1639     OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
1640     std::shared_ptr<ConnectionRecord> connection =
1641         std::make_shared<ConnectionRecord>(abilityRecord->GetToken(), abilityRecord, callback);
1642     sptr<IRemoteObject> token = abilityRecord->GetToken();
1643     connection->SetConnectState(ConnectionState::DISCONNECTING);
1644     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
1645     abilityRecord->SetAbilityState(AbilityState::INACTIVE);
1646     abilityRecord->connRecordList_.push_back(connection);
1647     connectManager->serviceMap_.emplace("first", abilityRecord);
1648     int res = connectManager->ScheduleDisconnectAbilityDoneLocked(token);
1649     EXPECT_EQ(res, INVALID_CONNECTION_STATE);
1650     abilityRecord->AddStartId();
1651     abilityRecord->SetAbilityState(AbilityState::ACTIVE);
1652     connectManager->serviceMap_.emplace("first", abilityRecord);
1653     res = connectManager->ScheduleDisconnectAbilityDoneLocked(token);
1654     EXPECT_EQ(res, ERR_OK);
1655 }
1656 
1657 /*
1658  * Feature: AbilityConnectManager
1659  * Function: ScheduleCommandAbilityDoneLocked
1660  * SubFunction: ScheduleCommandAbilityDoneLocked
1661  * FunctionPoints: NA
1662  * EnvConditions: NA
1663  * CaseDescription: Verify AbilityConnectManager ScheduleCommandAbilityDoneLocked
1664  */
1665 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_ScheduleCommandAbilityDoneLocked_001, TestSize.Level1)
1666 {
1667     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1668     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1669     sptr<IRemoteObject> token = abilityRecord->GetToken();
1670     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
1671     abilityRecord->SetAbilityState(AbilityState::INACTIVE);
1672     abilityRecord->connRecordList_.clear();
1673     connectManager->serviceMap_.emplace("first", abilityRecord);
1674     int res = connectManager->ScheduleCommandAbilityDoneLocked(token);
1675     EXPECT_EQ(res, ERR_OK);
1676 }
1677 
1678 /*
1679  * Feature: AbilityConnectManager
1680  * Function: CompleteCommandAbility
1681  * SubFunction: CompleteCommandAbility
1682  * FunctionPoints: NA
1683  * EnvConditions: NA
1684  * CaseDescription: Verify AbilityConnectManager CompleteCommandAbility
1685  */
1686 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_CompleteCommandAbility_001, TestSize.Level1)
1687 {
1688     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1689     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1690     connectManager->eventHandler_ = nullptr;
1691     connectManager->CompleteCommandAbility(abilityRecord);
1692     EXPECT_TRUE(abilityRecord->IsAbilityState(AbilityState::ACTIVE));
1693 }
1694 
1695 /*
1696  * Feature: AbilityConnectManager
1697  * Function: GetServiceRecordByElementName
1698  * SubFunction: GetServiceRecordByElementName
1699  * FunctionPoints: NA
1700  * EnvConditions: NA
1701  * CaseDescription: Verify AbilityConnectManager GetServiceRecordByElementName
1702  */
1703 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetServiceRecordByElementName_001, TestSize.Level1)
1704 {
1705     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1706     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1707     std::string element = "first";
1708     connectManager->serviceMap_.emplace(element, abilityRecord);
1709     auto res = connectManager->GetServiceRecordByElementName(element);
1710     EXPECT_NE(res, nullptr);
1711 }
1712 
1713 /*
1714  * Feature: AbilityConnectManager
1715  * Function: GetExtensionByTokenFromSeriveMap
1716  * SubFunction: GetExtensionByTokenFromSeriveMap
1717  * FunctionPoints: NA
1718  * EnvConditions: NA
1719  * CaseDescription: Verify AbilityConnectManager GetExtensionByTokenFromSeriveMap
1720  */
1721 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetExtensionByTokenFromSeriveMap_001, TestSize.Level1)
1722 {
1723     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1724     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1725     sptr<IRemoteObject> token = abilityRecord->GetToken();
1726     connectManager->serviceMap_.emplace("first", nullptr);
1727     auto res = connectManager->GetExtensionByTokenFromSeriveMap(token);
1728     EXPECT_EQ(res, nullptr);
1729 }
1730 
1731 /*
1732  * Feature: AbilityConnectManager
1733  * Function: GetConnectRecordListByCallback
1734  * SubFunction: GetConnectRecordListByCallback
1735  * FunctionPoints: NA
1736  * EnvConditions: NA
1737  * CaseDescription: Verify AbilityConnectManager GetConnectRecordListByCallback
1738  */
1739 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetConnectRecordListByCallback_001, TestSize.Level1)
1740 {
1741     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1742     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1743     sptr<IAbilityConnection> callback = new AbilityConnectCallback();
1744     connectManager->connectMap_.clear();
1745     auto res = connectManager->GetConnectRecordListByCallback(callback);
1746     EXPECT_EQ(res.size(), 0);
1747 }
1748 
1749 /*
1750  * Feature: AbilityConnectManager
1751  * Function: GetAbilityRecordByEventId
1752  * SubFunction: GetAbilityRecordByEventId
1753  * FunctionPoints: NA
1754  * EnvConditions: NA
1755  * CaseDescription: Verify AbilityConnectManager GetAbilityRecordByEventId
1756  */
1757 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetAbilityRecordByEventId_001, TestSize.Level1)
1758 {
1759     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1760     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1761     int64_t eventId = 0;
1762     abilityRecord->SetEventId(eventId);
1763     connectManager->serviceMap_.emplace("first", abilityRecord);
1764     connectManager->serviceMap_.emplace("second", nullptr);
1765     auto res = connectManager->GetAbilityRecordByEventId(eventId);
1766     EXPECT_NE(res, nullptr);
1767 }
1768 
1769 /*
1770  * Feature: AbilityConnectManager
1771  * Function: LoadAbility
1772  * SubFunction: LoadAbility
1773  * FunctionPoints: NA
1774  * EnvConditions: NA
1775  * CaseDescription: Verify AbilityConnectManager LoadAbility
1776  */
1777 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_LoadAbility_001, TestSize.Level1)
1778 {
1779     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1780     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1781     abilityRecord->isLauncherRoot_ = true;
1782     abilityRecord->isRestarting_ = true;
1783     abilityRecord->isLauncherAbility_ = true;
1784     abilityRecord->restartCount_ = -1;
1785     EXPECT_FALSE(abilityRecord->CanRestartRootLauncher());
1786     connectManager->LoadAbility(abilityRecord);
1787 }
1788 
1789 /*
1790  * Feature: AbilityConnectManager
1791  * Function: LoadAbility
1792  * SubFunction: LoadAbility
1793  * FunctionPoints: NA
1794  * EnvConditions: NA
1795  * CaseDescription: Verify AbilityConnectManager LoadAbility
1796  */
1797 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_LoadAbility_002, TestSize.Level1)
1798 {
1799     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1800     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1801     std::shared_ptr<CallerRecord> caller1 = std::make_shared<CallerRecord>(0, abilityRecord);
1802     std::shared_ptr<CallerRecord> caller2 = std::make_shared<CallerRecord>();
1803     abilityRecord->isLauncherRoot_ = false;
1804     abilityRecord->isCreateByConnect_ = false;
1805     abilityRecord->callerList_.push_back(caller1);
1806     EXPECT_TRUE(abilityRecord->CanRestartRootLauncher());
1807     connectManager->LoadAbility(abilityRecord);
1808     abilityRecord->callerList_.push_back(caller2);
1809     connectManager->LoadAbility(abilityRecord);
1810     abilityRecord->callerList_.push_back(nullptr);
1811     connectManager->LoadAbility(abilityRecord);
1812 }
1813 
1814 /*
1815  * Feature: AbilityConnectManager
1816  * Function: PostTimeOutTask
1817  * SubFunction: PostTimeOutTask
1818  * FunctionPoints: NA
1819  * EnvConditions: NA
1820  * CaseDescription: Verify AbilityConnectManager PostTimeOutTask
1821  */
1822 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_PostTimeOutTask_001, TestSize.Level1)
1823 {
1824     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1825     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1826     uint32_t messageId = 2;
1827     connectManager->PostTimeOutTask(abilityRecord, messageId);
1828 }
1829 
1830 /*
1831  * Feature: AbilityConnectManager
1832  * Function: HandleStartTimeoutTask
1833  * SubFunction: HandleStartTimeoutTask
1834  * FunctionPoints: NA
1835  * EnvConditions: NA
1836  * CaseDescription: Verify AbilityConnectManager HandleStartTimeoutTask
1837  */
1838 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleStartTimeoutTask_001, TestSize.Level1)
1839 {
1840     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1841     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1842     int resultCode = LOAD_ABILITY_TIMEOUT;
1843     abilityRecord->abilityInfo_.name = "abilityName";
1844     connectManager->HandleStartTimeoutTask(abilityRecord, resultCode);
1845 }
1846 
1847 /*
1848  * Feature: AbilityConnectManager
1849  * Function: HandleStartTimeoutTask
1850  * SubFunction: HandleStartTimeoutTask
1851  * FunctionPoints: NA
1852  * EnvConditions: NA
1853  * CaseDescription: Verify AbilityConnectManager HandleStartTimeoutTask
1854  */
1855 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleStartTimeoutTask_002, TestSize.Level1)
1856 {
1857     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1858     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1859     int resultCode = LOAD_ABILITY_TIMEOUT;
1860     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
1861     connectManager->HandleStartTimeoutTask(abilityRecord, resultCode);
1862 }
1863 
1864 /*
1865  * Feature: AbilityConnectManager
1866  * Function: HandleStartTimeoutTask
1867  * SubFunction: HandleStartTimeoutTask
1868  * FunctionPoints: NA
1869  * EnvConditions: NA
1870  * CaseDescription: Verify AbilityConnectManager HandleStartTimeoutTask
1871  */
1872 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleStartTimeoutTask_003, TestSize.Level1)
1873 {
1874     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1875     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1876     int resultCode = CONNECTION_TIMEOUT;
1877     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
1878     connectManager->HandleStartTimeoutTask(abilityRecord, resultCode);
1879 }
1880 
1881 /*
1882  * Feature: AbilityConnectManager
1883  * Function: HandleCommandTimeoutTask
1884  * SubFunction: HandleCommandTimeoutTask
1885  * FunctionPoints: NA
1886  * EnvConditions: NA
1887  * CaseDescription: Verify AbilityConnectManager HandleCommandTimeoutTask
1888  */
1889 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleCommandTimeoutTask_001, TestSize.Level1)
1890 {
1891     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1892     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1893     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
1894     connectManager->HandleCommandTimeoutTask(abilityRecord);
1895     abilityRecord->abilityInfo_.name = "abilityName";
1896     connectManager->HandleCommandTimeoutTask(abilityRecord);
1897 }
1898 
1899 /*
1900  * Feature: AbilityConnectManager
1901  * Function: HandleTerminateDisconnectTask
1902  * SubFunction: HandleTerminateDisconnectTask
1903  * FunctionPoints: NA
1904  * EnvConditions: NA
1905  * CaseDescription: Verify AbilityConnectManager HandleTerminateDisconnectTask
1906  */
1907 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleTerminateDisconnectTask_001, TestSize.Level1)
1908 {
1909     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1910     OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
1911     std::shared_ptr<ConnectionRecord> connection =
1912         std::make_shared<ConnectionRecord>(nullptr, nullptr, callback);
1913     AbilityConnectManager::ConnectListType connectlist;
1914     connectlist.push_back(nullptr);
1915     connectlist.push_back(connection);
1916     connectManager->HandleTerminateDisconnectTask(connectlist);
1917 }
1918 
1919 /*
1920  * Feature: AbilityConnectManager
1921  * Function: DispatchInactive
1922  * SubFunction: DispatchInactive
1923  * FunctionPoints: NA
1924  * EnvConditions: NA
1925  * CaseDescription: Verify AbilityConnectManager DispatchInactive
1926  */
1927 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_DispatchInactive_001, TestSize.Level1)
1928 {
1929     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1930     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1931     int state = 0;
1932     abilityRecord->SetAbilityState(AbilityState::INACTIVATING);
1933     abilityRecord->isCreateByConnect_ = false;
1934     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1935     connectManager->SetEventHandler(handler);
1936     int res = connectManager->DispatchInactive(abilityRecord, state);
1937     EXPECT_EQ(res, ERR_OK);
1938 }
1939 
1940 /*
1941  * Feature: AbilityConnectManager
1942  * Function: DispatchTerminate
1943  * SubFunction: DispatchTerminate
1944  * FunctionPoints: NA
1945  * EnvConditions: NA
1946  * CaseDescription: Verify AbilityConnectManager DispatchTerminate
1947  */
1948 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_DispatchTerminate_001, TestSize.Level1)
1949 {
1950     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1951     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1952     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
1953     connectManager->SetEventHandler(handler);
1954     int res = connectManager->DispatchTerminate(abilityRecord);
1955     EXPECT_EQ(res, ERR_OK);
1956 }
1957 
1958 /*
1959  * Feature: AbilityConnectManager
1960  * Function: CommandAbility
1961  * SubFunction: CommandAbility
1962  * FunctionPoints: NA
1963  * EnvConditions: NA
1964  * CaseDescription: Verify AbilityConnectManager CommandAbility
1965  */
1966 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_CommandAbility_001, TestSize.Level1)
1967 {
1968     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1969     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1970     connectManager->SetEventHandler(nullptr);
1971     connectManager->CommandAbility(abilityRecord);
1972 }
1973 
1974 /*
1975  * Feature: AbilityConnectManager
1976  * Function: TerminateDone
1977  * SubFunction: TerminateDone
1978  * FunctionPoints: NA
1979  * EnvConditions: NA
1980  * CaseDescription: Verify AbilityConnectManager TerminateDone
1981  */
1982 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_TerminateDone_001, TestSize.Level1)
1983 {
1984     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1985     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1986     abilityRecord->SetAbilityState(AbilityState::TERMINATING);
1987     connectManager->TerminateDone(abilityRecord);
1988 }
1989 
1990 /*
1991  * Feature: AbilityConnectManager
1992  * Function: IsAbilityConnected
1993  * SubFunction: IsAbilityConnected
1994  * FunctionPoints: NA
1995  * EnvConditions: NA
1996  * CaseDescription: Verify AbilityConnectManager IsAbilityConnected
1997  */
1998 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_IsAbilityConnected_001, TestSize.Level1)
1999 {
2000     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2001     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2002     std::list<std::shared_ptr<ConnectionRecord>> connectRecordList;
2003     OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
2004     std::shared_ptr<ConnectionRecord> connection =
2005         std::make_shared<ConnectionRecord>(abilityRecord->GetToken(), abilityRecord, callback);
2006     connectRecordList.push_back(connection);
2007     bool res1 = connectManager->IsAbilityConnected(nullptr, connectRecordList);
2008     EXPECT_FALSE(res1);
2009     connectRecordList.push_back(nullptr);
2010     bool res2 = connectManager->IsAbilityConnected(abilityRecord, connectRecordList);
2011     EXPECT_TRUE(res2);
2012 }
2013 
2014 /*
2015  * Feature: AbilityConnectManager
2016  * Function: RemoveServiceAbility
2017  * SubFunction: RemoveServiceAbility
2018  * FunctionPoints: NA
2019  * EnvConditions: NA
2020  * CaseDescription: Verify AbilityConnectManager RemoveServiceAbility
2021  */
2022 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_RemoveServiceAbility_001, TestSize.Level1)
2023 {
2024     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2025     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2026     AbilityInfo abilityInfo;
2027     abilityRecord->abilityInfo_ = abilityInfo;
2028     connectManager->serviceMap_.clear();
2029     connectManager->RemoveServiceAbility(abilityRecord);
2030 }
2031 
2032 /*
2033  * Feature: AbilityConnectManager
2034  * Function: OnCallBackDied
2035  * SubFunction: OnCallBackDied
2036  * FunctionPoints: NA
2037  * EnvConditions: NA
2038  * CaseDescription: Verify AbilityConnectManager OnCallBackDied
2039  */
2040 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnCallBackDied_001, TestSize.Level1)
2041 {
2042     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2043     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2044     wptr<IRemoteObject> remote{ abilityRecord->GetToken() };
2045     connectManager->SetEventHandler(nullptr);
2046     connectManager->OnCallBackDied(remote);
2047 }
2048 
2049 /*
2050  * Feature: AbilityConnectManager
2051  * Function: HandleCallBackDiedTask
2052  * SubFunction: HandleCallBackDiedTask
2053  * FunctionPoints: NA
2054  * EnvConditions: NA
2055  * CaseDescription: Verify AbilityConnectManager HandleCallBackDiedTask
2056  */
2057 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleCallBackDiedTask_001, TestSize.Level1)
2058 {
2059     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2060     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2061     sptr<IRemoteObject> connect = abilityRecord->GetToken();
2062     connectManager->connectMap_.clear();
2063     connectManager->HandleCallBackDiedTask(connect);
2064 }
2065 
2066 /*
2067  * Feature: AbilityConnectManager
2068  * Function: OnAbilityDied
2069  * SubFunction: OnAbilityDied
2070  * FunctionPoints: NA
2071  * EnvConditions: NA
2072  * CaseDescription: Verify AbilityConnectManager OnAbilityDied
2073  */
2074 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAbilityDied_001, TestSize.Level1)
2075 {
2076     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2077     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2078     int32_t currentUserId = 0;
2079     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
2080     connectManager->SetEventHandler(nullptr);
2081     connectManager->OnAbilityDied(abilityRecord, currentUserId);
2082     abilityRecord->abilityInfo_.type = AbilityType::EXTENSION;
2083     connectManager->OnAbilityDied(abilityRecord, currentUserId);
2084 }
2085 
2086 /*
2087  * Feature: AbilityConnectManager
2088  * Function: OnTimeOut
2089  * SubFunction: OnTimeOut
2090  * FunctionPoints: NA
2091  * EnvConditions: NA
2092  * CaseDescription: Verify AbilityConnectManager OnTimeOut
2093  */
2094 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnTimeOut_001, TestSize.Level1)
2095 {
2096     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2097     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2098     uint32_t msgId = 2;
2099     int64_t eventId = 1;
2100     abilityRecord->SetEventId(eventId);
2101     connectManager->serviceMap_.emplace("first", abilityRecord);
2102     connectManager->OnTimeOut(msgId, eventId);
2103     msgId = 0;
2104     connectManager->OnTimeOut(msgId, eventId);
2105 }
2106 
2107 /*
2108  * Feature: AbilityConnectManager
2109  * Function: HandleInactiveTimeout
2110  * SubFunction: HandleInactiveTimeout
2111  * FunctionPoints: NA
2112  * EnvConditions: NA
2113  * CaseDescription: Verify AbilityConnectManager HandleInactiveTimeout
2114  */
2115 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleInactiveTimeout_001, TestSize.Level1)
2116 {
2117     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2118     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2119     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
2120     connectManager->HandleInactiveTimeout(abilityRecord);
2121     abilityRecord->abilityInfo_.name = "abilityName";
2122     connectManager->HandleInactiveTimeout(abilityRecord);
2123 }
2124 
2125 /*
2126  * Feature: AbilityConnectManager
2127  * Function: HandleAbilityDiedTask
2128  * SubFunction: HandleAbilityDiedTask
2129  * FunctionPoints: NA
2130  * EnvConditions: NA
2131  * CaseDescription: Verify AbilityConnectManager HandleAbilityDiedTask
2132  */
2133 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleAbilityDiedTask_001, TestSize.Level1)
2134 {
2135     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2136     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2137     int32_t currentUserId = 0;
2138     connectManager->serviceMap_.clear();
2139     connectManager->HandleAbilityDiedTask(abilityRecord, currentUserId);
2140 }
2141 
2142 /*
2143  * Feature: AbilityConnectManager
2144  * Function: RestartAbility
2145  * SubFunction: RestartAbility
2146  * FunctionPoints: NA
2147  * EnvConditions: NA
2148  * CaseDescription: Verify AbilityConnectManager RestartAbility
2149  */
2150 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_RestartAbility_001, TestSize.Level1)
2151 {
2152     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2153     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2154     int32_t currentUserId = 0;
2155     connectManager->userId_ = 1;
2156     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
2157     connectManager->RestartAbility(abilityRecord, currentUserId);
2158     connectManager->userId_ = currentUserId;
2159     connectManager->RestartAbility(abilityRecord, currentUserId);
2160 }
2161 
2162 /*
2163  * Feature: AbilityConnectManager
2164  * Function: RestartAbility
2165  * SubFunction: RestartAbility
2166  * FunctionPoints: NA
2167  * EnvConditions: NA
2168  * CaseDescription: Verify AbilityConnectManager RestartAbility
2169  */
2170 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_RestartAbility_002, TestSize.Level1)
2171 {
2172     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2173     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2174     int32_t currentUserId = 0;
2175     connectManager->userId_ = currentUserId;
2176     abilityRecord->abilityInfo_.name = "abilityName";
2177     abilityRecord->SetRestartCount(-1);
2178     connectManager->RestartAbility(abilityRecord, currentUserId);
2179 }
2180 
2181 /*
2182  * Feature: AbilityConnectManager
2183  * Function: DumpState
2184  * SubFunction: DumpState
2185  * FunctionPoints: NA
2186  * EnvConditions: NA
2187  * CaseDescription: Verify AbilityConnectManager DumpState
2188  */
2189 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_DumpState_001, TestSize.Level1)
2190 {
2191     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2192     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2193     std::vector<std::string> info;
2194     bool isClient = false;
2195     std::string args = "args";
2196     connectManager->serviceMap_.emplace(args, abilityRecord);
2197     connectManager->DumpState(info, isClient, args);
2198     connectManager->serviceMap_.clear();
2199     connectManager->DumpState(info, isClient, args);
2200     args = "";
2201     connectManager->DumpState(info, isClient, args);
2202 }
2203 
2204 /*
2205  * Feature: AbilityConnectManager
2206  * Function: DumpStateByUri
2207  * SubFunction: DumpStateByUri
2208  * FunctionPoints: NA
2209  * EnvConditions: NA
2210  * CaseDescription: Verify AbilityConnectManager DumpStateByUri
2211  */
2212 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_DumpStateByUri_001, TestSize.Level1)
2213 {
2214     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2215     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2216     std::vector<std::string> info;
2217     bool isClient = false;
2218     std::string args = "args";
2219     std::vector<std::string> params;
2220     connectManager->serviceMap_.emplace(args, abilityRecord);
2221     connectManager->DumpStateByUri(info, isClient, args, params);
2222     connectManager->serviceMap_.clear();
2223     connectManager->DumpStateByUri(info, isClient, args, params);
2224 }
2225 
2226 /*
2227  * Feature: AbilityConnectManager
2228  * Function: GetExtensionRunningInfos
2229  * SubFunction: GetExtensionRunningInfos
2230  * FunctionPoints: NA
2231  * EnvConditions: NA
2232  * CaseDescription: Verify AbilityConnectManager GetExtensionRunningInfos
2233  */
2234 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetExtensionRunningInfos_001, TestSize.Level1)
2235 {
2236     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2237     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2238     int upperLimit = 1;
2239     std::vector<ExtensionRunningInfo> info;
2240     int32_t userId = 0;
2241     bool isPerm = false;
2242     ExtensionRunningInfo extensionInfo;
2243     info.push_back(extensionInfo);
2244     connectManager->serviceMap_.emplace("first", abilityRecord);
2245     connectManager->GetExtensionRunningInfos(upperLimit, info, userId, isPerm);
2246 }
2247 
2248 /*
2249  * Feature: AbilityConnectManager
2250  * Function: GetExtensionRunningInfo
2251  * SubFunction: GetExtensionRunningInfo
2252  * FunctionPoints: NA
2253  * EnvConditions: NA
2254  * CaseDescription: Verify AbilityConnectManager GetExtensionRunningInfo
2255  */
2256 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetExtensionRunningInfo_001, TestSize.Level1)
2257 {
2258     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2259     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2260     OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
2261     std::shared_ptr<ConnectionRecord> connection =
2262         std::make_shared<ConnectionRecord>(abilityRecord->GetToken(), abilityRecord, callback);
2263     int32_t userId = 0;
2264     std::vector<ExtensionRunningInfo> info;
2265     Want want;
2266     AbilityInfo abilityInfo;
2267     ApplicationInfo applicationInfo;
2268     want.SetElementName("device", "bundle", "ability", "module");
2269     abilityRecord->SetWant(want);
2270     abilityRecord->connRecordList_.push_back(nullptr);
2271     abilityRecord->connRecordList_.push_back(connection);
2272     connectManager->GetExtensionRunningInfo(abilityRecord, userId, info);
2273 }
2274 
2275 /*
2276  * Feature: AbilityConnectManager
2277  * Function: StopAllExtensions
2278  * SubFunction: StopAllExtensions
2279  * FunctionPoints: NA
2280  * EnvConditions: NA
2281  * CaseDescription: Verify AbilityConnectManager StopAllExtensions
2282  */
2283 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_StopAllExtensions_001, TestSize.Level1)
2284 {
2285     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2286     std::shared_ptr<AbilityRecord> abilityRecord1 = serviceRecord_;
2287     std::shared_ptr<AbilityRecord> abilityRecord2 = serviceRecord_;
2288     abilityRecord1->abilityInfo_.type = AbilityType::EXTENSION;
2289     abilityRecord2->abilityInfo_.type = AbilityType::PAGE;
2290     connectManager->serviceMap_.emplace("first", abilityRecord1);
2291     connectManager->serviceMap_.emplace("second", abilityRecord2);
2292     connectManager->StopAllExtensions();
2293 }
2294 }  // namespace AAFwk
2295 }  // namespace OHOS
2296