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