1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci#include <gtest/gtest.h>
16eace7efcSopenharmony_ci#include "gmock/gmock.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include <thread>
19eace7efcSopenharmony_ci#include <chrono>
20eace7efcSopenharmony_ci
21eace7efcSopenharmony_ci#define private public
22eace7efcSopenharmony_ci#define protected public
23eace7efcSopenharmony_ci#include "lifecycle_test_base.h"
24eace7efcSopenharmony_ci#undef private
25eace7efcSopenharmony_ci#undef protected
26eace7efcSopenharmony_ci
27eace7efcSopenharmony_ciusing namespace testing;
28eace7efcSopenharmony_ciusing namespace testing::ext;
29eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk;
30eace7efcSopenharmony_ci
31eace7efcSopenharmony_cinamespace OHOS {
32eace7efcSopenharmony_cinamespace AAFwk {
33eace7efcSopenharmony_ci#ifdef SUPPORT_ASAN
34eace7efcSopenharmony_ciconstexpr uint32_t LOAD_TIMEOUT = 150000;            // ms
35eace7efcSopenharmony_ciconstexpr uint32_t ACTIVE_TIMEOUT = 75000;          // ms
36eace7efcSopenharmony_ciconstexpr uint32_t INACTIVE_TIMEOUT = 7500;         // ms
37eace7efcSopenharmony_ci#else
38eace7efcSopenharmony_ciconstexpr uint32_t LOAD_TIMEOUT = 10000;            // ms
39eace7efcSopenharmony_ciconstexpr uint32_t ACTIVE_TIMEOUT = 5000;          // ms
40eace7efcSopenharmony_ciconstexpr uint32_t INACTIVE_TIMEOUT = 500;         // ms
41eace7efcSopenharmony_ci#endif
42eace7efcSopenharmony_ciclass LifecycleTest : public testing::Test, public LifecycleTestBase {
43eace7efcSopenharmony_cipublic:
44eace7efcSopenharmony_ci    static void SetUpTestCase();
45eace7efcSopenharmony_ci
46eace7efcSopenharmony_ci    static void TearDownTestCase();
47eace7efcSopenharmony_ci
48eace7efcSopenharmony_ci    void SetUp();
49eace7efcSopenharmony_ci
50eace7efcSopenharmony_ci    void TearDown();
51eace7efcSopenharmony_ci
52eace7efcSopenharmony_ci    bool StartNextAbility() override;
53eace7efcSopenharmony_ci
54eace7efcSopenharmony_ci    int AttachAbility(const OHOS::sptr<OHOS::AAFwk::AbilityScheduler>& scheduler,
55eace7efcSopenharmony_ci        const OHOS::sptr<OHOS::IRemoteObject>& token) override;
56eace7efcSopenharmony_ci
57eace7efcSopenharmony_ci    void OnStartabilityAms();
58eace7efcSopenharmony_cipublic:
59eace7efcSopenharmony_ci    int startLancherFlag_ = false;
60eace7efcSopenharmony_ci
61eace7efcSopenharmony_ci    std::shared_ptr<OHOS::AAFwk::AbilityRecord> launcherAbilityRecord_{ nullptr };  // launcher ability
62eace7efcSopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> launcherToken_{ nullptr };                      // token of launcher ability
63eace7efcSopenharmony_ci    std::shared_ptr<OHOS::AAFwk::AbilityRecord> nextAbilityRecord_{ nullptr };      // ability being launched
64eace7efcSopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> nextToken_{ nullptr };                          // token of ability being launched
65eace7efcSopenharmony_ci    OHOS::sptr<OHOS::AAFwk::AbilityScheduler> launcherScheduler_{ nullptr };        // launcher ability thread interface
66eace7efcSopenharmony_ci    OHOS::sptr<OHOS::AAFwk::AbilityScheduler> nextScheduler_{ nullptr };            // next ability thread interface
67eace7efcSopenharmony_ci    std::unique_ptr<LifeTestCommand> command_{ nullptr };                           // test command_ interact with ams_
68eace7efcSopenharmony_ci};
69eace7efcSopenharmony_ci
70eace7efcSopenharmony_civoid LifecycleTest::SetUpTestCase() {}
71eace7efcSopenharmony_ci
72eace7efcSopenharmony_civoid LifecycleTest::TearDownTestCase()
73eace7efcSopenharmony_ci{
74eace7efcSopenharmony_ci    OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
75eace7efcSopenharmony_ci}
76eace7efcSopenharmony_ci
77eace7efcSopenharmony_civoid LifecycleTest::SetUp() {}
78eace7efcSopenharmony_ci
79eace7efcSopenharmony_civoid LifecycleTest::TearDown() {}
80eace7efcSopenharmony_ci
81eace7efcSopenharmony_cibool LifecycleTest::StartNextAbility()
82eace7efcSopenharmony_ci{
83eace7efcSopenharmony_ci    return true;
84eace7efcSopenharmony_ci}
85eace7efcSopenharmony_ci
86eace7efcSopenharmony_ciint LifecycleTest::AttachAbility(
87eace7efcSopenharmony_ci    const OHOS::sptr<OHOS::AAFwk::AbilityScheduler>& scheduler, const OHOS::sptr<OHOS::IRemoteObject>& token)
88eace7efcSopenharmony_ci{
89eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
90eace7efcSopenharmony_ci    return abilityMs_->AttachAbilityThread(scheduler, token);
91eace7efcSopenharmony_ci}
92eace7efcSopenharmony_ci
93eace7efcSopenharmony_ci/*
94eace7efcSopenharmony_ci * Feature: Lifecycle schedule
95eace7efcSopenharmony_ci * Function: Lifecycle schedule
96eace7efcSopenharmony_ci * SubFunction: NA
97eace7efcSopenharmony_ci * FunctionPoints: AttachAbilityThread
98eace7efcSopenharmony_ci * EnvConditions:NA
99eace7efcSopenharmony_ci * CaseDescription: verify AttachAbilityThread parameters.
100eace7efcSopenharmony_ci * AttachAbilityThread fail if IAbilityScheduler or token is nullptr.
101eace7efcSopenharmony_ci */
102eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_001, TestSize.Level1)
103eace7efcSopenharmony_ci{
104eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
105eace7efcSopenharmony_ci    if (startLancherFlag_) {
106eace7efcSopenharmony_ci        EXPECT_TRUE(abilityMs_);
107eace7efcSopenharmony_ci        EXPECT_TRUE(launcherAbilityRecord_);
108eace7efcSopenharmony_ci        EXPECT_NE(abilityMs_->AttachAbilityThread(nullptr, launcherToken_), 0);
109eace7efcSopenharmony_ci        EXPECT_NE(abilityMs_->AttachAbilityThread(launcherScheduler_, nullptr), 0);
110eace7efcSopenharmony_ci        EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVE);
111eace7efcSopenharmony_ci    }
112eace7efcSopenharmony_ci}
113eace7efcSopenharmony_ci
114eace7efcSopenharmony_ci/*
115eace7efcSopenharmony_ci * Feature: Lifecycle schedule
116eace7efcSopenharmony_ci * Function: Lifecycle schedule
117eace7efcSopenharmony_ci * SubFunction: NA
118eace7efcSopenharmony_ci * FunctionPoints: AttachAbilityThread
119eace7efcSopenharmony_ci * EnvConditions:NA
120eace7efcSopenharmony_ci * CaseDescription: verify launcher AbilityRecord state_ when AttachAbilityThread success.
121eace7efcSopenharmony_ci * 1. AbilityState transferred from INITIAL to ACTIVATING.
122eace7efcSopenharmony_ci * 2. AbilityRecord is attached.
123eace7efcSopenharmony_ci */
124eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_002, TestSize.Level1)
125eace7efcSopenharmony_ci{
126eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
127eace7efcSopenharmony_ci    if (startLancherFlag_) {
128eace7efcSopenharmony_ci        EXPECT_TRUE(abilityMs_);
129eace7efcSopenharmony_ci        EXPECT_TRUE(launcherAbilityRecord_);
130eace7efcSopenharmony_ci        EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVE);
131eace7efcSopenharmony_ci        EXPECT_TRUE(launcherScheduler_);
132eace7efcSopenharmony_ci        EXPECT_TRUE(launcherToken_);
133eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
134eace7efcSopenharmony_ci        EXPECT_EQ(launcherAbilityRecord_->IsReady(), true);
135eace7efcSopenharmony_ci    }
136eace7efcSopenharmony_ci}
137eace7efcSopenharmony_ci
138eace7efcSopenharmony_ci/*
139eace7efcSopenharmony_ci * Feature: Lifecycle schedule
140eace7efcSopenharmony_ci * Function: Lifecycle schedule
141eace7efcSopenharmony_ci * SubFunction: NA
142eace7efcSopenharmony_ci * FunctionPoints: AttachAbilityThread
143eace7efcSopenharmony_ci * EnvConditions:NA
144eace7efcSopenharmony_ci * CaseDescription: verify AbilityRecord transition timeout handler.
145eace7efcSopenharmony_ci */
146eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_003, TestSize.Level1)
147eace7efcSopenharmony_ci{
148eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
149eace7efcSopenharmony_ci    if (startLancherFlag_) {
150eace7efcSopenharmony_ci        command_->callback_ = false;
151eace7efcSopenharmony_ci        command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
152eace7efcSopenharmony_ci        command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
153eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
154eace7efcSopenharmony_ci        pthread_t tid = 0;
155eace7efcSopenharmony_ci        pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
156eace7efcSopenharmony_ci        int ret =
157eace7efcSopenharmony_ci            LifecycleTest::SemTimedWaitMillis(LOAD_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
158eace7efcSopenharmony_ci        EXPECT_NE(ret, 0);
159eace7efcSopenharmony_ci        // check timeout handler
160eace7efcSopenharmony_ci        EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVE);
161eace7efcSopenharmony_ci        pthread_join(tid, nullptr);
162eace7efcSopenharmony_ci    }
163eace7efcSopenharmony_ci}
164eace7efcSopenharmony_ci
165eace7efcSopenharmony_ci/*
166eace7efcSopenharmony_ci * Feature: Lifecycle schedule
167eace7efcSopenharmony_ci * Function: Lifecycle schedule
168eace7efcSopenharmony_ci * SubFunction: NA
169eace7efcSopenharmony_ci * FunctionPoints: AttachAbilityThread
170eace7efcSopenharmony_ci * EnvConditions:NA
171eace7efcSopenharmony_ci * CaseDescription: verify AbilityTransitionDone parameters.
172eace7efcSopenharmony_ci * AbilityTransitionDone fail if launcher schedules incorrect Life state_.
173eace7efcSopenharmony_ci */
174eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_004, TestSize.Level1)
175eace7efcSopenharmony_ci{
176eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
177eace7efcSopenharmony_ci    if (startLancherFlag_) {
178eace7efcSopenharmony_ci        // AttachAbilityThread done and success
179eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
180eace7efcSopenharmony_ci
181eace7efcSopenharmony_ci        command_->callback_ = true;
182eace7efcSopenharmony_ci        command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
183eace7efcSopenharmony_ci        command_->abnormalState_ = OHOS::AAFwk::AbilityState::INACTIVE;
184eace7efcSopenharmony_ci        pthread_t tid = 0;
185eace7efcSopenharmony_ci        pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
186eace7efcSopenharmony_ci        int ret =
187eace7efcSopenharmony_ci            LifecycleTest::SemTimedWaitMillis(LOAD_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
188eace7efcSopenharmony_ci        if (ret != 0) {
189eace7efcSopenharmony_ci            // check timeout handler
190eace7efcSopenharmony_ci            GTEST_LOG_(INFO) << "timeout. It shouldn't happen.";
191eace7efcSopenharmony_ci            pthread_join(tid, nullptr);
192eace7efcSopenharmony_ci            return;
193eace7efcSopenharmony_ci        }
194eace7efcSopenharmony_ci        pthread_join(tid, nullptr);
195eace7efcSopenharmony_ci    }
196eace7efcSopenharmony_ci}
197eace7efcSopenharmony_ci
198eace7efcSopenharmony_ci/*
199eace7efcSopenharmony_ci * Feature: Lifecycle schedule
200eace7efcSopenharmony_ci * Function: Lifecycle schedule
201eace7efcSopenharmony_ci * SubFunction: NA
202eace7efcSopenharmony_ci * FunctionPoints: AttachAbilityThread
203eace7efcSopenharmony_ci * EnvConditions:NA
204eace7efcSopenharmony_ci * CaseDescription: AttachAbilityThread done, verify AbilityRecord state_ when AbilityStartThread success.
205eace7efcSopenharmony_ci * 1. Life transition from UNDEFINED to ACTIVATING to ACTIVE.
206eace7efcSopenharmony_ci * 2. AbilityRecord is attached.
207eace7efcSopenharmony_ci */
208eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_005, TestSize.Level1)
209eace7efcSopenharmony_ci{
210eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
211eace7efcSopenharmony_ci    if (startLancherFlag_) {
212eace7efcSopenharmony_ci        // AttachAbilityThread done and success
213eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
214eace7efcSopenharmony_ci        command_->callback_ = true;
215eace7efcSopenharmony_ci        command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
216eace7efcSopenharmony_ci        command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
217eace7efcSopenharmony_ci        pthread_t tid = 0;
218eace7efcSopenharmony_ci
219eace7efcSopenharmony_ci        pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
220eace7efcSopenharmony_ci        int ret =
221eace7efcSopenharmony_ci            LifecycleTest::SemTimedWaitMillis(LOAD_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
222eace7efcSopenharmony_ci        if (ret != 0) {
223eace7efcSopenharmony_ci            // check timeout handler. It won't happen normally.
224eace7efcSopenharmony_ci            GTEST_LOG_(INFO) << "timeout. It shouldn't happen.";
225eace7efcSopenharmony_ci            pthread_join(tid, nullptr);
226eace7efcSopenharmony_ci            return;
227eace7efcSopenharmony_ci        }
228eace7efcSopenharmony_ci        PacMap saveData;
229eace7efcSopenharmony_ci        abilityMs_->AbilityTransitionDone(launcherToken_, command_->state_, saveData);
230eace7efcSopenharmony_ci        if (launcherAbilityRecord_->GetAbilityState() != OHOS::AAFwk::AbilityState::ACTIVE) {
231eace7efcSopenharmony_ci            EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVE);
232eace7efcSopenharmony_ci        }
233eace7efcSopenharmony_ci        EXPECT_EQ(launcherAbilityRecord_->IsReady(), true);
234eace7efcSopenharmony_ci        pthread_join(tid, nullptr);
235eace7efcSopenharmony_ci    }
236eace7efcSopenharmony_ci}
237eace7efcSopenharmony_ci
238eace7efcSopenharmony_ci/*
239eace7efcSopenharmony_ci * Feature: Lifecycle schedule
240eace7efcSopenharmony_ci * Function: Lifecycle schedule
241eace7efcSopenharmony_ci * SubFunction: NA
242eace7efcSopenharmony_ci * FunctionPoints: AttachAbilityThread
243eace7efcSopenharmony_ci * EnvConditions:NA
244eace7efcSopenharmony_ci * CaseDescription:  hnadeler is timeout
245eace7efcSopenharmony_ci */
246eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_StartLauncherAbilityLifeCycle_006, TestSize.Level1)
247eace7efcSopenharmony_ci{
248eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
249eace7efcSopenharmony_ci    if (startLancherFlag_) {
250eace7efcSopenharmony_ci        command_->callback_ = false;
251eace7efcSopenharmony_ci        command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
252eace7efcSopenharmony_ci        command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
253eace7efcSopenharmony_ci        pthread_t tid = 0;
254eace7efcSopenharmony_ci        pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
255eace7efcSopenharmony_ci        int ret = LifecycleTest::SemTimedWaitMillis(ACTIVE_TIMEOUT, command_->sem_);
256eace7efcSopenharmony_ci        EXPECT_NE(ret, 0);
257eace7efcSopenharmony_ci        // check AttachAbilityThread timeout handler
258eace7efcSopenharmony_ci        EXPECT_EQ(launcherAbilityRecord_->IsReady(), false);
259eace7efcSopenharmony_ci        pthread_join(tid, nullptr);
260eace7efcSopenharmony_ci    }
261eace7efcSopenharmony_ci}
262eace7efcSopenharmony_ci
263eace7efcSopenharmony_ci/*
264eace7efcSopenharmony_ci * Feature: Lifecycle schedule
265eace7efcSopenharmony_ci * Function: Lifecycle schedule
266eace7efcSopenharmony_ci * SubFunction: NA
267eace7efcSopenharmony_ci * FunctionPoints: AbilityTransitionDone
268eace7efcSopenharmony_ci * EnvConditions:NA
269eace7efcSopenharmony_ci * CaseDescription: launcher OnInactive timeout, verify launcher AbilityTransitionDone timeout handler.
270eace7efcSopenharmony_ci */
271eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_001, TestSize.Level1)
272eace7efcSopenharmony_ci{
273eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
274eace7efcSopenharmony_ci    if (startLancherFlag_) {
275eace7efcSopenharmony_ci        command_->callback_ = false;
276eace7efcSopenharmony_ci        command_->expectState_ = OHOS::AAFwk::AbilityState::INACTIVE;
277eace7efcSopenharmony_ci        command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
278eace7efcSopenharmony_ci        // launcher is in inactivating process.
279eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
280eace7efcSopenharmony_ci        EXPECT_TRUE(StartNextAbility());
281eace7efcSopenharmony_ci        launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
282eace7efcSopenharmony_ci        pthread_t tid = 0;
283eace7efcSopenharmony_ci        pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
284eace7efcSopenharmony_ci        int ret = LifecycleTest::SemTimedWaitMillis(INACTIVE_TIMEOUT, command_->sem_);
285eace7efcSopenharmony_ci        EXPECT_NE(ret, 0);
286eace7efcSopenharmony_ci        // check AbilityTransitionDone timeout handler
287eace7efcSopenharmony_ci        EXPECT_NE(nextAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVATING);
288eace7efcSopenharmony_ci        pthread_join(tid, nullptr);
289eace7efcSopenharmony_ci    }
290eace7efcSopenharmony_ci}
291eace7efcSopenharmony_ci
292eace7efcSopenharmony_ci/*
293eace7efcSopenharmony_ci * Feature: Lifecycle schedule
294eace7efcSopenharmony_ci * Function: Lifecycle schedule
295eace7efcSopenharmony_ci * SubFunction: NA
296eace7efcSopenharmony_ci * FunctionPoints: AbilityTransitionDone
297eace7efcSopenharmony_ci * EnvConditions:NA
298eace7efcSopenharmony_ci * CaseDescription: verify AbilityTransitionDone parameters.
299eace7efcSopenharmony_ci * AbilityTransitionDone fail if life state_ is incompatible with
300eace7efcSopenharmony_ci * OnInactive process. Or launcher schedules incorrect life state_.
301eace7efcSopenharmony_ci */
302eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_002, TestSize.Level1)
303eace7efcSopenharmony_ci{
304eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
305eace7efcSopenharmony_ci    if (startLancherFlag_) {
306eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
307eace7efcSopenharmony_ci        EXPECT_TRUE(StartNextAbility());
308eace7efcSopenharmony_ci        // launcher is in inactivating process.
309eace7efcSopenharmony_ci        PacMap saveData;
310eace7efcSopenharmony_ci        EXPECT_NE(abilityMs_->AbilityTransitionDone(launcherToken_, OHOS::AAFwk::AbilityState::ACTIVE, saveData), 0);
311eace7efcSopenharmony_ci        EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVATING);
312eace7efcSopenharmony_ci        EXPECT_EQ(nextAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::INITIAL);
313eace7efcSopenharmony_ci    }
314eace7efcSopenharmony_ci}
315eace7efcSopenharmony_ci
316eace7efcSopenharmony_ci/*
317eace7efcSopenharmony_ci * Feature: Lifecycle schedule
318eace7efcSopenharmony_ci * Function: Lifecycle schedule
319eace7efcSopenharmony_ci * SubFunction: NA
320eace7efcSopenharmony_ci * FunctionPoints: AttachAbilityThread
321eace7efcSopenharmony_ci * EnvConditions:NA
322eace7efcSopenharmony_ci * CaseDescription: launcher OnInactive done, verify new ability AttachAbilityThread timeout handler.
323eace7efcSopenharmony_ci */
324eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_003, TestSize.Level1)
325eace7efcSopenharmony_ci{
326eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
327eace7efcSopenharmony_ci    if (startLancherFlag_) {
328eace7efcSopenharmony_ci        command_->callback_ = false;
329eace7efcSopenharmony_ci        command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
330eace7efcSopenharmony_ci        command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
331eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
332eace7efcSopenharmony_ci        EXPECT_TRUE(StartNextAbility());
333eace7efcSopenharmony_ci        launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
334eace7efcSopenharmony_ci        PacMap saveData;
335eace7efcSopenharmony_ci        EXPECT_EQ(abilityMs_->AbilityTransitionDone(launcherToken_, OHOS::AAFwk::AbilityState::INACTIVE, saveData), 0);
336eace7efcSopenharmony_ci        // launcher oninactive done.
337eace7efcSopenharmony_ci        pthread_t tid = 0;
338eace7efcSopenharmony_ci        pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
339eace7efcSopenharmony_ci        int ret = LifecycleTest::SemTimedWaitMillis(
340eace7efcSopenharmony_ci            INACTIVE_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
341eace7efcSopenharmony_ci        EXPECT_NE(ret, 0);
342eace7efcSopenharmony_ci        // check timeout handler
343eace7efcSopenharmony_ci        EXPECT_EQ(nextAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::ACTIVATING);
344eace7efcSopenharmony_ci        pthread_join(tid, nullptr);
345eace7efcSopenharmony_ci    }
346eace7efcSopenharmony_ci}
347eace7efcSopenharmony_ci
348eace7efcSopenharmony_ci/*
349eace7efcSopenharmony_ci * Feature: Lifecycle schedule
350eace7efcSopenharmony_ci * Function: Lifecycle schedule
351eace7efcSopenharmony_ci * SubFunction: NA
352eace7efcSopenharmony_ci * FunctionPoints: AbilityTransitionDone
353eace7efcSopenharmony_ci * EnvConditions:NA
354eace7efcSopenharmony_ci * CaseDescription: launcher OnInactive done, verify AbilityTransitionDone parameter.
355eace7efcSopenharmony_ci * AbilityTransitionDone fail if new ability
356eace7efcSopenharmony_ci * IAbilityScheduler is nullptr.
357eace7efcSopenharmony_ci */
358eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_004, TestSize.Level1)
359eace7efcSopenharmony_ci{
360eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
361eace7efcSopenharmony_ci    if (startLancherFlag_) {
362eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
363eace7efcSopenharmony_ci        EXPECT_TRUE(StartNextAbility());
364eace7efcSopenharmony_ci        launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
365eace7efcSopenharmony_ci        PacMap saveData;
366eace7efcSopenharmony_ci        EXPECT_EQ(abilityMs_->AbilityTransitionDone(launcherToken_, OHOS::AAFwk::AbilityState::INACTIVE, saveData), 0);
367eace7efcSopenharmony_ci        // launcher oninactive done.
368eace7efcSopenharmony_ci        nextAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INITIAL);
369eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(nextScheduler_, nextToken_), 0);
370eace7efcSopenharmony_ci        EXPECT_NE(abilityMs_->AbilityTransitionDone(nullptr, OHOS::AAFwk::AbilityState::ACTIVE, saveData), 0);
371eace7efcSopenharmony_ci    }
372eace7efcSopenharmony_ci}
373eace7efcSopenharmony_ci
374eace7efcSopenharmony_ci/*
375eace7efcSopenharmony_ci * Feature: Lifecycle schedule
376eace7efcSopenharmony_ci * Function: Lifecycle schedule
377eace7efcSopenharmony_ci * SubFunction: NA
378eace7efcSopenharmony_ci * FunctionPoints: AbilityTransitionDone
379eace7efcSopenharmony_ci * EnvConditions:NA
380eace7efcSopenharmony_ci * CaseDescription: launcher OnInactive done. verify AbilityTransitionDone parameter.
381eace7efcSopenharmony_ci * AbilityTransitionDone fail if new ability
382eace7efcSopenharmony_ci * schedules incorrect state_.
383eace7efcSopenharmony_ci */
384eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_005, TestSize.Level1)
385eace7efcSopenharmony_ci{
386eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
387eace7efcSopenharmony_ci    if (startLancherFlag_) {
388eace7efcSopenharmony_ci        command_->callback_ = true;
389eace7efcSopenharmony_ci        command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
390eace7efcSopenharmony_ci        command_->abnormalState_ = OHOS::AAFwk::AbilityState::INACTIVE;
391eace7efcSopenharmony_ci        command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
392eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
393eace7efcSopenharmony_ci        EXPECT_TRUE(StartNextAbility());
394eace7efcSopenharmony_ci        launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
395eace7efcSopenharmony_ci        PacMap saveData;
396eace7efcSopenharmony_ci        EXPECT_EQ(abilityMs_->AbilityTransitionDone(launcherToken_, OHOS::AAFwk::AbilityState::INACTIVE, saveData), 0);
397eace7efcSopenharmony_ci        // launcher oninactive done.
398eace7efcSopenharmony_ci        nextAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INITIAL);
399eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(nextScheduler_, nextToken_), 0);
400eace7efcSopenharmony_ci        pthread_t tid = 0;
401eace7efcSopenharmony_ci        pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
402eace7efcSopenharmony_ci        int ret =
403eace7efcSopenharmony_ci            LifecycleTest::SemTimedWaitMillis(LOAD_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
404eace7efcSopenharmony_ci        if (ret != 0) {
405eace7efcSopenharmony_ci            // check timeout handler
406eace7efcSopenharmony_ci            pthread_join(tid, nullptr);
407eace7efcSopenharmony_ci            return;
408eace7efcSopenharmony_ci        }
409eace7efcSopenharmony_ci        pthread_join(tid, nullptr);
410eace7efcSopenharmony_ci    }
411eace7efcSopenharmony_ci}
412eace7efcSopenharmony_ci
413eace7efcSopenharmony_ci/*
414eace7efcSopenharmony_ci * Feature: Lifecycle schedule
415eace7efcSopenharmony_ci * Function: Lifecycle schedule
416eace7efcSopenharmony_ci * SubFunction: NA
417eace7efcSopenharmony_ci * FunctionPoints: AbilityTransitionDone
418eace7efcSopenharmony_ci * EnvConditions:NA
419eace7efcSopenharmony_ci * CaseDescription: launcher OnInactive done. verify new ability AbilityTransitionDone timeout handler.
420eace7efcSopenharmony_ci */
421eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_006, TestSize.Level1)
422eace7efcSopenharmony_ci{
423eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
424eace7efcSopenharmony_ci    if (startLancherFlag_) {
425eace7efcSopenharmony_ci        command_->callback_ = false;
426eace7efcSopenharmony_ci        command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
427eace7efcSopenharmony_ci        command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
428eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
429eace7efcSopenharmony_ci        EXPECT_TRUE(StartNextAbility());
430eace7efcSopenharmony_ci        launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
431eace7efcSopenharmony_ci        // launcher oninactive done.
432eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(nextScheduler_, nextToken_), 0);
433eace7efcSopenharmony_ci        pthread_t tid = 0;
434eace7efcSopenharmony_ci        pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
435eace7efcSopenharmony_ci        int ret =
436eace7efcSopenharmony_ci            LifecycleTest::SemTimedWaitMillis(ACTIVE_TIMEOUT + DELAY_TEST_TIME, command_->sem_);
437eace7efcSopenharmony_ci        EXPECT_NE(ret, 0);
438eace7efcSopenharmony_ci        pthread_join(tid, nullptr);
439eace7efcSopenharmony_ci        return;
440eace7efcSopenharmony_ci    }
441eace7efcSopenharmony_ci}
442eace7efcSopenharmony_ci
443eace7efcSopenharmony_ci/*
444eace7efcSopenharmony_ci * Feature: Lifecycle schedule
445eace7efcSopenharmony_ci * Function: Lifecycle schedule
446eace7efcSopenharmony_ci * SubFunction: NA
447eace7efcSopenharmony_ci * FunctionPoints: AttachAbilityThread AbilityTransitionDone
448eace7efcSopenharmony_ci * EnvConditions:NA
449eace7efcSopenharmony_ci * CaseDescription: launcher OnInactive done and starts new ability success. verify new AbilityRecord.
450eace7efcSopenharmony_ci * 1. Launcher oninactive done and is INACTIVE.
451eace7efcSopenharmony_ci * 2. new ability is ACTIVE.
452eace7efcSopenharmony_ci */
453eace7efcSopenharmony_ciHWTEST_F(LifecycleTest, AAFWK_AbilityMS_startAbilityLifeCycle_007, TestSize.Level1)
454eace7efcSopenharmony_ci{
455eace7efcSopenharmony_ci    auto abilityMs_ = std::make_shared<AbilityManagerService>();
456eace7efcSopenharmony_ci    if (startLancherFlag_) {
457eace7efcSopenharmony_ci        command_->callback_ = true;
458eace7efcSopenharmony_ci        command_->expectState_ = OHOS::AAFwk::AbilityState::ACTIVE;
459eace7efcSopenharmony_ci        command_->state_ = OHOS::AAFwk::AbilityState::INITIAL;
460eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(launcherScheduler_, launcherToken_), 0);
461eace7efcSopenharmony_ci        EXPECT_TRUE(StartNextAbility());
462eace7efcSopenharmony_ci        launcherAbilityRecord_->SetAbilityState(OHOS::AAFwk::AbilityState::INACTIVATING);
463eace7efcSopenharmony_ci        PacMap saveData;
464eace7efcSopenharmony_ci        EXPECT_EQ(abilityMs_->AbilityTransitionDone(
465eace7efcSopenharmony_ci            launcherToken_, OHOS::AAFwk::AbilityState::INACTIVE, saveData), OHOS::ERR_OK);
466eace7efcSopenharmony_ci        // launcher oninactive done.
467eace7efcSopenharmony_ci        EXPECT_EQ(launcherAbilityRecord_->GetAbilityState(), OHOS::AAFwk::AbilityState::INACTIVE);
468eace7efcSopenharmony_ci        EXPECT_EQ(AttachAbility(nextScheduler_, nextToken_), 0);
469eace7efcSopenharmony_ci        pthread_t tid = 0;
470eace7efcSopenharmony_ci        pthread_create(&tid, nullptr, LifecycleTest::AbilityStartThread, command_.get());
471eace7efcSopenharmony_ci        int ret = LifecycleTest::SemTimedWaitMillis(ACTIVE_TIMEOUT * 2, command_->sem_);
472eace7efcSopenharmony_ci        if (ret != 0) {
473eace7efcSopenharmony_ci            // check timeout handler
474eace7efcSopenharmony_ci            pthread_join(tid, nullptr);
475eace7efcSopenharmony_ci            return;
476eace7efcSopenharmony_ci        }
477eace7efcSopenharmony_ci        pthread_join(tid, nullptr);
478eace7efcSopenharmony_ci    }
479eace7efcSopenharmony_ci}
480eace7efcSopenharmony_ci}  // namespace AAFwk
481eace7efcSopenharmony_ci}  // namespace OHOS
482