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 // 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 "hilog_tag_wrapper.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace AppExecFwk {
29 class AppMgrServiceInnerMock : public AppMgrServiceInner {
30 public:
OpenAppSpawnConnection()31 virtual int32_t OpenAppSpawnConnection() override
32 {
33 return 0;
34 }
35 };
36 class AmsServiceStartupTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
43
SetUpTestCase()44 void AmsServiceStartupTest::SetUpTestCase()
45 {}
46
TearDownTestCase()47 void AmsServiceStartupTest::TearDownTestCase()
48 {}
49
SetUp()50 void AmsServiceStartupTest::SetUp()
51 {}
52
TearDown()53 void AmsServiceStartupTest::TearDown()
54 {}
55
56 /*
57 * Feature: AppMgrService
58 * Function: Service
59 * SubFunction: StartUp
60 * FunctionPoints: AppMgrService startup
61 * EnvConditions: Mobile that can run ohos test framework
62 * CaseDescription: Verify if AppMgrService startup successfully
63 */
64 HWTEST_F(AmsServiceStartupTest, Startup_001, TestSize.Level1)
65 {
66 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_001 start");
67 std::shared_ptr<AppMgrService> appMgrService = std::make_shared<AppMgrService>();
68 std::shared_ptr<AppMgrServiceInnerMock> innerService = std::make_shared<AppMgrServiceInnerMock>();
69 appMgrService->SetInnerService(innerService);
70 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
71 appMgrService->OnStart();
72 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, appMgrService->QueryServiceState().serviceRunningState);
73 appMgrService->OnStop();
74 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
75 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_001 end");
76 }
77
78 /*
79 * Feature: AppMgrService
80 * Function: Service
81 * SubFunction: StartUp
82 * FunctionPoints: AppMgrService startup again
83 * EnvConditions: Mobile that can run ohos test framework
84 * CaseDescription: Verify if start up repeatly system act normal
85 */
86 HWTEST_F(AmsServiceStartupTest, Startup_002, TestSize.Level1)
87 {
88 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_002 start");
89 std::shared_ptr<AppMgrService> appMgrService = std::make_shared<AppMgrService>();
90 std::shared_ptr<AppMgrServiceInnerMock> innerService = std::make_shared<AppMgrServiceInnerMock>();
91 appMgrService->SetInnerService(innerService);
92 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
93 appMgrService->OnStart();
94 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, appMgrService->QueryServiceState().serviceRunningState);
95 appMgrService->OnStart();
96 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, appMgrService->QueryServiceState().serviceRunningState);
97 appMgrService->OnStop();
98 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
99 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_002 end");
100 }
101
102 /*
103 * Feature: AppMgrService
104 * Function: Service
105 * SubFunction: StartUp
106 * FunctionPoints: AppMgrService stop without start
107 * EnvConditions: Mobile that can run ohos test framework
108 * CaseDescription: Verify if stop without start system act normal
109 */
110 HWTEST_F(AmsServiceStartupTest, Startup_003, TestSize.Level1)
111 {
112 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_003 start");
113 std::shared_ptr<AppMgrService> appMgrService = std::make_shared<AppMgrService>();
114 std::shared_ptr<AppMgrServiceInnerMock> innerService = std::make_shared<AppMgrServiceInnerMock>();
115 appMgrService->SetInnerService(innerService);
116 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
117 appMgrService->OnStop();
118 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
119 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_003 end");
120 }
121
122 /*
123 * Feature: AppMgrService
124 * Function: Service
125 * SubFunction: StartUp
126 * FunctionPoints: AppMgrService stop repeatly
127 * EnvConditions: Mobile that can run ohos test framework
128 * CaseDescription: Verify if stop repeatly system act normal
129 */
130 HWTEST_F(AmsServiceStartupTest, Startup_004, TestSize.Level1)
131 {
132 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_004 start");
133 std::shared_ptr<AppMgrService> appMgrService = std::make_shared<AppMgrService>();
134 std::shared_ptr<AppMgrServiceInnerMock> innerService = std::make_shared<AppMgrServiceInnerMock>();
135 appMgrService->SetInnerService(innerService);
136 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
137 appMgrService->OnStart();
138 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, appMgrService->QueryServiceState().serviceRunningState);
139 appMgrService->OnStop();
140 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
141 appMgrService->OnStop();
142 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
143 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_004 end");
144 }
145
146 /*
147 * Feature: AppMgrService
148 * Function: Service
149 * SubFunction: StartUp
150 * FunctionPoints: AppMgrService start and stop repeatly
151 * EnvConditions: Mobile that can run ohos test framework
152 * CaseDescription: Verify if start and stop repeatly system act normal
153 */
154 HWTEST_F(AmsServiceStartupTest, Startup_005, TestSize.Level1)
155 {
156 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_005 start");
157 std::shared_ptr<AppMgrService> appMgrService = std::make_shared<AppMgrService>();
158 std::shared_ptr<AppMgrServiceInnerMock> innerService = std::make_shared<AppMgrServiceInnerMock>();
159 appMgrService->SetInnerService(innerService);
160 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
161 appMgrService->OnStart();
162 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, appMgrService->QueryServiceState().serviceRunningState);
163 appMgrService->OnStop();
164 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
165 appMgrService->OnStart();
166 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, appMgrService->QueryServiceState().serviceRunningState);
167 appMgrService->OnStop();
168 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, appMgrService->QueryServiceState().serviceRunningState);
169 TAG_LOGI(AAFwkTag::TEST, "ams_service_startup_005 end");
170 }
171 } // namespace AppExecFwk
172 } // namespace OHOS
173