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_tag_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     TAG_LOGD(AAFwkTag::TEST, "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)
__anon09001f170102() 76         .WillOnce([mockApplication]() {
77             mockApplication->Post();
78             return true;
79             });
80     client->ScheduleForegroundApplication();
81     mockApplication->Wait();
82     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_001 end");
83 }
84 
85 /*
86  * Feature: AppScheduler ZIDL interface
87  * Function: ScheduleBackgroundApplication
88  * SubFunction: NA
89  * FunctionPoints: scheduleBackgroundApplication interface
90  * EnvConditions: Application already running
91  * CaseDescription: Test the interface ScheduleBackgroundApplication of AppScheduler
92  */
93 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_002, TestSize.Level1)
94 {
95     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_002 start");
96     sptr<MockApplication> mockApplication(new MockApplication());
97     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
98 
99     EXPECT_CALL(*mockApplication, ScheduleBackgroundApplication())
100         .Times(1)
101         .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
102     client->ScheduleBackgroundApplication();
103     mockApplication->Wait();
104     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_002 end");
105 }
106 
107 /*
108  * Feature: AppScheduler ZIDL interface
109  * Function: ScheduleTerminateApplication
110  * SubFunction: NA
111  * FunctionPoints: scheduleTerminateApplication interface
112  * EnvConditions: Application already running
113  * CaseDescription: Test the interface ScheduleTerminateApplication of AppScheduler
114  */
115 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_003, TestSize.Level1)
116 {
117     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_003 start");
118     sptr<MockApplication> mockApplication(new MockApplication());
119     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
120 
121     EXPECT_CALL(*mockApplication, ScheduleTerminateApplication(_))
122         .Times(1)
123         .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
124     client->ScheduleTerminateApplication();
125     mockApplication->Wait();
126     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_003 end");
127 }
128 
129 /*
130  * Feature: AppScheduler ZIDL interface
131  * Function: ScheduleShrinkMemory
132  * SubFunction: NA
133  * FunctionPoints: scheduleShrinkMemory interface
134  * EnvConditions: Application already running
135  * CaseDescription: Test the interface ScheduleShrinkMemory of AppScheduler
136  */
137 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_004, TestSize.Level1)
138 {
139     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_004 start");
140     sptr<MockApplication> mockApplication(new MockApplication());
141     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
142     int level = 1;
143 
144     EXPECT_CALL(*mockApplication, ScheduleShrinkMemory(_))
145         .Times(1)
146         .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::ShrinkMemory));
147     client->ScheduleShrinkMemory(level);
148     mockApplication->Wait();
149 
150     int shrinkLevel = mockApplication->GetShrinkLevel();
151     EXPECT_EQ(level, shrinkLevel);
152     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_004 end");
153 }
154 
155 /*
156  * Feature: AppScheduler ZIDL interface
157  * Function: ScheduleLowMemory
158  * SubFunction: NA
159  * FunctionPoints: scheduleLowMemory interface
160  * EnvConditions: Application already running
161  * CaseDescription: Test the interface ScheduleLowMemory of AppScheduler
162  */
163 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_005, TestSize.Level1)
164 {
165     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_005 start");
166     sptr<MockApplication> mockApplication(new MockApplication());
167     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
168 
169     EXPECT_CALL(*mockApplication, ScheduleLowMemory())
170         .Times(1)
171         .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
172     client->ScheduleLowMemory();
173     mockApplication->Wait();
174     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_005 end");
175 }
176 
177 /*
178  * Feature: AppScheduler ZIDL interface
179  * Function: ScheduleLaunchApplication
180  * SubFunction: NA
181  * FunctionPoints: scheduleLaunchApplication interface
182  * EnvConditions: Application already running
183  * CaseDescription: Test the interface ScheduleLaunchApplication of AppScheduler
184  */
185 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_006, TestSize.Level1)
186 {
187     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_006 start");
188     sptr<MockApplication> mockApplication(new MockApplication());
189     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
190 
191     std::string applicationName("mockApplicationInfo");
192     ApplicationInfo applicationInfo;
193     applicationInfo.name = applicationName;
194     std::string profileName("mockProfile");
195     Profile profile(profileName);
196     std::string processName("mockProcessInfo");
197     ProcessInfo processInfo(processName, 1);
198 
199     AppLaunchData launchData;
200     launchData.SetApplicationInfo(applicationInfo);
201     launchData.SetProfile(profile);
202     launchData.SetProcessInfo(processInfo);
203 
204     Configuration config;
205     EXPECT_CALL(*mockApplication, ScheduleLaunchApplication(_, _))
206         .Times(1)
207         .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::LaunchApplication));
208     client->ScheduleLaunchApplication(launchData, config);
209     mockApplication->Wait();
210 
211     bool isEqual = mockApplication->CompareAppLaunchData(launchData);
212     EXPECT_EQ(true, isEqual);
213     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_006 end");
214 }
215 
216 /*
217  * Feature: AppScheduler ZIDL interface
218  * Function: ScheduleCleanAbility
219  * SubFunction: NA
220  * FunctionPoints: scheduleCleanAbility interface
221  * EnvConditions: Application already running
222  * CaseDescription: Test the interface ScheduleCleanAbility of AppScheduler
223  */
224 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_008, TestSize.Level1)
225 {
226     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_008 start");
227     sptr<MockApplication> mockApplication(new MockApplication());
228     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
229 
230     EXPECT_CALL(*mockApplication, ScheduleCleanAbility(_, _))
231         .Times(1)
232         .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post));
233     client->ScheduleCleanAbility(GetMockToken());
234     mockApplication->Wait();
235     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_008 end");
236 }
237 
238 /*
239  * Feature: AppScheduler ZIDL interface
240  * Function: ScheduleCleanAbility
241  * SubFunction: NA
242  * FunctionPoints: scheduleProfileChanged interface
243  * EnvConditions: Application already running
244  * CaseDescription: Test the interface ScheduleProfileChanged of AppScheduler
245  */
246 HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_009, TestSize.Level1)
247 {
248     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_009 start");
249     sptr<MockApplication> mockApplication(new MockApplication());
250     sptr<IAppScheduler> client = iface_cast<IAppScheduler>(mockApplication);
251     std::string profileName("mockProfile");
252     Profile profile(profileName);
253 
254     EXPECT_CALL(*mockApplication, ScheduleProfileChanged(_))
255         .Times(1)
256         .WillOnce(Invoke(mockApplication.GetRefPtr(), &MockApplication::ProfileChanged));
257     client->ScheduleProfileChanged(profile);
258     mockApplication->Wait();
259 
260     bool isEqual = mockApplication->CompareProfile(profile);
261     EXPECT_EQ(true, isEqual);
262     TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_009 end");
263 }
264 }  // namespace AppExecFwk
265 }  // namespace OHOS
266