1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <gtest/gtest.h>
16
17 #include "app_scheduler_host.h"
18 #include "app_scheduler_proxy.h"
19 #include "semaphore_ex.h"
20
21 #include "mock_ability_token.h"
22 #include "mock_app_scheduler.h"
23 #include "mock_application.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::AppExecFwk;
27 using OHOS::iface_cast;
28 using OHOS::sptr;
29 using testing::_;
30 using testing::Invoke;
31 using testing::InvokeWithoutArgs;
32
33 namespace {
34 const int32_t COUNT = 10000;
35 } // namespace
36 class AmsIpcAppSchedulerModuleTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
GetMockToken() const42 sptr<MockAbilityToken> GetMockToken() const
43 {
44 return mock_token_;
45 }
46
47 private:
48 sptr<MockAbilityToken> mock_token_;
49 };
50
SetUpTestCase()51 void AmsIpcAppSchedulerModuleTest::SetUpTestCase()
52 {}
53
TearDownTestCase()54 void AmsIpcAppSchedulerModuleTest::TearDownTestCase()
55 {}
56
SetUp()57 void AmsIpcAppSchedulerModuleTest::SetUp()
58 {}
59
TearDown()60 void AmsIpcAppSchedulerModuleTest::TearDown()
61 {}
62
63 /*
64 * Feature: ApplicationFramework
65 * Function: AppManagerService
66 * SubFunction: IApplicationScheduler
67 * FunctionPoints: test ScheduleForegroundApplication API,then check the function whether is good or not
68 * EnvConditions: system running normally
69 * CaseDescription: execute ScheduleForegroundApplication API 10000 times
70 */
71 HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_001, TestSize.Level3)
72 {
73 for (int i = 0; i < COUNT; i++) {
74 sptr<MockApplication> mockApplication(new MockApplication());
75 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
76
77 EXPECT_CALL(*mockApplication, ScheduleForegroundApplication())
78 .Times(1)
__anon9d34096f0202() 79 .WillOnce([mockApplication]() {
80 mockApplication->Post();
81 return true;
82 });
83 client->ScheduleForegroundApplication();
84 mockApplication->Wait();
85 }
86 }
87
88 /*
89 * Feature: ApplicationFramework
90 * Function: AppManagerService
91 * SubFunction: IApplicationScheduler
92 * FunctionPoints: test ScheduleBackgroundApplication API,then check the function whether is good or not
93 * EnvConditions: system running normally
94 * CaseDescription: execute ScheduleBackgroundApplication API 10000 times
95 */
96 HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_002, TestSize.Level3)
97 {
98 for (int i = 0; i < COUNT; i++) {
99 sptr<MockApplication> mockApplication(new MockApplication());
100 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
101
102 EXPECT_CALL(*mockApplication, ScheduleBackgroundApplication())
103 .Times(1)
104 .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
105 client->ScheduleBackgroundApplication();
106 mockApplication->Wait();
107 }
108 }
109
110 /*
111 * Feature: ApplicationFramework
112 * Function: AppManagerService
113 * SubFunction: IApplicationScheduler
114 * FunctionPoints: test ScheduleTerminateApplication API,then check the function whether is good or not
115 * EnvConditions: system running normally
116 * CaseDescription: execute ScheduleTerminateApplication API 10000 times
117 */
118 HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_003, TestSize.Level3)
119 {
120 for (int i = 0; i < COUNT; i++) {
121 sptr<MockApplication> mockApplication(new MockApplication());
122 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
123
124 EXPECT_CALL(*mockApplication, ScheduleTerminateApplication(_))
125 .Times(1)
126 .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
127 client->ScheduleTerminateApplication();
128 mockApplication->Wait();
129 }
130 }
131
132 /*
133 * Feature: ApplicationFramework
134 * Function: AppManagerService
135 * SubFunction: IApplicationScheduler
136 * FunctionPoints: test ScheduleTrimMemory API,then check the function whether is good or not
137 * EnvConditions: system running normally
138 * CaseDescription: execute ScheduleTrimMemory API 10000 times
139 */
140 HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_004, TestSize.Level3)
141 {
142 for (int i = 0; i < COUNT; i++) {
143 sptr<MockApplication> mockApplication(new MockApplication());
144 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
145
146 EXPECT_CALL(*mockApplication, ScheduleShrinkMemory(_))
147 .Times(1)
148 .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::ShrinkMemory));
149 int level = 1;
150 client->ScheduleShrinkMemory(level);
151 mockApplication->Wait();
152 int getLevel = mockApplication->GetShrinkLevel();
153 EXPECT_EQ(getLevel, level) << "execute fail, index is " << i;
154 }
155 }
156
157 /*
158 * Feature: ApplicationFramework
159 * Function: AppManagerService
160 * SubFunction: IApplicationScheduler
161 * FunctionPoints: test scheduleLowMemory API,then check the function whether is good or not
162 * EnvConditions: system running normally
163 * CaseDescription: execute LowMemoryWarning API 10000 times
164 */
165 HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_005, TestSize.Level3)
166 {
167 for (int i = 0; i < COUNT; i++) {
168 sptr<MockApplication> mockApplication(new MockApplication());
169 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
170
171 EXPECT_CALL(*mockApplication, ScheduleLowMemory())
172 .Times(1)
173 .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
174 client->ScheduleLowMemory();
175 mockApplication->Wait();
176 }
177 }
178
179 /*
180 * Feature: ApplicationFramework
181 * Function: AppManagerService
182 * SubFunction: IApplicationScheduler
183 * FunctionPoints: test scheduleProfileChanged API,then check the function whether is good or not
184 * EnvConditions: system running normally
185 * CaseDescription: execute scheduleProfileChanged API 10000 times
186 */
187 HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_006, TestSize.Level3)
188 {
189 for (int i = 0; i < COUNT; i++) {
190 sptr<MockApplication> mockApplication(new MockApplication());
191 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
192
193 std::string profileName("mockProfile");
194 Profile profile(profileName);
195
196 EXPECT_CALL(*mockApplication, ScheduleProfileChanged(_))
197 .Times(1)
198 .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::ProfileChanged));
199 client->ScheduleProfileChanged(profile);
200 mockApplication->Wait();
201 bool result = mockApplication->CompareProfile(profile);
202 EXPECT_EQ(result, true) << "execute fail, index is " << i;
203 }
204 }
205
206 /*
207 * Feature: ApplicationFramework
208 * Function: AppManagerService
209 * SubFunction: IApplicationScheduler
210 * FunctionPoints: test ScheduleLaunchApplication API,then check the function whether is good or not
211 * EnvConditions: system running normally
212 * CaseDescription: execute ScheduleLaunchApplication API 10000 times
213 */
214 HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_008, TestSize.Level3)
215 {
216 for (int i = 0; i < COUNT; i++) {
217 sptr<MockApplication> mockApplication(new MockApplication());
218 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
219
220 std::string applicationName("mockApplicationInfo");
221 ApplicationInfo applicationInfo;
222 applicationInfo.name = applicationName;
223 std::string profileName("mockProfile");
224 Profile profile(profileName);
225 std::string processName("mockProcessInfo");
226 ProcessInfo processInfo(processName, 123);
227
228 AppLaunchData launchData;
229 launchData.SetApplicationInfo(applicationInfo);
230 launchData.SetProfile(profile);
231 launchData.SetProcessInfo(processInfo);
232
233 Configuration config;
234 EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _))
235 .Times(1)
236 .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication));
237 client->ScheduleLaunchApplication(launchData, config);
238 mockApplication->Wait();
239
240 bool isEqual = mockApplication->CompareAppLaunchData(launchData);
241 EXPECT_EQ(true, isEqual) << "execute fail, index is " << i;
242 }
243 }
244
245 /*
246 * Feature: ApplicationFramework
247 * Function: AppManagerService
248 * SubFunction: IApplicationScheduler
249 * FunctionPoints: test ScheduleCleanAbility API,then check the function whether is good or not
250 * EnvConditions: system running normally
251 * CaseDescription: execute ScheduleCleanAbility API 10000 times
252 */
253 HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_009, TestSize.Level3)
254 {
255 for (int i = 0; i < COUNT; i++) {
256 sptr<MockApplication> mockApplication(new MockApplication());
257 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
258 EXPECT_CALL(*mockApplication, ScheduleCleanAbility(_, _))
259 .Times(1)
260 .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
261 client->ScheduleCleanAbility(GetMockToken());
262 mockApplication->Wait();
263 }
264 }
265
266 /*
267 * Feature: ApplicationFramework
268 * Function: AppManagerService
269 * SubFunction: IApplicationScheduler
270 * FunctionPoints: test ScheduleConfigurationUpdated API,then check the function whether is good or not
271 * EnvConditions: system running normally
272 * CaseDescription: execute ScheduleConfigurationUpdated API 10000 times
273 */
274 HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_010, TestSize.Level3)
275 {
276 OHOS::Semaphore sem(0);
277 Configuration testConfig;
278 std::string val = "ZH-HANS";
279 testConfig.AddItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, val);
280 for (int i = 0; i < COUNT; i++) {
281 sptr<MockAppScheduler> mockAppScheduler(new MockAppScheduler());
282 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockAppScheduler);
283 bool testResult = false;
284
__anon9d34096f0302(const Configuration& config) 285 auto mockHandler = [&](const Configuration& config) {
286 testResult = (val == config.GetItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE));
287 sem.Post();
288 };
289
290 EXPECT_CALL(*mockAppScheduler, ScheduleConfigurationUpdated(_)).Times(1).WillOnce(Invoke(mockHandler));
291
292 client->ScheduleConfigurationUpdated(testConfig);
293
294 sem.Wait();
295
296 EXPECT_TRUE(testResult);
297 }
298 }
299