1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #define private public
18 #define protected public
19 #include "ability_manager_service.h"
20 #include "ability_event_handler.h"
21 #include "mission_list_manager.h"
22 #undef private
23 #undef protected
24 
25 #include "ability_config.h"
26 #include "app_process_data.h"
27 #include "system_ability_definition.h"
28 #include "ability_manager_errors.h"
29 #include "want.h"
30 #include "mock_ability_scheduler.h"
31 #include "mock_app_mgr_client.h"
32 #include "mock_bundle_mgr.h"
33 #include "sa_mgr_client.h"
34 #include "mock_ability_connect_callback.h"
35 #include "if_system_ability_manager.h"
36 #include "iservice_registry.h"
37 
38 using namespace testing;
39 using namespace testing::ext;
40 using namespace OHOS::AppExecFwk;
41 
42 namespace OHOS {
43 namespace AAFwk {
44 namespace {
45 const int32_t MOCK_MAIN_USER_ID = 100;
46 const int32_t MOCK_U0_USER_ID = 0;
47 static int MOCK_MISSION_ID = 10000;
48 }
WaitUntilTaskFinished()49 static void WaitUntilTaskFinished()
50 {
51     const uint32_t maxRetryCount = 1000;
52     const uint32_t sleepTime = 1000;
53     uint32_t count = 0;
54     auto handler = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
55     std::atomic<bool> taskCalled(false);
56     auto f = [&taskCalled]() { taskCalled.store(true); };
57     if (handler->SubmitTask(f)) {
58         while (!taskCalled.load()) {
59             ++count;
60             if (count >= maxRetryCount) {
61                 break;
62             }
63             usleep(sleepTime);
64         }
65     }
66 }
67 
WaitUntilTaskFinishedByTimer()68 static void WaitUntilTaskFinishedByTimer()
69 {
70     const uint32_t maxRetryCount = 1000;
71     const uint32_t sleepTime = 1000;
72     uint32_t count = 0;
73     auto handler = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
74     std::atomic<bool> taskCalled(false);
75     auto f = [&taskCalled]() { taskCalled.store(true); };
76     int sleepingTime = 5000;
77     if (handler->SubmitTask(f, "AbilityManagerServiceTest", sleepingTime)) {
78         while (!taskCalled.load()) {
79             ++count;
80             if (count >= maxRetryCount) {
81                 break;
82             }
83             usleep(sleepTime);
84         }
85     }
86 }
87 
88 class AbilityTimeoutModuleTest : public testing::Test {
89 public:
90     static void SetUpTestCase();
91     static void TearDownTestCase();
92     void SetUp();
93     void TearDown();
94     void MockOnStart();
95     static void MockOnStop();
96     static constexpr int TEST_WAIT_TIME = 100000;
97     std::shared_ptr<AbilityRecord> CreateRootLauncher();
98     std::shared_ptr<AbilityRecord> CreateCommonAbility();
99     std::shared_ptr<AbilityRecord> CreateLauncherAbility();
100     std::shared_ptr<AbilityRecord> CreateServiceAbility();
101     std::shared_ptr<AbilityRecord> CreateExtensionAbility();
102 
103 public:
104     std::shared_ptr<AbilityManagerService> abilityMs_ = DelayedSingleton<AbilityManagerService>::GetInstance();
105 };
106 
SetUpTestCase()107 void AbilityTimeoutModuleTest::SetUpTestCase()
108 {
109     GTEST_LOG_(INFO) << "SetUpTestCase.";
110 }
111 
TearDownTestCase()112 void AbilityTimeoutModuleTest::TearDownTestCase()
113 {
114     MockOnStop();
115     GTEST_LOG_(INFO) << "TearDownTestCase.";
116 }
117 
SetUp()118 void AbilityTimeoutModuleTest::SetUp()
119 {
120     MockOnStart();
121 }
122 
TearDown()123 void AbilityTimeoutModuleTest::TearDown()
124 {
125     WaitUntilTaskFinishedByTimer();
126     abilityMs_->subManagersHelper_->currentMissionListManager_.reset();
127 }
128 
MockOnStart()129 void AbilityTimeoutModuleTest::MockOnStart()
130 {
131     if (!abilityMs_) {
132         GTEST_LOG_(ERROR) << "Mock OnStart failed.";
133         return;
134     }
135     if (abilityMs_->state_ == ServiceRunningState::STATE_RUNNING) {
136         return;
137     }
138     abilityMs_->taskHandler_ = TaskHandlerWrap::CreateQueueHandler(AbilityConfig::NAME_ABILITY_MGR_SERVICE);
139     EXPECT_TRUE(abilityMs_->taskHandler_);
140 
141     // init user controller.
142     abilityMs_->userController_ = std::make_shared<UserController>();
143     EXPECT_TRUE(abilityMs_->userController_);
144     abilityMs_->userController_->Init();
145 
146     AmsConfigurationParameter::GetInstance().Parse();
147     abilityMs_->subManagersHelper_ = std::make_shared<SubManagersHelper>(nullptr, nullptr);
148     abilityMs_->subManagersHelper_->InitSubManagers(MOCK_MAIN_USER_ID, true);
149     abilityMs_->SwitchManagers(MOCK_U0_USER_ID, false);
150 
151     abilityMs_->userController_->SetCurrentUserId(MOCK_MAIN_USER_ID);
152 
153     abilityMs_->state_ = ServiceRunningState::STATE_RUNNING;
154     abilityMs_->iBundleManager_ = new BundleMgrService();
155 
156     WaitUntilTaskFinished();
157 }
158 
MockOnStop()159 void AbilityTimeoutModuleTest::MockOnStop()
160 {
161     WaitUntilTaskFinishedByTimer();
162     auto abilityMs_ = DelayedSingleton<AbilityManagerService>::GetInstance();
163     if (!abilityMs_) {
164         GTEST_LOG_(ERROR) << "Mock OnStart failed.";
165         return;
166     }
167 
168     abilityMs_->subManagersHelper_->connectManagers_.clear();
169     abilityMs_->subManagersHelper_->currentConnectManager_.reset();
170     abilityMs_->iBundleManager_.clear();
171     abilityMs_->subManagersHelper_->dataAbilityManagers_.clear();
172     abilityMs_->subManagersHelper_->currentDataAbilityManager_.reset();
173     abilityMs_->subManagersHelper_->pendingWantManagers_.clear();
174     abilityMs_->subManagersHelper_->currentPendingWantManager_.reset();
175     abilityMs_->subManagersHelper_->missionListManagers_.clear();
176     abilityMs_->subManagersHelper_->currentMissionListManager_.reset();
177     abilityMs_->userController_.reset();
178     abilityMs_->abilityController_.clear();
179     abilityMs_->OnStop();
180 }
181 
CreateRootLauncher()182 std::shared_ptr<AbilityRecord> AbilityTimeoutModuleTest::CreateRootLauncher()
183 {
184     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
185         currentMissionListManager_.get());
186     if (!curListManager ||
187         !curListManager->launcherList_) {
188         return nullptr;
189     }
190     auto lauList = curListManager->launcherList_;
191     AbilityRequest abilityRequest;
192     abilityRequest.abilityInfo.type = AbilityType::PAGE;
193     abilityRequest.abilityInfo.name = AbilityConfig::LAUNCHER_ABILITY_NAME;
194     abilityRequest.abilityInfo.bundleName = AbilityConfig::LAUNCHER_BUNDLE_NAME;
195     abilityRequest.appInfo.isLauncherApp = true;
196     abilityRequest.appInfo.name = AbilityConfig::LAUNCHER_BUNDLE_NAME;
197     auto abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
198     auto mission = std::make_shared<Mission>(MOCK_MISSION_ID++, abilityRecord, abilityRequest.abilityInfo.bundleName);
199     abilityRecord->SetMissionId(mission->GetMissionId());
200     abilityRecord->SetLauncherRoot();
201     lauList->AddMissionToTop(mission);
202     EXPECT_TRUE(lauList->GetAbilityRecordByToken(abilityRecord->GetToken()) != nullptr);
203 
204     return abilityRecord;
205 }
206 
CreateLauncherAbility()207 std::shared_ptr<AbilityRecord> AbilityTimeoutModuleTest::CreateLauncherAbility()
208 {
209     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
210         currentMissionListManager_.get());
211     if (!curListManager ||
212         !curListManager->launcherList_) {
213         return nullptr;
214     }
215     auto lauList = curListManager->launcherList_;
216     AbilityRequest abilityRequest;
217     abilityRequest.abilityInfo.type = AbilityType::PAGE;
218     abilityRequest.abilityInfo.name = "com.ix.hiworld.SecAbility";
219     abilityRequest.abilityInfo.bundleName = "com.ix.hiworld";
220     abilityRequest.appInfo.isLauncherApp = true;
221     abilityRequest.appInfo.name = "com.ix.hiworld";
222     auto abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
223     auto mission = std::make_shared<Mission>(MOCK_MISSION_ID++, abilityRecord, abilityRequest.abilityInfo.bundleName);
224     abilityRecord->SetMissionId(mission->GetMissionId());
225     lauList->AddMissionToTop(mission);
226     EXPECT_TRUE(lauList->GetAbilityRecordByToken(abilityRecord->GetToken()) != nullptr);
227 
228     return abilityRecord;
229 }
230 
CreateServiceAbility()231 std::shared_ptr<AbilityRecord> AbilityTimeoutModuleTest::CreateServiceAbility()
232 {
233     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
234         currentMissionListManager_.get());
235     if (!curListManager) {
236         return nullptr;
237     }
238 
239     AbilityRequest abilityRequest;
240     abilityRequest.abilityInfo.type = AbilityType::SERVICE;
241     abilityRequest.abilityInfo.name = "om.ix.Common.ServiceAbility";
242     abilityRequest.abilityInfo.bundleName = "com.ix.Common";
243     abilityRequest.appInfo.isLauncherApp = false;
244     abilityRequest.appInfo.name = "com.ix.Common";
245     auto abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
246     return abilityRecord;
247 }
248 
CreateExtensionAbility()249 std::shared_ptr<AbilityRecord> AbilityTimeoutModuleTest::CreateExtensionAbility()
250 {
251     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
252         currentMissionListManager_.get());
253     if (!curListManager) {
254         return nullptr;
255     }
256 
257     AbilityRequest abilityRequest;
258     abilityRequest.abilityInfo.type = AbilityType::EXTENSION;
259     abilityRequest.abilityInfo.name = "om.ix.Common.ExtensionAbility";
260     abilityRequest.abilityInfo.bundleName = "com.ix.Common";
261     abilityRequest.appInfo.isLauncherApp = false;
262     abilityRequest.appInfo.name = "com.ix.Common";
263     auto abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
264 
265     return abilityRecord;
266 }
267 
CreateCommonAbility()268 std::shared_ptr<AbilityRecord> AbilityTimeoutModuleTest::CreateCommonAbility()
269 {
270     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
271         currentMissionListManager_.get());
272     if (!curListManager) {
273         return nullptr;
274     }
275 
276     AbilityRequest abilityRequest;
277     abilityRequest.abilityInfo.type = AbilityType::PAGE;
278     abilityRequest.abilityInfo.name = "om.ix.Common.MainAbility";
279     abilityRequest.abilityInfo.bundleName = "com.ix.Common";
280     abilityRequest.appInfo.isLauncherApp = false;
281     abilityRequest.appInfo.name = "com.ix.Common";
282     auto abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
283     auto mission = std::make_shared<Mission>(MOCK_MISSION_ID++, abilityRecord, abilityRequest.abilityInfo.bundleName);
284     EXPECT_TRUE(abilityRecord != nullptr);
285     EXPECT_TRUE(mission != nullptr);
286     abilityRecord->SetMissionId(mission->GetMissionId());
287     auto missionList = std::make_shared<MissionList>(MissionListType::CURRENT);
288     missionList->AddMissionToTop(mission);
289     curListManager->MoveMissionListToTop(missionList);
290     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(abilityRecord->GetToken()) != nullptr);
291 
292     return abilityRecord;
293 }
294 
295 
296 /*
297  * Function: OnAbilityDied
298  * SubFunction: NA
299  * FunctionPoints: OnAbilityDied
300  * EnvConditions: NA
301  * CaseDescription: OnAbilityDied
302  */
303 HWTEST_F(AbilityTimeoutModuleTest, OnAbilityDied_001, TestSize.Level1)
304 {
305     // test config is success.
306     EXPECT_TRUE(abilityMs_ != nullptr);
307     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
308         currentMissionListManager_.get());
309     EXPECT_TRUE(curListManager != nullptr);
310     auto lauList = curListManager->launcherList_;
311     EXPECT_TRUE(lauList != nullptr);
312 
313     int maxRestart = -1;
314     maxRestart = AmsConfigurationParameter::GetInstance().GetMaxRestartNum(true);
315     EXPECT_TRUE(maxRestart > -1);
316 
317     // add rootlauncher to abilityMs.
318     auto ability = CreateRootLauncher();
319     auto rootLauncher = lauList->GetTopAbility();
320     EXPECT_EQ(rootLauncher, ability);
321     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
322 
323     GTEST_LOG_(INFO) << "userId:" << abilityMs_->GetUserId();
324     GTEST_LOG_(INFO) << "currentmanager userId" << curListManager->userId_;
325 
326     // died rootlauncher ability
327     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
328     rootLauncher->SetOwnerMissionUserId(MOCK_MAIN_USER_ID);
329     abilityMs_->OnAbilityDied(rootLauncher);
330 
331     EXPECT_TRUE(lauList->GetAbilityRecordByToken(rootLauncher->GetToken()) != nullptr);
332     EXPECT_TRUE(rootLauncher->IsRestarting());
333     EXPECT_TRUE(rootLauncher->restartCount_ < rootLauncher->restartMax_);
334     GTEST_LOG_(INFO) << "restart count:" << rootLauncher->restartCount_;
335 }
336 
337 
338 /*
339  * Function: OnAbilityDied
340  * SubFunction: NA
341  * FunctionPoints: OnAbilityDied
342  * EnvConditions: NA
343  * CaseDescription: OnAbilityDied
344  */
345 HWTEST_F(AbilityTimeoutModuleTest, OnAbilityDied_002, TestSize.Level1)
346 {
347     // test config is success.
348     EXPECT_TRUE(abilityMs_ != nullptr);
349     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
350         currentMissionListManager_.get());
351     EXPECT_TRUE(curListManager != nullptr);
352     auto lauList = curListManager->launcherList_;
353     EXPECT_TRUE(lauList != nullptr);
354 
355     int maxRestart = -1;
356     maxRestart = AmsConfigurationParameter::GetInstance().GetMaxRestartNum(true);
357     EXPECT_TRUE(maxRestart > -1);
358 
359     // add rootlauncher to abilityMs.
360     auto ability = CreateRootLauncher();
361     auto rootLauncher = lauList->GetTopAbility();
362     EXPECT_EQ(rootLauncher, ability);
363     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
364     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
365     rootLauncher->SetOwnerMissionUserId(MOCK_MAIN_USER_ID);
366 
367     // add common ability to abilityMs
368     auto commonAbility = CreateCommonAbility();
369     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
370     EXPECT_EQ(topAbility, commonAbility);
371     topAbility->SetAbilityState(AbilityState::FOREGROUND);
372 
373     // died rootlauncher ability
374     abilityMs_->OnAbilityDied(rootLauncher);
375     WaitUntilTaskFinishedByTimer();
376     topAbility = curListManager->GetCurrentTopAbilityLocked();
377     EXPECT_TRUE(topAbility != nullptr);
378     EXPECT_EQ(topAbility, rootLauncher);
379     EXPECT_TRUE(lauList->GetAbilityRecordByToken(rootLauncher->GetToken()) != nullptr);
380     EXPECT_TRUE(rootLauncher->IsRestarting());
381     EXPECT_TRUE(rootLauncher->restartCount_ < rootLauncher->restartMax_);
382     GTEST_LOG_(INFO) << "restart count:" << rootLauncher->restartCount_;
383 }
384 
385 /*
386  * Function: OnAbilityDied
387  * SubFunction: NA
388  * FunctionPoints: OnAbilityDied
389  * EnvConditions: NA
390  * CaseDescription: OnAbilityDied
391  */
392 HWTEST_F(AbilityTimeoutModuleTest, OnAbilityDied_003, TestSize.Level1)
393 {
394     // test config is success.
395     EXPECT_TRUE(abilityMs_ != nullptr);
396     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
397         currentMissionListManager_.get());
398     EXPECT_TRUE(curListManager != nullptr);
399     auto lauList = curListManager->launcherList_;
400     EXPECT_TRUE(lauList != nullptr);
401 
402     int maxRestart = -1;
403     maxRestart = AmsConfigurationParameter::GetInstance().GetMaxRestartNum(true);
404     EXPECT_TRUE(maxRestart > -1);
405 
406     // add rootlauncher to abilityMs.
407     auto ability = CreateRootLauncher();
408     auto rootLauncher = lauList->GetTopAbility();
409     EXPECT_EQ(rootLauncher, ability);
410     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
411 
412     // died rootlauncher ability
413     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
414     rootLauncher->SetOwnerMissionUserId(MOCK_MAIN_USER_ID);
415     int i = 0;
416     while (i < rootLauncher->restartMax_) {
417         abilityMs_->OnAbilityDied(rootLauncher);
418         usleep(100);
419         i++;
420     }
421 
422     EXPECT_TRUE(lauList->GetAbilityRecordByToken(rootLauncher->GetToken()) != nullptr);
423     EXPECT_TRUE(rootLauncher->IsRestarting());
424     EXPECT_TRUE(rootLauncher->restartCount_ == 0);
425     GTEST_LOG_(INFO) << "restartCount." << rootLauncher->restartCount_;
426 }
427 
428 /*
429  * Function: HandleLoadTimeOut
430  * SubFunction: NA
431  * FunctionPoints: HandleLoadTimeOut
432  * EnvConditions: NA
433  * CaseDescription: HandleLoadTimeOut
434  */
435 HWTEST_F(AbilityTimeoutModuleTest, HandleLoadTimeOut_001, TestSize.Level1)
436 {
437     // test config is success.
438     EXPECT_TRUE(abilityMs_ != nullptr);
439     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
440         currentMissionListManager_.get());
441     EXPECT_TRUE(curListManager != nullptr);
442     auto lauList = curListManager->launcherList_;
443     EXPECT_TRUE(lauList != nullptr);
444 
445     // add rootlauncher to abilityMs.
446     auto ability = CreateRootLauncher();
447     auto rootLauncher = lauList->GetTopAbility();
448     EXPECT_EQ(rootLauncher, ability);
449     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
450 
451     // rootlauncher load timeout
452     abilityMs_->HandleLoadTimeOut(rootLauncher->GetAbilityRecordId());
453     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(rootLauncher->GetToken()) != nullptr);
454     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
455     EXPECT_EQ(rootLauncher, topAbility);
456 }
457 
458 /*
459  * Function: HandleLoadTimeOut
460  * SubFunction: NA
461  * FunctionPoints: HandleLoadTimeOut
462  * EnvConditions: NA
463  * CaseDescription: HandleLoadTimeOut
464  */
465 HWTEST_F(AbilityTimeoutModuleTest, HandleLoadTimeOut_002, TestSize.Level1)
466 {
467     // test config is success.
468     EXPECT_TRUE(abilityMs_ != nullptr);
469     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
470         currentMissionListManager_.get());
471     EXPECT_TRUE(curListManager != nullptr);
472     auto lauList = curListManager->launcherList_;
473     EXPECT_TRUE(lauList != nullptr);
474 
475     // add rootlauncher to abilityMs.
476     auto ability = CreateRootLauncher();
477     auto rootLauncher = lauList->GetTopAbility();
478     EXPECT_EQ(rootLauncher, ability);
479     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
480     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
481 
482     // add common ability to abilityMs
483     auto commonAbility = CreateCommonAbility();
484     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
485     EXPECT_EQ(topAbility, commonAbility);
486 
487     // rootlauncher load timeout
488     abilityMs_->HandleLoadTimeOut(commonAbility->GetAbilityRecordId());
489     WaitUntilTaskFinishedByTimer();
490     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) == nullptr);
491     topAbility = curListManager->GetCurrentTopAbilityLocked();
492     EXPECT_EQ(rootLauncher, topAbility);
493 }
494 
495 /*
496  * Function: HandleLoadTimeOut
497  * SubFunction: NA
498  * FunctionPoints: HandleLoadTimeOut
499  * EnvConditions: NA
500  * CaseDescription: HandleLoadTimeOut
501  */
502 HWTEST_F(AbilityTimeoutModuleTest, HandleLoadTimeOut_003, TestSize.Level1)
503 {
504     // test config is success.
505     EXPECT_TRUE(abilityMs_ != nullptr);
506     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
507         currentMissionListManager_.get());
508     EXPECT_TRUE(curListManager != nullptr);
509     auto lauList = curListManager->launcherList_;
510     EXPECT_TRUE(lauList != nullptr);
511 
512     // add rootlauncher to abilityMs.
513     auto ability = CreateRootLauncher();
514     auto rootLauncher = lauList->GetTopAbility();
515     EXPECT_EQ(rootLauncher, ability);
516     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
517     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
518 
519     // add common ability to abilityMs as caller
520     auto callerAbility = CreateCommonAbility();
521     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
522     EXPECT_EQ(topAbility, callerAbility);
523     callerAbility->SetAbilityState(AbilityState::FOREGROUND);
524 
525     // add common ability to abilityMs
526     auto commonAbility = CreateCommonAbility();
527     Want want;
528     commonAbility->AddCallerRecord(callerAbility->GetToken(), -1, want);
529     topAbility = curListManager->GetCurrentTopAbilityLocked();
530     EXPECT_EQ(topAbility, commonAbility);
531 
532     // rootlauncher load timeout
533     abilityMs_->HandleLoadTimeOut(commonAbility->GetAbilityRecordId());
534     WaitUntilTaskFinishedByTimer();
535     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) == nullptr);
536     topAbility = curListManager->GetCurrentTopAbilityLocked();
537     EXPECT_EQ(callerAbility, topAbility);
538 }
539 
540 /*
541  * Function: HandleLoadTimeOut
542  * SubFunction: NA
543  * FunctionPoints: HandleLoadTimeOut
544  * EnvConditions: NA
545  * CaseDescription: HandleLoadTimeOut
546  */
547 HWTEST_F(AbilityTimeoutModuleTest, HandleLoadTimeOut_004, TestSize.Level1)
548 {
549     // test config is success.
550     EXPECT_TRUE(abilityMs_ != nullptr);
551     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
552         currentMissionListManager_.get());
553     EXPECT_TRUE(curListManager != nullptr);
554     auto lauList = curListManager->launcherList_;
555     EXPECT_TRUE(lauList != nullptr);
556 
557     // add rootlauncher to abilityMs.
558     auto ability = CreateRootLauncher();
559     auto rootLauncher = lauList->GetTopAbility();
560     EXPECT_EQ(rootLauncher, ability);
561     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
562     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
563 
564     // add launcher ability to abilityMs as caller
565     auto callerAbility = CreateLauncherAbility();
566     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
567     EXPECT_EQ(topAbility, callerAbility);
568     callerAbility->SetAbilityState(AbilityState::FOREGROUND);
569 
570     // add common ability to abilityMs
571     auto commonAbility = CreateCommonAbility();
572     Want want;
573     commonAbility->AddCallerRecord(callerAbility->GetToken(), -1, want);
574     topAbility = curListManager->GetCurrentTopAbilityLocked();
575     EXPECT_EQ(topAbility, commonAbility);
576 
577     // rootlauncher load timeout
578     abilityMs_->HandleLoadTimeOut(commonAbility->GetAbilityRecordId());
579     WaitUntilTaskFinishedByTimer();
580     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) == nullptr);
581     topAbility = curListManager->GetCurrentTopAbilityLocked();
582     EXPECT_EQ(rootLauncher, topAbility);
583 }
584 
585 /*
586  * Function: HandleLoadTimeOut
587  * SubFunction: NA
588  * FunctionPoints: HandleLoadTimeOut
589  * EnvConditions: NA
590  * CaseDescription: HandleLoadTimeOut
591  */
592 HWTEST_F(AbilityTimeoutModuleTest, HandleLoadTimeOut_005, TestSize.Level1)
593 {
594     // test config is success.
595     EXPECT_TRUE(abilityMs_ != nullptr);
596     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
597         currentMissionListManager_.get());
598     EXPECT_TRUE(curListManager != nullptr);
599     auto lauList = curListManager->launcherList_;
600     EXPECT_TRUE(lauList != nullptr);
601 
602     // add rootlauncher to abilityMs.
603     auto ability = CreateRootLauncher();
604     auto rootLauncher = lauList->GetTopAbility();
605     EXPECT_EQ(rootLauncher, ability);
606     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
607     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
608 
609     // add service ability to abilityMs as caller
610     auto callerAbility = CreateServiceAbility();
611 
612     // add common ability to abilityMs
613     auto commonAbility = CreateCommonAbility();
614     Want want;
615     commonAbility->AddCallerRecord(callerAbility->GetToken(), -1, want);
616     auto currentList = curListManager->currentMissionLists_;
617     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
618     EXPECT_EQ(topAbility, commonAbility);
619 
620     // rootlauncher load timeout
621     abilityMs_->HandleLoadTimeOut(commonAbility->GetAbilityRecordId());
622     WaitUntilTaskFinishedByTimer();
623     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) == nullptr);
624     topAbility = curListManager->GetCurrentTopAbilityLocked();
625     EXPECT_EQ(rootLauncher, topAbility);
626 }
627 
628 /*
629  * Function: HandleLoadTimeOut
630  * SubFunction: NA
631  * FunctionPoints: HandleLoadTimeOut
632  * EnvConditions: NA
633  * CaseDescription: HandleLoadTimeOut
634  */
635 HWTEST_F(AbilityTimeoutModuleTest, HandleLoadTimeOut_006, TestSize.Level1)
636 {
637     // test config is success.
638     EXPECT_TRUE(abilityMs_ != nullptr);
639     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
640         currentMissionListManager_.get());
641     EXPECT_TRUE(curListManager != nullptr);
642     auto lauList = curListManager->launcherList_;
643     EXPECT_TRUE(lauList != nullptr);
644 
645     // add rootlauncher to abilityMs.
646     auto ability = CreateRootLauncher();
647     auto rootLauncher = lauList->GetTopAbility();
648     EXPECT_EQ(rootLauncher, ability);
649     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
650     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
651 
652     // add extension ability to abilityMs as caller
653     auto callerAbility = CreateExtensionAbility();
654 
655     // add common ability to abilityMs
656     auto commonAbility = CreateCommonAbility();
657     Want want;
658     commonAbility->AddCallerRecord(callerAbility->GetToken(), -1, want);
659     auto currentList = curListManager->currentMissionLists_;
660     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
661     EXPECT_EQ(topAbility, commonAbility);
662 
663     // rootlauncher load timeout
664     abilityMs_->HandleLoadTimeOut(commonAbility->GetAbilityRecordId());
665     WaitUntilTaskFinishedByTimer();
666     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) == nullptr);
667     topAbility = curListManager->GetCurrentTopAbilityLocked();
668     EXPECT_EQ(rootLauncher, topAbility);
669 }
670 
671 /*
672  * Function: HandleLoadTimeOut
673  * SubFunction: NA
674  * FunctionPoints: HandleLoadTimeOut
675  * EnvConditions: NA
676  * CaseDescription: HandleLoadTimeOut
677  */
678 HWTEST_F(AbilityTimeoutModuleTest, HandleLoadTimeOut_007, TestSize.Level1)
679 {
680     // test config is success.
681     EXPECT_TRUE(abilityMs_ != nullptr);
682     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
683         currentMissionListManager_.get());
684     EXPECT_TRUE(curListManager != nullptr);
685     auto lauList = curListManager->launcherList_;
686     EXPECT_TRUE(lauList != nullptr);
687 
688     // add rootlauncher to abilityMs.
689     auto ability = CreateRootLauncher();
690     auto rootLauncher = lauList->GetTopAbility();
691     EXPECT_EQ(rootLauncher, ability);
692     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
693     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
694 
695     // add common laucher ability to abilityMs
696     auto commonLauncherAbility = CreateLauncherAbility();
697     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
698     EXPECT_EQ(topAbility, commonLauncherAbility);
699 
700     // rootlauncher load timeout
701     abilityMs_->HandleLoadTimeOut(commonLauncherAbility->GetAbilityRecordId());
702     WaitUntilTaskFinishedByTimer();
703     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonLauncherAbility->GetToken()) == nullptr);
704     topAbility = curListManager->GetCurrentTopAbilityLocked();
705     EXPECT_EQ(rootLauncher, topAbility);
706 }
707 
708 /*
709  * Function: HandleForegroundTimeOut
710  * SubFunction: NA
711  * FunctionPoints: HandleForegroundTimeOut
712  * EnvConditions: NA
713  * CaseDescription: HandleForegroundTimeOut
714  */
715 HWTEST_F(AbilityTimeoutModuleTest, HandleForegroundTimeOut_001, TestSize.Level1)
716 {
717     // test config is success.
718     EXPECT_TRUE(abilityMs_ != nullptr);
719     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
720         currentMissionListManager_.get());
721     EXPECT_TRUE(curListManager != nullptr);
722     auto lauList = curListManager->launcherList_;
723     EXPECT_TRUE(lauList != nullptr);
724 
725 
726     // add rootlauncher to abilityMs.
727     auto ability = CreateRootLauncher();
728     auto rootLauncher = lauList->GetTopAbility();
729     EXPECT_EQ(rootLauncher, ability);
730     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
731     rootLauncher->SetAbilityState(AbilityState::FOREGROUNDING);
732 
733     // rootlauncher load timeout
734     abilityMs_->HandleForegroundTimeOut(rootLauncher->GetAbilityRecordId());
735     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(rootLauncher->GetToken()) != nullptr);
736     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
737     EXPECT_EQ(rootLauncher, topAbility);
738 }
739 
740 /*
741  * Function: HandleForegroundTimeOut
742  * SubFunction: NA
743  * FunctionPoints: HandleForegroundTimeOut
744  * EnvConditions: NA
745  * CaseDescription: HandleForegroundTimeOut
746  */
747 HWTEST_F(AbilityTimeoutModuleTest, HandleForegroundTimeOut_002, TestSize.Level1)
748 {
749     // test config is success.
750     EXPECT_TRUE(abilityMs_ != nullptr);
751     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
752         currentMissionListManager_.get());
753     EXPECT_TRUE(curListManager != nullptr);
754     auto lauList = curListManager->launcherList_;
755     EXPECT_TRUE(lauList != nullptr);
756 
757     // add rootlauncher to abilityMs.
758     auto ability = CreateRootLauncher();
759     auto rootLauncher = lauList->GetTopAbility();
760     EXPECT_EQ(rootLauncher, ability);
761     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
762     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
763 
764     // add common ability to abilityMs
765     auto commonAbility = CreateCommonAbility();
766     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
767     EXPECT_EQ(topAbility, commonAbility);
768     commonAbility->SetAbilityState(AbilityState::FOREGROUNDING);
769 
770     // rootlauncher load timeout
771     abilityMs_->HandleForegroundTimeOut(commonAbility->GetAbilityRecordId());
772     WaitUntilTaskFinishedByTimer();
773     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) != nullptr);
774     topAbility = curListManager->GetCurrentTopAbilityLocked();
775     EXPECT_EQ(rootLauncher, topAbility);
776 }
777 
778 /*
779  * Function: HandleForegroundTimeOut
780  * SubFunction: NA
781  * FunctionPoints: HandleForegroundTimeOut
782  * EnvConditions: NA
783  * CaseDescription: HandleForegroundTimeOut
784  */
785 HWTEST_F(AbilityTimeoutModuleTest, HandleForegroundTimeOut_003, TestSize.Level1)
786 {
787     // test config is success.
788     EXPECT_TRUE(abilityMs_ != nullptr);
789     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
790         currentMissionListManager_.get());
791     EXPECT_TRUE(curListManager != nullptr);
792     auto lauList = curListManager->launcherList_;
793     EXPECT_TRUE(lauList != nullptr);
794 
795     // add rootlauncher to abilityMs.
796     auto ability = CreateRootLauncher();
797     auto rootLauncher = lauList->GetTopAbility();
798     EXPECT_EQ(rootLauncher, ability);
799     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
800     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
801 
802     // add common ability to abilityMs as caller
803     auto callerAbility = CreateCommonAbility();
804     auto topAbility = curListManager>GetCurrentTopAbilityLocked();
805     EXPECT_EQ(topAbility, callerAbility);
806     callerAbility->SetAbilityState(AbilityState::FOREGROUND);
807 
808     // add common ability to abilityMs
809     auto commonAbility = CreateCommonAbility();
810     Want want;
811     commonAbility->AddCallerRecord(callerAbility->GetToken(), -1, want);
812     topAbility = curListManager->GetCurrentTopAbilityLocked();
813     EXPECT_EQ(topAbility, commonAbility);
814     commonAbility->SetAbilityState(AbilityState::FOREGROUNDING);
815 
816     // rootlauncher load timeout
817     abilityMs_->HandleForegroundTimeOut(commonAbility->GetAbilityRecordId());
818     WaitUntilTaskFinishedByTimer();
819     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) != nullptr);
820     topAbility = curListManager->GetCurrentTopAbilityLocked();
821     EXPECT_EQ(callerAbility, topAbility);
822 }
823 
824 /*
825  * Function: HandleForegroundTimeOut
826  * SubFunction: NA
827  * FunctionPoints: HandleForegroundTimeOut
828  * EnvConditions: NA
829  * CaseDescription: HandleForegroundTimeOut
830  */
831 HWTEST_F(AbilityTimeoutModuleTest, HandleForegroundTimeOut_004, TestSize.Level1)
832 {
833     // test config is success.
834     EXPECT_TRUE(abilityMs_ != nullptr);
835     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
836         currentMissionListManager_.get());
837     EXPECT_TRUE(curListManager != nullptr);
838     auto lauList = curListManager->launcherList_;
839     EXPECT_TRUE(lauList != nullptr);
840 
841     // add rootlauncher to abilityMs.
842     auto ability = CreateRootLauncher();
843     auto rootLauncher = lauList->GetTopAbility();
844     EXPECT_EQ(rootLauncher, ability);
845     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
846     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
847 
848     // add launcher ability to abilityMs as caller
849     auto callerAbility = CreateLauncherAbility();
850     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
851     EXPECT_EQ(topAbility, callerAbility);
852     callerAbility->SetAbilityState(AbilityState::FOREGROUND);
853 
854     // add common ability to abilityMs
855     auto commonAbility = CreateCommonAbility();
856     Want want;
857     commonAbility->AddCallerRecord(callerAbility->GetToken(), -1, want);
858     topAbility = curListManager->GetCurrentTopAbilityLocked();
859     EXPECT_EQ(topAbility, commonAbility);
860     commonAbility->SetAbilityState(AbilityState::FOREGROUNDING);
861 
862     // rootlauncher load timeout
863     abilityMs_->HandleForegroundTimeOut(commonAbility->GetAbilityRecordId());
864     WaitUntilTaskFinishedByTimer();
865     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) != nullptr);
866     topAbility = curListManager->GetCurrentTopAbilityLocked();
867     EXPECT_EQ(rootLauncher, topAbility);
868 }
869 
870 /*
871  * Function: HandleForegroundTimeOut
872  * SubFunction: NA
873  * FunctionPoints: HandleForegroundTimeOut
874  * EnvConditions: NA
875  * CaseDescription: HandleForegroundTimeOut
876  */
877 HWTEST_F(AbilityTimeoutModuleTest, HandleForegroundTimeOut_005, TestSize.Level1)
878 {
879     // test config is success.
880     EXPECT_TRUE(abilityMs_ != nullptr);
881     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
882         currentMissionListManager_.get());
883     EXPECT_TRUE(curListManager != nullptr);
884     auto lauList = curListManager->launcherList_;
885     EXPECT_TRUE(lauList != nullptr);
886 
887     // add rootlauncher to abilityMs.
888     auto ability = CreateRootLauncher();
889     auto rootLauncher = lauList->GetTopAbility();
890     EXPECT_EQ(rootLauncher, ability);
891     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
892     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
893 
894     // add service ability to abilityMs as caller
895     auto callerAbility = CreateServiceAbility();
896 
897     // add common ability to abilityMs
898     auto commonAbility = CreateCommonAbility();
899     Want want;
900     commonAbility->AddCallerRecord(callerAbility->GetToken(), -1, want);
901     auto currentList = curListManager->currentMissionLists_;
902     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
903     EXPECT_EQ(topAbility, commonAbility);
904     commonAbility->SetAbilityState(AbilityState::FOREGROUNDING);
905 
906     // rootlauncher load timeout
907     abilityMs_->HandleForegroundTimeOut(commonAbility->GetAbilityRecordId());
908     WaitUntilTaskFinishedByTimer();
909     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) != nullptr);
910     topAbility = curListManager->GetCurrentTopAbilityLocked();
911     EXPECT_EQ(rootLauncher, topAbility);
912 }
913 
914 /*
915  * Function: HandleForegroundTimeOut
916  * SubFunction: NA
917  * FunctionPoints: HandleForegroundTimeOut
918  * EnvConditions: NA
919  * CaseDescription: HandleForegroundTimeOut
920  */
921 HWTEST_F(AbilityTimeoutModuleTest, HandleForegroundTimeOut_006, TestSize.Level1)
922 {
923     // test config is success.
924     EXPECT_TRUE(abilityMs_ != nullptr);
925     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
926         currentMissionListManager_.get());
927     EXPECT_TRUE(curListManager != nullptr);
928     auto lauList = curListManager->launcherList_;
929     EXPECT_TRUE(lauList != nullptr);
930 
931     // add rootlauncher to abilityMs.
932     auto ability = CreateRootLauncher();
933     auto rootLauncher = lauList->GetTopAbility();
934     EXPECT_EQ(rootLauncher, ability);
935     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
936     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
937 
938     // add extension ability to abilityMs as caller
939     auto callerAbility = CreateExtensionAbility();
940 
941     // add common ability to abilityMs
942     auto commonAbility = CreateCommonAbility();
943     Want want;
944     commonAbility->AddCallerRecord(callerAbility->GetToken(), -1, want);
945     auto currentList = curListManager->currentMissionLists_;
946     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
947     EXPECT_EQ(topAbility, commonAbility);
948     commonAbility->SetAbilityState(AbilityState::FOREGROUNDING);
949 
950     // rootlauncher load timeout
951     abilityMs_->HandleForegroundTimeOut(commonAbility->GetAbilityRecordId());
952     WaitUntilTaskFinishedByTimer();
953     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonAbility->GetToken()) != nullptr);
954     topAbility = curListManager->GetCurrentTopAbilityLocked();
955     EXPECT_EQ(rootLauncher, topAbility);
956 }
957 
958 /*
959  * Function: HandleForegroundTimeOut
960  * SubFunction: NA
961  * FunctionPoints: HandleForegroundTimeOut
962  * EnvConditions: NA
963  * CaseDescription: HandleForegroundTimeOut
964  */
965 HWTEST_F(AbilityTimeoutModuleTest, HandleForegroundTimeOut_007, TestSize.Level1)
966 {
967     // test config is success.
968     EXPECT_TRUE(abilityMs_ != nullptr);
969     auto curListManager = reinterpret_cast<MissionListManager*>(abilityMs_->subManagersHelper_->
970         currentMissionListManager_.get());
971     EXPECT_TRUE(curListManager != nullptr);
972     auto lauList = curListManager->launcherList_;
973     EXPECT_TRUE(lauList != nullptr);
974 
975     // add rootlauncher to abilityMs.
976     auto ability = CreateRootLauncher();
977     auto rootLauncher = lauList->GetTopAbility();
978     EXPECT_EQ(rootLauncher, ability);
979     EXPECT_TRUE(rootLauncher->IsLauncherRoot());
980     rootLauncher->SetAbilityState(AbilityState::FOREGROUND);
981 
982     // add common laucher ability to abilityMs
983     auto commonLauncherAbility = CreateLauncherAbility();
984     auto topAbility = curListManager->GetCurrentTopAbilityLocked();
985     EXPECT_EQ(topAbility, commonLauncherAbility);
986     commonLauncherAbility->SetAbilityState(AbilityState::FOREGROUNDING);
987 
988     // rootlauncher load timeout
989     abilityMs_->HandleForegroundTimeOut(commonLauncherAbility->GetAbilityRecordId());
990     WaitUntilTaskFinishedByTimer();
991     EXPECT_TRUE(curListManager->GetAbilityRecordByToken(commonLauncherAbility->GetToken()) != nullptr);
992     topAbility = curListManager->GetCurrentTopAbilityLocked();
993     EXPECT_EQ(rootLauncher, topAbility);
994 }
995 }  // namespace AAFwk
996 }  // namespace OHOS
997