• 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 #include <gtest/gtest.h>
16 #include "gmock/gmock.h"
17 
18 #include <thread>
19 #include <chrono>
20 
21 #define private public
22 #define protected public
23 #include "system_ability_definition.h"
24 #include "lifecycle_test_base.h"
25 #include "mock_bundle_manager.h"
26 #include "sa_mgr_client.h"
27 #undef private
28 #undef protected
29 
30 using namespace testing::ext;
31 using namespace OHOS::AppExecFwk;
32 
33 namespace OHOS {
34 namespace AAFwk {
35 namespace {
36 const std::string NAME_BUNDLE_MGR_SERVICE = "BundleMgrService";
37 }
38 
WaitUntilTaskFinished()39 static void WaitUntilTaskFinished()
40 {
41     const uint32_t maxRetryCount = 1000;
42     const uint32_t sleepTime = 1000;
43     uint32_t count = 0;
44     auto handler = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->GetEventHandler();
45     if (handler == nullptr) {
46         GTEST_LOG_(ERROR) << "handler is nullptr";
47         return;
48     }
49     std::atomic<bool> taskCalled(false);
50     auto f = [&taskCalled]() { taskCalled.store(true); };
51     if (handler->PostTask(f)) {
52         while (!taskCalled.load()) {
53             ++count;
54             if (count >= maxRetryCount) {
55                 break;
56             }
57             usleep(sleepTime);
58         }
59     }
60 }
61 
62 class LifecycleTest : public testing::Test, public LifecycleTestBase {
63 public:
64     static void SetUpTestCase(void);
65 
66     static void TearDownTestCase(void);
67 
68     void SetUp() override;
69 
70     void TearDown() override;
71 
72     bool StartLauncherAbility() override;
73 
74     bool StartNextAbility() override;
75 
76     int AttachAbility(const OHOS::sptr<OHOS::AAFwk::AbilityScheduler> &scheduler,
77         const OHOS::sptr<OHOS::IRemoteObject> &token) override;
78 
79     void OnStartabilityAms();
80 public:
81     int startLancherFlag_ = false;
82 
83     std::shared_ptr<AbilityManagerService> abilityMs_ = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance();
84     std::shared_ptr<OHOS::AAFwk::AbilityRecord> launcherAbilityRecord_ {nullptr};  // launcher ability
85     OHOS::sptr<OHOS::IRemoteObject> launcherToken_ {nullptr};                      // token of launcher ability
86     std::shared_ptr<OHOS::AAFwk::AbilityRecord> nextAbilityRecord_ {nullptr};      // ability being launched
87     OHOS::sptr<OHOS::IRemoteObject> nextToken_ {nullptr};                          // token of ability being launched
88     OHOS::sptr<OHOS::AAFwk::AbilityScheduler> launcherScheduler_ {nullptr};        // launcher ability thread interface
89     OHOS::sptr<OHOS::AAFwk::AbilityScheduler> nextScheduler_ {nullptr};            // next ability thread interface
90     std::unique_ptr<LifeTestCommand> command_ {nullptr};                           // test command_ interact with ams_
91 };
92 
OnStartabilityAms()93 void LifecycleTest::OnStartabilityAms()
94 {
95     if (abilityMs_) {
96         if (abilityMs_->state_ == ServiceRunningState::STATE_RUNNING) {
97             return;
98         }
99 
100         abilityMs_->state_ = ServiceRunningState::STATE_RUNNING;
101         abilityMs_->eventLoop_ = AppExecFwk::EventRunner::Create(AbilityConfig::NAME_ABILITY_MGR_SERVICE);
102         EXPECT_TRUE(abilityMs_->eventLoop_);
103         abilityMs_->handler_ = std::make_shared<AbilityEventHandler>(abilityMs_->eventLoop_, abilityMs_);
104         abilityMs_->connectManager_ = std::make_shared<AbilityConnectManager>(0);
105         abilityMs_->connectManagers_.emplace(0, abilityMs_->connectManager_);
106         EXPECT_TRUE(abilityMs_->handler_);
107         EXPECT_TRUE(abilityMs_->connectManager_);
108         abilityMs_->connectManager_->SetEventHandler(abilityMs_->handler_);
109         abilityMs_->dataAbilityManager_ = std::make_shared<DataAbilityManager>();
110         abilityMs_->dataAbilityManagers_.emplace(0, abilityMs_->dataAbilityManager_);
111         EXPECT_TRUE(abilityMs_->dataAbilityManager_);
112         abilityMs_->amsConfigResolver_ = std::make_shared<AmsConfigurationParameter>();
113         EXPECT_TRUE(abilityMs_->amsConfigResolver_);
114         abilityMs_->amsConfigResolver_->Parse();
115         abilityMs_->currentMissionListManager_ = std::make_shared<MissionListManager>(0);
116         abilityMs_->currentMissionListManager_->Init();
117         abilityMs_->pendingWantManager_ = std::make_shared<PendingWantManager>();
118         EXPECT_TRUE(abilityMs_->pendingWantManager_);
119         int userId = abilityMs_->GetUserId();
120         abilityMs_->SetStackManager(userId, true);
121         EXPECT_TRUE(abilityMs_->GetStackManager());
122         abilityMs_->stackManagers_.emplace(0, abilityMs_->GetStackManager());
123         abilityMs_->eventLoop_->Run();
124         return;
125     }
126 
127     GTEST_LOG_(INFO) << "OnStart fail";
128 }
129 
SetUpTestCase(void)130 void LifecycleTest::SetUpTestCase(void)
131 {
132     OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
133         OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
134 }
135 
TearDownTestCase(void)136 void LifecycleTest::TearDownTestCase(void)
137 {
138     OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
139 }
140 
SetUp(void)141 void LifecycleTest::SetUp(void)
142 {
143     OnStartabilityAms();
144     WaitUntilTaskFinished();
145     StartLauncherAbility();
146     command_ = std::make_unique<LifeTestCommand>();
147 }
148 
TearDown(void)149 void LifecycleTest::TearDown(void)
150 {
151     abilityMs_->OnStop();
152     launcherAbilityRecord_.reset();
153     launcherToken_ = nullptr;
154     nextAbilityRecord_.reset();
155     nextToken_ = nullptr;
156     launcherScheduler_ = nullptr;
157     nextScheduler_ = nullptr;
158     command_.reset();
159     startLancherFlag_ = false;
160 }
161 
StartLauncherAbility()162 bool LifecycleTest::StartLauncherAbility()
163 {
164     auto stackManager = abilityMs_->GetStackManager();
165     EXPECT_TRUE(stackManager);
166     auto topAbility = stackManager->GetCurrentTopAbility();
167     if (topAbility) {
168         topAbility->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
169     }
170 
171     ElementName element("device", "com.ix.hiWord", "LauncherAbility");
172     Want want;
173     want.AddEntity(Want::FLAG_HOME_INTENT_FROM_SYSTEM);
174     want.SetElement(element);
175     int ref = abilityMs_->StartAbility(want, -1);
176     WaitUntilTaskFinished();
177     EXPECT_EQ(ref, 0);
178     if (ref != 0) {
179         GTEST_LOG_(ERROR) << "fail to start Launcher ability";
180         return false;
181     }
182 
183     launcherAbilityRecord_ = (stackManager->GetCurrentTopAbility());
184     EXPECT_TRUE(launcherAbilityRecord_);
185     if (launcherAbilityRecord_) {
186         GTEST_LOG_(ERROR) << "launcherAbilityRecord_ is not null";
187         launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
188         launcherToken_ = launcherAbilityRecord_->GetToken();
189         launcherScheduler_ = new AbilityScheduler();
190         startLancherFlag_ = true;
191         return true;
192     }
193     return false;
194 }
195 
StartNextAbility()196 bool LifecycleTest::StartNextAbility()
197 {
198     Want want;
199     ElementName element("device", "com.ix.hiMusic", "MusicAbility");
200     want.SetElement(element);
201 
202     auto stackManager = abilityMs_->GetStackManager();
203     EXPECT_TRUE(stackManager);
204     if (stackManager) {
205         GTEST_LOG_(ERROR) << "top BundleName :"
206                           << stackManager->GetCurrentTopAbility()->GetWant().GetElement().GetBundleName();
207         GTEST_LOG_(ERROR) << "top AbilityName :"
208                           << stackManager->GetCurrentTopAbility()->GetWant().GetElement().GetAbilityName();
209         stackManager->GetCurrentTopAbility()->SetAbilityState(OHOS::AAFwk::AbilityState::ACTIVE);
210     }
211     int ref = abilityMs_->StartAbility(want, -1);
212     WaitUntilTaskFinished();
213     EXPECT_EQ(ref, 0);
214     if (ref != 0) {
215         GTEST_LOG_(ERROR) << "fail to start next ability";
216         return false;
217     }
218 
219     nextAbilityRecord_ = stackManager->GetCurrentTopAbility();
220     if (nextAbilityRecord_ != nullptr) {
221         nextToken_ = nextAbilityRecord_->GetToken();
222         nextScheduler_ = new AbilityScheduler();
223         nextAbilityRecord_->SetScheduler(nextScheduler_);
224         GTEST_LOG_(INFO) << "nextAbilityRecord_ is not null";
225     } else {
226         GTEST_LOG_(ERROR) << "next ability is nullptr";
227         return false;
228     }
229     nextScheduler_ = new AbilityScheduler();
230     return true;
231 }
232 
AttachAbility(const OHOS::sptr<OHOS::AAFwk::AbilityScheduler> & scheduler,const OHOS::sptr<OHOS::IRemoteObject> & token)233 int LifecycleTest::AttachAbility(
234     const OHOS::sptr<OHOS::AAFwk::AbilityScheduler> &scheduler, const OHOS::sptr<OHOS::IRemoteObject> &token)
235 {
236     int ret = abilityMs_->AttachAbilityThread(scheduler, token);
237     static int32_t windowToken = 0;
238     abilityMs_->AddWindowInfo(token, ++windowToken);
239     return ret;
240 }
241 
242 /*
243  * Feature: Lifecycle schedule
244  * Function: Lifecycle schedule
245  * SubFunction: NA
246  * FunctionPoints: AttachAbilityThread
247  * EnvConditions:NA
248  * CaseDescription: verify AttachAbilityThread parameters.
249  * AttachAbilityThread fail if IAbilityScheduler or token is nullptr.
250  */
251 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_001, TestSize.Level1)
252 {
253     if (startLancherFlag_) {
254         EXPECT_TRUE(abilityMs_);
255         EXPECT_TRUE(launcherAbilityRecord_);
256         EXPECT_NE(abilityMs_->AttachAbilityThread(nullptr, launcherToken_), 0);
257         EXPECT_NE(abilityMs_->AttachAbilityThread(launcherScheduler_, nullptr), 0);
258         EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVE);
259     }
260 }
261 
262 /*
263  * Feature: Lifecycle schedule
264  * Function: Lifecycle schedule
265  * SubFunction: NA
266  * FunctionPoints: AttachAbilityThread
267  * EnvConditions:NA
268  * CaseDescription: verify launcher AbilityRecord state_ when AttachAbilityThread success.
269  * 1. AbilityState transferred from INITIAL to ACTIVATING.
270  * 2. AbilityRecord is attached.
271  */
272 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_002, TestSize.Level1)
273 {
274     if (startLancherFlag_) {
275         EXPECT_TRUE(abilityMs_);
276         EXPECT_TRUE(launcherAbilityRecord_);
277         EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVE);
278         EXPECT_TRUE(launcherScheduler_);
279         EXPECT_TRUE(launcherToken_);
280         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
281         EXPECT_EQ(launcherAbilityRecord_->IsReady(), true);
282     }
283 }
284 
285 /*
286  * Feature: Lifecycle schedule
287  * Function: Lifecycle schedule
288  * SubFunction: NA
289  * FunctionPoints: AttachAbilityThread
290  * EnvConditions:NA
291  * CaseDescription: verify AbilityRecord transition timeout handler.
292  */
293 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_003, TestSize.Level1)
294 {
295     if (startLancherFlag_) {
296         command_->callback_ = false;
297         command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
298         command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
299         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
300         pthread_t tid = 0;
301         pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
302         int ret =
303             LifecycleTest::SemTimedWaitMillis(AbilityManagerService::LOAD_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
304         EXPECT_NE(ret, 0);
305         // check timeout handler
306         EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVE);
307         pthread_join(tid, nullptr);
308     }
309 }
310 
311 /*
312  * Feature: Lifecycle schedule
313  * Function: Lifecycle schedule
314  * SubFunction: NA
315  * FunctionPoints: AttachAbilityThread
316  * EnvConditions:NA
317  * CaseDescription: verify AbilityTransitionDone parameters.
318  * AbilityTransitionDone fail if launcher schedules incorrect Life state_.
319  */
320 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_004, TestSize.Level1)
321 {
322     if (startLancherFlag_) {
323         // AttachAbilityThread done and success
324         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
325 
326         command_->callback_ = true;
327         command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
328         command_->abnormalState_ = OHOS::AAFwk::AbilityState::INACTIVE;
329         pthread_t tid = 0;
330         pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
331         int ret =
332             LifecycleTest::SemTimedWaitMillis(AbilityManagerService::LOAD_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
333         if (ret != 0) {
334             // check timeout handler
335             GTEST_LOG_(INFO) << "timeout. It shouldn't happen.";
336             pthread_join(tid, nullptr);
337             return;
338         }
339         pthread_join(tid, nullptr);
340     }
341 }
342 
343 /*
344  * Feature: Lifecycle schedule
345  * Function: Lifecycle schedule
346  * SubFunction: NA
347  * FunctionPoints: AttachAbilityThread
348  * EnvConditions:NA
349  * CaseDescription: AttachAbilityThread done, verify AbilityRecord state_ when AbilityStartThread success.
350  * 1. Life transition from UNDEFINED to ACTIVATING to ACTIVE.
351  * 2. AbilityRecord is attached.
352  */
353 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_005, TestSize.Level1)
354 {
355     if (startLancherFlag_) {
356         // AttachAbilityThread done and success
357         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
358         command_->callback_ = true;
359         command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
360         command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
361         pthread_t tid = 0;
362 
363         pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
364         int ret =
365             LifecycleTest::SemTimedWaitMillis(AbilityManagerService::LOAD_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
366         if (ret != 0) {
367             // check timeout handler. It won't happen normally.
368             GTEST_LOG_(INFO) << "timeout. It shouldn't happen.";
369             pthread_join(tid, nullptr);
370             return;
371         }
372         PacMap saveData;
373         abilityMs_->AbilityTransitionDone(launcherToken_, command_->state_, saveData);
374         if (launcherAbilityRecord_->GetAbilityState() != OHOS::AAFwk::AbilityState::ACTIVE) {
375             WaitUntilTaskFinished();
376             EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVE);
377         }
378         EXPECT_EQ(launcherAbilityRecord_->IsReady(), true);
379         pthread_join(tid, nullptr);
380     }
381 }
382 
383 /*
384  * Feature: Lifecycle schedule
385  * Function: Lifecycle schedule
386  * SubFunction: NA
387  * FunctionPoints: AttachAbilityThread
388  * EnvConditions:NA
389  * CaseDescription:  hnadeler is timeout
390  */
391 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_006, TestSize.Level1)
392 {
393     if (startLancherFlag_) {
394         command_->callback_ = false;
395         command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
396         command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
397         pthread_t tid = 0;
398         pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
399         int ret = LifecycleTest::SemTimedWaitMillis(AbilityManagerService::ACTIVE_TIMEOUT, command_->sem_);
400         EXPECT_NE(ret, 0);
401         // check AttachAbilityThread timeout handler
402         EXPECT_EQ(launcherAbilityRecord_->IsReady(), false);
403         pthread_join(tid, nullptr);
404     }
405 }
406 
407 /*
408  * Feature: Lifecycle schedule
409  * Function: Lifecycle schedule
410  * SubFunction: NA
411  * FunctionPoints: AbilityTransitionDone
412  * EnvConditions:NA
413  * CaseDescription: launcher OnInactive timeout, verify launcher AbilityTransitionDone timeout handler.
414  */
415 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_001, TestSize.Level1)
416 {
417     if (startLancherFlag_) {
418         command_->callback_ = false;
419         command_->expectState_ = OHOS::AAFwk::AbilityState::INACTIVE;
420         command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
421         // launcher is in inactivating process.
422         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
423         EXPECT_TRUE(StartNextAbility());
424         launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
425         pthread_t tid = 0;
426         pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
427         int ret = LifecycleTest::SemTimedWaitMillis(AbilityManagerService::INACTIVE_TIMEOUT, command_->sem_);
428         EXPECT_NE(ret, 0);
429         // check AbilityTransitionDone timeout handler
430         EXPECT_NE(nextAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVATING);
431         pthread_join(tid, nullptr);
432     }
433 }
434 
435 /*
436  * Feature: Lifecycle schedule
437  * Function: Lifecycle schedule
438  * SubFunction: NA
439  * FunctionPoints: AbilityTransitionDone
440  * EnvConditions:NA
441  * CaseDescription: verify AbilityTransitionDone parameters.
442  * AbilityTransitionDone fail if life state_ is incompatible with
443  * OnInactive process. Or launcher schedules incorrect life state_.
444  */
445 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_002, TestSize.Level1)
446 {
447     if (startLancherFlag_) {
448         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
449         EXPECT_TRUE(StartNextAbility());
450         // launcher is in inactivating process.
451         PacMap saveData;
452         EXPECT_NE(abilityMs_->AbilityTransitionDone(launcherToken_, OHOS::AAFwk::AbilityState::ACTIVE, saveData), 0);
453         WaitUntilTaskFinished();
454         EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVATING);
455         EXPECT_EQ(nextAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::INITIAL);
456     }
457 }
458 
459 /*
460  * Feature: Lifecycle schedule
461  * Function: Lifecycle schedule
462  * SubFunction: NA
463  * FunctionPoints: AttachAbilityThread
464  * EnvConditions:NA
465  * CaseDescription: launcher OnInactive done, verify new ability AttachAbilityThread timeout handler.
466  */
467 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_003, TestSize.Level1)
468 {
469     if (startLancherFlag_) {
470         command_->callback_ = false;
471         command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
472         command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
473         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
474         WaitUntilTaskFinished();
475         EXPECT_TRUE(StartNextAbility());
476         launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
477         PacMap saveData;
478         EXPECT_EQ(abilityMs_->AbilityTransitionDone(launcherToken_, OHOS::AAFwk::AbilityState::INACTIVE, saveData), 0);
479         // launcher oninactive done.
480         pthread_t tid = 0;
481         pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
482         int ret = LifecycleTest::SemTimedWaitMillis(
483             AbilityManagerService::INACTIVE_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
484         EXPECT_NE(ret, 0);
485         // check timeout handler
486         EXPECT_EQ(nextAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVATING);
487         pthread_join(tid, nullptr);
488 
489         WaitUntilTaskFinished();
490     }
491 }
492 
493 /*
494  * Feature: Lifecycle schedule
495  * Function: Lifecycle schedule
496  * SubFunction: NA
497  * FunctionPoints: AbilityTransitionDone
498  * EnvConditions:NA
499  * CaseDescription: launcher OnInactive done, verify AbilityTransitionDone parameter.
500  * AbilityTransitionDone fail if new ability
501  * IAbilityScheduler is nullptr.
502  */
503 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_004, TestSize.Level1)
504 {
505     if (startLancherFlag_) {
506         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
507         EXPECT_TRUE(StartNextAbility());
508         launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
509         PacMap saveData;
510         EXPECT_EQ(abilityMs_->AbilityTransitionDone(launcherToken_, OHOS::AAFwk::AbilityState::INACTIVE, saveData), 0);
511         // launcher oninactive done.
512         nextAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INITIAL);
513         EXPECT_EQ(AttachAbility(nextScheduler_, nextToken_), 0);
514         EXPECT_NE(abilityMs_->AbilityTransitionDone(nullptr, OHOS::AAFwk::AbilityState::ACTIVE, saveData), 0);
515     }
516 }
517 
518 /*
519  * Feature: Lifecycle schedule
520  * Function: Lifecycle schedule
521  * SubFunction: NA
522  * FunctionPoints: AbilityTransitionDone
523  * EnvConditions:NA
524  * CaseDescription: launcher OnInactive done. verify AbilityTransitionDone parameter.
525  * AbilityTransitionDone fail if new ability
526  * schedules incorrect state_.
527  */
528 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_005, TestSize.Level1)
529 {
530     if (startLancherFlag_) {
531         command_->callback_ = true;
532         command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
533         command_->abnormalState_ = OHOS::AAFwk::AbilityState::INACTIVE;
534         command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
535         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
536         EXPECT_TRUE(StartNextAbility());
537         launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
538         PacMap saveData;
539         EXPECT_EQ(abilityMs_->AbilityTransitionDone(launcherToken_, OHOS::AAFwk::AbilityState::INACTIVE, saveData), 0);
540         WaitUntilTaskFinished();
541         // launcher oninactive done.
542         nextAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INITIAL);
543         EXPECT_EQ(AttachAbility(nextScheduler_, nextToken_), 0);
544         pthread_t tid = 0;
545         pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
546         int ret =
547             LifecycleTest::SemTimedWaitMillis(AbilityManagerService::LOAD_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
548         if (ret != 0) {
549             // check timeout handler
550             pthread_join(tid, nullptr);
551             return;
552         }
553         pthread_join(tid, nullptr);
554     }
555 }
556 
557 /*
558  * Feature: Lifecycle schedule
559  * Function: Lifecycle schedule
560  * SubFunction: NA
561  * FunctionPoints: AbilityTransitionDone
562  * EnvConditions:NA
563  * CaseDescription: launcher OnInactive done. verify new ability AbilityTransitionDone timeout handler.
564  */
565 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_006, TestSize.Level1)
566 {
567 
568     if (startLancherFlag_) {
569         command_->callback_ = false;
570         command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
571         command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
572         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
573         EXPECT_TRUE(StartNextAbility());
574         launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
575         // launcher oninactive done.
576         EXPECT_EQ(AttachAbility(nextScheduler_, nextToken_), 0);
577         pthread_t tid = 0;
578         pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
579         int ret =
580             LifecycleTest::SemTimedWaitMillis(AbilityManagerService::ACTIVE_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
581         EXPECT_NE(ret, 0);
582         pthread_join(tid, nullptr);
583         return;
584     }
585 }
586 
587 /*
588  * Feature: Lifecycle schedule
589  * Function: Lifecycle schedule
590  * SubFunction: NA
591  * FunctionPoints: AttachAbilityThread AbilityTransitionDone
592  * EnvConditions:NA
593  * CaseDescription: launcher OnInactive done and starts new ability success. verify new AbilityRecord.
594  * 1. Launcher oninactive done and is INACTIVE.
595  * 2. new ability is ACTIVE.
596  * 3. Launcher is transferred from INACTIVE to MOVING_BACKGROUND when new ability started.
597  */
598 HWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_007, TestSize.Level1)
599 {
600     if (startLancherFlag_) {
601         command_->callback_ = true;
602         command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
603         command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
604         EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
605         EXPECT_TRUE(StartNextAbility());
606         launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
607         PacMap saveData;
608         EXPECT_EQ(abilityMs_->AbilityTransitionDone(
609             launcherToken_, OHOS::AAFwk::AbilityState::INACTIVE, saveData), OHOS::ERR_OK);
610         // launcher oninactive done.
611         WaitUntilTaskFinished();
612         EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVE);
613         EXPECT_EQ(AttachAbility(nextScheduler_, nextToken_), 0);
614         pthread_t tid = 0;
615         pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
616         int ret = LifecycleTest::SemTimedWaitMillis(AbilityManagerService::ACTIVE_TIMEOUT * 2, command_->sem_);
617         if (ret != 0) {
618             // check timeout handler
619             pthread_join(tid, nullptr);
620             return;
621         }
622         pthread_join(tid, nullptr);
623     }
624 }
625 }  // namespace AAFwk
626 }  // namespace OHOS