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 
16 #include <gtest/gtest.h>
17 
18 #include "ability_manager_errors.h"
19 #define private public
20 #define protected public
21 #include "ability_record.h"
22 #include "app_scheduler.h"
23 #undef private
24 #undef protected
25 #include "app_debug_listener_stub_mock.h"
26 #include "app_mgr_client_mock.h"
27 #include "app_process_data.h"
28 #include "app_state_call_back_mock.h"
29 #include "bundle_info.h"
30 #include "element_name.h"
31 #include "mock_sa_call.h"
32 #include "param.h"
33 
34 using namespace testing;
35 using namespace testing::ext;
36 using namespace OHOS::AppExecFwk;
37 
38 namespace OHOS {
39 namespace AAFwk {
40 namespace {
41 const int32_t USER_ID = 100;
42 const std::string STRING_APP_STATE = "BEGIN";
43 }  // namespace
44 
45 class AppSchedulerTest : public testing::Test {
46 public:
47     static void SetUpTestCase(void);
48     static void TearDownTestCase(void);
49     void SetUp();
50     void TearDown();
51 
52     static AbilityRequest GenerateAbilityRequest(const std::string& deviceName, const std::string& abilityName,
53         const std::string& appName, const std::string& bundleName);
54 
55     std::shared_ptr<AppStateCallbackMock> appStateMock_ = std::make_shared<AppStateCallbackMock>();
56     std::unique_ptr<AppMgrClientMock> clientMock_ = std::make_unique<AppMgrClientMock>();
57 };
58 
SetUpTestCase(void)59 void AppSchedulerTest::SetUpTestCase(void)
60 {}
TearDownTestCase(void)61 void AppSchedulerTest::TearDownTestCase(void)
62 {}
SetUp()63 void AppSchedulerTest::SetUp()
64 {}
TearDown()65 void AppSchedulerTest::TearDown()
66 {}
67 
GenerateAbilityRequest(const std::string & deviceName,const std::string & abilityName,const std::string & appName,const std::string & bundleName)68 AbilityRequest AppSchedulerTest::GenerateAbilityRequest(const std::string& deviceName, const std::string& abilityName,
69     const std::string& appName, const std::string& bundleName)
70 {
71     ElementName element(deviceName, abilityName, bundleName);
72     Want want;
73     want.SetElement(element);
74 
75     AbilityInfo abilityInfo;
76     abilityInfo.applicationName = appName;
77     ApplicationInfo appinfo;
78     appinfo.name = appName;
79 
80     AbilityRequest abilityRequest;
81     abilityRequest.want = want;
82     abilityRequest.abilityInfo = abilityInfo;
83     abilityRequest.appInfo = appinfo;
84 
85     return abilityRequest;
86 }
87 
88 /**
89  * @tc.name: AppScheduler_GetConfiguration_0100
90  * @tc.desc: GetConfiguration
91  * @tc.type: FUNC
92  * @tc.require: SR000GH1GO
93  */
94 HWTEST_F(AppSchedulerTest, AppScheduler_GetConfiguration_0100, TestSize.Level1)
95 {
96     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = nullptr;
97 
98     Configuration config;
99     auto result = DelayedSingleton<AppScheduler>::GetInstance()->GetConfiguration(config);
100 
101     EXPECT_EQ(result, INNER_ERR);
102 }
103 
104 /**
105  * @tc.name: AppScheduler_GetProcessRunningInfosByUserId_0100
106  * @tc.desc: GetProcessRunningInfosByUserId
107  * @tc.type: FUNC
108  * @tc.require: SR000GH1GO
109  */
110 HWTEST_F(AppSchedulerTest, AppScheduler_GetProcessRunningInfosByUserId_0100, TestSize.Level1)
111 {
112     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = nullptr;
113 
114     std::vector<RunningProcessInfo> info;
115     int32_t userId = USER_ID;
116     auto result = DelayedSingleton<AppScheduler>::GetInstance()->GetProcessRunningInfosByUserId(info, userId);
117 
118     EXPECT_EQ(result, INNER_ERR);
119 }
120 
121 /**
122  * @tc.name: AppScheduler_ConvertAppState_0100
123  * @tc.desc: ConvertAppState
124  * @tc.type: FUNC
125  * @tc.require: SR000GH1GO
126  */
127 HWTEST_F(AppSchedulerTest, AppScheduler_ConvertAppState_0100, TestSize.Level1)
128 {
129     AppState state = AppState::BEGIN;
130     auto result = DelayedSingleton<AppScheduler>::GetInstance()->ConvertAppState(state);
131 
132     EXPECT_EQ(result, STRING_APP_STATE);
133 }
134 
135 /*
136  * Feature: AppScheduler
137  * Function: Init
138  * SubFunction: NA
139  * FunctionPoints: AppSchedulerTest Init
140  * EnvConditions:NA
141  * CaseDescription: Appstatecallback is nullptr causes init to fail
142  */
143 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_001, TestSize.Level1)
144 {
145     std::shared_ptr<AppStateCallbackMock> appStateMock;
146     EXPECT_EQ(false, DelayedSingleton<AppScheduler>::GetInstance()->Init(appStateMock));
147 }
148 
149 /*
150  * Feature: AppScheduler
151  * Function: Init
152  * SubFunction: NA
153  * FunctionPoints: AppScheduler Init
154  * EnvConditions: NA
155  * CaseDescription: Verify Init
156  */
157 HWTEST_F(AppSchedulerTest, AppScheduler_Init_001, TestSize.Level1)
158 {
159     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
160     DelayedSingleton<AppScheduler>::GetInstance()->isInit_ = true;
161     std::weak_ptr<AppStateCallback> callback(appStateMock_);
162     bool res = DelayedSingleton<AppScheduler>::GetInstance()->Init(callback);
163     EXPECT_TRUE(res);
164 }
165 
166 /*
167  * Feature: AppScheduler
168  * Function: Init
169  * SubFunction: NA
170  * FunctionPoints: AppScheduler Init
171  * EnvConditions: NA
172  * CaseDescription: Verify Init
173  */
174 HWTEST_F(AppSchedulerTest, AppScheduler_Init_002, TestSize.Level1)
175 {
176     EXPECT_CALL(*clientMock_, ConnectAppMgrService()).Times(1)
177         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
178     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
179     DelayedSingleton<AppScheduler>::GetInstance()->isInit_ = false;
180     std::weak_ptr<AppStateCallback> callback(appStateMock_);
181     bool res = DelayedSingleton<AppScheduler>::GetInstance()->Init(callback);
182     EXPECT_FALSE(res);
183     clientMock_.reset();
184     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_.reset();
185 }
186 
187 /*
188  * Feature: AppScheduler
189  * Function: Init
190  * SubFunction: NA
191  * FunctionPoints: AppScheduler Init
192  * EnvConditions: NA
193  * CaseDescription: Verify Init
194  */
195 HWTEST_F(AppSchedulerTest, AppScheduler_Init_003, TestSize.Level1)
196 {
197     clientMock_ = std::make_unique<AppMgrClientMock>();
198     EXPECT_CALL(*clientMock_, RegisterAppStateCallback(_)).Times(1)
199         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
200     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
201     DelayedSingleton<AppScheduler>::GetInstance()->isInit_ = false;
202     std::weak_ptr<AppStateCallback> callback(appStateMock_);
203     bool res = DelayedSingleton<AppScheduler>::GetInstance()->Init(callback);
204     EXPECT_FALSE(res);
205 }
206 
207 /*
208  * Feature: AppScheduler
209  * Function: LoadAbility
210  * SubFunction: NA
211  * FunctionPoints: AppScheduler LoadAbility
212  * EnvConditions:NA
213  * CaseDescription: Verify the fail process of loadability
214  */
215 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_004, TestSize.Level1)
216 {
217     std::string deviceName = "device";
218     std::string abilityName = "FirstAbility";
219     std::string appName = "FirstApp";
220     std::string bundleName = "com.ix.First.Test";
221     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
222     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
223     auto token = record->GetToken();
224 
225     std::string preDeviceName = "device";
226     std::string preAbilityName = "SecondAbility";
227     std::string preAppName = "SecondApp";
228     std::string preBundleName = "com.ix.Second.Test";
229     auto preAbilityReq = GenerateAbilityRequest(preDeviceName, preAbilityName, preAppName, preBundleName);
230     auto preRecord = AbilityRecord::CreateAbilityRecord(preAbilityReq);
231     auto preToken = preRecord->GetToken();
232     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = nullptr;
233     AbilityRuntime::LoadParam loadParam;
234     loadParam.abilityRecordId = 0;
235     loadParam.token = token;
236     loadParam.preToken = preToken;
237     EXPECT_NE((int)ERR_OK, DelayedSingleton<AppScheduler>::GetInstance()->LoadAbility(
238         loadParam, record->GetAbilityInfo(), record->GetApplicationInfo(), record->GetWant()));
239 }
240 
241 /*
242  * Feature: AppScheduler
243  * Function: LoadAbility
244  * SubFunction: NA
245  * FunctionPoints: AppScheduler LoadAbility
246  * EnvConditions: NA
247  * CaseDescription: Verify LoadAbility
248  */
249 HWTEST_F(AppSchedulerTest, AppScheduler_LoadAbility_001, TestSize.Level1)
250 {
251     EXPECT_CALL(*clientMock_, LoadAbility(_, _, _, _)).Times(1)
252         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
253     sptr<IRemoteObject> token;
254     sptr<IRemoteObject> preToken;
255     AbilityInfo abilityInfo;
256     ApplicationInfo applicationInfo;
257     Want want;
258     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
259     AbilityRuntime::LoadParam loadParam;
260     loadParam.abilityRecordId = 0;
261     loadParam.token = token;
262     loadParam.preToken = preToken;
263     int res = DelayedSingleton<AppScheduler>::GetInstance()->LoadAbility(loadParam, abilityInfo, applicationInfo, want);
264     EXPECT_EQ(res, INNER_ERR);
265 }
266 
267 /*
268  * Feature: AppScheduler
269  * Function: TerminateAbility
270  * SubFunction: NA
271  * FunctionPoints: AppScheduler TerminateAbility
272  * EnvConditions: NA
273  * CaseDescription: Verify TerminateAbility
274  */
275 HWTEST_F(AppSchedulerTest, AppScheduler_TerminateAbility_001, TestSize.Level1)
276 {
277     EXPECT_CALL(*clientMock_, TerminateAbility(_, _)).Times(1)
278         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
279     sptr<IRemoteObject> token;
280     bool clearMissionFlag = true;
281     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
282     int res = DelayedSingleton<AppScheduler>::GetInstance()->TerminateAbility(token, clearMissionFlag);
283     EXPECT_EQ(res, INNER_ERR);
284 }
285 
286 /*
287  * Feature: AppScheduler
288  * Function: TerminateAbility
289  * SubFunction: NA
290  * FunctionPoints: AppScheduler TerminateAbility
291  * EnvConditions:NA
292  * CaseDescription: Verify appmgrclient_ Is nullptr causes TerminateAbility to fail
293  */
294 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_006, TestSize.Level1)
295 {
296     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = nullptr;
297     std::string deviceName = "device";
298     std::string abilityName = "FirstAbility";
299     std::string appName = "FirstApp";
300     std::string bundleName = "com.ix.First.Test";
301     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
302     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
303     auto token = record->GetToken();
304 
305     EXPECT_NE((int)ERR_OK, DelayedSingleton<AppScheduler>::GetInstance()->TerminateAbility(token, false));
306 }
307 
308 /*
309  * Feature: AppScheduler
310  * Function: TerminateAbility
311  * SubFunction: NA
312  * FunctionPoints: AppScheduler TerminateAbility
313  * EnvConditions:NA
314  * CaseDescription: Verify appmgrclient_ Is not nullptr causes TerminateAbility to success
315  */
316 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_007, TestSize.Level1)
317 {
318     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
319 
320     std::string deviceName = "device";
321     std::string abilityName = "FirstAbility";
322     std::string appName = "FirstApp";
323     std::string bundleName = "com.ix.First.Test";
324     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
325     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
326     auto token = record->GetToken();
327 
328     EXPECT_EQ((int)ERR_OK, DelayedSingleton<AppScheduler>::GetInstance()->TerminateAbility(token, false));
329 }
330 
331 /*
332  * Feature: AppScheduler
333  * Function: MoveToForeground
334  * SubFunction: NA
335  * FunctionPoints: AppScheduler MoveToForeground
336  * EnvConditions:NA
337  * CaseDescription: Verify appmgrclient_ Is null causes movetoforground to be invalid
338  */
339 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_008, TestSize.Level1)
340 {
341     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = nullptr;
342 
343     std::string deviceName = "device";
344     std::string abilityName = "FirstAbility";
345     std::string appName = "FirstApp";
346     std::string bundleName = "com.ix.First.Test";
347     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
348     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
349     auto token = record->GetToken();
350     ASSERT_NE(token, nullptr);
351 
352     DelayedSingleton<AppScheduler>::GetInstance()->MoveToForeground(token);
353 }
354 
355 /*
356  * Feature: AppScheduler
357  * Function: MoveToForeground
358  * SubFunction: NA
359  * FunctionPoints: AppScheduler MoveToForeground
360  * EnvConditions:NA
361  * CaseDescription: Verify the normal process of movetoforground
362  */
363 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_009, TestSize.Level1)
364 {
365     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
366 
367     std::string deviceName = "device";
368     std::string abilityName = "FirstAbility";
369     std::string appName = "FirstApp";
370     std::string bundleName = "com.ix.First.Test";
371     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
372     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
373     auto token = record->GetToken();
374     ASSERT_NE(token, nullptr);
375 
376     DelayedSingleton<AppScheduler>::GetInstance()->MoveToForeground(token);
377 }
378 
379 /*
380  * Feature: AppScheduler
381  * Function: MoveToBackground
382  * SubFunction: NA
383  * FunctionPoints: AppScheduler MoveToBackground
384  * EnvConditions:NA
385  * CaseDescription: Verify appmgrclient_ Is null causes OnAbilityRequestDone to be invalid
386  */
387 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_010, TestSize.Level1)
388 {
389     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = nullptr;
390 
391     std::string deviceName = "device";
392     std::string abilityName = "FirstAbility";
393     std::string appName = "FirstApp";
394     std::string bundleName = "com.ix.First";
395     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
396     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
397     auto token = record->GetToken();
398     ASSERT_NE(token, nullptr);
399 
400     DelayedSingleton<AppScheduler>::GetInstance()->MoveToBackground(token);
401 }
402 
403 /*
404  * Feature: AppScheduler
405  * Function: MoveToBackground GetAbilityState
406  * SubFunction: NA
407  * FunctionPoints: AppScheduler MoveToBackground and GetAbilityState
408  * EnvConditions:NA
409  * CaseDescription: Verify appmgrclient_ Is not nullptr causes onabilityrequestdone invoke
410  */
411 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_011, TestSize.Level1)
412 {
413     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
414     std::string deviceName = "device";
415     std::string abilityName = "FirstAbility";
416     std::string appName = "FirstApp";
417     std::string bundleName = "com.ix.First";
418     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
419     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
420     auto token = record->GetToken();
421 
422     DelayedSingleton<AppScheduler>::GetInstance()->MoveToBackground(token);
423     EXPECT_EQ(
424         AppAbilityState::ABILITY_STATE_UNDEFINED, DelayedSingleton<AppScheduler>::GetInstance()->GetAbilityState());
425 }
426 
427 /*
428  * Feature: AppScheduler
429  * Function: ConvertToAppAbilityState
430  * SubFunction: NA
431  * FunctionPoints: AppScheduler ConvertToAppAbilityState
432  * EnvConditions:NA
433  * CaseDescription: Verify ConvertToAppAbilityState result
434  */
435 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_012, TestSize.Level1)
436 {
437     EXPECT_EQ(AppAbilityState::ABILITY_STATE_FOREGROUND,
438         DelayedSingleton<AppScheduler>::GetInstance()->ConvertToAppAbilityState(
439             static_cast<int>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND)));
440 
441     EXPECT_EQ(AppAbilityState::ABILITY_STATE_BACKGROUND,
442         DelayedSingleton<AppScheduler>::GetInstance()->ConvertToAppAbilityState(
443             static_cast<int>(AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND)));
444 
445     EXPECT_EQ(AppAbilityState::ABILITY_STATE_UNDEFINED,
446         DelayedSingleton<AppScheduler>::GetInstance()->ConvertToAppAbilityState(
447             static_cast<int>(AppExecFwk::AbilityState::ABILITY_STATE_CREATE)));
448 }
449 
450 /*
451  * Feature: AppScheduler
452  * Function: ConvertToAppAbilityState
453  * SubFunction: NA
454  * FunctionPoints: AppScheduler ConvertToAppAbilityState
455  * EnvConditions:NA
456  * CaseDescription: Verify ConvertToAppAbilityState result
457  */
458 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_013, TestSize.Level1)
459 {
460     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = nullptr;
461     EXPECT_EQ(false, DelayedSingleton<AppScheduler>::GetInstance()->Init(appStateMock_));
462 }
463 
464 /*
465  * Feature: AppScheduler
466  * Function: AbilityBehaviorAnalysis
467  * SubFunction: NA
468  * FunctionPoints: AppScheduler AbilityBehaviorAnalysis
469  * EnvConditions:NA
470  * CaseDescription: Verify appmgrclient_ Is not nullptr causes AbilityBehaviorAnalysis to success
471  */
472 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_014, TestSize.Level1)
473 {
474     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
475 
476     std::string deviceName = "device";
477     std::string abilityName = "FirstAbility";
478     std::string appName = "FirstApp";
479     std::string bundleName = "com.ix.First";
480     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
481     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
482     auto token = record->GetToken();
483     ASSERT_NE(token, nullptr);
484     const int32_t visibility = 1;
485     const int32_t perceptibility = 1;
486     const int32_t connectionState = 1;
487 
488     DelayedSingleton<AppScheduler>::GetInstance()->AbilityBehaviorAnalysis(
489         token, nullptr, visibility, perceptibility, connectionState);
490 
491     auto pretoken = record->GetToken();
492     DelayedSingleton<AppScheduler>::GetInstance()->AbilityBehaviorAnalysis(
493         token, pretoken, visibility, perceptibility, connectionState);
494 
495     const int32_t visibility_1 = 0;
496     DelayedSingleton<AppScheduler>::GetInstance()->AbilityBehaviorAnalysis(
497         token, token, visibility_1, perceptibility, connectionState);
498 
499     const int32_t perceptibility_1 = 0;
500     DelayedSingleton<AppScheduler>::GetInstance()->AbilityBehaviorAnalysis(
501         token, token, visibility_1, perceptibility_1, connectionState);
502 
503     const int32_t connectionState_1 = 0;
504     DelayedSingleton<AppScheduler>::GetInstance()->AbilityBehaviorAnalysis(
505         token, token, visibility_1, perceptibility_1, connectionState_1);
506 }
507 
508 /*
509  * Feature: AppScheduler
510  * Function: AbilityBehaviorAnalysis
511  * SubFunction: NA
512  * FunctionPoints: AppScheduler AbilityBehaviorAnalysis
513  * EnvConditions:NA
514  * CaseDescription: Verify appmgrclient_ Is nullptr causes AbilityBehaviorAnalysis to fail
515  */
516 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_015, TestSize.Level1)
517 {
518     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = nullptr;
519 
520     std::string deviceName = "device";
521     std::string abilityName = "FirstAbility";
522     std::string appName = "FirstApp";
523     std::string bundleName = "com.ix.First";
524     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
525     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
526     auto token = record->GetToken();
527     ASSERT_NE(token, nullptr);
528     const int32_t visibility = 0;
529     const int32_t perceptibility = 1;
530     const int32_t connectionState = 1;
531 
532     DelayedSingleton<AppScheduler>::GetInstance()->AbilityBehaviorAnalysis(
533         token, nullptr, visibility, perceptibility, connectionState);
534 }
535 
536 /*
537  * Feature: AppScheduler
538  * Function: KillProcessByAbilityToken
539  * SubFunction: NA
540  * FunctionPoints: AppScheduler KillProcessByAbilityToken
541  * EnvConditions:NA
542  * CaseDescription: Verify appmgrclient_ Is not nullptr causes KillProcessByAbilityToken to success
543  */
544 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_016, TestSize.Level1)
545 {
546     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
547 
548     std::string deviceName = "device";
549     std::string abilityName = "FirstAbility";
550     std::string appName = "FirstApp";
551     std::string bundleName = "com.ix.First";
552     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
553     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
554     auto token = record->GetToken();
555     ASSERT_NE(token, nullptr);
556 
557     DelayedSingleton<AppScheduler>::GetInstance()->KillProcessByAbilityToken(token);
558 }
559 
560 /*
561  * Feature: AppScheduler
562  * Function: KillProcessByAbilityToken
563  * SubFunction: NA
564  * FunctionPoints: AppScheduler KillProcessByAbilityToken
565  * EnvConditions:NA
566  * CaseDescription: Verify appmgrclient_ Is nullptr causes KillProcessByAbilityToken to fail
567  */
568 HWTEST_F(AppSchedulerTest, AppScheduler_oprator_017, TestSize.Level1)
569 {
570     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = nullptr;
571 
572     std::string deviceName = "device";
573     std::string abilityName = "FirstAbility";
574     std::string appName = "FirstApp";
575     std::string bundleName = "com.ix.First";
576     auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName);
577     auto record = AbilityRecord::CreateAbilityRecord(abilityReq);
578     auto token = record->GetToken();
579     ASSERT_NE(token, nullptr);
580 
581     DelayedSingleton<AppScheduler>::GetInstance()->KillProcessByAbilityToken(token);
582 }
583 
584 /*
585  * Feature: AppScheduler
586  * Function: UpdateAbilityState
587  * SubFunction: NA
588  * FunctionPoints: AppScheduler UpdateAbilityState
589  * EnvConditions: NA
590  * CaseDescription: Verify UpdateAbilityState
591  */
592 HWTEST_F(AppSchedulerTest, AppScheduler_UpdateAbilityState_001, TestSize.Level1)
593 {
594     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
595     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_, nullptr);
596     sptr<IRemoteObject> token = nullptr;
597     AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_CREATE;
598     DelayedSingleton<AppScheduler>::GetInstance()->UpdateAbilityState(token, state);
599 }
600 
601 /*
602  * Feature: AppScheduler
603  * Function: UpdateExtensionState
604  * SubFunction: NA
605  * FunctionPoints: AppScheduler UpdateExtensionState
606  * EnvConditions: NA
607  * CaseDescription: Verify UpdateExtensionState
608  */
609 HWTEST_F(AppSchedulerTest, AppScheduler_UpdateExtensionState_001, TestSize.Level1)
610 {
611     EXPECT_CALL(*clientMock_, UpdateExtensionState(_, _)).Times(1)
612         .WillOnce(Return(AppMgrResultCode::RESULT_OK));
613     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
614     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_, nullptr);
615     sptr<IRemoteObject> token = nullptr;
616     AppExecFwk::ExtensionState state = AppExecFwk::ExtensionState::EXTENSION_STATE_READY;
617     DelayedSingleton<AppScheduler>::GetInstance()->UpdateExtensionState(token, state);
618 }
619 
620 /*
621  * Feature: AppScheduler
622  * Function: KillProcessesByUserId
623  * SubFunction: NA
624  * FunctionPoints: AppScheduler KillProcessesByUserId
625  * EnvConditions: NA
626  * CaseDescription: Verify KillProcessesByUserId
627  */
628 HWTEST_F(AppSchedulerTest, AppScheduler_KillProcessesByUserId_001, TestSize.Level1)
629 {
630     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
631     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_, nullptr);
632     int32_t userId = 0;
633     DelayedSingleton<AppScheduler>::GetInstance()->KillProcessesByUserId(userId);
634 }
635 
636 /*
637  * Feature: AppScheduler
638  * Function: OnAbilityRequestDone
639  * SubFunction: NA
640  * FunctionPoints: AppScheduler OnAbilityRequestDone
641  * EnvConditions: NA
642  * CaseDescription: Verify OnAbilityRequestDone
643  */
644 HWTEST_F(AppSchedulerTest, AppScheduler_OnAbilityRequestDone_001, TestSize.Level1)
645 {
646     sptr<IRemoteObject> token = nullptr;
647     AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_CREATE;
648     ASSERT_NE(appStateMock_, nullptr);
649     DelayedSingleton<AppScheduler>::GetInstance()->callback_ = appStateMock_;
650     DelayedSingleton<AppScheduler>::GetInstance()->OnAbilityRequestDone(token, state);
651 }
652 
653 /*
654  * Feature: AppScheduler
655  * Function: NotifyStartResidentProcess
656  * SubFunction: NA
657  * FunctionPoints: AppScheduler NotifyStartResidentProcess
658  * EnvConditions: NA
659  * CaseDescription: Verify NotifyStartResidentProcess
660  */
661 HWTEST_F(AppSchedulerTest, AppScheduler_NotifyStartResidentProcess_001, TestSize.Level1)
662 {
663     std::vector<AppExecFwk::BundleInfo> bundleInfos;
664     ASSERT_NE(appStateMock_, nullptr);
665     DelayedSingleton<AppScheduler>::GetInstance()->callback_ = appStateMock_;
666     DelayedSingleton<AppScheduler>::GetInstance()->NotifyStartResidentProcess(bundleInfos);
667 }
668 
669 /*
670  * Feature: AppScheduler
671  * Function: KillApplication
672  * SubFunction: NA
673  * FunctionPoints: AppScheduler KillApplication
674  * EnvConditions: NA
675  * CaseDescription: Verify KillApplication
676  */
677 HWTEST_F(AppSchedulerTest, AppScheduler_KillApplication_001, TestSize.Level1)
678 {
679     EXPECT_CALL(*clientMock_, KillApplication(_, _)).Times(1)
680         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
681     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
682     std::string bundleName = "bundleName";
683     int res = DelayedSingleton<AppScheduler>::GetInstance()->KillApplication(bundleName);
684     EXPECT_EQ(res, INNER_ERR);
685 }
686 
687 /*
688  * Feature: AppScheduler
689  * Function: KillApplication
690  * SubFunction: NA
691  * FunctionPoints: AppScheduler KillApplication
692  * EnvConditions: NA
693  * CaseDescription: Verify KillApplication
694  */
695 HWTEST_F(AppSchedulerTest, AppScheduler_KillApplication_002, TestSize.Level1)
696 {
697     EXPECT_CALL(*clientMock_, KillApplication(_, _)).Times(1)
698         .WillOnce(Return(AppMgrResultCode::RESULT_OK));
699     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
700     std::string bundleName = "bundleName";
701     int res = DelayedSingleton<AppScheduler>::GetInstance()->KillApplication(bundleName);
702     EXPECT_EQ(res, ERR_OK);
703 }
704 
705 /*
706  * Feature: AppScheduler
707  * Function: KillApplicationByUid
708  * SubFunction: NA
709  * FunctionPoints: AppScheduler KillApplicationByUid
710  * EnvConditions: NA
711  * CaseDescription: Verify KillApplicationByUid
712  */
713 HWTEST_F(AppSchedulerTest, AppScheduler_KillApplicationByUid_001, TestSize.Level1)
714 {
715     EXPECT_CALL(*clientMock_, KillApplicationByUid(_, _, _)).Times(1)
716         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
717     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
718     std::string bundleName = "bundleName";
719     int32_t uid = 0;
720     int res = DelayedSingleton<AppScheduler>::GetInstance()->KillApplicationByUid(bundleName, uid);
721     EXPECT_EQ(res, INNER_ERR);
722 }
723 
724 /*
725  * Feature: AppScheduler
726  * Function: KillApplicationByUid
727  * SubFunction: NA
728  * FunctionPoints: AppScheduler KillApplicationByUid
729  * EnvConditions: NA
730  * CaseDescription: Verify KillApplicationByUid
731  */
732 HWTEST_F(AppSchedulerTest, AppScheduler_KillApplicationByUid_002, TestSize.Level1)
733 {
734     EXPECT_CALL(*clientMock_, KillApplicationByUid(_, _, _)).Times(1)
735         .WillOnce(Return(AppMgrResultCode::RESULT_OK));
736     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
737     std::string bundleName = "bundleName";
738     int32_t uid = 0;
739     int res = DelayedSingleton<AppScheduler>::GetInstance()->KillApplicationByUid(bundleName, uid);
740     EXPECT_EQ(res, ERR_OK);
741 }
742 
743 /*
744  * Feature: AppScheduler
745  * Function: PrepareTerminate
746  * SubFunction: NA
747  * FunctionPoints: AppScheduler PrepareTerminate
748  * EnvConditions: NA
749  * CaseDescription: Verify PrepareTerminate
750  */
751 HWTEST_F(AppSchedulerTest, AppScheduler_PrepareTerminate_001, TestSize.Level1)
752 {
753     sptr<IRemoteObject> token = nullptr;
754     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
755     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_, nullptr);
756     DelayedSingleton<AppScheduler>::GetInstance()->PrepareTerminate(token);
757 }
758 
759 /*
760  * Feature: AppScheduler
761  * Function: OnAppStateChanged
762  * SubFunction: NA
763  * FunctionPoints: AppScheduler OnAppStateChanged
764  * EnvConditions: NA
765  * CaseDescription: Verify OnAppStateChanged
766  */
767 HWTEST_F(AppSchedulerTest, AppScheduler_OnAppStateChanged_001, TestSize.Level1)
768 {
769     AppExecFwk::AppProcessData appData;
770     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance(), nullptr);
771     DelayedSingleton<AppScheduler>::GetInstance()->OnAppStateChanged(appData);
772 }
773 
774 /*
775  * Feature: AppScheduler
776  * Function: GetRunningProcessInfoByToken
777  * SubFunction: NA
778  * FunctionPoints: AppScheduler GetRunningProcessInfoByToken
779  * EnvConditions: NA
780  * CaseDescription: Verify GetRunningProcessInfoByToken
781  */
782 HWTEST_F(AppSchedulerTest, AppScheduler_GetRunningProcessInfoByToken_001, TestSize.Level1)
783 {
784     sptr<IRemoteObject> token;
785     AppExecFwk::RunningProcessInfo info;
786     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance(), nullptr);
787     DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByToken(token, info);
788 }
789 
790 /*
791  * Feature: AppScheduler
792  * Function: GetRunningProcessInfoByPid
793  * SubFunction: NA
794  * FunctionPoints: AppScheduler GetRunningProcessInfoByPid
795  * EnvConditions: NA
796  * CaseDescription: Verify GetRunningProcessInfoByPid
797  */
798 HWTEST_F(AppSchedulerTest, AppScheduler_GetRunningProcessInfoByPid_001, TestSize.Level1)
799 {
800     pid_t pid = 0;
801     AppExecFwk::RunningProcessInfo info;
802     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance(), nullptr);
803     DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByPid(pid, info);
804 }
805 
806 /*
807  * Feature: AppScheduler
808  * Function: StartupResidentProcess
809  * SubFunction: NA
810  * FunctionPoints: AppScheduler StartupResidentProcess
811  * EnvConditions: NA
812  * CaseDescription: Verify StartupResidentProcess
813  */
814 HWTEST_F(AppSchedulerTest, AppScheduler_StartupResidentProcess_001, TestSize.Level1)
815 {
816     EXPECT_CALL(*clientMock_, StartupResidentProcess(_)).Times(1);
817     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
818     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_, nullptr);
819     std::vector<AppExecFwk::BundleInfo> bundleInfos;
820     DelayedSingleton<AppScheduler>::GetInstance()->StartupResidentProcess(bundleInfos);
821 }
822 
823 /*
824  * Feature: AppScheduler
825  * Function: StartSpecifiedAbility
826  * SubFunction: NA
827  * FunctionPoints: AppScheduler StartSpecifiedAbility
828  * EnvConditions: NA
829  * CaseDescription: Verify StartSpecifiedAbility
830  */
831 HWTEST_F(AppSchedulerTest, AppScheduler_StartSpecifiedAbility_001, TestSize.Level1)
832 {
833     EXPECT_CALL(*clientMock_, StartSpecifiedAbility(_, _, _)).Times(1);
834     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
835     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_, nullptr);
836     AAFwk::Want want;
837     AppExecFwk::AbilityInfo abilityInfo;
838     DelayedSingleton<AppScheduler>::GetInstance()->StartSpecifiedAbility(want, abilityInfo);
839 }
840 
841 /*
842  * Feature: AppScheduler
843  * Function: GetProcessRunningInfos
844  * SubFunction: NA
845  * FunctionPoints: AppScheduler GetProcessRunningInfos
846  * EnvConditions: NA
847  * CaseDescription: Verify GetProcessRunningInfos
848  */
849 HWTEST_F(AppSchedulerTest, AppScheduler_GetProcessRunningInfos_001, TestSize.Level1)
850 {
851     EXPECT_CALL(*clientMock_, GetAllRunningProcesses(_)).Times(1);
852     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
853     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_, nullptr);
854     std::vector<AppExecFwk::RunningProcessInfo> info;
855     DelayedSingleton<AppScheduler>::GetInstance()->GetProcessRunningInfos(info);
856 }
857 
858 /*
859  * Feature: AppScheduler
860  * Function: GetProcessRunningInfosByUserId
861  * SubFunction: NA
862  * FunctionPoints: AppScheduler GetProcessRunningInfosByUserId
863  * EnvConditions: NA
864  * CaseDescription: Verify GetProcessRunningInfosByUserId
865  */
866 HWTEST_F(AppSchedulerTest, AppScheduler_GetProcessRunningInfosByUserId_001, TestSize.Level1)
867 {
868     EXPECT_CALL(*clientMock_, GetProcessRunningInfosByUserId(_, _)).Times(1);
869     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
870     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_, nullptr);
871     std::vector<AppExecFwk::RunningProcessInfo> info;
872     int32_t userId = 0;
873     DelayedSingleton<AppScheduler>::GetInstance()->GetProcessRunningInfosByUserId(info, userId);
874 }
875 
876 /*
877  * Feature: AppScheduler
878  * Function: ConvertAppState
879  * SubFunction: NA
880  * FunctionPoints: AppScheduler ConvertAppState
881  * EnvConditions: NA
882  * CaseDescription: Verify ConvertAppState
883  */
884 HWTEST_F(AppSchedulerTest, AppScheduler_ConvertAppState_001, TestSize.Level1)
885 {
886     AppState state = AppState::BEGIN;
887     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance(), nullptr);
888     DelayedSingleton<AppScheduler>::GetInstance()->ConvertAppState(state);
889 }
890 
891 /*
892  * Feature: AppScheduler
893  * Function: StartUserTest
894  * SubFunction: NA
895  * FunctionPoints: AppScheduler StartUserTest
896  * EnvConditions: NA
897  * CaseDescription: Verify StartUserTest
898  */
899 HWTEST_F(AppSchedulerTest, AppScheduler_StartUserTest_001, TestSize.Level1)
900 {
901     EXPECT_CALL(*clientMock_, StartUserTestProcess(_, _, _, _)).Times(1)
902         .WillOnce(Return(AppMgrResultCode::RESULT_OK));
903     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
904     Want want;
905     sptr<IRemoteObject> observer;
906     AppExecFwk::BundleInfo bundleInfo;
907     int32_t userId = 0;
908     int res = DelayedSingleton<AppScheduler>::GetInstance()->StartUserTest(want, observer, bundleInfo, userId);
909     EXPECT_EQ(res, ERR_OK);
910 }
911 
912 /*
913  * Feature: AppScheduler
914  * Function: StartUserTest
915  * SubFunction: NA
916  * FunctionPoints: AppScheduler StartUserTest
917  * EnvConditions: NA
918  * CaseDescription: Verify StartUserTest
919  */
920 HWTEST_F(AppSchedulerTest, AppScheduler_StartUserTest_002, TestSize.Level1)
921 {
922     EXPECT_CALL(*clientMock_, StartUserTestProcess(_, _, _, _)).Times(1)
923         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
924     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
925     Want want;
926     sptr<IRemoteObject> observer;
927     AppExecFwk::BundleInfo bundleInfo;
928     int32_t userId = 0;
929     int res = DelayedSingleton<AppScheduler>::GetInstance()->StartUserTest(want, observer, bundleInfo, userId);
930     EXPECT_EQ(res, INNER_ERR);
931 }
932 
933 /*
934  * Feature: AppScheduler
935  * Function: FinishUserTest
936  * SubFunction: NA
937  * FunctionPoints: AppScheduler FinishUserTest
938  * EnvConditions: NA
939  * CaseDescription: Verify FinishUserTest
940  */
941 HWTEST_F(AppSchedulerTest, AppScheduler_FinishUserTest_001, TestSize.Level1)
942 {
943     EXPECT_CALL(*clientMock_, FinishUserTest(_, _, _)).Times(1)
944         .WillOnce(Return(AppMgrResultCode::RESULT_OK));
945     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
946     std::string msg = "msg";
947     int64_t resultCode = 0;
948     std::string bundleName = "bundleName";
949     int res = DelayedSingleton<AppScheduler>::GetInstance()->FinishUserTest(msg, resultCode, bundleName);
950     EXPECT_EQ(res, ERR_OK);
951 }
952 
953 /*
954  * Feature: AppScheduler
955  * Function: FinishUserTest
956  * SubFunction: NA
957  * FunctionPoints: AppScheduler FinishUserTest
958  * EnvConditions: NA
959  * CaseDescription: Verify FinishUserTest
960  */
961 HWTEST_F(AppSchedulerTest, AppScheduler_FinishUserTest_002, TestSize.Level1)
962 {
963     EXPECT_CALL(*clientMock_, FinishUserTest(_, _, _)).Times(1)
964         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
965     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
966     std::string msg = "msg";
967     int64_t resultCode = 0;
968     std::string bundleName = "bundleName";
969     int res = DelayedSingleton<AppScheduler>::GetInstance()->FinishUserTest(msg, resultCode, bundleName);
970     EXPECT_EQ(res, INNER_ERR);
971 }
972 
973 /*
974  * Feature: AppScheduler
975  * Function: UpdateConfiguration
976  * SubFunction: NA
977  * FunctionPoints: AppScheduler UpdateConfiguration
978  * EnvConditions: NA
979  * CaseDescription: Verify UpdateConfiguration
980  */
981 HWTEST_F(AppSchedulerTest, AppScheduler_UpdateConfiguration_001, TestSize.Level1)
982 {
983     EXPECT_CALL(*clientMock_, UpdateConfiguration(_, _)).Times(1)
984         .WillOnce(Return(AppMgrResultCode::RESULT_OK));
985     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
986     AppExecFwk::Configuration config;
987     int res = DelayedSingleton<AppScheduler>::GetInstance()->UpdateConfiguration(config);
988     EXPECT_EQ(res, ERR_OK);
989 }
990 
991 /*
992  * Feature: AppScheduler
993  * Function: UpdateConfiguration
994  * SubFunction: NA
995  * FunctionPoints: AppScheduler UpdateConfiguration
996  * EnvConditions: NA
997  * CaseDescription: Verify UpdateConfiguration
998  */
999 HWTEST_F(AppSchedulerTest, AppScheduler_UpdateConfiguration_002, TestSize.Level1)
1000 {
1001     EXPECT_CALL(*clientMock_, UpdateConfiguration(_, _)).Times(1)
1002         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
1003     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
1004     AppExecFwk::Configuration config;
1005     int res = DelayedSingleton<AppScheduler>::GetInstance()->UpdateConfiguration(config);
1006     EXPECT_EQ(res, INNER_ERR);
1007 }
1008 
1009 /*
1010  * Feature: AppScheduler
1011  * Function: GetConfiguration
1012  * SubFunction: NA
1013  * FunctionPoints: AppScheduler GetConfiguration
1014  * EnvConditions: NA
1015  * CaseDescription: Verify GetConfiguration
1016  */
1017 HWTEST_F(AppSchedulerTest, AppScheduler_GetConfiguration_001, TestSize.Level1)
1018 {
1019     EXPECT_CALL(*clientMock_, GetConfiguration(_)).Times(1)
1020         .WillOnce(Return(AppMgrResultCode::RESULT_OK));
1021     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
1022     AppExecFwk::Configuration config;
1023     int res = DelayedSingleton<AppScheduler>::GetInstance()->GetConfiguration(config);
1024     EXPECT_EQ(res, ERR_OK);
1025 }
1026 
1027 /*
1028  * Feature: AppScheduler
1029  * Function: GetConfiguration
1030  * SubFunction: NA
1031  * FunctionPoints: AppScheduler GetConfiguration
1032  * EnvConditions: NA
1033  * CaseDescription: Verify GetConfiguration
1034  */
1035 HWTEST_F(AppSchedulerTest, AppScheduler_GetConfiguration_002, TestSize.Level1)
1036 {
1037     EXPECT_CALL(*clientMock_, GetConfiguration(_)).Times(1)
1038         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
1039     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
1040     AppExecFwk::Configuration config;
1041     int res = DelayedSingleton<AppScheduler>::GetInstance()->GetConfiguration(config);
1042     EXPECT_EQ(res, INNER_ERR);
1043 }
1044 
1045 /*
1046  * Feature: AppScheduler
1047  * Function: GetAbilityRecordsByProcessID
1048  * SubFunction: NA
1049  * FunctionPoints: AppScheduler GetAbilityRecordsByProcessID
1050  * EnvConditions: NA
1051  * CaseDescription: Verify GetAbilityRecordsByProcessID
1052  */
1053 HWTEST_F(AppSchedulerTest, AppScheduler_GetAbilityRecordsByProcessID_001, TestSize.Level1)
1054 {
1055     EXPECT_CALL(*clientMock_, GetAbilityRecordsByProcessID(_, _)).Times(1)
1056         .WillOnce(Return(AppMgrResultCode::RESULT_OK));
1057     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
1058     int pid = 0;
1059     std::vector<sptr<IRemoteObject>> tokens;
1060     int res = DelayedSingleton<AppScheduler>::GetInstance()->GetAbilityRecordsByProcessID(pid, tokens);
1061     EXPECT_EQ(res, ERR_OK);
1062 }
1063 
1064 /*
1065  * Feature: AppScheduler
1066  * Function: GetAbilityRecordsByProcessID
1067  * SubFunction: NA
1068  * FunctionPoints: AppScheduler GetAbilityRecordsByProcessID
1069  * EnvConditions: NA
1070  * CaseDescription: Verify GetAbilityRecordsByProcessID
1071  */
1072 HWTEST_F(AppSchedulerTest, AppScheduler_GetAbilityRecordsByProcessID_002, TestSize.Level1)
1073 {
1074     EXPECT_CALL(*clientMock_, GetAbilityRecordsByProcessID(_, _)).Times(1)
1075         .WillOnce(Return(AppMgrResultCode::ERROR_SERVICE_NOT_READY));
1076     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::move(clientMock_);
1077     int pid = 0;
1078     std::vector<sptr<IRemoteObject>> tokens;
1079     int res = DelayedSingleton<AppScheduler>::GetInstance()->GetAbilityRecordsByProcessID(pid, tokens);
1080     EXPECT_EQ(res, INNER_ERR);
1081     clientMock_.reset();
1082     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_.reset();
1083 }
1084 
1085 /**
1086  * @tc.name: SetCurrentUserId_001
1087  * @tc.desc: set current userId.
1088  * @tc.type: FUNC
1089  */
1090 HWTEST_F(AppSchedulerTest, AppScheduler_SetCurrentUserId_001, TestSize.Level1)
1091 {
1092     int32_t userId = 0;
1093     DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
1094     ASSERT_NE(DelayedSingleton<AppScheduler>::GetInstance()->appMgrClient_, nullptr);
1095     DelayedSingleton<AppScheduler>::GetInstance()->SetCurrentUserId(userId);
1096 }
1097 
1098 /**
1099  * @tc.name: AppScheduler_NotifyFault_001
1100  * @tc.desc: Verify that the NotifyFault interface calls normally
1101  * @tc.type: FUNC
1102  */
1103 HWTEST_F(AppSchedulerTest, AppScheduler_NotifyFault_001, TestSize.Level1)
1104 {
1105     AppExecFwk::FaultData faultData;
1106     int res = DelayedSingleton<AppScheduler>::GetInstance()->NotifyFault(faultData);
1107     EXPECT_EQ(res, INNER_ERR);
1108 }
1109 
1110 /**
1111  * @tc.name: AppScheduler_RegisterAppDebugListener_001
1112  * @tc.desc: Test the state of RegisterAppDebugListener
1113  * @tc.type: FUNC
1114  */
1115 HWTEST_F(AppSchedulerTest, AppScheduler_RegisterAppDebugListener_001, TestSize.Level1)
1116 {
1117     sptr<AppExecFwk::IAppDebugListener> listener = nullptr;
1118     int res = DelayedSingleton<AppScheduler>::GetInstance()->RegisterAppDebugListener(listener);
1119     EXPECT_EQ(res, INNER_ERR);
1120 }
1121 
1122 /**
1123  * @tc.name: AppScheduler_RegisterAppDebugListener_002
1124  * @tc.desc: Test the state of RegisterAppDebugListener
1125  * @tc.type: FUNC
1126  */
1127 HWTEST_F(AppSchedulerTest, AppScheduler_RegisterAppDebugListener_002, TestSize.Level1)
1128 {
1129     AAFwk::IsMockSaCall::IsMockSaCallWithPermission();
1130     auto listener = new AppDebugListenerStubMock();
1131     int res = DelayedSingleton<AppScheduler>::GetInstance()->RegisterAppDebugListener(listener);
1132     EXPECT_EQ(res, ERR_OK);
1133 }
1134 
1135 /**
1136  * @tc.name: AppScheduler_UnregisterAppDebugListener_001
1137  * @tc.desc: Test the state of UnregisterAppDebugListener
1138  * @tc.type: FUNC
1139  */
1140 HWTEST_F(AppSchedulerTest, AppScheduler_UnregisterAppDebugListener_001, TestSize.Level1)
1141 {
1142     sptr<AppExecFwk::IAppDebugListener> listener = nullptr;
1143     int res = DelayedSingleton<AppScheduler>::GetInstance()->UnregisterAppDebugListener(listener);
1144     EXPECT_EQ(res, INNER_ERR);
1145 }
1146 
1147 /**
1148  * @tc.name: AppScheduler_UnregisterAppDebugListener_002
1149  * @tc.desc: Test the state of UnregisterAppDebugListener
1150  * @tc.type: FUNC
1151  */
1152 HWTEST_F(AppSchedulerTest, AppScheduler_UnregisterAppDebugListener_002, TestSize.Level1)
1153 {
1154     AAFwk::IsMockSaCall::IsMockSaCallWithPermission();
1155     auto listener = new AppDebugListenerStubMock();
1156     int res = DelayedSingleton<AppScheduler>::GetInstance()->UnregisterAppDebugListener(listener);
1157     EXPECT_EQ(res, ERR_OK);
1158 }
1159 
1160 /**
1161  * @tc.name: AppScheduler_AttachAppDebug_001
1162  * @tc.desc: Test the state of AttachAppDebug
1163  * @tc.type: FUNC
1164  */
1165 HWTEST_F(AppSchedulerTest, AppScheduler_AttachAppDebug_001, TestSize.Level1)
1166 {
1167     AAFwk::IsMockSaCall::IsMockSaCallWithPermission();
1168     std::string bundleName = "bundleName";
1169     int res = DelayedSingleton<AppScheduler>::GetInstance()->AttachAppDebug(bundleName);
1170     EXPECT_EQ(res, ERR_OK);
1171 }
1172 
1173 /**
1174  * @tc.name: AppScheduler_DetachAppDebug_001
1175  * @tc.desc: Test the state of DetachAppDebug
1176  * @tc.type: FUNC
1177  */
1178 HWTEST_F(AppSchedulerTest, AppScheduler_DetachAppDebug_001, TestSize.Level1)
1179 {
1180     std::string bundleName = "bundleName";
1181     int res = DelayedSingleton<AppScheduler>::GetInstance()->DetachAppDebug(bundleName);
1182     EXPECT_EQ(res, ERR_OK);
1183 }
1184 
1185 /**
1186  * @tc.name: AppScheduler_RegisterAbilityDebugResponse_001
1187  * @tc.desc: Test the state of RegisterAbilityDebugResponse
1188  * @tc.type: FUNC
1189  */
1190 HWTEST_F(AppSchedulerTest, AppScheduler_RegisterAbilityDebugResponse_001, TestSize.Level1)
1191 {
1192     sptr<AppExecFwk::IAbilityDebugResponse> response = nullptr;
1193     int res = DelayedSingleton<AppScheduler>::GetInstance()->RegisterAbilityDebugResponse(response);
1194     EXPECT_EQ(res, INNER_ERR);
1195 }
1196 }  // namespace AAFwk
1197 }  // namespace OHOS
1198