1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <climits>
17 #include <gtest/gtest.h>
18 #include "ability_thread.h"
19 #include "ability_local_record.h"
20 #include "ability_loader.h"
21 #include "ability_impl_factory.h"
22 #include "data_ability_helper.h"
23 #include "context_deal.h"
24 #include "ohos_application.h"
25 #include "sys_mgr_client.h"
26 #include "ability_manager_interface.h"
27 #include "ability_manager_client.h"
28 #include "system_ability_definition.h"
29 #include "demo_ability_test.h"
30 #include "mock_bundle_manager.h"
31 #include "mock_ability_manager_service.h"
32
33 namespace OHOS {
34 namespace AppExecFwk {
35 using namespace testing::ext;
36 using namespace OHOS;
37 using namespace AAFwk;
38 using OHOS::AppExecFwk::ElementName;
39 using namespace OHOS::AppExecFwk;
40 /*
41 * Parameters:
42 * Action
43 * Entity
44 * Flag
45 * ElementName
46 */
47 const std::string ABILITY_NAME("DemoAbility");
48 class AbilityBaseTest : public testing::Test {
49 public:
50 static void SetUpTestCase(void);
51 static void TearDownTestCase(void);
52 void SetUp();
53 void TearDown();
54 OHOS::sptr<OHOS::IRemoteObject> abilityObject_;
55 static constexpr int TEST_WAIT_TIME = 500 * 1000; // 500 ms
56 static const int RESULT_CODE = 1992;
57 };
58
SetUpTestCase(void)59 void AbilityBaseTest::SetUpTestCase(void)
60 {}
61
TearDownTestCase(void)62 void AbilityBaseTest::TearDownTestCase(void)
63 {}
64
SetUp(void)65 void AbilityBaseTest::SetUp(void)
66 {
67 abilityObject_ = new MockAbilityManagerService();
68 auto sysMgr = OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance();
69 EXPECT_TRUE(sysMgr != nullptr);
70 sysMgr->RegisterSystemAbility(OHOS::ABILITY_MGR_SERVICE_ID, abilityObject_);
71 sysMgr->RegisterSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
72 }
73
TearDown(void)74 void AbilityBaseTest::TearDown(void)
75 {
76 abilityObject_ = nullptr;
77 }
78
79 /**
80 * @tc.number: AaFwk_Ability_AbilityFwk_Start_Test_0100
81 * @tc.name: AbilityFwk Start
82 * @tc.desc: The first step of startability is the attach AMS.
83 */
84 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Start_Test_0100, Function | MediumTest | Level1)
85 {
86 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
87
88 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
89 EXPECT_NE(abilityToken, nullptr);
90 if (abilityToken != nullptr) {
91 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
92 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
93 abilityInfo->name = ABILITY_NAME;
94 abilityInfo->isNativeAbility = true;
95 std::shared_ptr<AbilityLocalRecord> abilityRecord =
96 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
97
98 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
99 usleep(AbilityBaseTest::TEST_WAIT_TIME);
100 }
101 }
102
103 /**
104 * @tc.number: AaFwk_Ability_AbilityFwk_Start_Test_0200
105 * @tc.name: AbilityFwk Start
106 * @tc.desc: When connecting AMS,the instance of application is empty.
107 */
108 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Start_Test_0200, Function | MediumTest | Level1)
109 {
110 std::shared_ptr<OHOSApplication> application = nullptr;
111
112 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
113 EXPECT_NE(abilityToken, nullptr);
114 if (abilityToken != nullptr) {
115 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
116 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
117 abilityInfo->name = ABILITY_NAME;
118 abilityInfo->isNativeAbility = true;
119 std::shared_ptr<AbilityLocalRecord> abilityRecord =
120 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
121
122 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
123 usleep(AbilityBaseTest::TEST_WAIT_TIME);
124 }
125 }
126
127 /**
128 * @tc.number: AaFwk_Ability_AbilityFwk_Start_Test_0300
129 * @tc.name: AbilityFwk Start
130 * @tc.desc: When connecting AMS,the instance of abilityRecord is empty
131 */
132 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Start_Test_0300, Function | MediumTest | Level1)
133 {
134 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
135
136 std::shared_ptr<AbilityLocalRecord> abilityRecord = nullptr;
137
138 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
139 usleep(AbilityBaseTest::TEST_WAIT_TIME);
140 }
141
142 /**
143 * @tc.number: AaFwk_Ability_AbilityFwk_Start_Test_0400
144 * @tc.name: AbilityFwk Start
145 * @tc.desc: The ability name is empty, so the ability instance cannot be created.
146 */
147 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Start_Test_0400, Function | MediumTest | Level1)
148 {
149 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
150
151 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
152 EXPECT_NE(abilityToken, nullptr);
153 if (abilityToken != nullptr) {
154 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
155 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
156 std::shared_ptr<AbilityLocalRecord> abilityRecord =
157 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
158
159 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
160 usleep(AbilityBaseTest::TEST_WAIT_TIME);
161 }
162 }
163
164 /**
165 * @tc.number: AaFwk_Ability_AbilityFwk_Start_Test_0500
166 * @tc.name: AbilityFwk Start
167 * @tc.desc: The ability type is unknown, so the AbilityImpl instance cannot be created.
168 */
169 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Start_Test_0500, Function | MediumTest | Level1)
170 {
171 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
172
173 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
174 EXPECT_NE(abilityToken, nullptr);
175 if (abilityToken != nullptr) {
176 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
177 abilityInfo->name = ABILITY_NAME;
178 abilityInfo->isNativeAbility = true;
179 std::shared_ptr<AbilityLocalRecord> abilityRecord =
180 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
181
182 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
183 usleep(AbilityBaseTest::TEST_WAIT_TIME);
184 }
185 }
186
187 /**
188 * @tc.number: AaFwk_Ability_AbilityFwk_Start_Test_0600
189 * @tc.name: AbilityFwk Start
190 * @tc.desc: The interface OnSaveAbilityState()/OnRestoreAbilityState() of demoability was called.
191 */
192 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Start_Test_0600, Function | MediumTest | Level1)
193 {
194 std::shared_ptr<ContextDeal> contextDeal = std::make_shared<ContextDeal>();
195 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
196 std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
197 std::shared_ptr<ProcessInfo> processInfo = std::make_shared<ProcessInfo>();
198
199 appInfo->codePath = "codePath";
200 appInfo->dataBaseDir = "dataBaseDir";
201 appInfo->dataDir = "dataDir";
202 appInfo->cacheDir = "cacheDir";
203 appInfo->bundleName = "bundleName";
204
205 contextDeal->SetProcessInfo(processInfo);
206 contextDeal->SetApplicationInfo(appInfo);
207 contextDeal->SetApplicationContext(application);
208 application->AttachBaseContext(contextDeal);
209
210 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
211 EXPECT_NE(abilityToken, nullptr);
212 if (abilityToken != nullptr) {
213 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
214 abilityInfo->codePath = "codePath";
215 abilityInfo->resourcePath = "resourcePath";
216
217 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
218 abilityInfo->name = ABILITY_NAME;
219 abilityInfo->isNativeAbility = true;
220 std::shared_ptr<AbilityLocalRecord> abilityRecord =
221 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
222
223 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
224
225 sptr<IRemoteObject> remoteObject_ =
226 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
227 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
228 Want want;
229 sptr<IAbilityConnection> connect = nullptr;
230
231 // Just to test two interfaces OnRestoreAbilityState/OnSaveAbilityState
232 abms->ConnectAbility(want, connect, abilityToken);
233 usleep(AbilityBaseTest::TEST_WAIT_TIME);
234 }
235 }
236
237 /**
238 * @tc.number: AaFwk_Ability_Lifecycle_Test_0100
239 * @tc.name: Ability Lifecycle
240 * @tc.desc: The ability life cycle will change from initial state initial to inactive by calling the interface
241 * OnnStart(), and then to active by calling OnActive().
242 */
243 HWTEST_F(AbilityBaseTest, AaFwk_Ability_Lifecycle_Test_0100, Function | MediumTest | Level1)
244 {
245 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
246
247 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
248 EXPECT_NE(abilityToken, nullptr);
249 if (abilityToken != nullptr) {
250 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
251 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
252 abilityInfo->name = ABILITY_NAME;
253 abilityInfo->isNativeAbility = true;
254 std::shared_ptr<AbilityLocalRecord> abilityRecord =
255 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
256
257 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
258
259 sptr<IRemoteObject> remoteObject_ =
260 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
261 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
262 Want want;
263 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
264 usleep(AbilityBaseTest::TEST_WAIT_TIME);
265 }
266 }
267
268 /**
269 * @tc.number: AaFwk_Ability_AbilityFwk_Lifecycle_Test_0200
270 * @tc.name: Ability Lifecycle
271 * @tc.desc: The ability life cycle will change from initial state initial to inactive by calling the interface
272 * OnnStart(), and then to active by calling OnNewWant()+OnActive().
273 */
274 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Lifecycle_Test_0200, Function | MediumTest | Level1)
275 {
276 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
277
278 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
279 EXPECT_NE(abilityToken, nullptr);
280 if (abilityToken != nullptr) {
281 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
282 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
283 abilityInfo->name = ABILITY_NAME;
284 abilityInfo->isNativeAbility = true;
285 std::shared_ptr<AbilityLocalRecord> abilityRecord =
286 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
287
288 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
289
290 sptr<IRemoteObject> remoteObject_ =
291 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
292 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
293
294 abms->DisconnectAbility(nullptr);
295 usleep(AbilityBaseTest::TEST_WAIT_TIME);
296 }
297 }
298
299 /**
300 * @tc.number: AaFwk_Ability_AbilityFwk_Lifecycle_Test_0300
301 * @tc.name: Ability Lifecycle
302 * @tc.desc: Ability life cycle changes: initial - > active - > background, the interface of demoability will be
303 called:
304 * OnStart()->OnActive()->OnInactive()->OnBackground().
305 */
306 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Lifecycle_Test_0300, Function | MediumTest | Level1)
307 {
308 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
309
310 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
311 EXPECT_NE(abilityToken, nullptr);
312 if (abilityToken != nullptr) {
313 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
314 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
315 abilityInfo->name = ABILITY_NAME;
316 abilityInfo->isNativeAbility = true;
317 std::shared_ptr<AbilityLocalRecord> abilityRecord =
318 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
319
320 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
321
322 sptr<IRemoteObject> remoteObject_ =
323 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
324 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
325 Want want;
326
327 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
328 usleep(AbilityBaseTest::TEST_WAIT_TIME);
329 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_BACKGROUND);
330 usleep(AbilityBaseTest::TEST_WAIT_TIME);
331 }
332 }
333
334 /**
335 * @tc.number: AaFwk_Ability_AbilityFwk_Lifecycle_Test_0400
336 * @tc.name: Ability Lifecycle
337 * @tc.desc: Ability life cycle changes: initial - > active - > inactive, the interface of demoability will be
338 called:
339 * OnStart()->OnActive()->OnInactive().
340 */
341 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Lifecycle_Test_0400, Function | MediumTest | Level1)
342 {
343 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
344
345 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
346 EXPECT_NE(abilityToken, nullptr);
347 if (abilityToken != nullptr) {
348 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
349 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
350 abilityInfo->name = ABILITY_NAME;
351 abilityInfo->isNativeAbility = true;
352 std::shared_ptr<AbilityLocalRecord> abilityRecord =
353 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
354
355 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
356
357 sptr<IRemoteObject> remoteObject_ =
358 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
359 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
360 Want want;
361
362 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
363 usleep(AbilityBaseTest::TEST_WAIT_TIME);
364 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_INACTIVE);
365 usleep(AbilityBaseTest::TEST_WAIT_TIME);
366 }
367 }
368
369 /**
370 * @tc.number: AaFwk_Ability_AbilityFwk_Lifecycle_Test_0500
371 * @tc.name: Ability Lifecycle
372 * @tc.desc: Ability life cycle changes: initial - > active , the interface of demoability will be called:
373 * OnStart()->OnActive()->OnInactive()->OnBackground()->OnForeground().
374 */
375 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Lifecycle_Test_0500, Function | MediumTest | Level1)
376 {
377 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
378
379 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
380 EXPECT_NE(abilityToken, nullptr);
381 if (abilityToken != nullptr) {
382 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
383 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
384 abilityInfo->name = ABILITY_NAME;
385 abilityInfo->isNativeAbility = true;
386 std::shared_ptr<AbilityLocalRecord> abilityRecord =
387 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
388
389 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
390
391 sptr<IRemoteObject> remoteObject_ =
392 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
393 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
394 Want want;
395
396 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
397 usleep(AbilityBaseTest::TEST_WAIT_TIME);
398 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_BACKGROUND);
399 usleep(AbilityBaseTest::TEST_WAIT_TIME);
400 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_INACTIVE);
401 usleep(AbilityBaseTest::TEST_WAIT_TIME);
402 }
403 }
404
405 /**
406 * @tc.number: AaFwk_Ability_AbilityFwk_Lifecycle_Test_0600
407 * @tc.name: Ability Lifecycle
408 * @tc.desc: Ability life cycle changes: initial - > active , the interface of demoability will be called:
409 * OnStart()->OnActive()->OnInactive()->OnBackground()->OnStop().
410 */
411 HWTEST_F(AbilityBaseTest, AaFwk_Ability_AbilityFwk_Lifecycle_Test_0600, Function | MediumTest | Level1)
412 {
413 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
414
415 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
416 EXPECT_NE(abilityToken, nullptr);
417 if (abilityToken != nullptr) {
418 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
419 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
420 abilityInfo->name = ABILITY_NAME;
421 abilityInfo->isNativeAbility = true;
422 std::shared_ptr<AbilityLocalRecord> abilityRecord =
423 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
424
425 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
426
427 sptr<IRemoteObject> remoteObject_ =
428 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
429 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
430 Want want;
431
432 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
433 usleep(AbilityBaseTest::TEST_WAIT_TIME);
434 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_BACKGROUND);
435 usleep(AbilityBaseTest::TEST_WAIT_TIME);
436 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_INITIAL);
437 usleep(AbilityBaseTest::TEST_WAIT_TIME);
438 }
439 }
440
441 /**
442 * @tc.number: AaFwk_Ability_TerminateAbility_ForResult_Test_0100
443 * @tc.name: TerminateAbility_ForResult
444 * @tc.desc: 1. TerminateAbility with parameters
445 * 2. AMS returns parameters through sendresult
446 * 3. Compare the returned parameters with the passed in parameters.
447 */
448 HWTEST_F(AbilityBaseTest, AaFwk_Ability_TerminateAbility_ForResult_Test_0100, Function | MediumTest | Level1)
449 {
450 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
451
452 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
453 EXPECT_NE(abilityToken, nullptr);
454 if (abilityToken != nullptr) {
455 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
456 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
457 abilityInfo->name = ABILITY_NAME;
458 abilityInfo->isNativeAbility = true;
459 std::shared_ptr<AbilityLocalRecord> abilityRecord =
460 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
461
462 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
463
464 sptr<IRemoteObject> remoteObject_ =
465 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
466 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
467 Want want;
468 sptr<IAbilityConnection> connect = nullptr;
469
470 abms->TerminateAbility(nullptr, RESULT_CODE, &want);
471 usleep(AbilityBaseTest::TEST_WAIT_TIME);
472 }
473 }
474
475 /*
476 * Parameters:
477 * Action
478 * Entity
479 * Flag
480 * ElementName
481 */
482 class AbilityTerminateTest : public testing::Test {
483 public:
484 static void SetUpTestCase(void);
485 static void TearDownTestCase(void);
486 void SetUp();
487 void TearDown();
488
489 public:
490 OHOS::sptr<OHOS::IRemoteObject> abilityObject_;
491 static constexpr int TEST_WAIT_TIME = 500 * 1000; // 500 ms
492 static const int RESULT_CODE = 1992;
493 };
494
SetUpTestCase(void)495 void AbilityTerminateTest::SetUpTestCase(void)
496 {}
497
TearDownTestCase(void)498 void AbilityTerminateTest::TearDownTestCase(void)
499 {}
500
SetUp(void)501 void AbilityTerminateTest::SetUp(void)
502 {
503 abilityObject_ = new MockAbilityManagerService();
504 auto sysMgr = OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance();
505 EXPECT_TRUE(sysMgr != nullptr);
506 sysMgr->RegisterSystemAbility(OHOS::ABILITY_MGR_SERVICE_ID, abilityObject_);
507 }
508
TearDown(void)509 void AbilityTerminateTest::TearDown(void)
510 {
511 abilityObject_ = nullptr;
512 }
513
514 /**
515 * @tc.number: AaFwk_Ability_Terminate_test_0100
516 * @tc.name: TerminateAbility
517 * @tc.desc: When the ability state is inactive, the call to terminateability terminates.
518 */
519 HWTEST_F(AbilityTerminateTest, AaFwk_Ability_Terminate_test_0100, Function | MediumTest | Level1)
520 {
521 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
522 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
523 EXPECT_NE(abilityToken, nullptr);
524 if (abilityToken != nullptr) {
525 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
526 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
527 abilityInfo->name = ABILITY_NAME;
528 abilityInfo->isNativeAbility = true;
529 std::shared_ptr<AbilityLocalRecord> abilityRecord =
530 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
531
532 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_001 AbilityThreadMain";
533 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
534
535 Want want;
536 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
537 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_INACTIVE);
538 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
539
540 auto ability = AbilityLoader::GetInstance().GetAbilityByName(abilityInfo->name);
541 EXPECT_NE(ability, nullptr);
542 if (ability != nullptr) {
543 ability->SetResult(RESULT_CODE, want);
544 ability->TerminateAbility();
545 }
546 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
547 }
548 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_001 TerminateAbility";
549 }
550
551 /**
552 * @tc.number: AaFwk_Ability_Terminate_test_0200
553 * @tc.name: TerminateAbility
554 * @tc.desc: When the ability state is active, the call to terminateability terminates.
555 */
556 HWTEST_F(AbilityTerminateTest, AaFwk_Ability_Terminate_test_0200, Function | MediumTest | Level1)
557 {
558 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
559 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
560 EXPECT_NE(abilityToken, nullptr);
561 if (abilityToken != nullptr) {
562 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
563 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
564 abilityInfo->name = ABILITY_NAME;
565 abilityInfo->isNativeAbility = true;
566 std::shared_ptr<AbilityLocalRecord> abilityRecord =
567 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
568
569 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_002 AbilityThreadMain";
570 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
571
572 Want want;
573 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
574 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
575 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
576
577 auto ability = AbilityLoader::GetInstance().GetAbilityByName(abilityInfo->name);
578 EXPECT_NE(ability, nullptr);
579 if (ability != nullptr) {
580 ability->SetResult(RESULT_CODE, want);
581 ability->TerminateAbility();
582 }
583 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
584 }
585 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_002 TerminateAbility";
586 }
587
588 /**
589 * @tc.number: AaFwk_Ability_Terminate_test_0300
590 * @tc.name: TerminateAbility
591 * @tc.desc: When the ability state is BACKGROUND, the call to terminateability terminates.
592 */
593 HWTEST_F(AbilityTerminateTest, AaFwk_Ability_Terminate_test_0300, Function | MediumTest | Level1)
594 {
595 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
596 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
597 EXPECT_NE(abilityToken, nullptr);
598 if (abilityToken != nullptr) {
599 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
600 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
601 abilityInfo->name = ABILITY_NAME;
602 abilityInfo->isNativeAbility = true;
603 std::shared_ptr<AbilityLocalRecord> abilityRecord =
604 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
605
606 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_0300 AbilityThreadMain";
607 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
608
609 Want want;
610 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
611 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_BACKGROUND);
612 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
613
614 auto ability = AbilityLoader::GetInstance().GetAbilityByName(abilityInfo->name);
615 EXPECT_NE(ability, nullptr);
616 if (ability != nullptr) {
617 ability->SetResult(RESULT_CODE, want);
618 ability->TerminateAbility();
619 }
620 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
621 }
622 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_0300 TerminateAbility";
623 }
624
625 /**
626 * @tc.number: AaFwk_Ability_Terminate_test_0400
627 * @tc.name: TerminateAbility
628 * @tc.desc: When the ability state is ABILITY_STATE_INITIAL, the call to terminateability terminates.
629 */
630 HWTEST_F(AbilityTerminateTest, AaFwk_Ability_Terminate_test_0400, Function | MediumTest | Level1)
631 {
632 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
633 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
634 EXPECT_NE(abilityToken, nullptr);
635 if (abilityToken != nullptr) {
636 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
637 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
638 abilityInfo->name = ABILITY_NAME;
639 abilityInfo->isNativeAbility = true;
640 std::shared_ptr<AbilityLocalRecord> abilityRecord =
641 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
642
643 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_0400 AbilityThreadMain";
644 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
645
646 Want want;
647 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
648 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_INITIAL);
649 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
650
651 auto ability = AbilityLoader::GetInstance().GetAbilityByName(abilityInfo->name);
652 EXPECT_NE(ability, nullptr);
653 if (ability != nullptr) {
654 ability->SetResult(RESULT_CODE, want);
655 ability->TerminateAbility();
656 }
657 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
658 }
659 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_0400 TerminateAbility";
660 }
661
662 /**
663 * @tc.number: AaFwk_Ability_Terminate_test_0500
664 * @tc.name: TerminateAbility
665 * @tc.desc: When the ability state is inactive, the call to terminateability terminates.
666 */
667 HWTEST_F(AbilityTerminateTest, AaFwk_Ability_Terminate_test_0500, Function | MediumTest | Level1)
668 {
669 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
670 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
671 EXPECT_NE(abilityToken, nullptr);
672 if (abilityToken != nullptr) {
673 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
674 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
675 abilityInfo->name = ABILITY_NAME;
676 abilityInfo->isNativeAbility = true;
677 std::shared_ptr<AbilityLocalRecord> abilityRecord =
678 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
679
680 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_005 AbilityThreadMain";
681 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
682
683 Want want;
684 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
685 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_INACTIVE);
686 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
687
688 mockAMS->TerminateAbility(nullptr, RESULT_CODE, &want);
689 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
690 }
691 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_005 TerminateAbility";
692 }
693
694 /**
695 * @tc.number: AaFwk_Ability_Terminate_test_0600
696 * @tc.name: TerminateAbility
697 * @tc.desc: When the ability state is active, the call to terminateability terminates.
698 */
699 HWTEST_F(AbilityTerminateTest, AaFwk_Ability_Terminate_test_0600, Function | MediumTest | Level1)
700 {
701 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
702 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
703 EXPECT_NE(abilityToken, nullptr);
704 if (abilityToken != nullptr) {
705 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
706 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
707 abilityInfo->name = ABILITY_NAME;
708 abilityInfo->isNativeAbility = true;
709 std::shared_ptr<AbilityLocalRecord> abilityRecord =
710 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
711
712 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_006 AbilityThreadMain";
713 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
714
715 Want want;
716 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
717 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
718 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
719
720 mockAMS->TerminateAbility(nullptr, RESULT_CODE, &want);
721 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
722 }
723 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_006 TerminateAbility";
724 }
725
726 /**
727 * @tc.number: AaFwk_Ability_Terminate_test_0700
728 * @tc.name: TerminateAbility
729 * @tc.desc: When the ability state is background, the call to terminateability terminates.
730 */
731 HWTEST_F(AbilityTerminateTest, AaFwk_Ability_Terminate_test_0700, Function | MediumTest | Level1)
732 {
733 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
734 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
735 EXPECT_NE(abilityToken, nullptr);
736 if (abilityToken != nullptr) {
737 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
738 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
739 abilityInfo->name = ABILITY_NAME;
740 abilityInfo->isNativeAbility = true;
741 std::shared_ptr<AbilityLocalRecord> abilityRecord =
742 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
743
744 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_007 AbilityThreadMain";
745 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
746
747 Want want;
748 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
749 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_BACKGROUND);
750 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
751
752 mockAMS->TerminateAbility(nullptr, RESULT_CODE, &want);
753 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
754 }
755 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_007 TerminateAbility";
756 }
757
758 /**
759 * @tc.number: AaFwk_Ability_Terminate_test_0800
760 * @tc.name: TerminateAbility
761 * @tc.desc: When the ability state is initial, the call to terminateability terminates.
762 */
763 HWTEST_F(AbilityTerminateTest, AaFwk_Ability_Terminate_test_0800, Function | MediumTest | Level1)
764 {
765 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
766 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
767 EXPECT_NE(abilityToken, nullptr);
768 if (abilityToken != nullptr) {
769 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
770 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
771 abilityInfo->name = ABILITY_NAME;
772 abilityInfo->isNativeAbility = true;
773 std::shared_ptr<AbilityLocalRecord> abilityRecord =
774 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
775
776 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_008 AbilityThreadMain";
777 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
778
779 Want want;
780 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
781 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_INITIAL);
782 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
783
784 mockAMS->TerminateAbility(nullptr, RESULT_CODE, &want);
785 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
786 }
787 GTEST_LOG_(INFO) << "AaFwk_Ability_Terminate_test_008 TerminateAbility";
788 }
789
790 /**
791 * @tc.number: AaFwk_WMS_window_test_0100
792 * @tc.name: WMS Link
793 * @tc.desc: Start pageability and call GetWindow to get the window handle.
794 */
795 HWTEST_F(AbilityTerminateTest, AaFwk_WMS_window_test_0100, Function | MediumTest | Level1)
796 {
797 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
798 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
799 EXPECT_NE(abilityToken, nullptr);
800 if (abilityToken != nullptr) {
801 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
802 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
803 abilityInfo->name = ABILITY_NAME;
804 abilityInfo->isNativeAbility = true;
805 std::shared_ptr<AbilityLocalRecord> abilityRecord =
806 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
807
808 GTEST_LOG_(INFO) << "AaFwk_WMS_window_test_001 AbilityThreadMain";
809 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
810
811 Want want;
812 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
813 GTEST_LOG_(INFO) << "AaFwk_WMS_window_test_001 StartAbility";
814 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
815 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
816 mockAMS->TerminateAbility(nullptr, RESULT_CODE, &want);
817 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
818 }
819 GTEST_LOG_(INFO) << "AaFwk_WMS_window_test_001 TerminateAbility";
820 }
821
822 /**
823 * @tc.number: AaFwk_WMS_window_test_0200
824 * @tc.name: WMS Link
825 * @tc.desc: Pageability switches to the foreground and calls Window.show.
826 * Pageability switches to the background and calls Window.hide.
827 */
828 HWTEST_F(AbilityTerminateTest, AaFwk_WMS_window_test_0200, Function | MediumTest | Level1)
829 {
830 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
831 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
832 EXPECT_NE(abilityToken, nullptr);
833 if (abilityToken != nullptr) {
834 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
835 abilityInfo->type = AppExecFwk::AbilityType::PAGE;
836 abilityInfo->name = ABILITY_NAME;
837 abilityInfo->isNativeAbility = true;
838 std::shared_ptr<AbilityLocalRecord> abilityRecord =
839 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
840
841 GTEST_LOG_(INFO) << "AaFwk_WMS_window_test_002 AbilityThreadMain";
842 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
843
844 Want want;
845 MockAbilityManagerService *mockAMS = iface_cast<MockAbilityManagerService>(abilityObject_);
846 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_BACKGROUND);
847 GTEST_LOG_(INFO) << "AaFwk_WMS_window_test_002 BackGround";
848 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
849 mockAMS->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
850 GTEST_LOG_(INFO) << "AaFwk_WMS_window_test_002 Active";
851 usleep(AbilityTerminateTest::TEST_WAIT_TIME);
852
853 mockAMS->TerminateAbility(nullptr, RESULT_CODE, &want);
854 }
855 GTEST_LOG_(INFO) << "AaFwk_WMS_window_test_002 TerminateAbility";
856 }
857
858 /**
859 * @tc.number: AaFwk_DataAbility_Launch_0100
860 * @tc.name: DataAbilityHelper
861 * @tc.desc: The AbilityManager could receive the shelder of abilitythread for dataability when the dataability
862 * launched.
863 */
864 HWTEST_F(AbilityTerminateTest, AaFwk_DataAbility_Launch_0100, Function | MediumTest | Level1)
865 {
866 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Launch_0100";
867
868 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
869 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
870 EXPECT_NE(abilityToken, nullptr);
871 if (abilityToken != nullptr) {
872 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
873 abilityInfo->type = AppExecFwk::AbilityType::DATA;
874 abilityInfo->name = "DemoAbility";
875 std::shared_ptr<AbilityLocalRecord> abilityRecord =
876 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
877
878 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
879
880 sptr<IRemoteObject> remoteObject_ =
881 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
882 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
883
884 Uri uri("testuri");
885 EXPECT_TRUE(abms->AcquireDataAbility(uri, false, nullptr) == nullptr);
886 }
887 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Launch_0100";
888 }
889
890 /**
891 * @tc.number: AaFwk_DataAbility_Start_0100
892 * @tc.name: DataAbilityHelper
893 * @tc.desc: The AbilityManager could receive the inactive state from abilitythread for dataability when the
894 dataability
895 * change its lifecycle state to inactive.
896 */
897 HWTEST_F(AbilityTerminateTest, AaFwk_DataAbility_Start_0100, Function | MediumTest | Level1)
898 {
899 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Start_0100";
900
901 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
902 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
903 EXPECT_NE(abilityToken, nullptr);
904 if (abilityToken != nullptr) {
905 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
906 abilityInfo->type = AppExecFwk::AbilityType::DATA;
907 abilityInfo->name = "DemoAbility";
908 std::shared_ptr<AbilityLocalRecord> abilityRecord =
909 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
910
911 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
912
913 sptr<IRemoteObject> remoteObject_ =
914 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
915 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
916 Want want;
917 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_INACTIVE);
918
919 usleep(AbilityBaseTest::TEST_WAIT_TIME);
920 }
921 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Start_0100";
922 }
923
924 /**
925 * @tc.number: AaFwk_DataAbility_Start_0200
926 * @tc.name: DataAbilityHelper
927 * @tc.desc: The AbilityManager could not receive the initial state from abilitythread for dataability.
928 * And the OnStop coulde be called. When the dataability change its lifecycle state to initial.
929 */
930 HWTEST_F(AbilityTerminateTest, AaFwk_DataAbility_Start_0200, Function | MediumTest | Level1)
931 {
932 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Start_0200";
933
934 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
935 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
936 EXPECT_NE(abilityToken, nullptr);
937 if (abilityToken != nullptr) {
938 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
939 abilityInfo->type = AppExecFwk::AbilityType::DATA;
940 abilityInfo->name = "DemoAbility";
941 std::shared_ptr<AbilityLocalRecord> abilityRecord =
942 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
943
944 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
945
946 sptr<IRemoteObject> remoteObject_ =
947 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
948 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
949 Want want;
950 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_INITIAL);
951
952 usleep(AbilityBaseTest::TEST_WAIT_TIME);
953 }
954 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Start_0200";
955 }
956
957 /**
958 * @tc.number: AaFwk_DataAbility_Start_0300
959 * @tc.name: DataAbilityHelper
960 * @tc.desc: The AbilityManager could not receive the active state from abilitythread for dataability.
961 * And the OnActive coulde be called. When the dataability change its lifecycle state to active.
962 */
963 HWTEST_F(AbilityTerminateTest, AaFwk_DataAbility_Start_0300, Function | MediumTest | Level1)
964 {
965 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Start_0300";
966
967 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
968 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
969 EXPECT_NE(abilityToken, nullptr);
970 if (abilityToken != nullptr) {
971 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
972 abilityInfo->type = AppExecFwk::AbilityType::DATA;
973 abilityInfo->name = "DemoAbility";
974 std::shared_ptr<AbilityLocalRecord> abilityRecord =
975 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
976
977 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
978
979 sptr<IRemoteObject> remoteObject_ =
980 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
981 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
982 Want want;
983 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_ACTIVE);
984
985 usleep(AbilityBaseTest::TEST_WAIT_TIME);
986 }
987 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Start_0300";
988 }
989
990 /**
991 * @tc.number: AaFwk_DataAbility_Start_0400
992 * @tc.name: DataAbilityHelper
993 * @tc.desc: The AbilityManager could not receive the background state from abilitythread for dataability.
994 * And the OnBackground coulde be called. When the dataability change its lifecycle state to background.
995 */
996 HWTEST_F(AbilityTerminateTest, AaFwk_DataAbility_Start_0400, Function | MediumTest | Level1)
997 {
998 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Start_0400";
999
1000 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
1001 sptr<IRemoteObject> abilityToken = sptr<IRemoteObject>(new AbilityThread());
1002 EXPECT_NE(abilityToken, nullptr);
1003 if (abilityToken != nullptr) {
1004 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
1005 abilityInfo->type = AppExecFwk::AbilityType::DATA;
1006 abilityInfo->name = "DemoAbility";
1007 std::shared_ptr<AbilityLocalRecord> abilityRecord =
1008 std::make_shared<AbilityLocalRecord>(abilityInfo, abilityToken);
1009
1010 AbilityThread::AbilityThreadMain(application, abilityRecord, nullptr);
1011
1012 sptr<IRemoteObject> remoteObject_ =
1013 OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
1014 sptr<AAFwk::IAbilityManager> abms = iface_cast<AAFwk::IAbilityManager>(remoteObject_);
1015 Want want;
1016 abms->StartAbility(want, AbilityLifeCycleState::ABILITY_STATE_BACKGROUND);
1017
1018 usleep(AbilityBaseTest::TEST_WAIT_TIME);
1019 }
1020 GTEST_LOG_(INFO) << "AaFwk_DataAbility_Start_0400";
1021 }
1022 } // namespace AppExecFwk
1023 } // namespace OHOS
1024