• 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 #include <memory>
18 
19 #define private public
20 #define protected public
21 #include "ability_connect_manager.h"
22 #undef private
23 #undef protected
24 
25 #include "ability_config.h"
26 #include "ability_manager_errors.h"
27 #include "ability_scheduler.h"
28 #include "ability_util.h"
29 #include "bundlemgr/mock_bundle_manager.h"
30 #include "mock_ability_connect_callback.h"
31 #include "sa_mgr_client.h"
32 #include "system_ability_definition.h"
33 
34 using namespace testing::ext;
35 using namespace OHOS::AppExecFwk;
36 
37 namespace OHOS {
38 namespace AAFwk {
39 template<typename F>
WaitUntilTaskCalled(const F & f,const std::shared_ptr<TaskHandlerWrap> & handler,std::atomic<bool> & taskCalled)40 static void WaitUntilTaskCalled(const F& f, const std::shared_ptr<TaskHandlerWrap>& handler,
41     std::atomic<bool>& taskCalled)
42 {
43     const uint32_t maxRetryCount = 1000;
44     const uint32_t sleepTime = 1000;
45     uint32_t count = 0;
46     if (handler->SubmitTask(f)) {
47         while (!taskCalled.load()) {
48             ++count;
49             // if delay more than 1 second, break
50             if (count >= maxRetryCount) {
51                 break;
52             }
53             usleep(sleepTime);
54         }
55     }
56 }
57 
WaitUntilTaskDone(const std::shared_ptr<TaskHandlerWrap> & handler)58 static void WaitUntilTaskDone(const std::shared_ptr<TaskHandlerWrap>& handler)
59 {
60     std::atomic<bool> taskCalled(false);
61     auto f = [&taskCalled]() { taskCalled.store(true); };
62     WaitUntilTaskCalled(f, handler, taskCalled);
63 }
64 
65 class AbilityConnectManagerTest : public testing::Test {
66 public:
67     static void SetUpTestCase(void);
68     static void TearDownTestCase(void);
69     void SetUp();
70     void TearDown();
71 
72     AbilityConnectManager* ConnectManager() const;
73     std::shared_ptr<TaskHandlerWrap> TaskHandler() const;
74     std::shared_ptr<EventHandlerWrap> EventHandler() const;
75 
76     AbilityRequest GenerateAbilityRequest(const std::string& deviceName, const std::string& abilityName,
77         const std::string& appName, const std::string& bundleName, const std::string& moduleName);
78 
79     static constexpr int TEST_WAIT_TIME = 1000000;
80 
81     sptr<SessionInfo> MockSessionInfo(int32_t persistentId);
82     std::shared_ptr<AbilityRecord> InitAbilityRecord();
83 
84 protected:
85     AbilityRequest abilityRequest_{};
86     AbilityRequest abilityRequest1_{};
87     AbilityRequest abilityRequest2_{};
88     std::shared_ptr<AbilityRecord> serviceRecord_{ nullptr };
89     std::shared_ptr<AbilityRecord> serviceRecord1_{ nullptr };
90     std::shared_ptr<AbilityRecord> serviceRecord2_{ nullptr };
91     OHOS::sptr<Token> serviceToken_{ nullptr };
92     OHOS::sptr<Token> serviceToken1_{ nullptr };
93     OHOS::sptr<Token> serviceToken2_{ nullptr };
94     OHOS::sptr<IAbilityConnection> callbackA_{ nullptr };
95     OHOS::sptr<IAbilityConnection> callbackB_{ nullptr };
96 
97 private:
98     std::shared_ptr<AbilityConnectManager> connectManager_;
99     std::shared_ptr<TaskHandlerWrap> taskHandler_;
100     std::shared_ptr<EventHandlerWrap> eventHandler_;
101 };
102 
GenerateAbilityRequest(const std::string & deviceName,const std::string & abilityName,const std::string & appName,const std::string & bundleName,const std::string & moduleName)103 AbilityRequest AbilityConnectManagerTest::GenerateAbilityRequest(const std::string& deviceName,
104     const std::string& abilityName, const std::string& appName, const std::string& bundleName,
105     const std::string& moduleName)
106 {
107     ElementName element(deviceName, bundleName, abilityName, moduleName);
108     Want want;
109     want.SetElement(element);
110 
111     AbilityInfo abilityInfo;
112     abilityInfo.visible = true;
113     abilityInfo.applicationName = appName;
114     abilityInfo.type = AbilityType::SERVICE;
115     abilityInfo.name = abilityName;
116     abilityInfo.bundleName = bundleName;
117     abilityInfo.moduleName = moduleName;
118     abilityInfo.deviceId = deviceName;
119     ApplicationInfo appinfo;
120     appinfo.name = appName;
121     abilityInfo.applicationInfo = appinfo;
122     AbilityRequest abilityRequest;
123     abilityRequest.want = want;
124     abilityRequest.abilityInfo = abilityInfo;
125     abilityRequest.appInfo = appinfo;
126     abilityInfo.process = bundleName;
127 
128     return abilityRequest;
129 }
130 
MockSessionInfo(int32_t persistentId)131 sptr<SessionInfo> AbilityConnectManagerTest::MockSessionInfo(int32_t persistentId)
132 {
133     sptr<SessionInfo> sessionInfo = new (std::nothrow) SessionInfo();
134     if (!sessionInfo) {
135         HILOG_ERROR("sessionInfo is nullptr");
136         return nullptr;
137     }
138     sessionInfo->persistentId = persistentId;
139     return sessionInfo;
140 }
141 
InitAbilityRecord()142 std::shared_ptr<AbilityRecord> AbilityConnectManagerTest::InitAbilityRecord()
143 {
144     AbilityRequest abilityRequest;
145     abilityRequest.appInfo.bundleName = "com.example.unittest";
146     abilityRequest.abilityInfo.name = "MainAbility";
147     abilityRequest.abilityInfo.type = AbilityType::PAGE;
148     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
149     return abilityRecord;
150 }
151 
SetUpTestCase(void)152 void AbilityConnectManagerTest::SetUpTestCase(void)
153 {}
TearDownTestCase(void)154 void AbilityConnectManagerTest::TearDownTestCase(void)
155 {}
156 
SetUp(void)157 void AbilityConnectManagerTest::SetUp(void)
158 {
159     connectManager_ = std::make_unique<AbilityConnectManager>(0);
160     taskHandler_ = TaskHandlerWrap::CreateQueueHandler("AbilityConnectManagerTest");
161     eventHandler_ = std::make_shared<EventHandlerWrap>(taskHandler_);
162     // generate ability request
163     std::string deviceName = "device";
164     std::string abilityName = "ServiceAbility";
165     std::string appName = "hiservcie";
166     std::string bundleName = "com.ix.hiservcie";
167     std::string moduleName = "entry";
168     abilityRequest_ = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName);
169     serviceRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_);
170     serviceToken_ = serviceRecord_->GetToken();
171     std::string deviceName1 = "device";
172     std::string abilityName1 = "musicServiceAbility";
173     std::string appName1 = "musicservcie";
174     std::string bundleName1 = "com.ix.musicservcie";
175     std::string moduleName1 = "entry";
176     abilityRequest1_ = GenerateAbilityRequest(deviceName1, abilityName1, appName1, bundleName1, moduleName1);
177     serviceRecord1_ = AbilityRecord::CreateAbilityRecord(abilityRequest1_);
178     std::string deviceName2 = "device";
179     std::string abilityName2 = "residentServiceAbility";
180     std::string appName2 = "residentservcie";
181     std::string bundleName2 = "com.ix.residentservcie";
182     std::string moduleName2 = "entry";
183     abilityRequest2_ = GenerateAbilityRequest(deviceName2, abilityName2, appName2, bundleName2, moduleName2);
184     serviceRecord2_ = AbilityRecord::CreateAbilityRecord(abilityRequest2_);
185     serviceToken2_ = serviceRecord_->GetToken();
186     serviceToken1_ = serviceRecord_->GetToken();
187     callbackA_ = new AbilityConnectCallback();
188     callbackB_ = new AbilityConnectCallback();
189     // mock bms
190     OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
191         OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
192 }
193 
TearDown(void)194 void AbilityConnectManagerTest::TearDown(void)
195 {
196     // reset the callback count
197     AbilityConnectCallback::onAbilityConnectDoneCount = 0;
198     AbilityConnectCallback::onAbilityDisconnectDoneCount = 0;
199     serviceRecord_ = nullptr;
200 }
201 
ConnectManager() const202 AbilityConnectManager* AbilityConnectManagerTest::ConnectManager() const
203 {
204     return connectManager_.get();
205 }
206 
TaskHandler() const207 std::shared_ptr<TaskHandlerWrap> AbilityConnectManagerTest::TaskHandler() const
208 {
209     return taskHandler_;
210 }
211 
EventHandler() const212 std::shared_ptr<EventHandlerWrap> AbilityConnectManagerTest::EventHandler() const
213 {
214     return eventHandler_;
215 }
216 
217 /*
218  * Feature: AbilityConnectManager
219  * Function: StartAbility
220  * SubFunction: NA
221  * FunctionPoints: StartAbility
222  * EnvConditions:NA
223  * CaseDescription: Verify the normal process of startability
224  */
225 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_001, TestSize.Level1)
226 {
227     ConnectManager()->SetTaskHandler(TaskHandler());
228     ConnectManager()->SetEventHandler(EventHandler());
229 
230     auto result = ConnectManager()->StartAbility(abilityRequest_);
231     EXPECT_EQ(OHOS::ERR_OK, result);
232     WaitUntilTaskDone(TaskHandler());
233 
234     auto elementName = abilityRequest_.want.GetElement().GetURI();
235     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
236     EXPECT_NE(service, nullptr);
237     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
238 
239     service->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
240 
241     auto result1 = ConnectManager()->StartAbility(abilityRequest_);
242     WaitUntilTaskDone(TaskHandler());
243     EXPECT_EQ(OHOS::ERR_OK, result1);
244     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
245 
246     service->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVATING);
247     auto result2 = ConnectManager()->StartAbility(abilityRequest_);
248     WaitUntilTaskDone(TaskHandler());
249     EXPECT_EQ(OHOS::ERR_OK, result2);
250     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
251 }
252 
253 /*
254  * Feature: AbilityConnectManager
255  * Function: TerminateAbility
256  * SubFunction: NA
257  * FunctionPoints: StartAbility and TerminateAbility
258  * EnvConditions:NA
259  * CaseDescription: Verify the following:
260  * 1.token is nullptr, terminate ability failed
261  * 2.token is not nullptr, terminate ability success, and verify the status
262  */
263 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_002, TestSize.Level1)
264 {
265     ConnectManager()->SetTaskHandler(TaskHandler());
266     ConnectManager()->SetEventHandler(EventHandler());
267 
268     auto result = ConnectManager()->StartAbility(abilityRequest_);
269     EXPECT_EQ(OHOS::ERR_OK, result);
270     WaitUntilTaskDone(TaskHandler());
271 
272     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
273     auto result1 = ConnectManager()->TerminateAbility(nullToken);
274     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result1);
275 
276     auto elementName = abilityRequest_.want.GetElement().GetURI();
277     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
278     EXPECT_NE(service, nullptr);
279 
280     auto result2 = ConnectManager()->TerminateAbility(service->GetToken());
281     WaitUntilTaskDone(TaskHandler());
282     EXPECT_EQ(OHOS::ERR_OK, result2);
283     EXPECT_EQ(service->GetAbilityState(), TERMINATING);
284 }
285 
286 /*
287  * Feature: AbilityConnectManager
288  * Function: TerminateAbility
289  * SubFunction: NA
290  * FunctionPoints: StartAbility and TerminateAbility
291  * EnvConditions:NA
292  * CaseDescription: Verify ability is terminating, terminate ability success
293  */
294 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_003, TestSize.Level1)
295 {
296     ConnectManager()->SetTaskHandler(TaskHandler());
297     ConnectManager()->SetEventHandler(EventHandler());
298 
299     auto result = ConnectManager()->StartAbility(abilityRequest_);
300     EXPECT_EQ(OHOS::ERR_OK, result);
301     WaitUntilTaskDone(TaskHandler());
302 
303     auto elementName = abilityRequest_.want.GetElement().GetURI();
304     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
305     EXPECT_NE(service, nullptr);
306 
307     service->SetTerminatingState();
308     auto result1 = ConnectManager()->TerminateAbility(service->GetToken());
309     WaitUntilTaskDone(TaskHandler());
310     EXPECT_EQ(OHOS::ERR_OK, result1);
311     EXPECT_NE(service->GetAbilityState(), TERMINATING);
312 }
313 
314 /*
315  * Feature: AbilityConnectManager
316  * Function: TerminateAbility
317  * SubFunction: NA
318  * FunctionPoints: StartAbility and TerminateAbility
319  * EnvConditions: NA
320  * CaseDescription: Verify service is connected, terminate ability failed
321  */
322 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_004, TestSize.Level1)
323 {
324     ConnectManager()->SetTaskHandler(TaskHandler());
325     ConnectManager()->SetEventHandler(EventHandler());
326 
327     auto result = ConnectManager()->StartAbility(abilityRequest_);
328     EXPECT_EQ(OHOS::ERR_OK, result);
329     WaitUntilTaskDone(TaskHandler());
330 
331     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
332     EXPECT_EQ(0, result1);
333 
334     auto elementName = abilityRequest_.want.GetElement().GetURI();
335     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
336     EXPECT_NE(service, nullptr);
337 
338     auto result2 = ConnectManager()->TerminateAbility(service->GetToken());
339     WaitUntilTaskDone(TaskHandler());
340     EXPECT_EQ(0, result2);
341     EXPECT_EQ(service->GetAbilityState(), TERMINATING);
342 }
343 
344 /*
345  * Feature: AbilityConnectManager
346  * Function: StopServiceAbility
347  * SubFunction: NA
348  * FunctionPoints: StartAbility and StopServiceAbility
349  * EnvConditions: NA
350  * CaseDescription: Verify the following:
351  * 1.token is nullptr, stop service ability failed
352  * 2.token is not nullptr, stop service ability success, and verify the status
353  */
354 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_005, TestSize.Level1)
355 {
356     ConnectManager()->SetTaskHandler(TaskHandler());
357     ConnectManager()->SetEventHandler(EventHandler());
358 
359     auto result = ConnectManager()->StartAbility(abilityRequest_);
360     EXPECT_EQ(OHOS::ERR_OK, result);
361     WaitUntilTaskDone(TaskHandler());
362 
363     auto elementName = abilityRequest_.want.GetElement().GetURI();
364     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
365     EXPECT_NE(service, nullptr);
366 
367     AbilityRequest otherRequest;
368     auto result1 = ConnectManager()->StopServiceAbility(otherRequest);
369     WaitUntilTaskDone(TaskHandler());
370     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result1);
371 
372     auto result2 = ConnectManager()->StopServiceAbility(abilityRequest_);
373     WaitUntilTaskDone(TaskHandler());
374     EXPECT_EQ(OHOS::ERR_OK, result2);
375     EXPECT_EQ(service->GetAbilityState(), TERMINATING);
376 }
377 
378 /*
379  * Feature: AbilityConnectManager
380  * Function: StopServiceAbility
381  * SubFunction: NA
382  * FunctionPoints: StartAbility and StopServiceAbility
383  * EnvConditions:NA
384  * CaseDescription: Verify ability is terminating, stop service ability success
385  */
386 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_006, TestSize.Level1)
387 {
388     ConnectManager()->SetTaskHandler(TaskHandler());
389     ConnectManager()->SetEventHandler(EventHandler());
390 
391     auto result = ConnectManager()->StartAbility(abilityRequest_);
392     EXPECT_EQ(OHOS::ERR_OK, result);
393     WaitUntilTaskDone(TaskHandler());
394 
395     auto elementName = abilityRequest_.want.GetElement().GetURI();
396     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
397     EXPECT_NE(service, nullptr);
398 
399     service->SetTerminatingState();
400     auto result1 = ConnectManager()->StopServiceAbility(abilityRequest_);
401     WaitUntilTaskDone(TaskHandler());
402     EXPECT_EQ(OHOS::ERR_OK, result1);
403     EXPECT_NE(service->GetAbilityState(), TERMINATING);
404 }
405 
406 /*
407  * Feature: AbilityConnectManager
408  * Function: StopServiceAbility
409  * SubFunction: NA
410  * FunctionPoints: StartAbility and StopServiceAbility
411  * EnvConditions: NA
412  * CaseDescription: Verify service is connected, stop service ability failed
413  */
414 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_007, TestSize.Level1)
415 {
416     ConnectManager()->SetTaskHandler(TaskHandler());
417     ConnectManager()->SetEventHandler(EventHandler());
418 
419     auto result = ConnectManager()->StartAbility(abilityRequest_);
420     EXPECT_EQ(OHOS::ERR_OK, result);
421     WaitUntilTaskDone(TaskHandler());
422 
423     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
424     EXPECT_EQ(0, result1);
425 
426     auto elementName = abilityRequest_.want.GetElement().GetURI();
427     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
428     EXPECT_NE(service, nullptr);
429 
430     auto result2 = ConnectManager()->StopServiceAbility(abilityRequest_);
431     WaitUntilTaskDone(TaskHandler());
432     EXPECT_EQ(0, result2);
433     EXPECT_EQ(service->GetAbilityState(), TERMINATING);
434 }
435 
436 /*
437  * Feature: AbilityConnectManager
438  * Function: ConnectAbilityLocked
439  * SubFunction: NA
440  * FunctionPoints: NA
441  * EnvConditions:NA
442  * CaseDescription: verify the scene of service not loaded and callback not bound.
443  */
444 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_008, TestSize.Level1)
445 {
446     int result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
447     EXPECT_EQ(0, result);
448 
449     auto connectMap = ConnectManager()->GetConnectMap();
450     auto connectRecordList = connectMap.at(callbackA_->AsObject());
451     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
452 
453     auto elementName = abilityRequest_.want.GetElement();
454     auto elementNameUri = elementName.GetURI();
455     auto serviceMap = ConnectManager()->GetServiceMap();
456     auto abilityRecord = serviceMap.at(elementNameUri);
457     connectRecordList = abilityRecord->GetConnectRecordList();
458     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
459 }
460 
461 /*
462  * Feature: AbilityConnectManager
463  * Function: ConnectAbilityLocked
464  * SubFunction: NA
465  * FunctionPoints: NA
466  * EnvConditions:NA
467  * CaseDescription: verify the scene of service load ability's timeout.
468  */
469 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_009, TestSize.Level1)
470 {
471     ConnectManager()->SetTaskHandler(TaskHandler());
472     ConnectManager()->SetEventHandler(EventHandler());
473     int result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
474     EXPECT_EQ(0, result);
475 
476     auto connectMap = ConnectManager()->GetConnectMap();
477     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
478     WaitUntilTaskDone(TaskHandler());
479     usleep(TEST_WAIT_TIME);
480 
481     connectMap = ConnectManager()->GetConnectMap();
482     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
483 }
484 
485 /*
486  * Feature: AbilityConnectManager
487  * Function: ConnectAbilityLocked
488  * SubFunction: NA
489  * FunctionPoints: NA
490  * EnvConditions:NA
491  * CaseDescription: verify the scene of service loaded and callback not bound.
492  */
493 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_010, TestSize.Level1)
494 {
495     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
496     EXPECT_EQ(0, result);
497 
498     result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr);
499     EXPECT_EQ(0, result);
500 
501     auto connectMap = ConnectManager()->GetConnectMap();
502     auto connectRecordList = connectMap.at(callbackA_->AsObject());
503     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
504 
505     connectRecordList = connectMap.at(callbackB_->AsObject());
506     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
507 
508     auto elementName = abilityRequest_.want.GetElement();
509     std::string elementNameUri = elementName.GetURI();
510     auto serviceMap = ConnectManager()->GetServiceMap();
511     auto abilityRecord = serviceMap.at(elementNameUri);
512     connectRecordList = abilityRecord->GetConnectRecordList();
513     EXPECT_EQ(2, static_cast<int>(connectRecordList.size()));
514 }
515 
516 /*
517  * Feature: AbilityConnectManager
518  * Function: ConnectAbilityLocked
519  * SubFunction: NA
520  * FunctionPoints: NA
521  * EnvConditions:NA
522  * CaseDescription: verify the scene of service connect ability's timeout.
523  */
524 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_011, TestSize.Level1)
525 {
526     ConnectManager()->SetTaskHandler(TaskHandler());
527     ConnectManager()->SetEventHandler(EventHandler());
528 
529     int result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
530     auto elementName = abilityRequest_.want.GetElement();
531     std::string elementNameUri = elementName.GetURI();
532     auto serviceMap = ConnectManager()->GetServiceMap();
533     auto abilityRecord = serviceMap.at(elementNameUri);
534     auto token = abilityRecord->GetToken();
535 
536     auto connectMap = ConnectManager()->GetConnectMap();
537     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
538 
539     auto scheduler = new AbilityScheduler();
540     ConnectManager()->AttachAbilityThreadLocked(scheduler, token->AsObject());
541     ConnectManager()->AbilityTransitionDone(token->AsObject(), OHOS::AAFwk::AbilityState::INACTIVE);
542 
543     WaitUntilTaskDone(TaskHandler());
544     usleep(TEST_WAIT_TIME);
545     connectMap = ConnectManager()->GetConnectMap();
546     EXPECT_EQ(0, result);
547     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
548 }
549 
550 /*
551  * Feature: AbilityConnectManager
552  * Function: ConnectAbilityLocked
553  * SubFunction: NA
554  * FunctionPoints: NA
555  * EnvConditions:NA
556  * CaseDescription: verify the scene of service loaded and callback bound.
557  */
558 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_012, TestSize.Level1)
559 {
560     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
561     EXPECT_EQ(0, result);
562 
563     result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
564     EXPECT_EQ(0, result);
565 
566     auto connectMap = ConnectManager()->GetConnectMap();
567     auto connectRecordList = connectMap.at(callbackA_->AsObject());
568     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
569 
570     auto elementName = abilityRequest_.want.GetElement();
571     std::string elementNameUri = elementName.GetURI();
572     auto serviceMap = ConnectManager()->GetServiceMap();
573     auto abilityRecord = serviceMap.at(elementNameUri);
574     connectRecordList = abilityRecord->GetConnectRecordList();
575     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
576 }
577 
578 /*
579  * Feature: AbilityConnectManager
580  * Function: ConnectAbilityLocked
581  * SubFunction: NA
582  * FunctionPoints: NA
583  * EnvConditions:NA
584  * CaseDescription: verify the scene of service not loaded and callback bound.
585  */
586 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_013, TestSize.Level1)
587 {
588     int result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
589     EXPECT_EQ(0, result);
590 
591     std::string deviceNameB = "device";
592     std::string abilityNameB = "ServiceAbilityB";
593     std::string appNameB = "hiservcieB";
594     std::string bundleNameB = "com.ix.hiservcieB";
595     std::string moduleNameB = "entry";
596     auto abilityRequestB = GenerateAbilityRequest(deviceNameB, abilityNameB, appNameB, bundleNameB, moduleNameB);
597     result = ConnectManager()->ConnectAbilityLocked(abilityRequestB, callbackA_, nullptr);
598     EXPECT_EQ(0, result);
599 
600     auto connectMap = ConnectManager()->GetConnectMap();
601     auto connectRecordList = connectMap.at(callbackA_->AsObject());
602     EXPECT_EQ(2, static_cast<int>(connectRecordList.size()));
603 
604     auto elementName = abilityRequest_.want.GetElement();
605     std::string elementNameUri = elementName.GetURI();
606     auto serviceMap = ConnectManager()->GetServiceMap();
607     auto abilityRecord = serviceMap.at(elementNameUri);
608     connectRecordList = abilityRecord->GetConnectRecordList();
609     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
610 
611     auto elementNameB = abilityRequest_.want.GetElement();
612     std::string elementNameUriB = elementNameB.GetURI();
613     abilityRecord = serviceMap.at(elementNameUriB);
614     connectRecordList = abilityRecord->GetConnectRecordList();
615     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
616 }
617 
618 /*
619  * Feature: AbilityConnectManager
620  * Function: ConnectAbilityLocked
621  * SubFunction: NA
622  * FunctionPoints: NA
623  * EnvConditions:NA
624  * CaseDescription: verify the scene of service loaded and callback bound, but service and callback was not associated.
625  */
626 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_014, TestSize.Level1)
627 {
628     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
629     EXPECT_EQ(0, result);
630 
631     std::string deviceNameB = "device";
632     std::string abilityNameB = "ServiceAbilityB";
633     std::string appNameB = "hiservcieB";
634     std::string bundleNameB = "com.ix.hiservcieB";
635     std::string moduleNameB = "entry";
636     auto abilityRequestB = GenerateAbilityRequest(deviceNameB, abilityNameB, appNameB, bundleNameB, moduleNameB);
637     result = ConnectManager()->ConnectAbilityLocked(abilityRequestB, callbackB_, nullptr);
638     EXPECT_EQ(0, result);
639 
640     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr);
641     auto connectMap = ConnectManager()->GetConnectMap();
642     auto connectRecordList = connectMap.at(callbackB_->AsObject());
643     EXPECT_EQ(2, static_cast<int>(connectRecordList.size()));
644 
645     connectRecordList = connectMap.at(callbackA_->AsObject());
646     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
647 
648     auto elementName = abilityRequest_.want.GetElement();
649     std::string elementNameUri = elementName.GetURI();
650     auto serviceMap = ConnectManager()->GetServiceMap();
651     auto abilityRecord = serviceMap.at(elementNameUri);
652     connectRecordList = abilityRecord->GetConnectRecordList();
653     EXPECT_EQ(2, static_cast<int>(connectRecordList.size()));
654 }
655 
656 /*
657  * Feature: AbilityConnectManager
658  * Function: AttachAbilityThreadLocked
659  * SubFunction: NA
660  * FunctionPoints: NA
661  * EnvConditions:NA
662  * CaseDescription: verify the AttachAbilityThreadLocked function when the parameter is null.
663  */
664 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_015, TestSize.Level1)
665 {
666     auto result = ConnectManager()->AttachAbilityThreadLocked(nullptr, nullptr);
667     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result);
668 }
669 
670 /*
671  * Feature: AbilityConnectManager
672  * Function: ScheduleConnectAbilityDoneLocked
673  * SubFunction: NA
674  * FunctionPoints: NA
675  * EnvConditions:NA
676  * CaseDescription: verify the ScheduleConnectAbilityDoneLocked function when the parameter is null.
677  */
678 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_016, TestSize.Level1)
679 {
680     auto callback = new AbilityConnectCallback();
681     auto result = ConnectManager()->ScheduleConnectAbilityDoneLocked(nullptr, callback);
682     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, result);
683 }
684 
685 /*
686  * Feature: AbilityConnectManager
687  * Function: ScheduleConnectAbilityDoneLocked
688  * SubFunction: NA
689  * FunctionPoints: NA
690  * EnvConditions:NA
691  * CaseDescription: verify the ScheduleConnectAbilityDoneLocked function when the state is CONNECTED.
692  */
693 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_017, TestSize.Level1)
694 {
695     auto callback = new AbilityConnectCallback();
696     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callback, nullptr);
697 
698     auto elementName = abilityRequest_.want.GetElement();
699     std::string elementNameUri = elementName.GetURI();
700     auto serviceMap = ConnectManager()->GetServiceMap();
701     auto abilityRecord = serviceMap.at(elementNameUri);
702     auto token = abilityRecord->GetToken();
703 
704     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
705     ConnectManager()->ScheduleConnectAbilityDoneLocked(token, callback);
706     auto abilityRecordB = Token::GetAbilityRecordByToken(token);
707     EXPECT_TRUE(abilityRecordB);
708     auto connectRecordList = abilityRecordB->GetConnectRecordList();
709     int size = connectRecordList.size();
710     EXPECT_EQ(1, size);
711     if (size != 0) {
712         auto connState = (*(connectRecordList.begin()))->GetConnectState();
713         EXPECT_EQ(ConnectionState::CONNECTED, connState);
714     }
715 }
716 
717 /*
718  * Feature: AbilityConnectManager
719  * Function: ScheduleConnectAbilityDoneLocked
720  * SubFunction: NA
721  * FunctionPoints: NA
722  * EnvConditions:NA
723  * CaseDescription: verify the input parameters.
724  */
725 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_001, TestSize.Level1)
726 {
727     // start test
728     // test1 for serviceToken is null but remoteObject is valid
729     OHOS::sptr<OHOS::IRemoteObject> object = new AbilityConnectCallback();
730     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(nullptr, object);
731     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, ret);
732 
733     // test2 for both of serviceToken and remoteObject are null
734     ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(nullptr, nullptr);
735     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, ret);
736 }
737 
738 /*
739  * Feature: AbilityConnectManager
740  * Function: ScheduleConnectAbilityDoneLocked
741  * SubFunction: NA
742  * FunctionPoints: NA
743  * EnvConditions:NA
744  * CaseDescription: verify the input serviceToken which corresponding ability record doesn't exist.
745  */
746 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_002, TestSize.Level1)
747 {
748     // test for serviceToken's abilityRecord is null
749     serviceRecord_ = nullptr;
750     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, nullptr);
751     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, ret);
752 }
753 
754 /*
755  * Feature: AbilityConnectManager
756  * Function: ScheduleConnectAbilityDoneLocked
757  * SubFunction: OnAbilityConnectDone
758  * FunctionPoints: NA
759  * EnvConditions:NA
760  * CaseDescription: verify the input serviceToken which corresponding connection list is empty.
761  */
762 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_003, TestSize.Level1)
763 {
764     // test for serviceToken's connection list is null
765     // start test
766     auto callback = new AbilityConnectCallback();
767     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
768     EXPECT_EQ(OHOS::AAFwk::INVALID_CONNECTION_STATE, ret);
769     auto connList = serviceRecord_->GetConnectRecordList();
770     EXPECT_EQ(true, connList.empty());  // the connection list size should be empty
771 }
772 
773 /*
774  * Feature: AbilityConnectManager
775  * Function: ScheduleConnectAbilityDoneLocked
776  * SubFunction: OnAbilityConnectDone
777  * FunctionPoints: NA
778  * EnvConditions:NA
779  * CaseDescription: verify the input serviceToken which corresponding connection list members' state
780  * is not CONNECTING or CONNECTED.
781  */
782 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_004, 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 callbackA_
786     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
787         serviceToken_, serviceRecord_, callbackA_);  // newConnRecord1's default state is INIT
788     serviceRecord_->AddConnectRecordToList(newConnRecord1);
789     // generate the second connection record of callbackB_
790     auto newConnRecord2 = ConnectionRecord::CreateConnectionRecord(serviceToken_, serviceRecord_, callbackB_);
791     newConnRecord2->SetConnectState(ConnectionState::DISCONNECTING);  // newConnRecord2's state is DISCONNECTING
792     serviceRecord_->AddConnectRecordToList(newConnRecord2);
793     auto connList = serviceRecord_->GetConnectRecordList();
794     EXPECT_EQ(2, static_cast<int>(connList.size()));  // the connection list members should be two
795     // start test
796     auto callback = new AbilityConnectCallback();
797     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
798     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
799     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
800     // connection callback should not be called, so check the count
801     EXPECT_EQ(0, AbilityConnectCallback::onAbilityConnectDoneCount);
802     EXPECT_EQ(0, AbilityConnectCallback::onAbilityDisconnectDoneCount);
803 }
804 
805 /*
806  * Feature: AbilityConnectManager
807  * Function: ScheduleConnectAbilityDoneLocked
808  * SubFunction: OnAbilityConnectDone
809  * FunctionPoints: NA
810  * EnvConditions:NA
811  * CaseDescription: verify the scene : 1.serviceToken's corresponding connection list member's state is CONNECTING.
812  * 2.But the connection callback is null.
813  */
814 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_005, TestSize.Level1)
815 {
816     // test for schedule the service connected done but the corresponding connection state is not CONNECTING
817     // generate the first connection record of null
818     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
819         serviceToken_, serviceRecord_, nullptr);  // newConnRecord1's default state is INIT
820     serviceRecord_->AddConnectRecordToList(newConnRecord1);
821     newConnRecord1->SetConnectState(ConnectionState::CONNECTING);  // newConnRecord1's state is CONNECTING
822     auto connList = serviceRecord_->GetConnectRecordList();
823     EXPECT_EQ(1, static_cast<int>(connList.size()));  // the connection list members should be zero
824     // start test
825     auto callback = new AbilityConnectCallback();
826     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
827     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
828     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
829     // connection callback should not be called, so check the count
830     EXPECT_EQ(0, AbilityConnectCallback::onAbilityConnectDoneCount);
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 null.
841  */
842 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_006, 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 null
846     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
847         serviceToken_, serviceRecord_, nullptr);  // newConnRecord1's default state is INIT
848     serviceRecord_->AddConnectRecordToList(newConnRecord1);
849     newConnRecord1->SetConnectState(ConnectionState::CONNECTED);  // newConnRecord1's state is CONNECTED
850     auto connList = serviceRecord_->GetConnectRecordList();
851     EXPECT_EQ(1, static_cast<int>(connList.size()));  // the connection list members should be zero
852     // start test
853     auto callback = new AbilityConnectCallback();
854     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
855     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
856     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
857     // connection callback should not be called, so check the count
858     EXPECT_EQ(0, AbilityConnectCallback::onAbilityConnectDoneCount);
859 }
860 
861 /*
862  * Feature: AbilityConnectManager
863  * Function: ScheduleConnectAbilityDoneLocked
864  * SubFunction: OnAbilityConnectDone
865  * FunctionPoints: NA
866  * EnvConditions:NA
867  * CaseDescription: verify the scene : 1.serviceToken's corresponding connection list member's state is CONNECTING.
868  * 2.But the connection callback is valid.
869  */
870 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_007, TestSize.Level1)
871 {
872     // test for schedule the service connected done but the corresponding connection state is not CONNECTING
873     // generate the first connection record of callbackA_
874     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
875         serviceToken_, serviceRecord_, callbackA_);  // newConnRecord1's default state is INIT
876     serviceRecord_->AddConnectRecordToList(newConnRecord1);
877     // generate the second connection record of callbackB_
878     auto newConnRecord2 = ConnectionRecord::CreateConnectionRecord(serviceToken_, serviceRecord_, callbackB_);
879     newConnRecord2->SetConnectState(ConnectionState::CONNECTING);  // newConnRecord2's state is CONNECTING
880     serviceRecord_->AddConnectRecordToList(newConnRecord2);
881     auto connList = serviceRecord_->GetConnectRecordList();
882     EXPECT_EQ(2, static_cast<int>(connList.size()));  // the connection list members should be two
883     // start test
884     auto callback = new AbilityConnectCallback();
885     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
886     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
887     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
888     // connection callback should not be called, so check the count
889     EXPECT_EQ(1, AbilityConnectCallback::onAbilityConnectDoneCount);
890     EXPECT_EQ(0, AbilityConnectCallback::onAbilityDisconnectDoneCount);
891 }
892 
893 /*
894  * Feature: AbilityConnectManager
895  * Function: ScheduleConnectAbilityDoneLocked
896  * SubFunction: OnAbilityConnectDone
897  * FunctionPoints: NA
898  * EnvConditions:NA
899  * CaseDescription: verify the scene : 1.serviceToken's corresponding connection list member's state is CONNECTED.
900  * 2.But the connection callback is valid.
901  */
902 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Connect_008, TestSize.Level1)
903 {
904     // test for schedule the service connected done but the corresponding connection state is not CONNECTING
905     // generate the first connection record of callbackA_
906     auto newConnRecord1 = ConnectionRecord::CreateConnectionRecord(
907         serviceToken_, serviceRecord_, callbackA_);               // newConnRecord1's default state is INIT
908     newConnRecord1->SetConnectState(ConnectionState::CONNECTED);  // newConnRecord1's state is CONNECTED
909     serviceRecord_->AddConnectRecordToList(newConnRecord1);
910     // generate the second connection record of callbackB_
911     auto newConnRecord2 = ConnectionRecord::CreateConnectionRecord(serviceToken_, serviceRecord_, callbackB_);
912     newConnRecord2->SetConnectState(ConnectionState::CONNECTING);  // newConnRecord2's state is CONNECTING
913     serviceRecord_->AddConnectRecordToList(newConnRecord2);
914     // generate the third connection record of callbackC
915     auto callbackC = new AbilityConnectCallback();
916     auto newConnRecord3 = ConnectionRecord::CreateConnectionRecord(serviceToken_, serviceRecord_, callbackC);
917     newConnRecord3->SetConnectState(ConnectionState::CONNECTING);  // newConnRecord3's state is CONNECTING
918     serviceRecord_->AddConnectRecordToList(newConnRecord3);
919     auto connList = serviceRecord_->GetConnectRecordList();
920     EXPECT_EQ(3, static_cast<int>(connList.size()));  // the connection list members should be three
921     // start test
922     auto callback = new AbilityConnectCallback();
923     serviceRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
924     int ret = ConnectManager()->ScheduleConnectAbilityDoneLocked(serviceToken_, callback);
925     EXPECT_EQ(OHOS::ERR_OK, ret);  // the result should be OK
926     // connection callback should not be called, so check the count
927     EXPECT_EQ(2, AbilityConnectCallback::onAbilityConnectDoneCount);
928     EXPECT_EQ(0, AbilityConnectCallback::onAbilityDisconnectDoneCount);
929 }
930 
931 /*
932  * Feature: AbilityConnectManager
933  * Function: DisconnectAbilityLocked
934  * SubFunction:
935  * FunctionPoints: DisconnectAbilityLocked and ConnectAbilityLocked
936  * EnvConditions:NA
937  * CaseDescription:Verify the following:
938  * 1. Disconnect ability a nonexistent connect, disconnect failed
939  * 2. If the current connect ability state is not connected, disconnect fails
940  * 3. Verify the success of disconnect ability
941  */
942 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Disconnect_001, TestSize.Level1)
943 {
944     ConnectManager()->SetTaskHandler(TaskHandler());
945     ConnectManager()->SetEventHandler(EventHandler());
946 
947     auto callback = new AbilityConnectCallback();
948     auto result = ConnectManager()->DisconnectAbilityLocked(callback);
949     EXPECT_EQ(result, OHOS::AAFwk::CONNECTION_NOT_EXIST);
950 
951     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
952     EXPECT_EQ(0, result1);
953 
954     auto result2 = ConnectManager()->DisconnectAbilityLocked(callbackA_);
955     EXPECT_EQ(result2, OHOS::AAFwk::INVALID_CONNECTION_STATE);
956 
957     auto list = ConnectManager()->GetConnectRecordListByCallback(callbackA_);
958     EXPECT_EQ(static_cast<int>(list.size()), 1);
959 
960     for (auto& it : list) {
961         it->SetConnectState(ConnectionState::CONNECTED);
962     }
963 
964     auto result3 = ConnectManager()->DisconnectAbilityLocked(callbackA_);
965     EXPECT_EQ(result3, OHOS::ERR_OK);
966 }
967 
968 /*
969  * Feature: AbilityConnectManager
970  * Function: DisconnectAbilityLocked
971  * SubFunction:
972  * FunctionPoints: DisconnectAbilityLocked and ConnectAbilityLocked
973  * EnvConditions:NA
974  * CaseDescription: Results after verifying the disconnect ability
975  */
976 HWTEST_F(AbilityConnectManagerTest, AAFWK_Kit_Disconnect_002, TestSize.Level1)
977 {
978     ConnectManager()->SetTaskHandler(TaskHandler());
979     ConnectManager()->SetEventHandler(EventHandler());
980 
981     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
982     EXPECT_EQ(0, result);
983 
984     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr);
985     EXPECT_EQ(0, result1);
986 
987     auto result2 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackA_, nullptr);
988     EXPECT_EQ(0, result2);
989 
990     auto result3 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackB_, nullptr);
991     EXPECT_EQ(0, result3);
992 
993     auto listA = ConnectManager()->GetConnectRecordListByCallback(callbackA_);
994     EXPECT_EQ(static_cast<int>(listA.size()), 2);
995 
996     for (auto& it : listA) {
997         it->SetConnectState(ConnectionState::CONNECTED);
998     }
999 
1000     auto listB = ConnectManager()->GetConnectRecordListByCallback(callbackB_);
1001     EXPECT_EQ(static_cast<int>(listB.size()), 2);
1002 
1003     for (auto& it : listB) {
1004         it->SetConnectState(ConnectionState::CONNECTED);
1005     }
1006 
1007     auto result5 = ConnectManager()->DisconnectAbilityLocked(callbackA_);
1008     WaitUntilTaskDone(TaskHandler());
1009     EXPECT_EQ(result5, OHOS::ERR_OK);
1010     auto serviceMap = ConnectManager()->GetServiceMap();
1011     EXPECT_EQ(static_cast<int>(serviceMap.size()), 2);
1012 
1013     auto connectMap = ConnectManager()->GetConnectMap();
1014     EXPECT_EQ(static_cast<int>(connectMap.size()), 1);
1015     for (auto& it : connectMap) {
1016         EXPECT_EQ(static_cast<int>(it.second.size()), 2);
1017     }
1018 }
1019 
1020 /*
1021  * Feature: AbilityConnectManager
1022  * Function: AbilityTransitionDone
1023  * SubFunction: NA
1024  * FunctionPoints: AbilityTransitionDone
1025  * EnvConditions:NA
1026  * CaseDescription: Verify the abilitytransitiondone process
1027  */
1028 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_019, TestSize.Level1)
1029 {
1030     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
1031     auto result = ConnectManager()->AbilityTransitionDone(nullToken, OHOS::AAFwk::AbilityState::INACTIVE);
1032     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
1033 
1034     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1035     auto elementName = abilityRequest_.want.GetElement();
1036     std::string elementNameUri = elementName.GetURI();
1037     auto serviceMap = ConnectManager()->GetServiceMap();
1038     auto abilityRecord = serviceMap.at(elementNameUri);
1039     auto token = abilityRecord->GetToken();
1040 
1041     auto result1 = ConnectManager()->AbilityTransitionDone(token, OHOS::AAFwk::AbilityState::INACTIVE);
1042     EXPECT_EQ(result1, OHOS::ERR_INVALID_VALUE);
1043 
1044     ConnectManager()->MoveToTerminatingMap(abilityRecord);
1045     auto result2 = ConnectManager()->AbilityTransitionDone(token, OHOS::AAFwk::AbilityState::INITIAL);
1046     EXPECT_EQ(result2, OHOS::ERR_OK);
1047 
1048     auto result3 = ConnectManager()->AbilityTransitionDone(token, OHOS::AAFwk::AbilityState::TERMINATING);
1049     EXPECT_EQ(result3, OHOS::ERR_INVALID_VALUE);
1050 }
1051 
1052 /*
1053  * Feature: AbilityConnectManager
1054  * Function: ScheduleDisconnectAbilityDoneLocked
1055  * SubFunction: NA
1056  * FunctionPoints: ScheduleDisconnectAbilityDoneLocked
1057  * EnvConditions:NA
1058  * CaseDescription: Verify the ScheduleDisconnectAbilityDoneLocked process
1059  */
1060 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_020, TestSize.Level1)
1061 {
1062     ConnectManager()->SetTaskHandler(TaskHandler());
1063     ConnectManager()->SetEventHandler(EventHandler());
1064 
1065     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
1066     auto result = ConnectManager()->ScheduleDisconnectAbilityDoneLocked(nullToken);
1067     EXPECT_EQ(result, OHOS::AAFwk::CONNECTION_NOT_EXIST);
1068 
1069     std::shared_ptr<AbilityRecord> ability = nullptr;
1070     OHOS::sptr<OHOS::IRemoteObject> token1 = new OHOS::AAFwk::Token(ability);
1071     auto result1 = ConnectManager()->ScheduleDisconnectAbilityDoneLocked(token1);
1072     EXPECT_EQ(result1, OHOS::AAFwk::CONNECTION_NOT_EXIST);
1073 
1074     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1075     auto elementName = abilityRequest_.want.GetElement();
1076     std::string elementNameUri = elementName.GetURI();
1077     auto serviceMap = ConnectManager()->GetServiceMap();
1078     auto abilityRecord = serviceMap.at(elementNameUri);
1079     auto token = abilityRecord->GetToken();
1080 
1081     auto listA = ConnectManager()->GetConnectRecordListByCallback(callbackA_);
1082     for (auto& it : listA) {
1083         it->SetConnectState(ConnectionState::CONNECTED);
1084     }
1085 
1086     auto result2 = ConnectManager()->DisconnectAbilityLocked(callbackA_);
1087     WaitUntilTaskDone(TaskHandler());
1088     EXPECT_EQ(result2, OHOS::ERR_OK);
1089 
1090     auto result3 = ConnectManager()->ScheduleDisconnectAbilityDoneLocked(token);
1091     EXPECT_EQ(result3, OHOS::AAFwk::INVALID_CONNECTION_STATE);
1092 
1093     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
1094 
1095     auto result4 = ConnectManager()->ScheduleDisconnectAbilityDoneLocked(token);
1096     EXPECT_EQ(result4, OHOS::ERR_OK);
1097 }
1098 
1099 /*
1100  * Feature: AbilityConnectManager
1101  * Function: ScheduleCommandAbilityDoneLocked
1102  * SubFunction: NA
1103  * FunctionPoints: ScheduleCommandAbilityDoneLocked
1104  * EnvConditions:NA
1105  * CaseDescription: Verify the ScheduleCommandAbilityDoneLocked process
1106  */
1107 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_021, TestSize.Level1)
1108 {
1109     ConnectManager()->SetTaskHandler(TaskHandler());
1110     ConnectManager()->SetEventHandler(EventHandler());
1111 
1112     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
1113     auto result = ConnectManager()->ScheduleCommandAbilityDoneLocked(nullToken);
1114     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
1115 
1116     std::shared_ptr<AbilityRecord> ability = nullptr;
1117     OHOS::sptr<OHOS::IRemoteObject> token1 = new OHOS::AAFwk::Token(ability);
1118     auto result1 = ConnectManager()->ScheduleCommandAbilityDoneLocked(token1);
1119     EXPECT_EQ(result1, OHOS::ERR_INVALID_VALUE);
1120 
1121     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1122     auto elementName = abilityRequest_.want.GetElement();
1123     std::string elementNameUri = elementName.GetURI();
1124     auto serviceMap = ConnectManager()->GetServiceMap();
1125     auto abilityRecord = serviceMap.at(elementNameUri);
1126     auto token = abilityRecord->GetToken();
1127 
1128     auto result2 = ConnectManager()->ScheduleCommandAbilityDoneLocked(token);
1129     EXPECT_EQ(result2, OHOS::AAFwk::INVALID_CONNECTION_STATE);
1130 
1131     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
1132     auto result3 = ConnectManager()->ScheduleCommandAbilityDoneLocked(token);
1133     EXPECT_EQ(result3, OHOS::ERR_OK);
1134 }
1135 
1136 /*
1137  * Feature: AbilityConnectManager
1138  * Function: GetExtensionByTokenFromServiceMap
1139  * SubFunction: NA
1140  * FunctionPoints: GetExtensionByTokenFromServiceMap
1141  * EnvConditions:NA
1142  * CaseDescription: Verify the GetExtensionByTokenFromServiceMap process
1143  */
1144 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_022, TestSize.Level1)
1145 {
1146     ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1147     auto elementName = abilityRequest_.want.GetElement();
1148     std::string elementNameUri = elementName.GetURI();
1149     auto serviceMap = ConnectManager()->GetServiceMap();
1150     auto abilityRecord = serviceMap.at(elementNameUri);
1151     auto token = abilityRecord->GetToken();
1152 
1153     auto ability = ConnectManager()->GetExtensionByTokenFromServiceMap(token);
1154     EXPECT_EQ(abilityRecord, ability);
1155 
1156     OHOS::sptr<OHOS::IRemoteObject> nullToken = nullptr;
1157     auto ability1 = ConnectManager()->GetExtensionByTokenFromServiceMap(nullToken);
1158     EXPECT_EQ(nullptr, ability1);
1159 }
1160 
1161 /*
1162  * Feature: AbilityConnectManager
1163  * Function: OnAbilityDied
1164  * SubFunction:
1165  * FunctionPoints: OnAbilityDied
1166  * EnvConditions:NA
1167  * CaseDescription: Verify the OnAbilityDied process
1168  */
1169 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_024, TestSize.Level1)
1170 {
1171     ConnectManager()->SetTaskHandler(TaskHandler());
1172     ConnectManager()->SetEventHandler(EventHandler());
1173 
1174     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1175     EXPECT_EQ(0, result);
1176 
1177     auto result1 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr);
1178     EXPECT_EQ(0, result1);
1179 
1180     auto result2 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackA_, nullptr);
1181     EXPECT_EQ(0, result2);
1182 
1183     auto result3 = ConnectManager()->ConnectAbilityLocked(abilityRequest1_, callbackB_, nullptr);
1184     EXPECT_EQ(0, result3);
1185 
1186     auto listA = ConnectManager()->GetConnectRecordListByCallback(callbackA_);
1187     EXPECT_EQ(static_cast<int>(listA.size()), 2);
1188 
1189     for (auto& it : listA) {
1190         it->SetConnectState(ConnectionState::CONNECTED);
1191     }
1192 
1193     auto listB = ConnectManager()->GetConnectRecordListByCallback(callbackB_);
1194     EXPECT_EQ(static_cast<int>(listB.size()), 2);
1195 
1196     for (auto& it : listB) {
1197         it->SetConnectState(ConnectionState::CONNECTED);
1198     }
1199 
1200     auto elementName = abilityRequest_.want.GetElement();
1201     std::string elementNameUri = elementName.GetURI();
1202     auto serviceMap = ConnectManager()->GetServiceMap();
1203     auto abilityRecord = serviceMap.at(elementNameUri);
1204     auto token = abilityRecord->GetToken();
1205 
1206     ConnectManager()->OnAbilityDied(abilityRecord, 0);
1207     WaitUntilTaskDone(TaskHandler());
1208     auto list = abilityRecord->GetConnectRecordList();
1209     EXPECT_EQ(static_cast<int>(list.size()), 0);
1210 
1211     auto elementName1 = abilityRequest1_.want.GetElement();
1212     std::string elementNameUri1 = elementName1.GetURI();
1213     serviceMap = ConnectManager()->GetServiceMap();
1214     auto abilityRecord1 = serviceMap.at(elementNameUri1);
1215     auto token1 = abilityRecord1->GetToken();
1216 
1217     ConnectManager()->OnAbilityDied(abilityRecord1, 0);
1218     WaitUntilTaskDone(TaskHandler());
1219     auto list1 = abilityRecord1->GetConnectRecordList();
1220     EXPECT_EQ(static_cast<int>(list1.size()), 0);
1221 }
1222 
1223 /*
1224  * Feature: AbilityConnectManager
1225  * Function: DispatchInactive
1226  * SubFunction:
1227  * FunctionPoints: DispatchInactive
1228  * EnvConditions:NA
1229  * CaseDescription: Verify the DispatchInactive process
1230  */
1231 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_025, TestSize.Level1)
1232 {
1233     std::shared_ptr<AbilityRecord> ability = nullptr;
1234     auto result = ConnectManager()->DispatchInactive(ability, OHOS::AAFwk::AbilityState::INACTIVATING);
1235     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
1236 
1237     ConnectManager()->SetTaskHandler(TaskHandler());
1238     ConnectManager()->SetEventHandler(EventHandler());
1239 
1240     auto result3 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1241     EXPECT_EQ(0, result3);
1242 
1243     auto elementName = abilityRequest_.want.GetElement();
1244     std::string elementNameUri = elementName.GetURI();
1245     auto serviceMap = ConnectManager()->GetServiceMap();
1246     auto abilityRecord = serviceMap.at(elementNameUri);
1247     auto token = abilityRecord->GetToken();
1248 
1249     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
1250     auto result1 = ConnectManager()->DispatchInactive(abilityRecord, OHOS::AAFwk::AbilityState::INACTIVATING);
1251     EXPECT_EQ(result1, OHOS::ERR_INVALID_VALUE);
1252 
1253     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
1254     auto result2 = ConnectManager()->DispatchInactive(abilityRecord, OHOS::AAFwk::AbilityState::INACTIVATING);
1255     EXPECT_EQ(result2, OHOS::ERR_OK);
1256     EXPECT_EQ(abilityRecord->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVE);
1257 }
1258 
1259 /*
1260  * Feature: AbilityConnectManager
1261  * Function: DispatchInactive
1262  * SubFunction:
1263  * FunctionPoints: DispatchInactive
1264  * EnvConditions:NA
1265  * CaseDescription: Verify the DispatchInactive process
1266  */
1267 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_026, TestSize.Level1)
1268 {
1269     std::shared_ptr<AbilityRecord> ability = nullptr;
1270     auto result = ConnectManager()->DispatchInactive(ability, OHOS::AAFwk::AbilityState::INACTIVATING);
1271     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
1272 
1273     ConnectManager()->SetTaskHandler(TaskHandler());
1274     ConnectManager()->SetEventHandler(EventHandler());
1275 
1276     auto result3 = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1277     EXPECT_EQ(0, result3);
1278 
1279     auto elementName = abilityRequest_.want.GetElement();
1280     std::string elementNameUri = elementName.GetURI();
1281     auto serviceMap = ConnectManager()->GetServiceMap();
1282     auto abilityRecord = serviceMap.at(elementNameUri);
1283     auto token = abilityRecord->GetToken();
1284 
1285     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
1286     auto result1 = ConnectManager()->DispatchInactive(abilityRecord, OHOS::AAFwk::AbilityState::INACTIVATING);
1287     EXPECT_EQ(result1, OHOS::ERR_INVALID_VALUE);
1288 
1289     abilityRecord->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
1290     auto result2 = ConnectManager()->DispatchInactive(abilityRecord, OHOS::AAFwk::AbilityState::INACTIVATING);
1291     EXPECT_EQ(result2, OHOS::ERR_OK);
1292     EXPECT_EQ(abilityRecord->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVE);
1293 }
1294 
1295 /*
1296  * Feature: AbilityConnectManager
1297  * Function: AddConnectDeathRecipient
1298  * SubFunction:
1299  * FunctionPoints: AddConnectDeathRecipient
1300  * EnvConditions:NA
1301  * CaseDescription: Verify the AddConnectDeathRecipient process
1302  */
1303 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_027, TestSize.Level1)
1304 {
1305     ConnectManager()->SetTaskHandler(TaskHandler());
1306     ConnectManager()->SetEventHandler(EventHandler());
1307 
1308     ConnectManager()->AddConnectDeathRecipient(nullptr);
1309     EXPECT_TRUE(ConnectManager()->recipientMap_.empty());
1310 
1311     ConnectManager()->AddConnectDeathRecipient(callbackA_);
1312     EXPECT_EQ(static_cast<int>(ConnectManager()->recipientMap_.size()), 1);
1313 
1314     // Add twice, do not add repeatedly
1315     ConnectManager()->AddConnectDeathRecipient(callbackA_);
1316     EXPECT_EQ(static_cast<int>(ConnectManager()->recipientMap_.size()), 1);
1317 }
1318 
1319 /*
1320  * Feature: AbilityConnectManager
1321  * Function: RemoveConnectDeathRecipient
1322  * SubFunction:
1323  * FunctionPoints: RemoveConnectDeathRecipient
1324  * EnvConditions:NA
1325  * CaseDescription: Verify the RemoveConnectDeathRecipient process
1326  */
1327 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_028, TestSize.Level1)
1328 {
1329     ConnectManager()->SetTaskHandler(TaskHandler());
1330     ConnectManager()->SetEventHandler(EventHandler());
1331 
1332     ConnectManager()->AddConnectDeathRecipient(callbackA_);
1333     EXPECT_EQ(static_cast<int>(ConnectManager()->recipientMap_.size()), 1);
1334 
1335     ConnectManager()->RemoveConnectDeathRecipient(nullptr);
1336     EXPECT_FALSE(ConnectManager()->recipientMap_.empty());
1337 
1338     ConnectManager()->RemoveConnectDeathRecipient(callbackA_);
1339     EXPECT_TRUE(ConnectManager()->recipientMap_.empty());
1340 }
1341 
1342 /*
1343  * Feature: AbilityConnectManager
1344  * Function: OnCallBackDied
1345  * SubFunction:
1346  * FunctionPoints: OnCallBackDied
1347  * EnvConditions:NA
1348  * CaseDescription: Verify the OnCallBackDied process
1349  */
1350 HWTEST_F(AbilityConnectManagerTest, AAFWK_Connect_Service_029, TestSize.Level1)
1351 {
1352     ConnectManager()->SetTaskHandler(TaskHandler());
1353     ConnectManager()->SetEventHandler(EventHandler());
1354 
1355     auto result = ConnectManager()->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
1356     WaitUntilTaskDone(TaskHandler());
1357     EXPECT_EQ(0, result);
1358 
1359     ConnectManager()->OnCallBackDied(nullptr);
1360     WaitUntilTaskDone(TaskHandler());
1361     auto connectMap = ConnectManager()->GetConnectMap();
1362     auto connectRecordList = connectMap.at(callbackA_->AsObject());
1363     EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
1364     for (auto& it : connectRecordList) {
1365         EXPECT_NE(it->GetAbilityConnectCallback(), nullptr);
1366     }
1367 
1368     ConnectManager()->OnCallBackDied(callbackA_->AsObject());
1369     WaitUntilTaskDone(TaskHandler());
1370     auto cMap = ConnectManager()->GetConnectMap();
1371     connectRecordList = connectMap.at(callbackA_->AsObject());
1372     EXPECT_EQ(1, static_cast<int>(connectMap.size()));
1373     for (auto& it : connectRecordList) {
1374         EXPECT_EQ(it->GetAbilityConnectCallback(), nullptr);
1375     }
1376 }
1377 
1378 /*
1379  * Feature: AbilityConnectManager
1380  * Function: StartAbilityLocked
1381  * SubFunction: StartAbilityLocked
1382  * FunctionPoints: NA
1383  * EnvConditions: NA
1384  * CaseDescription: Verify AbilityConnectManager StartAbilityLocked
1385  */
1386 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_StartAbilityLocked_001, TestSize.Level1)
1387 {
1388     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1389     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1390     AbilityRequest abilityRequest;
1391     abilityRequest.abilityInfo.deviceId = "id";
1392     abilityRequest.abilityInfo.bundleName = "bundle";
1393     abilityRequest.abilityInfo.name = "name";
1394     abilityRequest.abilityInfo.moduleName = "module";
1395     std::string stringUri = "id/bundle/module/name";
1396     AppExecFwk::ElementName element(abilityRequest.abilityInfo.deviceId, abilityRequest.abilityInfo.bundleName,
1397         abilityRequest.abilityInfo.name, abilityRequest.abilityInfo.moduleName);
1398     EXPECT_EQ(element.GetURI(), stringUri);
1399     abilityRecord->currentState_ = AbilityState::ACTIVE;
1400     abilityRecord->SetPreAbilityRecord(serviceRecord1_);
1401     connectManager->serviceMap_.emplace(stringUri, abilityRecord);
1402     int res = connectManager->StartAbilityLocked(abilityRequest);
1403     EXPECT_EQ(res, ERR_OK);
1404 }
1405 
1406 /*
1407  * Feature: AbilityConnectManager
1408  * Function: GetOrCreateServiceRecord
1409  * SubFunction: GetOrCreateServiceRecord
1410  * FunctionPoints: NA
1411  * EnvConditions: NA
1412  * CaseDescription: Verify AbilityConnectManager GetOrCreateServiceRecord
1413  */
1414 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetOrCreateServiceRecord_001, TestSize.Level1)
1415 {
1416     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1417     ASSERT_NE(connectManager, nullptr);
1418     AbilityRequest abilityRequest;
1419     bool isCreatedByConnect = false;
1420     std::shared_ptr<AbilityRecord> targetService = nullptr;
1421     bool isLoadedAbility = false;
1422     abilityRequest.abilityInfo.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
1423     connectManager->serviceMap_.clear();
1424     connectManager->GetOrCreateServiceRecord(abilityRequest, isCreatedByConnect, targetService, isLoadedAbility);
1425 }
1426 
1427 /*
1428  * Feature: AbilityConnectManager
1429  * Function: ConnectAbilityLocked
1430  * SubFunction: ConnectAbilityLocked
1431  * FunctionPoints: NA
1432  * EnvConditions: NA
1433  * CaseDescription: Verify AbilityConnectManager ConnectAbilityLocked
1434  */
1435 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_ConnectAbilityLocked_001, TestSize.Level1)
1436 {
1437     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1438     ASSERT_NE(connectManager, nullptr);
1439     ConnectManager()->SetTaskHandler(TaskHandler());
1440     ConnectManager()->SetEventHandler(EventHandler());
1441     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1442     AbilityRequest abilityRequest;
1443     sptr<IRemoteObject> callerToken = abilityRecord->GetToken();
1444     OHOS::sptr<IAbilityConnection> connect = new AbilityConnectCallback();
1445     OHOS::sptr<IAbilityConnection> callback1 = new AbilityConnectCallback();
1446     OHOS::sptr<IAbilityConnection> callback2 = new AbilityConnectCallback();
1447     std::shared_ptr<ConnectionRecord> connection1 =
1448         std::make_shared<ConnectionRecord>(callerToken, abilityRecord, callback1);
1449     std::shared_ptr<ConnectionRecord> connection2 =
1450         std::make_shared<ConnectionRecord>(callerToken, abilityRecord, callback2);
1451     abilityRequest.abilityInfo.deviceId = "id";
1452     abilityRequest.abilityInfo.bundleName = "bundle";
1453     abilityRequest.abilityInfo.name = "name";
1454     abilityRequest.abilityInfo.moduleName = "module";
1455     std::string stringUri = "id/bundle/name/module";
1456     abilityRecord->currentState_ = AbilityState::ACTIVE;
1457     abilityRecord->AddConnectRecordToList(connection1);
1458     connectManager->serviceMap_.emplace(stringUri, abilityRecord);
1459     connectManager->connectMap_.clear();
1460     connectManager->ConnectAbilityLocked(abilityRequest, connect, callerToken);
1461     abilityRecord->AddConnectRecordToList(connection2);
1462     connectManager->ConnectAbilityLocked(abilityRequest, connect, callerToken);
1463     connectManager->SetEventHandler(nullptr);
1464     connectManager->ConnectAbilityLocked(abilityRequest, connect, callerToken);
1465 }
1466 
1467 /*
1468  * Feature: AbilityConnectManager
1469  * Function: AttachAbilityThreadLocked
1470  * SubFunction: AttachAbilityThreadLocked
1471  * FunctionPoints: NA
1472  * EnvConditions: NA
1473  * CaseDescription: Verify AbilityConnectManager AttachAbilityThreadLocked
1474  */
1475 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_AttachAbilityThreadLocked_001, TestSize.Level1)
1476 {
1477     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1478     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1479     sptr<IAbilityScheduler> scheduler = nullptr;
1480     sptr<IRemoteObject> token = abilityRecord->GetToken();
1481     connectManager->serviceMap_.emplace("first", abilityRecord);
1482     connectManager->eventHandler_ = nullptr;
1483     connectManager->taskHandler_ = nullptr;
1484     int res = connectManager->AttachAbilityThreadLocked(scheduler, token);
1485     EXPECT_EQ(res, ERR_OK);
1486 }
1487 
1488 /*
1489  * Feature: AbilityConnectManager
1490  * Function: OnAppStateChanged
1491  * SubFunction: OnAppStateChanged
1492  * FunctionPoints: NA
1493  * EnvConditions: NA
1494  * CaseDescription: Verify AbilityConnectManager OnAppStateChanged
1495  */
1496 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAppStateChanged_001, TestSize.Level1)
1497 {
1498     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1499     ASSERT_NE(connectManager, nullptr);
1500     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1501     AppInfo info;
1502     std::string bundleName = "bundleName";
1503     std::string name = "name";
1504     int32_t uid = 0;
1505     info.processName = bundleName;
1506     abilityRecord->applicationInfo_.bundleName = bundleName;
1507     abilityRecord->applicationInfo_.name = name;
1508     abilityRecord->abilityInfo_.uid = uid;
1509     info.appData.push_back({name, uid});
1510     connectManager->serviceMap_.emplace("first", abilityRecord);
1511     connectManager->OnAppStateChanged(info);
1512 }
1513 
1514 /*
1515  * Feature: AbilityConnectManager
1516  * Function: OnAppStateChanged
1517  * SubFunction: OnAppStateChanged
1518  * FunctionPoints: NA
1519  * EnvConditions: NA
1520  * CaseDescription: Verify AbilityConnectManager OnAppStateChanged
1521  */
1522 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAppStateChanged_002, TestSize.Level1)
1523 {
1524     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1525     ASSERT_NE(connectManager, nullptr);
1526     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1527     AppInfo info;
1528     std::string bundleName = "bundleName";
1529     std::string name = "name";
1530     int32_t uid = 0;
1531     info.processName = "";
1532     abilityRecord->applicationInfo_.bundleName = bundleName;
1533     abilityRecord->applicationInfo_.name = name;
1534     abilityRecord->abilityInfo_.uid = uid;
1535     info.appData.push_back({name, uid});
1536     connectManager->serviceMap_.emplace("first", abilityRecord);
1537     connectManager->serviceMap_.emplace("first", nullptr);
1538     connectManager->OnAppStateChanged(info);
1539 }
1540 
1541 /*
1542  * Feature: AbilityConnectManager
1543  * Function: AbilityTransitionDone
1544  * SubFunction: AbilityTransitionDone
1545  * FunctionPoints: NA
1546  * EnvConditions: NA
1547  * CaseDescription: Verify AbilityConnectManager AbilityTransitionDone
1548  */
1549 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_AbilityTransitionDone_001, TestSize.Level1)
1550 {
1551     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1552     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1553     sptr<IRemoteObject> token = abilityRecord->GetToken();
1554     int state = AbilityState::INACTIVE;
1555     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
1556     abilityRecord->SetAbilityState(AbilityState::ACTIVE);
1557     connectManager->serviceMap_.emplace("first", abilityRecord);
1558     int res1 = connectManager->AbilityTransitionDone(token, state);
1559     EXPECT_EQ(res1, ERR_INVALID_VALUE);
1560     state = AbilityState::INITIAL;
1561     connectManager->MoveToTerminatingMap(abilityRecord);
1562     int res2 = connectManager->AbilityTransitionDone(token, state);
1563     EXPECT_EQ(res2, ERR_OK);
1564 }
1565 
1566 /*
1567  * Feature: AbilityConnectManager
1568  * Function: ScheduleConnectAbilityDoneLocked
1569  * SubFunction: ScheduleConnectAbilityDoneLocked
1570  * FunctionPoints: NA
1571  * EnvConditions: NA
1572  * CaseDescription: Verify AbilityConnectManager ScheduleConnectAbilityDoneLocked
1573  */
1574 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_ScheduleConnectAbilityDoneLocked_001, TestSize.Level1)
1575 {
1576     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1577     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1578     sptr<IRemoteObject> token = abilityRecord->GetToken();
1579     sptr<IRemoteObject> remoteObject = nullptr;
1580     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
1581     abilityRecord->SetAbilityState(AbilityState::INACTIVE);
1582     connectManager->serviceMap_.emplace("first", abilityRecord);
1583     int res = connectManager->ScheduleConnectAbilityDoneLocked(token, remoteObject);
1584     EXPECT_EQ(res, ERR_OK);
1585 }
1586 
1587 /*
1588  * Feature: AbilityConnectManager
1589  * Function: ScheduleDisconnectAbilityDoneLocked
1590  * SubFunction: ScheduleDisconnectAbilityDoneLocked
1591  * FunctionPoints: NA
1592  * EnvConditions: NA
1593  * CaseDescription: Verify AbilityConnectManager ScheduleDisconnectAbilityDoneLocked
1594  */
1595 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_ScheduleDisconnectAbilityDoneLocked_001, TestSize.Level1)
1596 {
1597     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1598     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1599     OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
1600     std::shared_ptr<ConnectionRecord> connection =
1601         std::make_shared<ConnectionRecord>(abilityRecord->GetToken(), abilityRecord, callback);
1602     sptr<IRemoteObject> token = abilityRecord->GetToken();
1603     connection->SetConnectState(ConnectionState::DISCONNECTING);
1604     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
1605     abilityRecord->SetAbilityState(AbilityState::INACTIVE);
1606     abilityRecord->connRecordList_.push_back(connection);
1607     connectManager->serviceMap_.emplace("first", abilityRecord);
1608     int res = connectManager->ScheduleDisconnectAbilityDoneLocked(token);
1609     EXPECT_EQ(res, INVALID_CONNECTION_STATE);
1610     abilityRecord->AddStartId();
1611     abilityRecord->SetAbilityState(AbilityState::ACTIVE);
1612     connectManager->serviceMap_.emplace("first", abilityRecord);
1613     res = connectManager->ScheduleDisconnectAbilityDoneLocked(token);
1614     EXPECT_EQ(res, ERR_OK);
1615 }
1616 
1617 /*
1618  * Feature: AbilityConnectManager
1619  * Function: ScheduleCommandAbilityDoneLocked
1620  * SubFunction: ScheduleCommandAbilityDoneLocked
1621  * FunctionPoints: NA
1622  * EnvConditions: NA
1623  * CaseDescription: Verify AbilityConnectManager ScheduleCommandAbilityDoneLocked
1624  */
1625 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_ScheduleCommandAbilityDoneLocked_001, TestSize.Level1)
1626 {
1627     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1628     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1629     sptr<IRemoteObject> token = abilityRecord->GetToken();
1630     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
1631     abilityRecord->SetAbilityState(AbilityState::INACTIVE);
1632     abilityRecord->connRecordList_.clear();
1633     connectManager->serviceMap_.emplace("first", abilityRecord);
1634     int res = connectManager->ScheduleCommandAbilityDoneLocked(token);
1635     EXPECT_EQ(res, ERR_OK);
1636 }
1637 
1638 /*
1639  * Feature: AbilityConnectManager
1640  * Function: CompleteCommandAbility
1641  * SubFunction: CompleteCommandAbility
1642  * FunctionPoints: NA
1643  * EnvConditions: NA
1644  * CaseDescription: Verify AbilityConnectManager CompleteCommandAbility
1645  */
1646 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_CompleteCommandAbility_001, TestSize.Level1)
1647 {
1648     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1649     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1650     connectManager->taskHandler_ = nullptr;
1651     connectManager->CompleteCommandAbility(abilityRecord);
1652     EXPECT_TRUE(abilityRecord->IsAbilityState(AbilityState::ACTIVE));
1653 }
1654 
1655 /*
1656  * Feature: AbilityConnectManager
1657  * Function: GetServiceRecordByElementName
1658  * SubFunction: GetServiceRecordByElementName
1659  * FunctionPoints: NA
1660  * EnvConditions: NA
1661  * CaseDescription: Verify AbilityConnectManager GetServiceRecordByElementName
1662  */
1663 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetServiceRecordByElementName_001, TestSize.Level1)
1664 {
1665     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1666     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1667     std::string element = "first";
1668     connectManager->serviceMap_.emplace(element, abilityRecord);
1669     auto res = connectManager->GetServiceRecordByElementName(element);
1670     EXPECT_NE(res, nullptr);
1671 }
1672 
1673 /*
1674  * Feature: AbilityConnectManager
1675  * Function: GetExtensionByTokenFromServiceMap
1676  * SubFunction: GetExtensionByTokenFromServiceMap
1677  * FunctionPoints: NA
1678  * EnvConditions: NA
1679  * CaseDescription: Verify AbilityConnectManager GetExtensionByTokenFromServiceMap
1680  */
1681 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetExtensionByTokenFromServiceMap_001, TestSize.Level1)
1682 {
1683     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1684     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1685     sptr<IRemoteObject> token = abilityRecord->GetToken();
1686     connectManager->serviceMap_.emplace("first", nullptr);
1687     auto res = connectManager->GetExtensionByTokenFromServiceMap(token);
1688     EXPECT_EQ(res, nullptr);
1689 }
1690 
1691 /*
1692  * Feature: AbilityConnectManager
1693  * Function: GetConnectRecordListByCallback
1694  * SubFunction: GetConnectRecordListByCallback
1695  * FunctionPoints: NA
1696  * EnvConditions: NA
1697  * CaseDescription: Verify AbilityConnectManager GetConnectRecordListByCallback
1698  */
1699 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetConnectRecordListByCallback_001, TestSize.Level1)
1700 {
1701     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1702     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1703     sptr<IAbilityConnection> callback = new AbilityConnectCallback();
1704     connectManager->connectMap_.clear();
1705     auto res = connectManager->GetConnectRecordListByCallback(callback);
1706     EXPECT_EQ(res.size(), 0u);
1707 }
1708 
1709 /*
1710  * Feature: AbilityConnectManager
1711  * Function: GetAbilityRecordById
1712  * SubFunction: GetAbilityRecordById
1713  * FunctionPoints: NA
1714  * EnvConditions: NA
1715  * CaseDescription: Verify AbilityConnectManager GetAbilityRecordById
1716  */
1717 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetAbilityRecordById_001, TestSize.Level1)
1718 {
1719     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1720     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1721     int64_t abilityRecordId = abilityRecord->GetRecordId();
1722     connectManager->serviceMap_.emplace("first", abilityRecord);
1723     connectManager->serviceMap_.emplace("second", nullptr);
1724     auto res = connectManager->GetAbilityRecordById(abilityRecordId);
1725     EXPECT_NE(res, nullptr);
1726 }
1727 
1728 /*
1729  * Feature: AbilityConnectManager
1730  * Function: LoadAbility
1731  * SubFunction: LoadAbility
1732  * FunctionPoints: NA
1733  * EnvConditions: NA
1734  * CaseDescription: Verify AbilityConnectManager LoadAbility
1735  */
1736 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_LoadAbility_001, TestSize.Level1)
1737 {
1738     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1739     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1740     abilityRecord->isLauncherRoot_ = true;
1741     abilityRecord->isRestarting_ = true;
1742     abilityRecord->isLauncherAbility_ = true;
1743     abilityRecord->restartCount_ = -1;
1744     EXPECT_FALSE(abilityRecord->CanRestartRootLauncher());
1745     connectManager->LoadAbility(abilityRecord);
1746 }
1747 
1748 /*
1749  * Feature: AbilityConnectManager
1750  * Function: LoadAbility
1751  * SubFunction: LoadAbility
1752  * FunctionPoints: NA
1753  * EnvConditions: NA
1754  * CaseDescription: Verify AbilityConnectManager LoadAbility
1755  */
1756 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_LoadAbility_002, TestSize.Level1)
1757 {
1758     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1759     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1760     std::shared_ptr<CallerRecord> caller1 = std::make_shared<CallerRecord>(0, abilityRecord);
1761     std::shared_ptr<CallerRecord> caller2 = std::make_shared<CallerRecord>();
1762     abilityRecord->isLauncherRoot_ = false;
1763     abilityRecord->isCreateByConnect_ = false;
1764     abilityRecord->callerList_.push_back(caller1);
1765     EXPECT_TRUE(abilityRecord->CanRestartRootLauncher());
1766     connectManager->LoadAbility(abilityRecord);
1767     abilityRecord->callerList_.push_back(caller2);
1768     connectManager->LoadAbility(abilityRecord);
1769     abilityRecord->callerList_.push_back(nullptr);
1770     connectManager->LoadAbility(abilityRecord);
1771 }
1772 
1773 /*
1774  * Feature: AbilityConnectManager
1775  * Function: PostTimeOutTask
1776  * SubFunction: PostTimeOutTask
1777  * FunctionPoints: NA
1778  * EnvConditions: NA
1779  * CaseDescription: Verify AbilityConnectManager PostTimeOutTask
1780  */
1781 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_PostTimeOutTask_001, TestSize.Level1)
1782 {
1783     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1784     ASSERT_NE(connectManager, nullptr);
1785     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1786     uint32_t messageId = 2;
1787     connectManager->PostTimeOutTask(abilityRecord, messageId);
1788 }
1789 
1790 /*
1791  * Feature: AbilityConnectManager
1792  * Function: HandleStartTimeoutTask
1793  * SubFunction: HandleStartTimeoutTask
1794  * FunctionPoints: NA
1795  * EnvConditions: NA
1796  * CaseDescription: Verify AbilityConnectManager HandleStartTimeoutTask
1797  */
1798 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleStartTimeoutTask_001, TestSize.Level1)
1799 {
1800     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1801     ASSERT_NE(connectManager, nullptr);
1802     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1803     int resultCode = LOAD_ABILITY_TIMEOUT;
1804     abilityRecord->abilityInfo_.name = "abilityName";
1805     connectManager->HandleStartTimeoutTask(abilityRecord, resultCode);
1806 }
1807 
1808 /*
1809  * Feature: AbilityConnectManager
1810  * Function: HandleStartTimeoutTask
1811  * SubFunction: HandleStartTimeoutTask
1812  * FunctionPoints: NA
1813  * EnvConditions: NA
1814  * CaseDescription: Verify AbilityConnectManager HandleStartTimeoutTask
1815  */
1816 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleStartTimeoutTask_002, TestSize.Level1)
1817 {
1818     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1819     ASSERT_NE(connectManager, nullptr);
1820     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1821     int resultCode = LOAD_ABILITY_TIMEOUT;
1822     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
1823     connectManager->HandleStartTimeoutTask(abilityRecord, resultCode);
1824 }
1825 
1826 /*
1827  * Feature: AbilityConnectManager
1828  * Function: HandleStartTimeoutTask
1829  * SubFunction: HandleStartTimeoutTask
1830  * FunctionPoints: NA
1831  * EnvConditions: NA
1832  * CaseDescription: Verify AbilityConnectManager HandleStartTimeoutTask
1833  */
1834 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleStartTimeoutTask_003, TestSize.Level1)
1835 {
1836     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1837     ASSERT_NE(connectManager, nullptr);
1838     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1839     int resultCode = CONNECTION_TIMEOUT;
1840     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
1841     connectManager->HandleStartTimeoutTask(abilityRecord, resultCode);
1842 }
1843 
1844 /*
1845  * Feature: AbilityConnectManager
1846  * Function: HandleCommandTimeoutTask
1847  * SubFunction: HandleCommandTimeoutTask
1848  * FunctionPoints: NA
1849  * EnvConditions: NA
1850  * CaseDescription: Verify AbilityConnectManager HandleCommandTimeoutTask
1851  */
1852 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleCommandTimeoutTask_001, TestSize.Level1)
1853 {
1854     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1855     ASSERT_NE(connectManager, nullptr);
1856     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1857     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
1858     connectManager->HandleCommandTimeoutTask(abilityRecord);
1859     abilityRecord->abilityInfo_.name = "abilityName";
1860     connectManager->HandleCommandTimeoutTask(abilityRecord);
1861 }
1862 
1863 /*
1864  * Feature: AbilityConnectManager
1865  * Function: HandleTerminateDisconnectTask
1866  * SubFunction: HandleTerminateDisconnectTask
1867  * FunctionPoints: NA
1868  * EnvConditions: NA
1869  * CaseDescription: Verify AbilityConnectManager HandleTerminateDisconnectTask
1870  */
1871 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleTerminateDisconnectTask_001, TestSize.Level1)
1872 {
1873     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1874     ASSERT_NE(connectManager, nullptr);
1875     OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
1876     std::shared_ptr<ConnectionRecord> connection =
1877         std::make_shared<ConnectionRecord>(nullptr, nullptr, callback);
1878     AbilityConnectManager::ConnectListType connectlist;
1879     connectlist.push_back(nullptr);
1880     connectlist.push_back(connection);
1881     connectManager->HandleTerminateDisconnectTask(connectlist);
1882 }
1883 
1884 /*
1885  * Feature: AbilityConnectManager
1886  * Function: DispatchInactive
1887  * SubFunction: DispatchInactive
1888  * FunctionPoints: NA
1889  * EnvConditions: NA
1890  * CaseDescription: Verify AbilityConnectManager DispatchInactive
1891  */
1892 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_DispatchInactive_001, TestSize.Level1)
1893 {
1894     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1895     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1896     int state = 0;
1897     abilityRecord->SetAbilityState(AbilityState::INACTIVATING);
1898     abilityRecord->isCreateByConnect_ = false;
1899     connectManager->SetTaskHandler(TaskHandler());
1900     connectManager->SetEventHandler(EventHandler());
1901     int res = connectManager->DispatchInactive(abilityRecord, state);
1902     EXPECT_EQ(res, ERR_OK);
1903 }
1904 
1905 /*
1906  * Feature: AbilityConnectManager
1907  * Function: DispatchTerminate
1908  * SubFunction: DispatchTerminate
1909  * FunctionPoints: NA
1910  * EnvConditions: NA
1911  * CaseDescription: Verify AbilityConnectManager DispatchTerminate
1912  */
1913 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_DispatchTerminate_001, TestSize.Level1)
1914 {
1915     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1916     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1917     connectManager->SetTaskHandler(TaskHandler());
1918     connectManager->SetEventHandler(EventHandler());
1919     int res = connectManager->DispatchTerminate(abilityRecord);
1920     EXPECT_EQ(res, ERR_OK);
1921 }
1922 
1923 /*
1924  * Feature: AbilityConnectManager
1925  * Function: CommandAbility
1926  * SubFunction: CommandAbility
1927  * FunctionPoints: NA
1928  * EnvConditions: NA
1929  * CaseDescription: Verify AbilityConnectManager CommandAbility
1930  */
1931 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_CommandAbility_001, TestSize.Level1)
1932 {
1933     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1934     ASSERT_NE(connectManager, nullptr);
1935     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1936     connectManager->SetEventHandler(nullptr);
1937     connectManager->CommandAbility(abilityRecord);
1938 }
1939 
1940 /*
1941  * Feature: AbilityConnectManager
1942  * Function: TerminateDone
1943  * SubFunction: TerminateDone
1944  * FunctionPoints: NA
1945  * EnvConditions: NA
1946  * CaseDescription: Verify AbilityConnectManager TerminateDone
1947  */
1948 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_TerminateDone_001, TestSize.Level1)
1949 {
1950     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1951     ASSERT_NE(connectManager, nullptr);
1952     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1953     abilityRecord->SetAbilityState(AbilityState::TERMINATING);
1954     connectManager->TerminateDone(abilityRecord);
1955 }
1956 
1957 /*
1958  * Feature: AbilityConnectManager
1959  * Function: IsAbilityConnected
1960  * SubFunction: IsAbilityConnected
1961  * FunctionPoints: NA
1962  * EnvConditions: NA
1963  * CaseDescription: Verify AbilityConnectManager IsAbilityConnected
1964  */
1965 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_IsAbilityConnected_001, TestSize.Level1)
1966 {
1967     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1968     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1969     std::list<std::shared_ptr<ConnectionRecord>> connectRecordList;
1970     OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
1971     std::shared_ptr<ConnectionRecord> connection =
1972         std::make_shared<ConnectionRecord>(abilityRecord->GetToken(), abilityRecord, callback);
1973     connectRecordList.push_back(connection);
1974     bool res1 = connectManager->IsAbilityConnected(nullptr, connectRecordList);
1975     EXPECT_FALSE(res1);
1976     connectRecordList.push_back(nullptr);
1977     bool res2 = connectManager->IsAbilityConnected(abilityRecord, connectRecordList);
1978     EXPECT_TRUE(res2);
1979 }
1980 
1981 /*
1982  * Feature: AbilityConnectManager
1983  * Function: RemoveServiceAbility
1984  * SubFunction: RemoveServiceAbility
1985  * FunctionPoints: NA
1986  * EnvConditions: NA
1987  * CaseDescription: Verify AbilityConnectManager RemoveServiceAbility
1988  */
1989 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_RemoveServiceAbility_001, TestSize.Level1)
1990 {
1991     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
1992     ASSERT_NE(connectManager, nullptr);
1993     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
1994     AbilityInfo abilityInfo;
1995     abilityRecord->abilityInfo_ = abilityInfo;
1996     connectManager->serviceMap_.clear();
1997     connectManager->RemoveServiceAbility(abilityRecord);
1998 }
1999 
2000 /*
2001  * Feature: AbilityConnectManager
2002  * Function: OnCallBackDied
2003  * SubFunction: OnCallBackDied
2004  * FunctionPoints: NA
2005  * EnvConditions: NA
2006  * CaseDescription: Verify AbilityConnectManager OnCallBackDied
2007  */
2008 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnCallBackDied_001, TestSize.Level1)
2009 {
2010     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2011     ASSERT_NE(connectManager, nullptr);
2012     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2013     wptr<IRemoteObject> remote{ abilityRecord->GetToken() };
2014     connectManager->SetEventHandler(nullptr);
2015     connectManager->OnCallBackDied(remote);
2016 }
2017 
2018 /*
2019  * Feature: AbilityConnectManager
2020  * Function: HandleCallBackDiedTask
2021  * SubFunction: HandleCallBackDiedTask
2022  * FunctionPoints: NA
2023  * EnvConditions: NA
2024  * CaseDescription: Verify AbilityConnectManager HandleCallBackDiedTask
2025  */
2026 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleCallBackDiedTask_001, TestSize.Level1)
2027 {
2028     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2029     ASSERT_NE(connectManager, nullptr);
2030     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2031     sptr<IRemoteObject> connect = abilityRecord->GetToken();
2032     connectManager->connectMap_.clear();
2033     connectManager->HandleCallBackDiedTask(connect);
2034 }
2035 
2036 /*
2037  * Feature: AbilityConnectManager
2038  * Function: OnAbilityDied
2039  * SubFunction: OnAbilityDied
2040  * FunctionPoints: NA
2041  * EnvConditions: NA
2042  * CaseDescription: Verify AbilityConnectManager OnAbilityDied
2043  */
2044 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnAbilityDied_001, TestSize.Level1)
2045 {
2046     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2047     ASSERT_NE(connectManager, nullptr);
2048     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2049     int32_t currentUserId = 0;
2050     abilityRecord->abilityInfo_.type = AbilityType::PAGE;
2051     connectManager->SetEventHandler(nullptr);
2052     connectManager->OnAbilityDied(abilityRecord, currentUserId);
2053     abilityRecord->abilityInfo_.type = AbilityType::EXTENSION;
2054     connectManager->OnAbilityDied(abilityRecord, currentUserId);
2055 }
2056 
2057 /*
2058  * Feature: AbilityConnectManager
2059  * Function: OnTimeOut
2060  * SubFunction: OnTimeOut
2061  * FunctionPoints: NA
2062  * EnvConditions: NA
2063  * CaseDescription: Verify AbilityConnectManager OnTimeOut
2064  */
2065 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_OnTimeOut_001, TestSize.Level1)
2066 {
2067     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2068     ASSERT_NE(connectManager, nullptr);
2069     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2070     uint32_t msgId = 2;
2071     connectManager->serviceMap_.emplace("first", abilityRecord);
2072     int64_t abilityRecordId = 1;
2073     connectManager->OnTimeOut(msgId, abilityRecordId);
2074     msgId = 0;
2075     connectManager->OnTimeOut(msgId, abilityRecordId);
2076 }
2077 
2078 /*
2079  * Feature: AbilityConnectManager
2080  * Function: HandleInactiveTimeout
2081  * SubFunction: HandleInactiveTimeout
2082  * FunctionPoints: NA
2083  * EnvConditions: NA
2084  * CaseDescription: Verify AbilityConnectManager HandleInactiveTimeout
2085  */
2086 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleInactiveTimeout_001, TestSize.Level1)
2087 {
2088     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2089     ASSERT_NE(connectManager, nullptr);
2090     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2091     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
2092     connectManager->HandleInactiveTimeout(abilityRecord);
2093     abilityRecord->abilityInfo_.name = "abilityName";
2094     connectManager->HandleInactiveTimeout(abilityRecord);
2095 }
2096 
2097 /*
2098  * Feature: AbilityConnectManager
2099  * Function: HandleAbilityDiedTask
2100  * SubFunction: HandleAbilityDiedTask
2101  * FunctionPoints: NA
2102  * EnvConditions: NA
2103  * CaseDescription: Verify AbilityConnectManager HandleAbilityDiedTask
2104  */
2105 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_HandleAbilityDiedTask_001, TestSize.Level1)
2106 {
2107     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2108     ASSERT_NE(connectManager, nullptr);
2109     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2110     int32_t currentUserId = 0;
2111     connectManager->serviceMap_.clear();
2112     connectManager->HandleAbilityDiedTask(abilityRecord, currentUserId);
2113 }
2114 
2115 /*
2116  * Feature: AbilityConnectManager
2117  * Function: RestartAbility
2118  * SubFunction: RestartAbility
2119  * FunctionPoints: NA
2120  * EnvConditions: NA
2121  * CaseDescription: Verify AbilityConnectManager RestartAbility
2122  */
2123 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_RestartAbility_001, TestSize.Level1)
2124 {
2125     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2126     ASSERT_NE(connectManager, nullptr);
2127     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2128     int32_t currentUserId = 0;
2129     connectManager->userId_ = 1;
2130     abilityRecord->abilityInfo_.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
2131     connectManager->RestartAbility(abilityRecord, currentUserId);
2132     connectManager->userId_ = currentUserId;
2133     connectManager->RestartAbility(abilityRecord, currentUserId);
2134 }
2135 
2136 /*
2137  * Feature: AbilityConnectManager
2138  * Function: RestartAbility
2139  * SubFunction: RestartAbility
2140  * FunctionPoints: NA
2141  * EnvConditions: NA
2142  * CaseDescription: Verify AbilityConnectManager RestartAbility
2143  */
2144 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_RestartAbility_002, TestSize.Level1)
2145 {
2146     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2147     ASSERT_NE(connectManager, nullptr);
2148     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2149     int32_t currentUserId = 0;
2150     connectManager->userId_ = currentUserId;
2151     abilityRecord->abilityInfo_.name = "abilityName";
2152     abilityRecord->SetRestartCount(-1);
2153     connectManager->RestartAbility(abilityRecord, currentUserId);
2154 }
2155 
2156 /*
2157  * Feature: AbilityConnectManager
2158  * Function: DumpState
2159  * SubFunction: DumpState
2160  * FunctionPoints: NA
2161  * EnvConditions: NA
2162  * CaseDescription: Verify AbilityConnectManager DumpState
2163  */
2164 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_DumpState_001, TestSize.Level1)
2165 {
2166     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2167     ASSERT_NE(connectManager, nullptr);
2168     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2169     std::vector<std::string> info;
2170     bool isClient = false;
2171     std::string args = "args";
2172     connectManager->serviceMap_.emplace(args, abilityRecord);
2173     connectManager->DumpState(info, isClient, args);
2174     connectManager->serviceMap_.clear();
2175     connectManager->DumpState(info, isClient, args);
2176     args = "";
2177     connectManager->DumpState(info, isClient, args);
2178 }
2179 
2180 /*
2181  * Feature: AbilityConnectManager
2182  * Function: DumpStateByUri
2183  * SubFunction: DumpStateByUri
2184  * FunctionPoints: NA
2185  * EnvConditions: NA
2186  * CaseDescription: Verify AbilityConnectManager DumpStateByUri
2187  */
2188 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_DumpStateByUri_001, TestSize.Level1)
2189 {
2190     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2191     ASSERT_NE(connectManager, nullptr);
2192     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2193     std::vector<std::string> info;
2194     bool isClient = false;
2195     std::string args = "args";
2196     std::vector<std::string> params;
2197     connectManager->serviceMap_.emplace(args, abilityRecord);
2198     connectManager->DumpStateByUri(info, isClient, args, params);
2199     connectManager->serviceMap_.clear();
2200     connectManager->DumpStateByUri(info, isClient, args, params);
2201 }
2202 
2203 /*
2204  * Feature: AbilityConnectManager
2205  * Function: GetExtensionRunningInfos
2206  * SubFunction: GetExtensionRunningInfos
2207  * FunctionPoints: NA
2208  * EnvConditions: NA
2209  * CaseDescription: Verify AbilityConnectManager GetExtensionRunningInfos
2210  */
2211 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetExtensionRunningInfos_001, TestSize.Level1)
2212 {
2213     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2214     ASSERT_NE(connectManager, nullptr);
2215     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2216     int upperLimit = 1;
2217     std::vector<ExtensionRunningInfo> info;
2218     int32_t userId = 0;
2219     bool isPerm = false;
2220     ExtensionRunningInfo extensionInfo;
2221     info.push_back(extensionInfo);
2222     connectManager->serviceMap_.emplace("first", abilityRecord);
2223     connectManager->GetExtensionRunningInfos(upperLimit, info, userId, isPerm);
2224 }
2225 
2226 /*
2227  * Feature: AbilityConnectManager
2228  * Function: GetExtensionRunningInfo
2229  * SubFunction: GetExtensionRunningInfo
2230  * FunctionPoints: NA
2231  * EnvConditions: NA
2232  * CaseDescription: Verify AbilityConnectManager GetExtensionRunningInfo
2233  */
2234 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_GetExtensionRunningInfo_001, TestSize.Level1)
2235 {
2236     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2237     ASSERT_NE(connectManager, nullptr);
2238     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2239     OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
2240     std::shared_ptr<ConnectionRecord> connection =
2241         std::make_shared<ConnectionRecord>(abilityRecord->GetToken(), abilityRecord, callback);
2242     int32_t userId = 0;
2243     std::vector<ExtensionRunningInfo> info;
2244     Want want;
2245     AbilityInfo abilityInfo;
2246     ApplicationInfo applicationInfo;
2247     want.SetElementName("device", "bundle", "ability", "module");
2248     abilityRecord->SetWant(want);
2249     abilityRecord->connRecordList_.push_back(nullptr);
2250     abilityRecord->connRecordList_.push_back(connection);
2251     connectManager->GetExtensionRunningInfo(abilityRecord, userId, info);
2252 }
2253 
2254 /*
2255  * Feature: AbilityConnectManager
2256  * Function: StopAllExtensions
2257  * SubFunction: StopAllExtensions
2258  * FunctionPoints: NA
2259  * EnvConditions: NA
2260  * CaseDescription: Verify AbilityConnectManager StopAllExtensions
2261  */
2262 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_StopAllExtensions_001, TestSize.Level1)
2263 {
2264     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2265     ASSERT_NE(connectManager, nullptr);
2266     std::shared_ptr<AbilityRecord> abilityRecord1 = serviceRecord_;
2267     std::shared_ptr<AbilityRecord> abilityRecord2 = serviceRecord_;
2268     abilityRecord1->abilityInfo_.type = AbilityType::EXTENSION;
2269     abilityRecord2->abilityInfo_.type = AbilityType::PAGE;
2270     connectManager->serviceMap_.emplace("first", abilityRecord1);
2271     connectManager->serviceMap_.emplace("second", abilityRecord2);
2272     connectManager->StopAllExtensions();
2273 }
2274 
2275 /*
2276  * Feature: AbilityConnectManager
2277  * Function: IsAbilityNeedKeepAlive
2278  * SubFunction:
2279  * FunctionPoints: IsAbilityNeedKeepAlive
2280  * EnvConditions:NA
2281  * CaseDescription: Verify the IsAbilityNeedKeepAlive need keep alive.
2282  * @tc.require: issueI6588V
2283  */
2284 HWTEST_F(AbilityConnectManagerTest, AAFWK_IsAbilityNeedKeepAlive_001, TestSize.Level1)
2285 {
2286     ConnectManager()->SetTaskHandler(TaskHandler());
2287     ConnectManager()->SetEventHandler(EventHandler());
2288 
2289     // mock bms return
2290     EXPECT_TRUE(ConnectManager()->IsAbilityNeedKeepAlive(serviceRecord2_));
2291 }
2292 
2293 /*
2294  * Feature: AbilityConnectManager
2295  * Function: RestartAbility
2296  * SubFunction:
2297  * FunctionPoints: RestartAbility
2298  * EnvConditions:NA
2299  * CaseDescription: Verify ability not restart the normal ability died.
2300  * @tc.require: issueI6588V
2301  */
2302 HWTEST_F(AbilityConnectManagerTest, AAFWK_RestartAbility_001, TestSize.Level1)
2303 {
2304     ConnectManager()->SetTaskHandler(TaskHandler());
2305     ConnectManager()->SetEventHandler(EventHandler());
2306 
2307     int userId = 0;
2308 
2309     auto result = ConnectManager()->StartAbility(abilityRequest_);
2310     EXPECT_EQ(OHOS::ERR_OK, result);
2311     WaitUntilTaskDone(TaskHandler());
2312 
2313     auto elementName = abilityRequest_.want.GetElement().GetURI();
2314     std::shared_ptr<AbilityRecord> service = ConnectManager()->GetServiceRecordByElementName(elementName);
2315     EXPECT_NE(service, nullptr);
2316     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
2317 
2318     // HandleTerminate
2319     ConnectManager()->OnAbilityDied(service, userId);
2320     WaitUntilTaskDone(TaskHandler());
2321     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 0);
2322 }
2323 
2324 /*
2325  * Feature: AbilityConnectManager
2326  * Function: RestartAbility
2327  * SubFunction:
2328  * FunctionPoints: RestartAbility
2329  * EnvConditions:NA
2330  * CaseDescription: Verify ability restart when the resident ability died.
2331  * @tc.require: issueI6588V
2332  */
2333 HWTEST_F(AbilityConnectManagerTest, AAFWK_RestartAbility_002, TestSize.Level1)
2334 {
2335     ConnectManager()->SetTaskHandler(TaskHandler());
2336     ConnectManager()->SetEventHandler(EventHandler());
2337 
2338     int userId = 0;
2339 
2340     auto result = ConnectManager()->StartAbility(abilityRequest2_);
2341     EXPECT_EQ(OHOS::ERR_OK, result);
2342     WaitUntilTaskDone(TaskHandler());
2343 
2344     auto elementName = abilityRequest2_.want.GetElement().GetURI();
2345     auto service = ConnectManager()->GetServiceRecordByElementName(elementName);
2346     EXPECT_NE(service, nullptr);
2347     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
2348 
2349     // HandleTerminate
2350     ConnectManager()->HandleAbilityDiedTask(service, userId);
2351     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
2352 }
2353 
2354 /*
2355  * Feature: AbilityConnectManager
2356  * Function: RestartAbility
2357  * SubFunction:
2358  * FunctionPoints: RestartAbility
2359  * EnvConditions:NA
2360  * CaseDescription: Verify ability restart when the resident ability died and restart out of max times.
2361  * @tc.require: issueI6588V
2362  */
2363 HWTEST_F(AbilityConnectManagerTest, AAFWK_RestartAbility_003, TestSize.Level1)
2364 {
2365     ConnectManager()->SetTaskHandler(TaskHandler());
2366     ConnectManager()->SetEventHandler(EventHandler());
2367 
2368     int userId = 0;
2369 
2370     auto result = ConnectManager()->StartAbility(abilityRequest2_);
2371     EXPECT_EQ(OHOS::ERR_OK, result);
2372     WaitUntilTaskDone(TaskHandler());
2373 
2374     auto elementName = abilityRequest2_.want.GetElement().GetURI();
2375     std::shared_ptr<AbilityRecord> service = ConnectManager()->GetServiceRecordByElementName(elementName);
2376     EXPECT_NE(service, nullptr);
2377     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
2378     // set the over interval time according the config; without init config, the interval time is 0.
2379     // ensure now - restartTime < intervalTime
2380     service->SetRestartTime(AbilityUtil::SystemTimeMillis() + 1000);
2381 
2382     // HandleTerminate
2383     ConnectManager()->HandleAbilityDiedTask(service, userId);
2384     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 0);
2385 }
2386 
2387 /*
2388  * Feature: AbilityConnectManager
2389  * Function: PostRestartResidentTask
2390  * SubFunction:
2391  * FunctionPoints: PostRestartResidentTask
2392  * EnvConditions:NA
2393  * CaseDescription: Verify the PostRestartResidentTask process.
2394  * @tc.require: issueI6588V
2395  */
2396 HWTEST_F(AbilityConnectManagerTest, AAFWK_PostRestartResidentTask_001, TestSize.Level1)
2397 {
2398     ConnectManager()->SetTaskHandler(TaskHandler());
2399     ConnectManager()->SetEventHandler(EventHandler());
2400 
2401     ConnectManager()->PostRestartResidentTask(abilityRequest2_);
2402     WaitUntilTaskDone(TaskHandler());
2403     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 0);
2404 }
2405 
2406 /*
2407  * Feature: AbilityConnectManager
2408  * Function: StartAbility
2409  * SubFunction: NA
2410  * FunctionPoints: StartAbility
2411  * EnvConditions:NA
2412  * CaseDescription: Verify the normal process of startability with session info
2413  */
2414 HWTEST_F(AbilityConnectManagerTest, AAFWK_Start_Service_With_SessionInfo_001, TestSize.Level1)
2415 {
2416     ConnectManager()->SetTaskHandler(TaskHandler());
2417     ConnectManager()->SetEventHandler(EventHandler());
2418 
2419     auto sessionInfo = MockSessionInfo(0);
2420     abilityRequest_.sessionInfo = sessionInfo;
2421     auto result = ConnectManager()->StartAbility(abilityRequest_);
2422     EXPECT_EQ(OHOS::ERR_OK, result);
2423     abilityRequest_.sessionInfo = nullptr;
2424     WaitUntilTaskDone(TaskHandler());
2425 
2426     auto service = ConnectManager()->GetUIExtensioBySessionInfo(sessionInfo);
2427     EXPECT_EQ(static_cast<int>(ConnectManager()->GetServiceMap().size()), 1);
2428 }
2429 
2430 /*
2431  * Feature: AbilityConnectManager
2432  * Function: StartAbilityLocked
2433  * SubFunction: StartAbilityLocked
2434  * FunctionPoints: NA
2435  * EnvConditions: NA
2436  * CaseDescription: Verify AbilityConnectManager StartAbilityLocked with session info
2437  */
2438 HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_StartAbilityLocked_With_SessionInfo_001, TestSize.Level1)
2439 {
2440     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2441     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2442     AbilityRequest abilityRequest;
2443     abilityRequest.abilityInfo.deviceId = "id";
2444     abilityRequest.abilityInfo.bundleName = "bundle";
2445     abilityRequest.abilityInfo.name = "name";
2446     abilityRequest.abilityInfo.moduleName = "module";
2447     std::string stringUri = "id/bundle/module/name";
2448     AppExecFwk::ElementName element(abilityRequest.abilityInfo.deviceId, abilityRequest.abilityInfo.bundleName,
2449         abilityRequest.abilityInfo.name, abilityRequest.abilityInfo.moduleName);
2450     EXPECT_EQ(element.GetURI(), stringUri);
2451     abilityRecord->currentState_ = AbilityState::ACTIVE;
2452     abilityRecord->SetPreAbilityRecord(serviceRecord1_);
2453     connectManager->serviceMap_.emplace(stringUri, abilityRecord);
2454     abilityRequest.sessionInfo = MockSessionInfo(0);
2455     int res = connectManager->StartAbilityLocked(abilityRequest);
2456     EXPECT_EQ(res, ERR_OK);
2457 }
2458 
2459 /*
2460  * Feature: MissionListManager
2461  * Function: MoveToBackground
2462  * SubFunction: NA
2463  * FunctionPoints: MissionListManager MoveToBackground
2464  * EnvConditions: NA
2465  * CaseDescription: Verify MoveToBackground
2466  */
2467 HWTEST_F(AbilityConnectManagerTest, MoveToBackground_001, TestSize.Level1)
2468 {
2469     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2470     ASSERT_NE(connectManager, nullptr);
2471     std::shared_ptr<AbilityRecord> abilityRecord;
2472     connectManager->MoveToBackground(abilityRecord);
2473     connectManager.reset();
2474 }
2475 
2476 /*
2477  * Feature: MissionListManager
2478  * Function: MoveToBackground
2479  * SubFunction: NA
2480  * FunctionPoints: MissionListManager MoveToBackground
2481  * EnvConditions: NA
2482  * CaseDescription: Verify MoveToBackground
2483  */
2484 HWTEST_F(AbilityConnectManagerTest, MoveToBackground_002, TestSize.Level1)
2485 {
2486     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2487     ASSERT_NE(connectManager, nullptr);
2488     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2489     abilityRecord->lifeCycleStateInfo_.sceneFlag = 1;
2490     connectManager->MoveToBackground(abilityRecord);
2491     connectManager.reset();
2492 }
2493 
2494 /*
2495  * Feature: MissionListManager
2496  * Function: MoveToBackground
2497  * SubFunction: NA
2498  * FunctionPoints: MissionListManager MoveToBackground
2499  * EnvConditions: NA
2500  * CaseDescription: Verify MoveToBackground
2501  */
2502 HWTEST_F(AbilityConnectManagerTest, MoveToBackground_003, TestSize.Level1)
2503 {
2504     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2505     ASSERT_NE(connectManager, nullptr);
2506     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2507     abilityRecord->lifeCycleStateInfo_.sceneFlag = 2;
2508     abilityRecord->SetClearMissionFlag(true);
2509     connectManager->MoveToBackground(abilityRecord);
2510     connectManager.reset();
2511 }
2512 
2513 
2514 /*
2515  * Feature: MissionListManager
2516  * Function: CompleteBackground
2517  * SubFunction: NA
2518  * FunctionPoints: MissionListManager CompleteBackground
2519  * EnvConditions: NA
2520  * CaseDescription: Verify CompleteBackground
2521  */
2522 HWTEST_F(AbilityConnectManagerTest, CompleteBackground_001, TestSize.Level1)
2523 {
2524     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2525     ASSERT_NE(connectManager, nullptr);
2526     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2527     abilityRecord->currentState_ = AbilityState::FOREGROUND;
2528     connectManager->CompleteBackground(abilityRecord);
2529     connectManager.reset();
2530 }
2531 
2532 /*
2533  * Feature: MissionListManager
2534  * Function: CompleteBackground
2535  * SubFunction: NA
2536  * FunctionPoints: MissionListManager CompleteBackground
2537  * EnvConditions: NA
2538  * CaseDescription: Verify CompleteBackground
2539  */
2540 HWTEST_F(AbilityConnectManagerTest, CompleteBackground_002, TestSize.Level1)
2541 {
2542     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2543     ASSERT_NE(connectManager, nullptr);
2544     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2545     abilityRecord->currentState_ = AbilityState::BACKGROUNDING;
2546     abilityRecord->SetPendingState(AbilityState::FOREGROUND);
2547     abilityRecord->SetSwitchingPause(true);
2548     connectManager->CompleteBackground(abilityRecord);
2549     connectManager.reset();
2550 }
2551 
2552 /*
2553  * Feature: MissionListManager
2554  * Function: CompleteBackground
2555  * SubFunction: NA
2556  * FunctionPoints: MissionListManager CompleteBackground
2557  * EnvConditions: NA
2558  * CaseDescription: Verify CompleteBackground
2559  */
2560 HWTEST_F(AbilityConnectManagerTest, CompleteBackground_003, TestSize.Level1)
2561 {
2562     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2563     ASSERT_NE(connectManager, nullptr);
2564     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2565     abilityRecord->currentState_ = AbilityState::BACKGROUNDING;
2566     abilityRecord->SetPendingState(AbilityState::BACKGROUND);
2567     abilityRecord->SetSwitchingPause(false);
2568     abilityRecord->SetStartedByCall(true);
2569     abilityRecord->SetStartToBackground(true);
2570     abilityRecord->isReady_ = true;
2571     connectManager->CompleteBackground(abilityRecord);
2572     connectManager.reset();
2573 }
2574 
2575 /*
2576  * Feature: MissionListManager
2577  * Function: CompleteBackground
2578  * SubFunction: NA
2579  * FunctionPoints: MissionListManager CompleteBackground
2580  * EnvConditions: NA
2581  * CaseDescription: Verify CompleteBackground
2582  */
2583 HWTEST_F(AbilityConnectManagerTest, CompleteBackground_004, TestSize.Level1)
2584 {
2585     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2586     ASSERT_NE(connectManager, nullptr);
2587     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2588     std::shared_ptr<AbilityRecord> abilityRecord2 = InitAbilityRecord();
2589     abilityRecord->currentState_ = AbilityState::BACKGROUNDING;
2590     abilityRecord->SetPendingState(AbilityState::BACKGROUND);
2591     abilityRecord->SetSwitchingPause(false);
2592     abilityRecord->SetStartedByCall(false);
2593     abilityRecord->SetStartToBackground(true);
2594     abilityRecord->isReady_ = true;
2595     abilityRecord2->currentState_ = AbilityState::BACKGROUND;
2596     connectManager->CompleteBackground(abilityRecord);
2597     connectManager.reset();
2598 }
2599 
2600 /*
2601  * Feature: MissionListManager
2602  * Function: CompleteBackground
2603  * SubFunction: NA
2604  * FunctionPoints: MissionListManager CompleteBackground
2605  * EnvConditions: NA
2606  * CaseDescription: Verify CompleteBackground
2607  */
2608 HWTEST_F(AbilityConnectManagerTest, CompleteBackground_005, TestSize.Level1)
2609 {
2610     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2611     ASSERT_NE(connectManager, nullptr);
2612     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2613     std::shared_ptr<AbilityRecord> abilityRecord2 = InitAbilityRecord();
2614     abilityRecord->currentState_ = AbilityState::BACKGROUNDING;
2615     abilityRecord->SetPendingState(AbilityState::BACKGROUND);
2616     abilityRecord->SetSwitchingPause(false);
2617     abilityRecord->SetStartedByCall(true);
2618     abilityRecord->SetStartToBackground(false);
2619     abilityRecord->isReady_ = true;
2620     abilityRecord2->currentState_ = AbilityState::BACKGROUND;
2621     connectManager->CompleteBackground(abilityRecord);
2622     connectManager.reset();
2623 }
2624 
2625 /*
2626  * Feature: MissionListManager
2627  * Function: CompleteBackground
2628  * SubFunction: NA
2629  * FunctionPoints: MissionListManager CompleteBackground
2630  * EnvConditions: NA
2631  * CaseDescription: Verify CompleteBackground
2632  */
2633 HWTEST_F(AbilityConnectManagerTest, CompleteBackground_006, TestSize.Level1)
2634 {
2635     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2636     ASSERT_NE(connectManager, nullptr);
2637     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2638     std::shared_ptr<AbilityRecord> abilityRecord2 = InitAbilityRecord();
2639     abilityRecord->currentState_ = AbilityState::BACKGROUNDING;
2640     abilityRecord->SetPendingState(AbilityState::BACKGROUND);
2641     abilityRecord->SetSwitchingPause(false);
2642     abilityRecord->SetStartedByCall(true);
2643     abilityRecord->SetStartToBackground(true);
2644     abilityRecord->isReady_ = false;
2645     abilityRecord2->currentState_ = AbilityState::FOREGROUND;
2646     connectManager->CompleteBackground(abilityRecord);
2647     connectManager.reset();
2648 }
2649 
2650 /*
2651  * Feature: MissionListManager
2652  * Function: PrintTimeOutLog
2653  * SubFunction: NA
2654  * FunctionPoints: MissionListManager PrintTimeOutLog
2655  * EnvConditions: NA
2656  * CaseDescription: Verify PrintTimeOutLog
2657  */
2658 HWTEST_F(AbilityConnectManagerTest, PrintTimeOutLog_001, TestSize.Level1)
2659 {
2660     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2661     ASSERT_NE(connectManager, nullptr);
2662     uint32_t msgId = 0;
2663     connectManager->PrintTimeOutLog(nullptr, msgId);
2664     connectManager.reset();
2665 }
2666 
2667 /*
2668  * Feature: MissionListManager
2669  * Function: PrintTimeOutLog
2670  * SubFunction: NA
2671  * FunctionPoints: MissionListManager PrintTimeOutLog
2672  * EnvConditions: NA
2673  * CaseDescription: Verify PrintTimeOutLog
2674  */
2675 HWTEST_F(AbilityConnectManagerTest, PrintTimeOutLog_002, TestSize.Level1)
2676 {
2677     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2678     ASSERT_NE(connectManager, nullptr);
2679     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2680     uint32_t msgId = 0;
2681     connectManager->PrintTimeOutLog(abilityRecord, msgId);
2682     connectManager.reset();
2683 }
2684 
2685 /*
2686  * Feature: MissionListManager
2687  * Function: PrintTimeOutLog
2688  * SubFunction: NA
2689  * FunctionPoints: MissionListManager PrintTimeOutLog
2690  * EnvConditions: NA
2691  * CaseDescription: Verify PrintTimeOutLog
2692  */
2693 HWTEST_F(AbilityConnectManagerTest, PrintTimeOutLog_003, TestSize.Level1)
2694 {
2695     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2696     ASSERT_NE(connectManager, nullptr);
2697     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2698     uint32_t msgId = 1;
2699     connectManager->PrintTimeOutLog(abilityRecord, msgId);
2700     connectManager.reset();
2701 }
2702 
2703 /*
2704  * Feature: MissionListManager
2705  * Function: PrintTimeOutLog
2706  * SubFunction: NA
2707  * FunctionPoints: MissionListManager PrintTimeOutLog
2708  * EnvConditions: NA
2709  * CaseDescription: Verify PrintTimeOutLog
2710  */
2711 HWTEST_F(AbilityConnectManagerTest, PrintTimeOutLog_004, TestSize.Level1)
2712 {
2713     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2714     ASSERT_NE(connectManager, nullptr);
2715     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2716     uint32_t msgId = 2;
2717     connectManager->PrintTimeOutLog(abilityRecord, msgId);
2718     connectManager.reset();
2719 }
2720 
2721 /*
2722  * Feature: MissionListManager
2723  * Function: PrintTimeOutLog
2724  * SubFunction: NA
2725  * FunctionPoints: MissionListManager PrintTimeOutLog
2726  * EnvConditions: NA
2727  * CaseDescription: Verify PrintTimeOutLog
2728  */
2729 HWTEST_F(AbilityConnectManagerTest, PrintTimeOutLog_005, TestSize.Level1)
2730 {
2731     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2732     ASSERT_NE(connectManager, nullptr);
2733     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2734     uint32_t msgId = 4;
2735     connectManager->PrintTimeOutLog(abilityRecord, msgId);
2736     connectManager.reset();
2737 }
2738 
2739 /*
2740  * Feature: MissionListManager
2741  * Function: PrintTimeOutLog
2742  * SubFunction: NA
2743  * FunctionPoints: MissionListManager PrintTimeOutLog
2744  * EnvConditions: NA
2745  * CaseDescription: Verify PrintTimeOutLog
2746  */
2747 HWTEST_F(AbilityConnectManagerTest, PrintTimeOutLog_006, TestSize.Level1)
2748 {
2749     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2750     ASSERT_NE(connectManager, nullptr);
2751     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2752     uint32_t msgId = 5;
2753     connectManager->PrintTimeOutLog(abilityRecord, msgId);
2754     connectManager.reset();
2755 }
2756 
2757 /*
2758  * Feature: MissionListManager
2759  * Function: PrintTimeOutLog
2760  * SubFunction: NA
2761  * FunctionPoints: MissionListManager PrintTimeOutLog
2762  * EnvConditions: NA
2763  * CaseDescription: Verify PrintTimeOutLog
2764  */
2765 HWTEST_F(AbilityConnectManagerTest, PrintTimeOutLog_007, TestSize.Level1)
2766 {
2767     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2768     ASSERT_NE(connectManager, nullptr);
2769     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2770     uint32_t msgId = 6;
2771     connectManager->PrintTimeOutLog(abilityRecord, msgId);
2772     connectManager.reset();
2773 }
2774 
2775 /*
2776  * Feature: MissionListManager
2777  * Function: PrintTimeOutLog
2778  * SubFunction: NA
2779  * FunctionPoints: MissionListManager PrintTimeOutLog
2780  * EnvConditions: NA
2781  * CaseDescription: Verify PrintTimeOutLog
2782  */
2783 HWTEST_F(AbilityConnectManagerTest, PrintTimeOutLog_008, TestSize.Level1)
2784 {
2785     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2786     ASSERT_NE(connectManager, nullptr);
2787     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2788     uint32_t msgId = 3;
2789     connectManager->PrintTimeOutLog(abilityRecord, msgId);
2790     connectManager.reset();
2791 }
2792 
2793 /*
2794  * Feature: AbilityConnectManager
2795  * Function: OnAbilityRequestDone
2796  * SubFunction: NA
2797  * FunctionPoints: AbilityConnectManager OnAbilityRequestDone
2798  * EnvConditions: NA
2799  * CaseDescription: Verify OnAbilityRequestDone
2800  * @tc.require: AR000I8B26
2801  */
2802 HWTEST_F(AbilityConnectManagerTest, OnAbilityRequestDone_001, TestSize.Level1)
2803 {
2804     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2805     std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
2806     sptr<IRemoteObject> token = abilityRecord->GetToken();
2807     abilityRecord->abilityInfo_.extensionAbilityType = ExtensionAbilityType::UI;
2808     abilityRecord->SetAbilityState(AbilityState::INACTIVE);
2809     connectManager->serviceMap_.emplace("first", abilityRecord);
2810     connectManager->OnAbilityRequestDone(token, 2);
2811     EXPECT_EQ(abilityRecord->GetAbilityState(), AbilityState::FOREGROUNDING);
2812     connectManager->serviceMap_.erase("first");
2813     abilityRecord->abilityInfo_.extensionAbilityType = ExtensionAbilityType::UNSPECIFIED;
2814     abilityRecord->SetAbilityState(AbilityState::INITIAL);
2815 }
2816 
2817 /*
2818  * Feature: AbilityConnectManager
2819  * Function: ScheduleCommandAbilityWindowDone
2820  * SubFunction: NA
2821  * FunctionPoints: AbilityConnectManager ScheduleCommandAbilityWindowDone
2822  * EnvConditions: NA
2823  * CaseDescription: Verify ScheduleCommandAbilityWindowDone
2824  * @tc.require: AR000I8B26
2825  */
2826 HWTEST_F(AbilityConnectManagerTest, ScheduleCommandAbilityWindowDone_001, TestSize.Level1)
2827 {
2828     ConnectManager()->SetTaskHandler(TaskHandler());
2829     ConnectManager()->SetEventHandler(EventHandler());
2830     auto sessionInfo = MockSessionInfo(0);
2831 
2832     sptr<IRemoteObject> nullToken = nullptr;
2833     auto result = ConnectManager()->ScheduleCommandAbilityWindowDone(
2834         nullToken, sessionInfo, WIN_CMD_FOREGROUND, ABILITY_CMD_FOREGROUND);
2835     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
2836 
2837     std::shared_ptr<AbilityRecord> ability = nullptr;
2838     OHOS::sptr<OHOS::IRemoteObject> token1 = new OHOS::AAFwk::Token(ability);
2839     auto result1 = ConnectManager()->ScheduleCommandAbilityWindowDone(
2840         token1, sessionInfo, WIN_CMD_FOREGROUND, ABILITY_CMD_FOREGROUND);
2841     EXPECT_EQ(result1, OHOS::ERR_INVALID_VALUE);
2842 
2843     sptr<SessionInfo> nullSession = nullptr;
2844     auto result2 = ConnectManager()->ScheduleCommandAbilityWindowDone(
2845         serviceToken_, nullSession, WIN_CMD_FOREGROUND, ABILITY_CMD_FOREGROUND);
2846     EXPECT_EQ(result2, OHOS::ERR_INVALID_VALUE);
2847 
2848     auto result3 = ConnectManager()->ScheduleCommandAbilityWindowDone(
2849         serviceToken_, sessionInfo, WIN_CMD_FOREGROUND, ABILITY_CMD_FOREGROUND);
2850     EXPECT_EQ(result3, OHOS::ERR_OK);
2851 }
2852 
2853 /*
2854  * Feature: AbilityConnectManager
2855  * Function: MoveToForeground
2856  * SubFunction: NA
2857  * FunctionPoints: MissionListManager MoveToForeground
2858  * EnvConditions: NA
2859  * CaseDescription: Verify MoveToForeground
2860  * @tc.require: AR000I8B26
2861  */
2862 HWTEST_F(AbilityConnectManagerTest, MoveToForeground_001, TestSize.Level1)
2863 {
2864     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2865     ASSERT_NE(connectManager, nullptr);
2866     connectManager->MoveToForeground(serviceRecord_);
2867     EXPECT_EQ(serviceRecord_->GetAbilityState(), AbilityState::FOREGROUNDING);
2868     serviceRecord_->SetAbilityState(AbilityState::INITIAL);
2869 }
2870 
2871 /*
2872  * Feature: AbilityConnectManager
2873  * Function: DispatchForeground
2874  * SubFunction:
2875  * FunctionPoints: DispatchForeground
2876  * EnvConditions:NA
2877  * CaseDescription: Verify the DispatchForeground process
2878  * @tc.require: AR000I8B26
2879  */
2880 HWTEST_F(AbilityConnectManagerTest, DispatchForeground_001, TestSize.Level1)
2881 {
2882     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2883     ASSERT_NE(connectManager, nullptr);
2884     std::shared_ptr<AbilityRecord> ability = nullptr;
2885     auto result = connectManager->DispatchForeground(ability);
2886     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
2887 
2888     result = connectManager->DispatchForeground(serviceRecord_);
2889     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
2890 
2891     connectManager->SetTaskHandler(TaskHandler());
2892     connectManager->SetEventHandler(EventHandler());
2893     result = connectManager->DispatchForeground(serviceRecord_);
2894     EXPECT_EQ(result, OHOS::ERR_OK);
2895 }
2896 
2897 /*
2898  * Feature: AbilityConnectManager
2899  * Function: DispatchBackground
2900  * SubFunction:
2901  * FunctionPoints: DispatchBackground
2902  * EnvConditions:NA
2903  * CaseDescription: Verify the DispatchBackground process
2904  * @tc.require: AR000I8B26
2905  */
2906 HWTEST_F(AbilityConnectManagerTest, DispatchBackground_001, TestSize.Level1)
2907 {
2908     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2909     ASSERT_NE(connectManager, nullptr);
2910     std::shared_ptr<AbilityRecord> ability = nullptr;
2911     auto result = connectManager->DispatchBackground(ability);
2912     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
2913 
2914     result = connectManager->DispatchBackground(serviceRecord_);
2915     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
2916 
2917     connectManager->SetTaskHandler(TaskHandler());
2918     connectManager->SetEventHandler(EventHandler());
2919     result = connectManager->DispatchBackground(serviceRecord_);
2920     EXPECT_EQ(result, OHOS::ERR_OK);
2921 }
2922 
2923 /*
2924  * Feature: AbilityConnectManager
2925  * Function: HandleCommandWindowTimeoutTask
2926  * SubFunction: HandleCommandWindowTimeoutTask
2927  * FunctionPoints: NA
2928  * EnvConditions: NA
2929  * CaseDescription: Verify AbilityConnectManager HandleCommandWindowTimeoutTask
2930  * @tc.require: AR000I8B26
2931  */
2932 HWTEST_F(AbilityConnectManagerTest, HandleCommandWindowTimeoutTask_001, TestSize.Level1)
2933 {
2934     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2935     ASSERT_NE(connectManager, nullptr);
2936     connectManager->HandleCommandWindowTimeoutTask(serviceRecord_, MockSessionInfo(0), WIN_CMD_FOREGROUND);
2937 }
2938 
2939 /*
2940  * Feature: AbilityConnectManager
2941  * Function: CommandAbilityWindow
2942  * SubFunction: CommandAbilityWindow
2943  * FunctionPoints: NA
2944  * EnvConditions: NA
2945  * CaseDescription: Verify AbilityConnectManager CommandAbilityWindow
2946  * @tc.require: AR000I8B26
2947  */
2948 HWTEST_F(AbilityConnectManagerTest, CommandAbilityWindow_001, TestSize.Level1)
2949 {
2950     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
2951     ASSERT_NE(connectManager, nullptr);
2952     connectManager->SetTaskHandler(TaskHandler());
2953     connectManager->SetEventHandler(EventHandler());
2954     connectManager->CommandAbilityWindow(serviceRecord_, MockSessionInfo(0), WIN_CMD_FOREGROUND);
2955 }
2956 
2957 /*
2958  * Feature: AbilityConnectManager
2959  * Function: CompleteForeground
2960  * SubFunction: CompleteForeground
2961  * FunctionPoints: NA
2962  * EnvConditions: NA
2963  * CaseDescription: Verify AbilityConnectManager CompleteForeground
2964  * @tc.require: AR000I8B26
2965  */
2966 HWTEST_F(AbilityConnectManagerTest, CompleteForeground_001, TestSize.Level1)
2967 {
2968     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
2969     ASSERT_NE(connectManager, nullptr);
2970     std::shared_ptr<AbilityRecord> abilityRecord = InitAbilityRecord();
2971     abilityRecord->currentState_ = AbilityState::BACKGROUND;
2972     connectManager->CompleteForeground(abilityRecord);
2973     EXPECT_EQ(abilityRecord->GetAbilityState(), AbilityState::BACKGROUND);
2974 
2975     abilityRecord->currentState_ = AbilityState::FOREGROUNDING;
2976     connectManager->CompleteForeground(abilityRecord);
2977     EXPECT_EQ(abilityRecord->GetAbilityState(), AbilityState::FOREGROUND);
2978     connectManager.reset();
2979 }
2980 
2981 /*
2982  * Feature: AbilityConnectManager
2983  * Function: AddUIExtWindowDeathRecipient
2984  * SubFunction: AddUIExtWindowDeathRecipient
2985  * FunctionPoints: NA
2986  * EnvConditions: NA
2987  * CaseDescription: Verify AbilityConnectManager AddUIExtWindowDeathRecipient
2988  * @tc.require: AR000I8B26
2989  */
2990 HWTEST_F(AbilityConnectManagerTest, AddUIExtWindowDeathRecipient_001, TestSize.Level1)
2991 {
2992     ConnectManager()->SetTaskHandler(TaskHandler());
2993     ConnectManager()->SetEventHandler(EventHandler());
2994     ConnectManager()->uiExtRecipientMap_.clear();
2995 
2996     ConnectManager()->AddUIExtWindowDeathRecipient(nullptr);
2997     EXPECT_TRUE(ConnectManager()->uiExtRecipientMap_.empty());
2998 
2999     ConnectManager()->AddUIExtWindowDeathRecipient(callbackA_->AsObject());
3000     EXPECT_EQ(static_cast<int>(ConnectManager()->uiExtRecipientMap_.size()), 1);
3001 
3002     // Add twice, do not add repeatedly
3003     ConnectManager()->AddUIExtWindowDeathRecipient(callbackA_->AsObject());
3004     EXPECT_EQ(static_cast<int>(ConnectManager()->uiExtRecipientMap_.size()), 1);
3005     ConnectManager()->uiExtRecipientMap_.clear();
3006 }
3007 
3008 /*
3009  * Feature: AbilityConnectManager
3010  * Function: RemoveUIExtWindowDeathRecipient
3011  * SubFunction:
3012  * FunctionPoints: RemoveUIExtWindowDeathRecipient
3013  * EnvConditions:NA
3014  * CaseDescription: Verify the RemoveUIExtWindowDeathRecipient process
3015  * @tc.require: AR000I8B26
3016  */
3017 HWTEST_F(AbilityConnectManagerTest, RemoveUIExtWindowDeathRecipient_001, TestSize.Level1)
3018 {
3019     ConnectManager()->SetTaskHandler(TaskHandler());
3020     ConnectManager()->SetEventHandler(EventHandler());
3021     ConnectManager()->uiExtRecipientMap_.clear();
3022 
3023     ConnectManager()->AddUIExtWindowDeathRecipient(callbackA_->AsObject());
3024     EXPECT_EQ(static_cast<int>(ConnectManager()->uiExtRecipientMap_.size()), 1);
3025 
3026     ConnectManager()->RemoveUIExtWindowDeathRecipient(nullptr);
3027     EXPECT_FALSE(ConnectManager()->uiExtRecipientMap_.empty());
3028 
3029     ConnectManager()->RemoveUIExtWindowDeathRecipient(callbackA_->AsObject());
3030     EXPECT_TRUE(ConnectManager()->uiExtRecipientMap_.empty());
3031 }
3032 
3033 /*
3034  * Feature: AbilityConnectManager
3035  * Function: OnUIExtWindowDied
3036  * SubFunction:
3037  * FunctionPoints: OnUIExtWindowDied
3038  * EnvConditions:NA
3039  * CaseDescription: Verify the OnUIExtWindowDied process
3040  * @tc.require: AR000I8B26
3041  */
3042 HWTEST_F(AbilityConnectManagerTest, OnUIExtWindowDied_001, TestSize.Level1)
3043 {
3044     ConnectManager()->SetTaskHandler(TaskHandler());
3045     ConnectManager()->SetEventHandler(EventHandler());
3046     ConnectManager()->uiExtRecipientMap_.clear();
3047     ConnectManager()->uiExtensionMap_.clear();
3048 
3049     ConnectManager()->uiExtensionMap_.emplace(
3050         callbackA_->AsObject(), AbilityConnectManager::UIExtWindowMapValType(serviceRecord_, MockSessionInfo(0)));
3051     ConnectManager()->AddUIExtWindowDeathRecipient(callbackA_->AsObject());
3052     ConnectManager()->OnUIExtWindowDied(nullptr);
3053     WaitUntilTaskDone(TaskHandler());
3054     EXPECT_EQ(static_cast<int>(ConnectManager()->uiExtRecipientMap_.size()), 1);
3055     EXPECT_EQ(static_cast<int>(ConnectManager()->uiExtensionMap_.size()), 1);
3056 
3057     ConnectManager()->OnUIExtWindowDied(callbackA_->AsObject());
3058     WaitUntilTaskDone(TaskHandler());
3059     EXPECT_TRUE(ConnectManager()->uiExtRecipientMap_.empty());
3060     EXPECT_TRUE(ConnectManager()->uiExtensionMap_.empty());
3061 }
3062 
3063 /*
3064  * Feature: AbilityConnectManager
3065  * Function: HandleUIExtWindowDiedTask
3066  * SubFunction: HandleUIExtWindowDiedTask
3067  * FunctionPoints: NA
3068  * EnvConditions: NA
3069  * CaseDescription: Verify AbilityConnectManager HandleUIExtWindowDiedTask
3070  * @tc.require: AR000I8B26
3071  */
3072 HWTEST_F(AbilityConnectManagerTest, HandleUIExtWindowDiedTask_001, TestSize.Level1)
3073 {
3074     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
3075     ASSERT_NE(connectManager, nullptr);
3076     connectManager->uiExtRecipientMap_.clear();
3077     connectManager->uiExtensionMap_.clear();
3078 
3079     connectManager->uiExtensionMap_.emplace(
3080         callbackA_->AsObject(), AbilityConnectManager::UIExtWindowMapValType(serviceRecord_, MockSessionInfo(0)));
3081     connectManager->AddUIExtWindowDeathRecipient(callbackA_->AsObject());
3082     connectManager->HandleUIExtWindowDiedTask(nullptr);
3083     EXPECT_EQ(static_cast<int>(connectManager->uiExtRecipientMap_.size()), 1);
3084     EXPECT_EQ(static_cast<int>(connectManager->uiExtensionMap_.size()), 1);
3085 
3086     connectManager->HandleUIExtWindowDiedTask(callbackA_->AsObject());
3087     EXPECT_TRUE(connectManager->uiExtRecipientMap_.empty());
3088     EXPECT_TRUE(connectManager->uiExtensionMap_.empty());
3089 }
3090 
3091 /*
3092  * Feature: AbilityConnectManager
3093  * Function: IsUIExtensionFocused
3094  * SubFunction: IsUIExtensionFocused
3095  * FunctionPoints: NA
3096  * EnvConditions: NA
3097  * CaseDescription: Verify AbilityConnectManager IsUIExtensionFocused
3098  */
3099 HWTEST_F(AbilityConnectManagerTest, IsUIExtensionFocused_001, TestSize.Level1)
3100 {
3101     std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
3102     ASSERT_NE(connectManager, nullptr);
3103     connectManager->uiExtensionMap_.clear();
3104     bool isFocused = connectManager->IsUIExtensionFocused(
3105         serviceRecord_->GetApplicationInfo().accessTokenId, serviceRecord1_->GetToken());
3106     EXPECT_EQ(isFocused, false);
3107 
3108     sptr<SessionInfo> sessionInfo = new (std::nothrow) SessionInfo();
3109     sessionInfo->callerToken = serviceRecord1_->GetToken();
3110     connectManager->uiExtensionMap_.emplace(
3111         callbackA_->AsObject(), AbilityConnectManager::UIExtWindowMapValType(serviceRecord_, sessionInfo));
3112     isFocused = connectManager->IsUIExtensionFocused(
3113         serviceRecord_->GetApplicationInfo().accessTokenId, serviceRecord1_->GetToken());
3114     EXPECT_EQ(isFocused, true);
3115     connectManager.reset();
3116 }
3117 }  // namespace AAFwk
3118 }  // namespace OHOS
3119