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