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 #include <gtest/gtest.h>
17 #include "app_scheduler_proxy.h"
18 #include "app_scheduler_host.h"
19 #include "hilog_wrapper.h"
20 #include "mock_ability_token.h"
21 #include "mock_application.h"
22
23 using namespace testing::ext;
24 using OHOS::iface_cast;
25 using OHOS::sptr;
26 using testing::_;
27 using testing::Invoke;
28 using testing::InvokeWithoutArgs;
29 namespace OHOS {
30 namespace AppExecFwk {
31 class AmsIpcAppSchedulerInterfaceTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp();
36 void TearDown();
GetMockToken() const37 sptr<MockAbilityToken> GetMockToken() const
38 {
39 return mock_token_;
40 }
41
42 private:
43 sptr<MockAbilityToken> mock_token_;
44 };
45
SetUpTestCase()46 void AmsIpcAppSchedulerInterfaceTest::SetUpTestCase()
47 {}
48
TearDownTestCase()49 void AmsIpcAppSchedulerInterfaceTest::TearDownTestCase()
50 {}
51
SetUp()52 void AmsIpcAppSchedulerInterfaceTest::SetUp()
53 {
54 mock_token_ = new (std::nothrow) MockAbilityToken();
55 }
56
TearDown()57 void AmsIpcAppSchedulerInterfaceTest::TearDown()
58 {}
59
60 /*
61 * Feature: AppScheduler ZIDL interface
62 * Function: ScheduleForegroundApplication
63 * SubFunction: NA
64 * FunctionPoints: ScheduleForegroundApplication interface
65 * EnvConditions: Application already running
66 * CaseDescription: Test the interface ScheduleForegroundApplication of AppScheduler
67 */
68 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_001, TestSize.Level1)
69 {
70 HILOG_DEBUG("AppSchedulerInterfaceTest_001 start");
71 sptr<MockApplication> mockApplication(new MockApplication());
72 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
73
74 EXPECT_CALL(*mockApplication, ScheduleForegroundApplication())
75 .Times(1)
76 .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
77 client->ScheduleForegroundApplication();
78 mockApplication->Wait();
79 HILOG_DEBUG("AppSchedulerInterfaceTest_001 end");
80 }
81
82 /*
83 * Feature: AppScheduler ZIDL interface
84 * Function: ScheduleBackgroundApplication
85 * SubFunction: NA
86 * FunctionPoints: scheduleBackgroundApplication interface
87 * EnvConditions: Application already running
88 * CaseDescription: Test the interface ScheduleBackgroundApplication of AppScheduler
89 */
90 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_002, TestSize.Level1)
91 {
92 HILOG_DEBUG("AppSchedulerInterfaceTest_002 start");
93 sptr<MockApplication> mockApplication(new MockApplication());
94 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
95
96 EXPECT_CALL(*mockApplication, ScheduleBackgroundApplication())
97 .Times(1)
98 .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
99 client->ScheduleBackgroundApplication();
100 mockApplication->Wait();
101 HILOG_DEBUG("AppSchedulerInterfaceTest_002 end");
102 }
103
104 /*
105 * Feature: AppScheduler ZIDL interface
106 * Function: ScheduleTerminateApplication
107 * SubFunction: NA
108 * FunctionPoints: scheduleTerminateApplication interface
109 * EnvConditions: Application already running
110 * CaseDescription: Test the interface ScheduleTerminateApplication of AppScheduler
111 */
112 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_003, TestSize.Level1)
113 {
114 HILOG_DEBUG("AppSchedulerInterfaceTest_003 start");
115 sptr<MockApplication> mockApplication(new MockApplication());
116 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
117
118 EXPECT_CALL(*mockApplication, ScheduleTerminateApplication())
119 .Times(1)
120 .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
121 client->ScheduleTerminateApplication();
122 mockApplication->Wait();
123 HILOG_DEBUG("AppSchedulerInterfaceTest_003 end");
124 }
125
126 /*
127 * Feature: AppScheduler ZIDL interface
128 * Function: ScheduleShrinkMemory
129 * SubFunction: NA
130 * FunctionPoints: scheduleShrinkMemory interface
131 * EnvConditions: Application already running
132 * CaseDescription: Test the interface ScheduleShrinkMemory of AppScheduler
133 */
134 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_004, TestSize.Level1)
135 {
136 HILOG_DEBUG("AppSchedulerInterfaceTest_004 start");
137 sptr<MockApplication> mockApplication(new MockApplication());
138 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
139 int level = 1;
140
141 EXPECT_CALL(*mockApplication, ScheduleShrinkMemory(_))
142 .Times(1)
143 .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::ShrinkMemory));
144 client->ScheduleShrinkMemory(level);
145 mockApplication->Wait();
146
147 int shrinkLevel = mockApplication->GetShrinkLevel();
148 EXPECT_EQ(level, shrinkLevel);
149 HILOG_DEBUG("AppSchedulerInterfaceTest_004 end");
150 }
151
152 /*
153 * Feature: AppScheduler ZIDL interface
154 * Function: ScheduleLowMemory
155 * SubFunction: NA
156 * FunctionPoints: scheduleLowMemory interface
157 * EnvConditions: Application already running
158 * CaseDescription: Test the interface ScheduleLowMemory of AppScheduler
159 */
160 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_005, TestSize.Level1)
161 {
162 HILOG_DEBUG("AppSchedulerInterfaceTest_005 start");
163 sptr<MockApplication> mockApplication(new MockApplication());
164 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
165
166 EXPECT_CALL(*mockApplication, ScheduleLowMemory())
167 .Times(1)
168 .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
169 client->ScheduleLowMemory();
170 mockApplication->Wait();
171 HILOG_DEBUG("AppSchedulerInterfaceTest_005 end");
172 }
173
174 /*
175 * Feature: AppScheduler ZIDL interface
176 * Function: ScheduleLaunchApplication
177 * SubFunction: NA
178 * FunctionPoints: scheduleLaunchApplication interface
179 * EnvConditions: Application already running
180 * CaseDescription: Test the interface ScheduleLaunchApplication of AppScheduler
181 */
182 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_006, TestSize.Level1)
183 {
184 HILOG_DEBUG("AppSchedulerInterfaceTest_006 start");
185 sptr<MockApplication> mockApplication(new MockApplication());
186 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
187
188 std::string applicationName("mockApplicationInfo");
189 ApplicationInfo applicationInfo;
190 applicationInfo.name = applicationName;
191 std::string profileName("mockProfile");
192 Profile profile(profileName);
193 std::string processName("mockProcessInfo");
194 ProcessInfo processInfo(processName, 1);
195
196 AppLaunchData launchData;
197 launchData.SetApplicationInfo(applicationInfo);
198 launchData.SetProfile(profile);
199 launchData.SetProcessInfo(processInfo);
200
201 Configuration config;
202 EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _))
203 .Times(1)
204 .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication));
205 client->ScheduleLaunchApplication(launchData, config);
206 mockApplication->Wait();
207
208 bool isEqual = mockApplication->CompareAppLaunchData(launchData);
209 EXPECT_EQ(true, isEqual);
210 HILOG_DEBUG("AppSchedulerInterfaceTest_006 end");
211 }
212
213 /*
214 * Feature: AppScheduler ZIDL interface
215 * Function: ScheduleCleanAbility
216 * SubFunction: NA
217 * FunctionPoints: scheduleCleanAbility interface
218 * EnvConditions: Application already running
219 * CaseDescription: Test the interface ScheduleCleanAbility of AppScheduler
220 */
221 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_008, TestSize.Level1)
222 {
223 HILOG_DEBUG("AppSchedulerInterfaceTest_008 start");
224 sptr<MockApplication> mockApplication(new MockApplication());
225 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
226
227 EXPECT_CALL(*mockApplication, ScheduleCleanAbility(_))
228 .Times(1)
229 .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
230 client->ScheduleCleanAbility(GetMockToken());
231 mockApplication->Wait();
232 HILOG_DEBUG("AppSchedulerInterfaceTest_008 end");
233 }
234
235 /*
236 * Feature: AppScheduler ZIDL interface
237 * Function: ScheduleCleanAbility
238 * SubFunction: NA
239 * FunctionPoints: scheduleProfileChanged interface
240 * EnvConditions: Application already running
241 * CaseDescription: Test the interface ScheduleProfileChanged of AppScheduler
242 */
243 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_009, TestSize.Level1)
244 {
245 HILOG_DEBUG("AppSchedulerInterfaceTest_009 start");
246 sptr<MockApplication> mockApplication(new MockApplication());
247 sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
248 std::string profileName("mockProfile");
249 Profile profile(profileName);
250
251 EXPECT_CALL(*mockApplication, ScheduleProfileChanged(_))
252 .Times(1)
253 .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::ProfileChanged));
254 client->ScheduleProfileChanged(profile);
255 mockApplication->Wait();
256
257 bool isEqual = mockApplication->CompareProfile(profile);
258 EXPECT_EQ(true, isEqual);
259 HILOG_DEBUG("AppSchedulerInterfaceTest_009 end");
260 }
261 } // namespace AppExecFwk
262 } // namespace OHOS
263