• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 // redefine private and protected since testcase need to invoke and test private function
17 #define private public
18 #define protected public
19 #include "app_mgr_service.h"
20 #undef private
21 #undef protected
22 #include <gtest/gtest.h>
23 #include "errors.h"
24 #include "hilog_wrapper.h"
25 #include "iremote_object.h"
26 #include "mock_app_mgr_service_inner.h"
27 #include "mock_native_token.h"
28 #include "system_memory_attr.h"
29 
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace OHOS::AppExecFwk;
33 using OHOS::iface_cast;
34 using OHOS::IRemoteObject;
35 using OHOS::sptr;
36 using testing::_;
37 using testing::InvokeWithoutArgs;
38 
39 class AmsServiceEventDriveTest : public testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp();
44     void TearDown();
45 
46     std::shared_ptr<MockAppMgrServiceInner> GetMockAppMgrServiceInner();
47     std::shared_ptr<AMSEventHandler> GetAmsEventHandler();
48 
49 protected:
50     std::shared_ptr<AppMgrService> appMgrService_ = std::make_shared<AppMgrService>();
51 };
52 
SetUpTestCase()53 void AmsServiceEventDriveTest::SetUpTestCase()
54 {
55     MockNativeToken::SetNativeToken();
56 }
57 
TearDownTestCase()58 void AmsServiceEventDriveTest::TearDownTestCase()
59 {}
60 
SetUp()61 void AmsServiceEventDriveTest::SetUp()
62 {
63     appMgrService_->OnStart();
64 }
65 
TearDown()66 void AmsServiceEventDriveTest::TearDown()
67 {
68     appMgrService_->OnStop();
69 }
70 
71 /*
72  * Feature: AppMgrService
73  * Function: Service
74  * SubFunction: EventDrive
75  * FunctionPoints: AppMgrService event drive program model
76  * EnvConditions: Mobile that can run ohos test framework
77  * CaseDescription: Verify if post AttachApplication task success
78  */
79 HWTEST_F(AmsServiceEventDriveTest, EventDrive_001, TestSize.Level1)
80 {
81     HILOG_INFO("ams_service_event_drive_test_001 start");
82 
83     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
84     appMgrService_->SetInnerService(innerService);
85     appMgrService_->OnStart();
86     innerService->SetWaitCount(2);
87 
88     EXPECT_CALL(*innerService, AddAppDeathRecipient(_, _))
89         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
90     EXPECT_CALL(*innerService, AttachApplication(_, _))
91         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
92 
93     sptr<IRemoteObject> client;
94     appMgrService_->AttachApplication(client);
95     innerService->Wait();
96 
97     HILOG_INFO("ams_service_event_drive_test_001 end");
98 }
99 
100 /*
101  * Feature: AppMgrService
102  * Function: Service
103  * SubFunction: EventDrive
104  * FunctionPoints: AppMgrService event drive program model
105  * EnvConditions: Mobile that can run ohos test framework
106  * CaseDescription: Verify if post ApplicationForegrounded task success
107  */
108 HWTEST_F(AmsServiceEventDriveTest, EventDrive_002, TestSize.Level1)
109 {
110     HILOG_INFO("ams_service_event_drive_test_002 start");
111 
112     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
113     std::unique_ptr<AppMgrService> appMgrService = std::make_unique<AppMgrService>();
114     appMgrService->SetInnerService(innerService);
115     appMgrService->OnStart();
116 
117     EXPECT_CALL(*innerService, ApplicationForegrounded(_))
118         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
119 
120     int32_t recordId = 0;
121     appMgrService->ApplicationForegrounded(recordId);
122     innerService->Wait();
123 
124     HILOG_INFO("ams_service_event_drive_test_002 end");
125 }
126 
127 /*
128  * Feature: AppMgrService
129  * Function: Service
130  * SubFunction: EventDrive
131  * FunctionPoints: AppMgrService event drive program model
132  * EnvConditions: Mobile that can run ohos test framework
133  * CaseDescription: Verify if post ApplicationBackgrounded task success
134  */
135 HWTEST_F(AmsServiceEventDriveTest, EventDrive_003, TestSize.Level1)
136 {
137     HILOG_INFO("ams_service_event_drive_test_003 start");
138 
139     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
140     appMgrService_->SetInnerService(innerService);
141     appMgrService_->OnStart();
142 
143     EXPECT_CALL(*innerService, ApplicationBackgrounded(_))
144         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
145 
146     int32_t recordId = 0;
147     appMgrService_->ApplicationBackgrounded(recordId);
148     innerService->Wait();
149 
150     HILOG_INFO("ams_service_event_drive_test_003 end");
151 }
152 
153 /*
154  * Feature: AppMgrService
155  * Function: Service
156  * SubFunction: EventDrive
157  * FunctionPoints: AppMgrService event drive program model
158  * EnvConditions: Mobile that can run ohos test framework
159  * CaseDescription: Verify if post ApplicationTerminated task success
160  */
161 HWTEST_F(AmsServiceEventDriveTest, EventDrive_004, TestSize.Level1)
162 {
163     HILOG_INFO("ams_service_event_drive_test_004 start");
164 
165     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
166     appMgrService_->SetInnerService(innerService);
167     appMgrService_->OnStart();
168 
169     EXPECT_CALL(*innerService, ApplicationTerminated(_))
170         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
171 
172     int32_t recordId = 0;
173     appMgrService_->ApplicationTerminated(recordId);
174     innerService->Wait();
175 
176     HILOG_INFO("ams_service_event_drive_test_004 end");
177 }
178 
179 /*
180  * Feature: AppMgrService
181  * Function: Service
182  * SubFunction: EventDrive
183  * FunctionPoints: AppMgrService event drive program model
184  * EnvConditions: Mobile that can run ohos test framework
185  * CaseDescription: Verify if post ClearUpApplicationData task success
186  */
187 HWTEST_F(AmsServiceEventDriveTest, EventDrive_006, TestSize.Level1)
188 {
189     HILOG_INFO("ams_service_event_drive_test_006 start");
190 
191     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
192     appMgrService_->SetInnerService(innerService);
193     appMgrService_->OnStart();
194 
195     EXPECT_CALL(*innerService, ClearUpApplicationData(_, _, _))
196         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
197 
198     std::string appName = "appName";
199     appMgrService_->ClearUpApplicationData(appName);
200     innerService->Wait();
201 
202     HILOG_INFO("ams_service_event_drive_test_006 end");
203 }
204 
205 /*
206  * Feature: AppMgrService
207  * Function: Service
208  * SubFunction: EventDrive
209  * FunctionPoints: AppMgrService event drive program model
210  * EnvConditions: Mobile that can run ohos test framework
211  * CaseDescription: Verify if post IsBackgroundRunningRestricted task success
212  */
213 HWTEST_F(AmsServiceEventDriveTest, EventDrive_008, TestSize.Level1)
214 {
215     HILOG_INFO("ams_service_event_drive_test_008 start");
216 
217     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
218     appMgrService_->SetInnerService(innerService);
219     appMgrService_->OnStart();
220 
221     EXPECT_CALL(*innerService, GetAllRunningProcesses(_)).WillOnce(Return(0));
222 
223     std::vector<RunningProcessInfo> runningProcessInfo;
224     EXPECT_EQ(0, appMgrService_->GetAllRunningProcesses(runningProcessInfo));
225 
226     HILOG_INFO("ams_service_event_drive_test_008 end");
227 }
228 
229 /*
230  * Feature: AppMgrService
231  * Function: Service
232  * SubFunction: EventDrive
233  * FunctionPoints: AppMgrService event drive program model
234  * EnvConditions: Mobile that can run ohos test framework
235  * CaseDescription: Verify if AttachApplication act normal without initialize AppMgrService
236  */
237 HWTEST_F(AmsServiceEventDriveTest, EventDrive_009, TestSize.Level1)
238 {
239     HILOG_INFO("ams_service_event_drive_test_009 start");
240 
241     appMgrService_->OnStop();
242     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
243     appMgrService_->SetInnerService(innerService);
244 
245     EXPECT_CALL(*innerService, AttachApplication(_, _)).Times(0);
246 
247     sptr<IRemoteObject> client;
248     appMgrService_->AttachApplication(client);
249 
250     HILOG_INFO("ams_service_event_drive_test_009 end");
251 }
252 
253 /*
254  * Feature: AppMgrService
255  * Function: Service
256  * SubFunction: EventDrive
257  * FunctionPoints: AppMgrService event drive program model
258  * EnvConditions: Mobile that can run ohos test framework
259  * CaseDescription: Verify if ApplicationForegrounded act normal without initialize AppMgrService
260  */
261 HWTEST_F(AmsServiceEventDriveTest, EventDrive_010, TestSize.Level1)
262 {
263     HILOG_INFO("ams_service_event_drive_test_010 start");
264 
265     appMgrService_->OnStop();
266     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
267     appMgrService_->SetInnerService(innerService);
268 
269     EXPECT_CALL(*innerService, ApplicationForegrounded(_)).Times(0);
270 
271     int32_t recordId = 0;
272     appMgrService_->ApplicationForegrounded(recordId);
273 
274     HILOG_INFO("ams_service_event_drive_test_010 end");
275 }
276 
277 /*
278  * Feature: AppMgrService
279  * Function: Service
280  * SubFunction: EventDrive
281  * FunctionPoints: AppMgrService event drive program model
282  * EnvConditions: Mobile that can run ohos test framework
283  * CaseDescription: Verify if ApplicationBackgrounded act normal without initialize AppMgrService
284  */
285 HWTEST_F(AmsServiceEventDriveTest, EventDrive_011, TestSize.Level1)
286 {
287     HILOG_INFO("ams_service_event_drive_test_011 start");
288 
289     appMgrService_->OnStop();
290     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
291     appMgrService_->SetInnerService(innerService);
292 
293     EXPECT_CALL(*innerService, ApplicationBackgrounded(_)).Times(0);
294 
295     int32_t recordId = 0;
296     appMgrService_->ApplicationBackgrounded(recordId);
297 
298     HILOG_INFO("ams_service_event_drive_test_011 end");
299 }
300 
301 /*
302  * Feature: AppMgrService
303  * Function: Service
304  * SubFunction: EventDrive
305  * FunctionPoints: AppMgrService event drive program model
306  * EnvConditions: Mobile that can run ohos test framework
307  * CaseDescription: Verify if ApplicationTerminated act normal without initialize AppMgrService
308  */
309 HWTEST_F(AmsServiceEventDriveTest, EventDrive_012, TestSize.Level1)
310 {
311     HILOG_INFO("ams_service_event_drive_test_012 start");
312 
313     appMgrService_->OnStop();
314     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
315     appMgrService_->SetInnerService(innerService);
316 
317     EXPECT_CALL(*innerService, ApplicationTerminated(_)).Times(0);
318 
319     int32_t recordId = 0;
320     appMgrService_->ApplicationTerminated(recordId);
321 
322     HILOG_INFO("ams_service_event_drive_test_012 end");
323 }
324 
325 /*
326  * Feature: AppMgrService
327  * Function: Service
328  * SubFunction: EventDrive
329  * FunctionPoints: AppMgrService event drive program model
330  * EnvConditions: Mobile that can run ohos test framework
331  * CaseDescription: Verify if AbilityCleaned act normal without initialize AppMgrService
332  */
333 HWTEST_F(AmsServiceEventDriveTest, EventDrive_013, TestSize.Level1)
334 {
335     HILOG_INFO("ams_service_event_drive_test_013 start");
336 
337     appMgrService_->OnStop();
338     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
339     appMgrService_->SetInnerService(innerService);
340 
341     EXPECT_CALL(*innerService, AbilityTerminated(_)).Times(0);
342 
343     sptr<IRemoteObject> token;
344     appMgrService_->AbilityCleaned(token);
345 
346     HILOG_INFO("ams_service_event_drive_test_013 end");
347 }
348 
349 /*
350  * Feature: AppMgrService
351  * Function: Service
352  * SubFunction: EventDrive
353  * FunctionPoints: AppMgrService event drive program model
354  * EnvConditions: Mobile that can run ohos test framework
355  * CaseDescription: Verify if ClearUpApplicationData act normal without initialize AppMgrService
356  */
357 HWTEST_F(AmsServiceEventDriveTest, EventDrive_014, TestSize.Level1)
358 {
359     HILOG_INFO("ams_service_event_drive_test_014 start");
360 
361     appMgrService_->OnStop();
362     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
363     appMgrService_->SetInnerService(innerService);
364 
365     EXPECT_CALL(*innerService, ClearUpApplicationData(_, _, _)).Times(0);
366 
367     std::string appName = "appName";
368     appMgrService_->ClearUpApplicationData(appName);
369 
370     HILOG_INFO("ams_service_event_drive_test_014 end");
371 }
372 
373 /*
374  * Feature: AppMgrService
375  * Function: Service
376  * SubFunction: EventDrive
377  * FunctionPoints: AppMgrService event drive program model
378  * EnvConditions: Mobile that can run ohos test framework
379  * CaseDescription: Verify if GetAllRunningProcesses act normal after AppMgrService stopped
380  */
381 HWTEST_F(AmsServiceEventDriveTest, EventDrive_016, TestSize.Level1)
382 {
383     HILOG_INFO("ams_service_event_drive_test_016 start");
384 
385     appMgrService_->OnStop();
386     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
387     appMgrService_->SetInnerService(innerService);
388 
389     std::vector<RunningProcessInfo> runningProcessInfo;
390     EXPECT_EQ(OHOS::ERR_INVALID_OPERATION, appMgrService_->GetAllRunningProcesses(runningProcessInfo));
391 
392     HILOG_INFO("ams_service_event_drive_test_016 end");
393 }
394 
395 /*
396  * Feature: AppMgrService
397  * Function: Service
398  * SubFunction: EventDrive
399  * FunctionPoints: AppMgrService event drive program model
400  * EnvConditions: Mobile that can run ohos test framework
401  * CaseDescription: Verify if AttachApplication act normal after AppMgrService stopped
402  */
403 HWTEST_F(AmsServiceEventDriveTest, EventDrive_017, TestSize.Level1)
404 {
405     HILOG_INFO("ams_service_event_drive_test_017 start");
406 
407     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
408     appMgrService_->SetInnerService(innerService);
409     appMgrService_->OnStart();
410     appMgrService_->OnStop();
411 
412     EXPECT_CALL(*innerService, AttachApplication(_, _)).Times(0);
413 
414     sptr<IRemoteObject> client;
415     appMgrService_->AttachApplication(client);
416 
417     HILOG_INFO("ams_service_event_drive_test_017 end");
418 }
419 
420 /*
421  * Feature: AppMgrService
422  * Function: Service
423  * SubFunction: EventDrive
424  * FunctionPoints: AppMgrService event drive program model
425  * EnvConditions: Mobile that can run ohos test framework
426  * CaseDescription: Verify if ApplicationForegrounded act normal after AppMgrService stopped
427  */
428 HWTEST_F(AmsServiceEventDriveTest, EventDrive_018, TestSize.Level1)
429 {
430     HILOG_INFO("ams_service_event_drive_test_018 start");
431 
432     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
433     appMgrService_->SetInnerService(innerService);
434     appMgrService_->OnStart();
435     appMgrService_->OnStop();
436 
437     EXPECT_CALL(*innerService, ApplicationForegrounded(_)).Times(0);
438 
439     int32_t recordId = 0;
440     appMgrService_->ApplicationForegrounded(recordId);
441 
442     HILOG_INFO("ams_service_event_drive_test_018 end");
443 }
444 
445 /*
446  * Feature: AppMgrService
447  * Function: Service
448  * SubFunction: EventDrive
449  * FunctionPoints: AppMgrService event drive program model
450  * EnvConditions: Mobile that can run ohos test framework
451  * CaseDescription: Verify if ApplicationBackgrounded act normal after AppMgrService stopped
452  */
453 HWTEST_F(AmsServiceEventDriveTest, EventDrive_019, TestSize.Level1)
454 {
455     HILOG_INFO("ams_service_event_drive_test_019 start");
456 
457     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
458     appMgrService_->SetInnerService(innerService);
459     appMgrService_->OnStart();
460     appMgrService_->OnStop();
461 
462     EXPECT_CALL(*innerService, ApplicationBackgrounded(_)).Times(0);
463 
464     int32_t recordId = 0;
465     appMgrService_->ApplicationBackgrounded(recordId);
466 
467     HILOG_INFO("ams_service_event_drive_test_019 end");
468 }
469 
470 /*
471  * Feature: AppMgrService
472  * Function: Service
473  * SubFunction: EventDrive
474  * FunctionPoints: AppMgrService event drive program model
475  * EnvConditions: Mobile that can run ohos test framework
476  * CaseDescription: Verify if ApplicationTerminated act normal after AppMgrService stopped
477  */
478 HWTEST_F(AmsServiceEventDriveTest, EventDrive_020, TestSize.Level1)
479 {
480     HILOG_INFO("ams_service_event_drive_test_020 start");
481 
482     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
483     appMgrService_->SetInnerService(innerService);
484     appMgrService_->OnStart();
485     appMgrService_->OnStop();
486 
487     EXPECT_CALL(*innerService, ApplicationTerminated(_)).Times(0);
488 
489     int32_t recordId = 0;
490     appMgrService_->ApplicationTerminated(recordId);
491 
492     HILOG_INFO("ams_service_event_drive_test_020 end");
493 }
494 
495 /*
496  * Feature: AppMgrService
497  * Function: Service
498  * SubFunction: EventDrive
499  * FunctionPoints: AppMgrService event drive program model
500  * EnvConditions: Mobile that can run ohos test framework
501  * CaseDescription: Verify if AbilityCleaned act normal after AppMgrService stopped
502  */
503 HWTEST_F(AmsServiceEventDriveTest, EventDrive_021, TestSize.Level1)
504 {
505     HILOG_INFO("ams_service_event_drive_test_021 start");
506     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
507     appMgrService_->SetInnerService(innerService);
508     appMgrService_->OnStart();
509     appMgrService_->OnStop();
510 
511     EXPECT_CALL(*innerService, AbilityTerminated(_)).Times(0);
512 
513     sptr<IRemoteObject> token;
514     appMgrService_->AbilityCleaned(token);
515 
516     HILOG_INFO("ams_service_event_drive_test_021 end");
517 }
518 
519 /*
520  * Feature: AppMgrService
521  * Function: Service
522  * SubFunction: EventDrive
523  * FunctionPoints: AppMgrService event drive program model
524  * EnvConditions: Mobile that can run ohos test framework
525  * CaseDescription: Verify if ClearUpApplicationData act normal after AppMgrService stopped
526  */
527 HWTEST_F(AmsServiceEventDriveTest, EventDrive_022, TestSize.Level1)
528 {
529     HILOG_INFO("ams_service_event_drive_test_022 start");
530     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
531     appMgrService_->SetInnerService(innerService);
532     appMgrService_->OnStart();
533     appMgrService_->OnStop();
534 
535     EXPECT_CALL(*innerService, ClearUpApplicationData(_, _, _)).Times(0);
536 
537     std::string appName = "appName";
538     appMgrService_->ClearUpApplicationData(appName);
539 
540     HILOG_INFO("ams_service_event_drive_test_022 end");
541 }
542 
543 /*
544  * Feature: AppMgrService
545  * Function: Service
546  * SubFunction: EventDrive
547  * FunctionPoints: AppMgrService event drive program model
548  * EnvConditions: Mobile that can run ohos test framework
549  * CaseDescription: Verify if GetAllRunningProcesses act normal after AppMgrService stopped
550  */
551 HWTEST_F(AmsServiceEventDriveTest, EventDrive_024, TestSize.Level1)
552 {
553     HILOG_INFO("ams_service_event_drive_test_024 start");
554     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
555     appMgrService_->SetInnerService(innerService);
556     appMgrService_->OnStart();
557     appMgrService_->OnStop();
558 
559     appMgrService_->SetInnerService(innerService);
560 
561     std::vector<RunningProcessInfo> runningProcessInfo;
562     EXPECT_EQ(OHOS::ERR_INVALID_OPERATION, appMgrService_->GetAllRunningProcesses(runningProcessInfo));
563 
564     HILOG_INFO("ams_service_event_drive_test_024 end");
565 }
566 
567 /*
568  * Feature: AppMgrService
569  * Function: Service
570  * SubFunction: EventDrive
571  * FunctionPoints: AppMgrService event drive program model
572  * EnvConditions: Mobile that can run ohos test framework
573  * CaseDescription: Verify if post application from background to foreground task act normal
574  */
575 HWTEST_F(AmsServiceEventDriveTest, EventDrive_025, TestSize.Level1)
576 {
577     HILOG_INFO("ams_service_event_drive_test_025 start");
578     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
579     appMgrService_->SetInnerService(innerService);
580     appMgrService_->OnStart();
581     int32_t waitCount = 2;
582     innerService->SetWaitCount(waitCount);
583 
584     EXPECT_CALL(*innerService, ApplicationBackgrounded(_))
585         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
586     EXPECT_CALL(*innerService, ApplicationForegrounded(_))
587         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
588 
589     int32_t recordId = 0;
590     appMgrService_->ApplicationBackgrounded(recordId);
591     appMgrService_->ApplicationForegrounded(recordId);
592     innerService->Wait();
593     HILOG_INFO("ams_service_event_drive_test_025 end");
594 }
595 
596 /*
597  * Feature: AppMgrService
598  * Function: Service
599  * SubFunction: EventDrive
600  * FunctionPoints: AppMgrService event drive program model
601  * EnvConditions: Mobile that can run ohos test framework
602  * CaseDescription: Verify if post application from foreground to background task act normal
603  */
604 HWTEST_F(AmsServiceEventDriveTest, EventDrive_026, TestSize.Level1)
605 {
606     HILOG_INFO("ams_service_event_drive_test_026 start");
607     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
608     appMgrService_->SetInnerService(innerService);
609     appMgrService_->OnStart();
610     int32_t waitCount = 2;
611     innerService->SetWaitCount(waitCount);
612 
613     EXPECT_CALL(*innerService, ApplicationForegrounded(_))
614         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
615     EXPECT_CALL(*innerService, ApplicationBackgrounded(_))
616         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
617 
618     int32_t recordId = 0;
619     appMgrService_->ApplicationForegrounded(recordId);
620     appMgrService_->ApplicationBackgrounded(recordId);
621     innerService->Wait();
622     HILOG_INFO("ams_service_event_drive_test_026 end");
623 }
624 
625 /*
626  * Feature: AppMgrService
627  * Function: Service
628  * SubFunction: EventDrive
629  * FunctionPoints: AppMgrService event drive program model
630  * EnvConditions: Mobile that can run ohos test framework
631  * CaseDescription: Verify if post application from background to terminate task act normal
632  */
633 HWTEST_F(AmsServiceEventDriveTest, EventDrive_027, TestSize.Level1)
634 {
635     HILOG_INFO("ams_service_event_drive_test_027 start");
636     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
637     appMgrService_->SetInnerService(innerService);
638     appMgrService_->OnStart();
639     int32_t waitCount = 2;
640     innerService->SetWaitCount(waitCount);
641 
642     EXPECT_CALL(*innerService, ApplicationBackgrounded(_))
643         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
644     EXPECT_CALL(*innerService, ApplicationTerminated(_))
645         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
646 
647     int32_t recordId = 0;
648     appMgrService_->ApplicationBackgrounded(recordId);
649     appMgrService_->ApplicationTerminated(recordId);
650     innerService->Wait();
651     HILOG_INFO("ams_service_event_drive_test_027 end");
652 }
653 
654 /*
655  * Feature: AppMgrService
656  * Function: Service
657  * SubFunction: EventDrive
658  * FunctionPoints: AppMgrService event drive program model
659  * EnvConditions: Mobile that can run ohos test framework
660  * CaseDescription: Verify if post application from foreground to terminated task act normal
661  */
662 HWTEST_F(AmsServiceEventDriveTest, EventDrive_028, TestSize.Level1)
663 {
664     HILOG_INFO("ams_service_event_drive_test_028 start");
665     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
666     appMgrService_->SetInnerService(innerService);
667     appMgrService_->OnStart();
668     int32_t waitCount = 2;
669     innerService->SetWaitCount(waitCount);
670 
671     EXPECT_CALL(*innerService, ApplicationForegrounded(_))
672         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
673     EXPECT_CALL(*innerService, ApplicationTerminated(_))
674         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
675 
676     int32_t recordId = 0;
677     appMgrService_->ApplicationForegrounded(recordId);
678     appMgrService_->ApplicationTerminated(recordId);
679     innerService->Wait();
680     HILOG_INFO("ams_service_event_drive_test_028 end");
681 }
682 
683 /*
684  * Feature: AppMgrService
685  * Function: Service
686  * SubFunction: EventDrive
687  * FunctionPoints: AppMgrService event drive program model
688  * EnvConditions: Mobile that can run ohos test framework
689  * CaseDescription: Verify if post application from terminate to foreground task act normal
690  */
691 HWTEST_F(AmsServiceEventDriveTest, EventDrive_029, TestSize.Level1)
692 {
693     HILOG_INFO("ams_service_event_drive_test_029 start");
694     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
695     appMgrService_->SetInnerService(innerService);
696     appMgrService_->OnStart();
697     int32_t waitCount = 2;
698     innerService->SetWaitCount(waitCount);
699 
700     EXPECT_CALL(*innerService, ApplicationTerminated(_))
701         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
702     EXPECT_CALL(*innerService, ApplicationForegrounded(_))
703         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
704 
705     int32_t recordId = 0;
706     appMgrService_->ApplicationTerminated(recordId);
707     appMgrService_->ApplicationForegrounded(recordId);
708     innerService->Wait();
709     HILOG_INFO("ams_service_event_drive_test_029 end");
710 }
711 
712 /*
713  * Feature: AppMgrService
714  * Function: Service
715  * SubFunction: EventDrive
716  * FunctionPoints: AppMgrService event drive program model
717  * EnvConditions: Mobile that can run ohos test framework.
718  * CaseDescription: Verify if post application from terminate to background task act normal
719  */
720 HWTEST_F(AmsServiceEventDriveTest, EventDrive_030, TestSize.Level1)
721 {
722     HILOG_INFO("ams_service_event_drive_test_030 start");
723     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
724     appMgrService_->SetInnerService(innerService);
725     appMgrService_->OnStart();
726     int32_t waitCount = 2;
727     innerService->SetWaitCount(waitCount);
728 
729     EXPECT_CALL(*innerService, ApplicationTerminated(_))
730         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
731     EXPECT_CALL(*innerService, ApplicationBackgrounded(_))
732         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
733 
734     int32_t recordId = 0;
735     appMgrService_->ApplicationTerminated(recordId);
736     appMgrService_->ApplicationBackgrounded(recordId);
737     innerService->Wait();
738     HILOG_INFO("ams_service_event_drive_test_030 end");
739 }
740 
741 /*
742  * Feature: AppMgrService
743  * Function: Service
744  * SubFunction: EventDrive
745  * FunctionPoints: AppMgrService event drive program model
746  * EnvConditions: Mobile that can run ohos test framework
747  * CaseDescription: Verify if post AddAppDeathRecipient task success
748  */
749 HWTEST_F(AmsServiceEventDriveTest, EventDrive_034, TestSize.Level1)
750 {
751     HILOG_INFO("ams_service_event_drive_test_034 start");
752     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
753     appMgrService_->SetInnerService(innerService);
754     appMgrService_->OnStart();
755 
756     EXPECT_CALL(*innerService, AddAppDeathRecipient(_, _))
757         .WillOnce(InvokeWithoutArgs(innerService.get(), &MockAppMgrServiceInner::Post));
758 
759     pid_t pid = 1;
760     appMgrService_->AddAppDeathRecipient(pid);
761     innerService->Wait();
762     HILOG_INFO("ams_service_event_drive_test_034 end");
763 }
764 
765 /*
766  * Feature: AppMgrService
767  * Function: Service
768  * SubFunction: EventDrive
769  * FunctionPoints: AppMgrService event drive program model
770  * EnvConditions: Mobile that can run ohos test framework
771  * CaseDescription: Verify if AddAppDeathRecipient act normal without initialize AppMgrService
772  */
773 HWTEST_F(AmsServiceEventDriveTest, EventDrive_035, TestSize.Level1)
774 {
775     HILOG_INFO("ams_service_event_drive_test_035 start");
776 
777     appMgrService_->OnStop();
778     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
779     appMgrService_->SetInnerService(innerService);
780 
781     EXPECT_CALL(*innerService, AddAppDeathRecipient(_, _)).Times(0);
782 
783     pid_t pid = 1;
784     appMgrService_->AddAppDeathRecipient(pid);
785 
786     HILOG_INFO("ams_service_event_drive_test_035 end");
787 }
788 
789 /*
790  * Feature: AppMgrService
791  * Function: Service
792  * SubFunction: EventDrive
793  * FunctionPoints: AppMgrService event drive program model
794  * EnvConditions: Mobile that can run ohos test framework
795  * CaseDescription: Verify if AddAppDeathRecipient act normal after AppMgrService stopped
796  */
797 HWTEST_F(AmsServiceEventDriveTest, EventDrive_036, TestSize.Level1)
798 {
799     HILOG_INFO("ams_service_event_drive_test_036 start");
800 
801     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
802     appMgrService_->SetInnerService(innerService);
803     appMgrService_->OnStart();
804     appMgrService_->OnStop();
805 
806     EXPECT_CALL(*innerService, AddAppDeathRecipient(_, _)).Times(0);
807 
808     pid_t pid = 1;
809     appMgrService_->AddAppDeathRecipient(pid);
810 
811     HILOG_INFO("ams_service_event_drive_test_036 end");
812 }
813 
814 /*
815  * Feature: AppMgrService
816  * Function: Service
817  * SubFunction: EventDrive
818  * FunctionPoints: AppMgrService event drive program model
819  * EnvConditions: Mobile that can run ohos test framework
820  * CaseDescription: Verify if QueryServiceState act normal
821  */
822 HWTEST_F(AmsServiceEventDriveTest, EventDrive_037, TestSize.Level1)
823 {
824     HILOG_INFO("ams_service_event_drive_test_037 start");
825 
826     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
827     appMgrService_->SetInnerService(innerService);
828     appMgrService_->OnStart();
829 
830     EXPECT_CALL(*innerService, QueryAppSpawnConnectionState())
831         .WillRepeatedly(Return(SpawnConnectionState::STATE_CONNECTED));
832     EXPECT_EQ(ServiceRunningState::STATE_RUNNING, appMgrService_->QueryServiceState().serviceRunningState);
833     EXPECT_EQ(SpawnConnectionState::STATE_CONNECTED, appMgrService_->QueryServiceState().connectionState);
834 
835     HILOG_INFO("ams_service_event_drive_test_037 end");
836 }
837 
838 /*
839  * Feature: AppMgrService
840  * Function: Service
841  * SubFunction: EventDrive
842  * FunctionPoints: AppMgrService event drive program model
843  * EnvConditions: Mobile that can run ohos test framework
844  * CaseDescription: Verify if QueryServiceState act normal without initialize AppMgrService
845  */
846 HWTEST_F(AmsServiceEventDriveTest, EventDrive_038, TestSize.Level1)
847 {
848     HILOG_INFO("ams_service_event_drive_test_038 start");
849 
850     appMgrService_->OnStop();
851     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
852     appMgrService_->SetInnerService(innerService);
853 
854     EXPECT_CALL(*innerService, QueryAppSpawnConnectionState()).Times(2);
855     EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService_->QueryServiceState().serviceRunningState);
856     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appMgrService_->QueryServiceState().connectionState);
857 
858     HILOG_INFO("ams_service_event_drive_test_038 end");
859 }
860 
861 /*
862  * Feature: AppMgrService
863  * Function: Service
864  * SubFunction: EventDrive
865  * FunctionPoints: AppMgrService event drive program model
866  * EnvConditions: Mobile that can run ohos test framework
867  * CaseDescription: Verify if QueryServiceState act normal after AppMgrService stopped
868  */
869 HWTEST_F(AmsServiceEventDriveTest, EventDrive_039, TestSize.Level1)
870 {
871     HILOG_INFO("ams_service_event_drive_test_039 start");
872 
873     std::shared_ptr<MockAppMgrServiceInner> innerService = std::make_shared<MockAppMgrServiceInner>();
874     appMgrService_->SetInnerService(innerService);
875     appMgrService_->OnStart();
876     appMgrService_->OnStop();
877 
878     EXPECT_CALL(*innerService, QueryAppSpawnConnectionState()).Times(2);
879     EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService_->QueryServiceState().serviceRunningState);
880     EXPECT_EQ(SpawnConnectionState::STATE_NOT_CONNECT, appMgrService_->QueryServiceState().connectionState);
881 
882     HILOG_INFO("ams_service_event_drive_test_039 end");
883 }
884