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 <gtest/gtest.h>
17 #include "ability_manager_service.h"
18 #include "system_ability_definition.h"
19 #include "bundlemgr/mock_bundle_manager.h"
20 #include "sa_mgr_client.h"
21 #include "parameter.h"
22
23 using namespace testing::ext;
24 using namespace OHOS::AppExecFwk;
25
26 namespace OHOS {
27 namespace AAFwk {
28 namespace {
29 const std::string TASK_NAME_START_SYSTEM_APP = "StartSystemApplication";
30 const std::string TASK_NAME_SUBSCRIBE_BACKGROUND_TASK = "SubscribeBackgroundTask";
31 const std::string TASK_NAME_START_RESIDENT_APPS = "StartResidentApps";
32 const std::string TASK_NAME_INIT_STARTUP_FLAG = "InitStartupFlag";
33 }
34
35 class AbilityServiceStartTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41
42 public:
43 std::shared_ptr<AbilityManagerService> aams_{ nullptr };
44 };
45
SetUpTestCase()46 void AbilityServiceStartTest::SetUpTestCase()
47 {
48 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
49 OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
50 }
TearDownTestCase()51 void AbilityServiceStartTest::TearDownTestCase()
52 {
53 OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
54 }
55
SetUp()56 void AbilityServiceStartTest::SetUp()
57 {
58 aams_ = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance();
59 }
60
TearDown()61 void AbilityServiceStartTest::TearDown()
62 {
63 aams_->OnStop();
64 }
65
66 /*
67 * Feature: AbilityManagerService
68 * Function: Service
69 * SubFunction: NA
70 * FunctionPoints: AbilityManager startup & stop
71 * EnvConditions: NA
72 * CaseDescription: Verify if AbilityManagerService startup & stop successfully.
73 */
74 HWTEST_F(AbilityServiceStartTest, StartUp_001, TestSize.Level1)
75 {
76 GTEST_LOG_(INFO) << "ability_manager_service_startup_001 start";
77 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, aams_->QueryServiceState());
78 aams_->OnStart();
79 auto handler = aams_->GetEventHandler();
80 ASSERT_NE(handler, nullptr);
81 handler->RemoveTask(TASK_NAME_START_SYSTEM_APP);
82 handler->RemoveTask(TASK_NAME_SUBSCRIBE_BACKGROUND_TASK);
83 handler->RemoveTask(TASK_NAME_START_RESIDENT_APPS);
84 handler->RemoveTask(TASK_NAME_INIT_STARTUP_FLAG);
85
86 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, aams_->QueryServiceState());
87 aams_->OnStop();
88 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, aams_->QueryServiceState());
89 GTEST_LOG_(INFO) << "ability_manager_service_startup_001 end";
90 }
91
92 /*
93 * Feature: AbilityManagerService
94 * Function: Service
95 * SubFunction: NA
96 * FunctionPoints: AbilityManager startup two times
97 * EnvConditions: NA
98 * CaseDescription: Verify if AbilityManagerService startup & stop successfully.
99 */
100 HWTEST_F(AbilityServiceStartTest, StartUp_002, TestSize.Level1)
101 {
102 GTEST_LOG_(INFO) << "ability_manager_service_startup_002 start";
103 aams_->OnStart();
104 auto handler = aams_->GetEventHandler();
105 ASSERT_NE(handler, nullptr);
106 handler->RemoveTask(TASK_NAME_START_SYSTEM_APP);
107 handler->RemoveTask(TASK_NAME_SUBSCRIBE_BACKGROUND_TASK);
108 handler->RemoveTask(TASK_NAME_START_RESIDENT_APPS);
109 handler->RemoveTask(TASK_NAME_INIT_STARTUP_FLAG);
110
111 aams_->OnStart();
112 handler = aams_->GetEventHandler();
113 ASSERT_NE(handler, nullptr);
114 handler->RemoveTask(TASK_NAME_START_SYSTEM_APP);
115 handler->RemoveTask(TASK_NAME_SUBSCRIBE_BACKGROUND_TASK);
116 handler->RemoveTask(TASK_NAME_START_RESIDENT_APPS);
117 handler->RemoveTask(TASK_NAME_INIT_STARTUP_FLAG);
118
119 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, aams_->QueryServiceState());
120 aams_->OnStop();
121 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, aams_->QueryServiceState());
122 GTEST_LOG_(INFO) << "ability_manager_service_startup_002 end";
123 }
124
125 /*
126 * Feature: AbilityManagerService
127 * Function: Service
128 * SubFunction: NA
129 * FunctionPoints: AbilityManager stop
130 * EnvConditions: NA
131 * CaseDescription: Verify if AbilityManagerService stop successfully.
132 */
133 HWTEST_F(AbilityServiceStartTest, StartUp_003, TestSize.Level1)
134 {
135 GTEST_LOG_(INFO) << "ability_manager_service_startup_003 start";
136 aams_->OnStop();
137 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, aams_->QueryServiceState());
138 GTEST_LOG_(INFO) << "ability_manager_service_startup_003 end";
139 }
140
141 /*
142 * Feature: AbilityManagerService
143 * Function: Service
144 * SubFunction: NA
145 * FunctionPoints: AbilityManager stop again
146 * EnvConditions: NA
147 * CaseDescription: Verify if AbilityManagerService stop successfully.
148 */
149 HWTEST_F(AbilityServiceStartTest, StartUp_004, TestSize.Level1)
150 {
151 GTEST_LOG_(INFO) << "ability_manager_service_startup_004 start";
152 aams_->OnStart();
153 auto handler = aams_->GetEventHandler();
154 ASSERT_NE(handler, nullptr);
155 handler->RemoveTask(TASK_NAME_START_SYSTEM_APP);
156 handler->RemoveTask(TASK_NAME_SUBSCRIBE_BACKGROUND_TASK);
157 handler->RemoveTask(TASK_NAME_START_RESIDENT_APPS);
158 handler->RemoveTask(TASK_NAME_INIT_STARTUP_FLAG);
159
160 aams_->OnStop();
161 aams_->OnStop();
162 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, aams_->QueryServiceState());
163 GTEST_LOG_(INFO) << "ability_manager_service_startup_004 end";
164 }
165
166 /*
167 * Feature: AbilityManagerService
168 * Function: Service
169 * SubFunction: NA
170 * FunctionPoints: AbilityManager start & stop 10 times
171 * EnvConditions: NA
172 * CaseDescription: Verify if AbilityManagerService start & stop successfully.
173 */
174 HWTEST_F(AbilityServiceStartTest, StartUp_005, TestSize.Level1)
175 {
176 GTEST_LOG_(INFO) << "ability_manager_service_startup_005 start";
177 for (int i = 0; i < 10; i++) {
178 aams_->OnStart();
179 auto handler = aams_->GetEventHandler();
180 ASSERT_NE(handler, nullptr);
181 handler->RemoveTask(TASK_NAME_START_SYSTEM_APP);
182 handler->RemoveTask(TASK_NAME_SUBSCRIBE_BACKGROUND_TASK);
183 handler->RemoveTask(TASK_NAME_START_RESIDENT_APPS);
184 handler->RemoveTask(TASK_NAME_INIT_STARTUP_FLAG);
185 GTEST_LOG_(INFO) << "start " << i << "times";
186 EXPECT_EQ(ServiceRunningState::STATE_RUNNING, aams_->QueryServiceState());
187 aams_->OnStop();
188 GTEST_LOG_(INFO) << "stop " << i << "times";
189 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, aams_->QueryServiceState());
190 }
191 GTEST_LOG_(INFO) << "ability_manager_service_startup_005 end";
192 }
193
194 /**
195 * @tc.name: AbilityServiceStartTest_StartUpEvent_001
196 * @tc.desc: OnStart
197 * @tc.type: FUNC
198 * @tc.require: issueI5JZEI
199 */
200 HWTEST_F(AbilityServiceStartTest, StartUpEvent_001, TestSize.Level1)
201 {
202 aams_->OnStart();
203 const int bufferLen = 128;
204 char paramOutBuf[bufferLen] = { 0 };
205 const char* hookMode = "true";
206 int ret = GetParameter("bootevent.bootanimation.started", "", paramOutBuf, bufferLen);
207 EXPECT_TRUE(strncmp(paramOutBuf, hookMode, strlen(hookMode)) == 0);
208
209 ret = GetParameter("bootevent.appfwk.ready", "", paramOutBuf, bufferLen);
210 EXPECT_TRUE(strncmp(paramOutBuf, hookMode, strlen(hookMode)) == 0);
211 aams_->OnStop();
212 }
213 } // namespace AAFwk
214 } // namespace OHOS
215