1 /*
2  * Copyright (c) 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 <functional>
17 #include <gtest/gtest.h>
18 #define private public
19 #include "work_scheduler_service.h"
20 #include "work_status.h"
21 #include "work_bundle_group_change_callback.h"
22 #include "work_scheduler_connection.h"
23 #include "work_queue_event_handler.h"
24 #include "conditions/battery_level_listener.h"
25 #include "common_event_manager.h"
26 #include "common_event_support.h"
27 #include "battery_info.h"
28 #include "conditions/battery_status_listener.h"
29 #include "conditions/charger_listener.h"
30 #include "event_publisher.h"
31 #include "json/json.h"
32 
33 #ifdef DEVICE_USAGE_STATISTICS_ENABLE
34 #include "bundle_active_client.h"
35 #endif
36 #ifdef DEVICE_STANDBY_ENABLE
37 #include "standby_service_client.h"
38 #include "allow_type.h"
39 #endif
40 #ifdef RESOURCESCHEDULE_BGTASKMGR_ENABLE
41 #include "scheduler_bg_task_subscriber.h"
42 #include "background_task_mgr_helper.h"
43 #include "resource_type.h"
44 #endif
45 #include "work_sched_errors.h"
46 #include "work_sched_hilog.h"
47 
48 #ifdef DEVICE_STANDBY_ENABLE
49 namespace OHOS {
50 namespace DevStandbyMgr {
SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)51 ErrCode StandbyServiceClient::SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
52 {
53     return ERR_OK;
54 }
55 }
56 }
57 #endif
58 
59 #ifdef RESOURCESCHEDULE_BGTASKMGR_ENABLE
60 namespace OHOS {
61 namespace BackgroundTaskMgr {
SubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)62 ErrCode BackgroundTaskMgrHelper::SubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber)
63 {
64     return ERR_OK;
65 }
66 }
67 }
68 #endif
69 
70 #ifdef DEVICE_USAGE_STATISTICS_ENABLE
71 namespace OHOS {
72 namespace DeviceUsageStats {
RegisterAppGroupCallBack(const sptr<IAppGroupCallback> & observer)73 ErrCode BundleActiveClient::RegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer)
74 {
75     return ERR_OK;
76 }
77 }
78 }
79 #endif
80 
81 namespace OHOS {
82 namespace WorkScheduler {
IsBaseAbilityReady()83 bool WorkSchedulerService::IsBaseAbilityReady()
84 {
85     return true;
86 }
87 }
88 }
89 
DecStrongRef(void const * obj)90 void OHOS::RefBase::DecStrongRef(void const* obj) {}
91 
92 using namespace testing::ext;
93 namespace OHOS {
94 namespace WorkScheduler {
95 class WorkSchedulerServiceTest : public testing::Test {
96 public:
SetUpTestCase()97     static void SetUpTestCase() {}
TearDownTestCase()98     static void TearDownTestCase() {}
SetUp()99     void SetUp() {}
TearDown()100     void TearDown() {}
101     static std::shared_ptr<WorkSchedulerService> workSchedulerService_;
102 };
103 
104 std::shared_ptr<WorkSchedulerService> WorkSchedulerServiceTest::workSchedulerService_ =
105     DelayedSingleton<WorkSchedulerService>::GetInstance();
106 
107 class MyWorkSchedulerService : public WorkSchedServiceStub {
StartWork(WorkInfo & workInfo)108     int32_t StartWork(WorkInfo& workInfo) { return 0; }
StopWork(WorkInfo & workInfo)109     int32_t StopWork(WorkInfo& workInfo) { return 0; };
StopAndCancelWork(WorkInfo & workInfo)110     int32_t StopAndCancelWork(WorkInfo& workInfo)  { return 0; }
StopAndClearWorks()111     int32_t StopAndClearWorks() { return 0; }
IsLastWorkTimeout(int32_t workId,bool & result)112     int32_t IsLastWorkTimeout(int32_t workId, bool &result) { return 0; }
ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)113     int32_t ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos) { return 0; }
GetWorkStatus(int32_t & workId,std::shared_ptr<WorkInfo> & workInfo)114     int32_t GetWorkStatus(int32_t &workId, std::shared_ptr<WorkInfo>& workInfo) { return 0; }
GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)115     int32_t GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos) { return 0; }
PauseRunningWorks(int32_t uid)116     int32_t PauseRunningWorks(int32_t uid) {return 0; }
ResumePausedWorks(int32_t uid)117     int32_t ResumePausedWorks(int32_t uid) {return 0; }
SetWorkSchedulerConfig(const std::string & configData,int32_t sourceType)118     int32_t SetWorkSchedulerConfig(const std::string &configData, int32_t sourceType) { return 0; }
119 };
120 /**
121  * @tc.name: onStart_001
122  * @tc.desc: Test WorkSchedulerService OnStart.
123  * @tc.type: FUNC
124  * @tc.require: I8ZDJI
125  */
126 HWTEST_F(WorkSchedulerServiceTest, onStart_001, TestSize.Level1)
127 {
128     workSchedulerService_->OnStart();
129     EXPECT_NE(workSchedulerService_, nullptr);
130 }
131 
132 /**
133  * @tc.name: startWork_001
134  * @tc.desc: Test WorkSchedulerService startWork.
135  * @tc.type: FUNC
136  * @tc.require: I8F08T
137  */
138 HWTEST_F(WorkSchedulerServiceTest, startWork_001, TestSize.Level1)
139 {
140     int32_t ret;
141 
142     workSchedulerService_->ready_ = false;
143     WorkInfo workinfo = WorkInfo();
144     ret = workSchedulerService_->StartWork(workinfo);
145     EXPECT_EQ(ret, E_SERVICE_NOT_READY);
146 
147     workSchedulerService_->ready_ = true;
148     ret = workSchedulerService_->StartWork(workinfo);
149     EXPECT_EQ(ret, E_CHECK_WORKINFO_FAILED);
150 
151     workSchedulerService_->checkBundle_ = false;
152     ret = workSchedulerService_->StartWork(workinfo);
153     EXPECT_EQ(ret, E_REPEAT_CYCLE_TIME_ERR);
154 
155     workinfo.RequestStorageLevel(WorkCondition::Storage::STORAGE_LEVEL_LOW_OR_OKAY);
156     ret = workSchedulerService_->StartWork(workinfo);
157     EXPECT_EQ(ret, 0);
158     ret = workSchedulerService_->StartWork(workinfo);
159     EXPECT_EQ(ret, E_ADD_REPEAT_WORK_ERR);
160 }
161 
162 /**
163  * @tc.name: stopWork_001
164  * @tc.desc: Test WorkSchedulerService stopWork.
165  * @tc.type: FUNC
166  * @tc.require: I8F08T
167  */
168 HWTEST_F(WorkSchedulerServiceTest, stopWork_001, TestSize.Level1)
169 {
170     int32_t ret;
171 
172     WS_HILOGI("WorkSchedulerServiceTest.stopWork_001 begin");
173     workSchedulerService_->ready_ = false;
174     workSchedulerService_->checkBundle_ = true;
175     WorkInfo workinfo = WorkInfo();
176     ret = workSchedulerService_->StopWork(workinfo);
177     EXPECT_EQ(ret, E_SERVICE_NOT_READY);
178 
179     workSchedulerService_->ready_ = true;
180     ret = workSchedulerService_->StopWork(workinfo);
181     EXPECT_EQ(ret, E_CHECK_WORKINFO_FAILED);
182 
183     workSchedulerService_->checkBundle_ = false;
184     ret = workSchedulerService_->StopWork(workinfo);
185     EXPECT_EQ(ret, 0);
186     WS_HILOGI("WorkSchedulerServiceTest.stopWork_001 end");
187 }
188 
189 /**
190  * @tc.name: StopAndCancelWork_001
191  * @tc.desc: Test WorkSchedulerService StopAndCancelWork.
192  * @tc.type: FUNC
193  * @tc.require: I8F08T
194  */
195 HWTEST_F(WorkSchedulerServiceTest, StopAndCancelWork_001, TestSize.Level1)
196 {
197     int32_t ret;
198 
199     WS_HILOGI("WorkSchedulerServiceTest.StopAndCancelWork_001 begin");
200     workSchedulerService_->ready_ = false;
201     workSchedulerService_->checkBundle_ = true;
202     WorkInfo workinfo = WorkInfo();
203     ret = workSchedulerService_->StopAndCancelWork(workinfo);
204     EXPECT_EQ(ret, E_SERVICE_NOT_READY);
205 
206     workSchedulerService_->ready_ = true;
207     ret = workSchedulerService_->StopAndCancelWork(workinfo);
208     EXPECT_EQ(ret, E_CHECK_WORKINFO_FAILED);
209 
210     workSchedulerService_->checkBundle_ = false;
211     ret = workSchedulerService_->StopAndCancelWork(workinfo);
212     EXPECT_EQ(ret, 0);
213     WS_HILOGI("WorkSchedulerServiceTest.StopAndCancelWork_001 end");
214 }
215 
216 /**
217  * @tc.name: StopAndClearWorks_001
218  * @tc.desc: Test WorkSchedulerService StopAndClearWorks.
219  * @tc.type: FUNC
220  * @tc.require: I8F08T
221  */
222 HWTEST_F(WorkSchedulerServiceTest, StopAndClearWorks_001, TestSize.Level1)
223 {
224     int32_t ret;
225 
226     WS_HILOGI("WorkSchedulerServiceTest.StopAndClearWorks_001 begin");
227     workSchedulerService_->ready_ = false;
228     workSchedulerService_->checkBundle_ = true;
229     ret = workSchedulerService_->StopAndClearWorks();
230     EXPECT_EQ(ret, E_SERVICE_NOT_READY);
231 
232     workSchedulerService_->ready_ = true;
233     ret = workSchedulerService_->StopAndClearWorks();
234     EXPECT_EQ(ret, 0);
235     WS_HILOGI("WorkSchedulerServiceTest.StopAndClearWorks_001 end");
236 }
237 
238 /**
239  * @tc.name: IsLastWorkTimeout_001
240  * @tc.desc: Test WorkSchedulerService IsLastWorkTimeout.
241  * @tc.type: FUNC
242  * @tc.require: I8F08T
243  */
244 HWTEST_F(WorkSchedulerServiceTest, IsLastWorkTimeout_001, TestSize.Level1)
245 {
246     bool result;
247     auto ret = workSchedulerService_->IsLastWorkTimeout(1, result);
248     EXPECT_EQ(ret, E_WORK_NOT_EXIST_FAILED);
249 }
250 
251 /**
252  * @tc.name: ObtainAllWorks_001
253  * @tc.desc: Test WorkSchedulerService ObtainAllWorks.
254  * @tc.type: FUNC
255  * @tc.require: IA4HTC
256  */
257 HWTEST_F(WorkSchedulerServiceTest, ObtainAllWorks_001, TestSize.Level1)
258 {
259     std::list<std::shared_ptr<WorkInfo>> workInfos;
260     auto ret = workSchedulerService_->ObtainAllWorks(workInfos);
261     EXPECT_EQ(ret, 0);
262 }
263 
264 /**
265  * @tc.name: GetWorkStatus_001
266  * @tc.desc: Test WorkSchedulerService GetWorkStatus.
267  * @tc.type: FUNC
268  * @tc.require: IA4HTC
269  */
270 HWTEST_F(WorkSchedulerServiceTest, GetWorkStatus_001, TestSize.Level1)
271 {
272     std::shared_ptr<WorkInfo> workInfo = std::make_shared<WorkInfo>();
273     int32_t workId;
274     auto ret = workSchedulerService_->GetWorkStatus(workId, workInfo);
275     EXPECT_EQ(ret, 0);
276 }
277 
278 /**
279  * @tc.name: GetAllRunningWorks_001
280  * @tc.desc: Test WorkSchedulerService GetAllRunningWorks.
281  * @tc.type: FUNC
282  * @tc.require: I8F08T
283  */
284 HWTEST_F(WorkSchedulerServiceTest, GetAllRunningWorks_001, TestSize.Level1)
285 {
286     std::list<std::shared_ptr<WorkInfo>> workInfos;
287 
288     auto ret = workSchedulerService_->GetAllRunningWorks(workInfos);
289     EXPECT_EQ(ret, E_INVALID_PROCESS_NAME);
290 }
291 
292 /**
293  * @tc.name: Datashare_001
294  * @tc.desc: Test Datashare
295  * @tc.type: FUNC
296  * @tc.require: I8ZDJI
297  */
298 HWTEST_F(WorkSchedulerServiceTest, Datashare_001, TestSize.Level1)
299 {
300     WS_HILOGI("====== test begin ====== ");
301     std::vector<std::string> argsInStr;
302     argsInStr.push_back("-k");
303     argsInStr.push_back("settings.power.suspend_sources");
304     std::string result;
305     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
306     WS_HILOGI("%{public}s", result.c_str());
307     EXPECT_EQ(result.empty(), 0);
308     WS_HILOGI("====== test end ====== ");
309 }
310 
311 HWTEST_F(WorkSchedulerServiceTest, ListenerStart_001, TestSize.Level1)
312 {
313     WS_HILOGI("====== ListenerStart_001 begin====== ");
314     for (auto pair : workSchedulerService_->workQueueManager_->listenerMap_)
315     {
316         pair.second->Start();
317     }
318     std::vector<std::pair<string, string>> infos = {
319         {"event", "info"},
320         {"network", "wifi"},
321         {"network", "disconnect"},
322         {"network", "invalid"},
323         {"charging", "usb"},
324         {"charging", "ac"},
325         {"charging", "wireless"},
326         {"charging", "none"},
327         {"charging", "invalid"},
328         {"storage", "low"},
329         {"storage", "ok"},
330         {"storage", "invalid"},
331         {"batteryStatus", "low"},
332         {"batteryStatus", "ok"},
333         {"batteryStatus", "invalid"},
334     };
335     EventPublisher eventPublisher;
336     for (auto it : infos) {
337         std::string result;
338         std::string eventType = it.first;
339         std::string eventValue = it.second;
340         eventPublisher.Dump(result, eventType, eventValue);
341         WS_HILOGI("%{public}s", result.c_str());
342         EXPECT_EQ(!result.empty(), true);
343     }
344     WS_HILOGI("====== ListenerStart_001 end ====== ");
345 }
346 
347 HWTEST_F(WorkSchedulerServiceTest, Dump_001, TestSize.Level1)
348 {
349     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_001 begin ====== ");
350     std::vector<std::string> argsInStr;
351     std::string result;
352     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
353     WS_HILOGI("%{public}s", result.c_str());
354 
355     argsInStr.clear();
356     result.clear();
357     argsInStr.push_back("-h");
358     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
359     WS_HILOGI("%{public}s", result.c_str());
360 
361     result.clear();
362     argsInStr.clear();
363     argsInStr.push_back("-a");
364     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
365     WS_HILOGI("%{public}s", result.c_str());
366 
367     result.clear();
368     argsInStr.clear();
369     argsInStr.push_back("-x");
370     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
371     WS_HILOGI("%{public}s", result.c_str());
372 
373     result.clear();
374     argsInStr.clear();
375     argsInStr.push_back("-memory");
376     argsInStr.push_back("100");
377     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
378     WS_HILOGI("%{public}s", result.c_str());
379 
380     result.clear();
381     argsInStr.clear();
382     argsInStr.push_back("-watchdog_time");
383     argsInStr.push_back("100");
384     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
385     WS_HILOGI("%{public}s", result.c_str());
386     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_001 end ====== ");
387 }
388 
389 HWTEST_F(WorkSchedulerServiceTest, Dump_002, TestSize.Level1)
390 {
391     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_002 begin ====== ");
392     std::vector<std::string> argsInStr;
393     std::string result;
394     argsInStr.push_back("-repeat_time_min");
395     argsInStr.push_back("100");
396     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
397     WS_HILOGI("%{public}s", result.c_str());
398 
399     result.clear();
400     argsInStr.clear();
401     argsInStr.push_back("-min_interval");
402     argsInStr.push_back("100");
403     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
404     WS_HILOGI("%{public}s", result.c_str());
405 
406     result.clear();
407     argsInStr.clear();
408     argsInStr.push_back("-test");
409     argsInStr.push_back("100");
410     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
411     WS_HILOGI("%{public}s", result.c_str());
412     EXPECT_EQ(result.empty(), false);
413     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_002 end ====== ");
414 }
415 
416 HWTEST_F(WorkSchedulerServiceTest, Dump_003, TestSize.Level1)
417 {
418     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_003 begin ====== ");
419     std::vector<std::string> argsInStr;
420     std::string result;
421 
422     argsInStr.clear();
423     result.clear();
424     argsInStr.push_back("-d");
425     argsInStr.push_back("storage");
426     argsInStr.push_back("ok");
427     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
428     WS_HILOGI("%{public}s", result.c_str());
429 
430     argsInStr.clear();
431     result.clear();
432     argsInStr.push_back("-t");
433     argsInStr.push_back("bundlename");
434     argsInStr.push_back("abilityname");
435     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
436     WS_HILOGI("%{public}s", result.c_str());
437 
438     WorkInfo workinfo = WorkInfo();
439     result.clear();
440     workinfo.RequestStorageLevel(WorkCondition::Storage::STORAGE_LEVEL_LOW_OR_OKAY);
441     workinfo.RefreshUid(2);
442     workinfo.SetElement("bundlename", "abilityname");
443     workSchedulerService_->AddWorkInner(workinfo);
444     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
445     WS_HILOGI("%{public}s", result.c_str());
446     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_003 end ====== ");
447 }
448 
449 HWTEST_F(WorkSchedulerServiceTest, Dump_004, TestSize.Level1)
450 {
451     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_004 begin ====== ");
452     std::vector<std::string> argsInStr;
453     std::string result;
454     argsInStr.push_back("-d");
455     argsInStr.push_back("storage");
456     argsInStr.push_back("ok");
457     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
458     WS_HILOGI("%{public}s", result.c_str());
459 
460     argsInStr.clear();
461     argsInStr.push_back("arg0");
462     argsInStr.push_back("arg1");
463     argsInStr.push_back("arg2");
464     argsInStr.push_back("arg3");
465     argsInStr.push_back("arg4");
466     result.clear();
467     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
468     WS_HILOGI("%{public}s", result.c_str());
469     EXPECT_EQ(result.empty(), false);
470     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_004 end ====== ");
471 }
472 
473 /**
474  * @tc.name: Dump_005
475  * @tc.desc: Test WorkSchedulerService Dump.
476  * @tc.type: FUNC
477  * @tc.require: I9RYLE
478  */
479 HWTEST_F(WorkSchedulerServiceTest, Dump_005, TestSize.Level1)
480 {
481     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_005 begin ====== ");
482     std::vector<std::string> argsInStr;
483     std::string result;
484     argsInStr.push_back("-count");
485     argsInStr.push_back("1");
486     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
487     WS_HILOGI("%{public}s", result.c_str());
488     EXPECT_EQ(result.empty(), false);
489     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_005 end ====== ");
490 }
491 
492 /**
493  * @tc.name: Dump_006
494  * @tc.desc: Test WorkSchedulerService Dump.
495  * @tc.type: FUNC
496  * @tc.require: IAHY0B
497  */
498 HWTEST_F(WorkSchedulerServiceTest, Dump_006, TestSize.Level1)
499 {
500     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_006 begin ====== ");
501     std::vector<std::string> argsInStr;
502     std::string result;
503     argsInStr.push_back("-s");
504     argsInStr.push_back("1");
505     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
506     WS_HILOGI("%{public}s", result.c_str());
507     EXPECT_EQ(result.empty(), false);
508 
509     argsInStr.clear();
510     result.clear();
511     argsInStr.push_back("-s");
512     argsInStr.push_back("1");
513     argsInStr.push_back("1");
514     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
515     WS_HILOGI("%{public}s", result.c_str());
516     EXPECT_EQ(result.empty(), false);
517 
518     argsInStr.clear();
519     result.clear();
520     argsInStr.push_back("-s");
521     argsInStr.push_back("1");
522     argsInStr.push_back("true");
523     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
524     WS_HILOGI("%{public}s", result.c_str());
525     EXPECT_EQ(result.empty(), true);
526 
527     argsInStr.clear();
528     result.clear();
529     argsInStr.push_back("-s");
530     argsInStr.push_back("1");
531     argsInStr.push_back("false");
532     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
533     WS_HILOGI("%{public}s", result.c_str());
534     EXPECT_EQ(result.empty(), true);
535     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_006 end ====== ");
536 }
537 
538 /**
539  * @tc.name: Dump_007
540  * @tc.desc: Test WorkSchedulerService Dump.
541  * @tc.type: FUNC
542  * @tc.require: IAJSVG
543  */
544 HWTEST_F(WorkSchedulerServiceTest, Dump_007, TestSize.Level1)
545 {
546     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_007 begin ====== ");
547     std::vector<std::string> argsInStr;
548     std::string result;
549     argsInStr.push_back("-x");
550     argsInStr.push_back("1");
551     argsInStr.push_back("1");
552     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
553     WS_HILOGI("%{public}s", result.c_str());
554     EXPECT_EQ(result.empty(), false);
555 
556     argsInStr.clear();
557     result.clear();
558     argsInStr.push_back("-x");
559     argsInStr.push_back("1");
560     argsInStr.push_back("p");
561     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
562     WS_HILOGI("%{public}s", result.c_str());
563     EXPECT_EQ(result.empty(), false);
564 
565     argsInStr.clear();
566     result.clear();
567     argsInStr.push_back("-x");
568     argsInStr.push_back("1");
569     argsInStr.push_back("r");
570     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
571     WS_HILOGI("%{public}s", result.c_str());
572     EXPECT_EQ(result.empty(), false);
573 
574     argsInStr.clear();
575     result.clear();
576     argsInStr.push_back("-cpu");
577     argsInStr.push_back("1");
578     workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
579     WS_HILOGI("%{public}s", result.c_str());
580     EXPECT_EQ(result.empty(), false);
581     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_007 end ====== ");
582 }
583 
584 /**
585  * @tc.name: Dump_008
586  * @tc.desc: Test WorkSchedulerService Dump.
587  * @tc.type: FUNC
588  * @tc.require: IAJSVG
589  */
590 HWTEST_F(WorkSchedulerServiceTest, Dump_008, TestSize.Level1)
591 {
592     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_008 begin ====== ");
593     std::vector<std::string> argsInStr;
594     std::string result;
595     argsInStr.push_back("-t");
596     argsInStr.push_back("bundlename");
597     argsInStr.push_back("abilityname");
598     workSchedulerService_->DumpProcessForUserMode(argsInStr, result);
599     EXPECT_EQ(result.empty(), true);
600     WS_HILOGI("====== WorkSchedulerServiceTest.Dump_008 end ====== ");
601 }
602 
603 HWTEST_F(WorkSchedulerServiceTest, WorkStandbyStateChangeCallbackTest_001, TestSize.Level1)
604 {
605     WS_HILOGI("====== WorkSchedulerServiceTest.WorkStandbyStateChangeCallbackTest_001 begin ====== ");
606     workSchedulerService_->standbyStateObserver_->OnDeviceIdleMode(true, false);
607     workSchedulerService_->standbyStateObserver_->OnDeviceIdleMode(true, true);
608     workSchedulerService_->standbyStateObserver_->OnDeviceIdleMode(false, true);
609     workSchedulerService_->standbyStateObserver_->OnAllowListChanged(0, "bundlename", 0, true);
610     EXPECT_NE(workSchedulerService_, nullptr);
611     WS_HILOGI("====== WorkSchedulerServiceTest.WorkStandbyStateChangeCallbackTest_001 end ====== ");
612 }
613 
614 HWTEST_F(WorkSchedulerServiceTest, WorkBundleGroupChangeCallback_001, TestSize.Level1)
615 {
616     WS_HILOGI("====== WorkSchedulerServiceTest.WorkBundleGroupChangeCallback_001 begin ====== ");
617     OHOS::DeviceUsageStats::AppGroupCallbackInfo appGroupCallbackInfo1(0, 1, 2, 0, "bundlename");
618     workSchedulerService_->groupObserver_->OnAppGroupChanged(appGroupCallbackInfo1);
619     OHOS::DeviceUsageStats::AppGroupCallbackInfo appGroupCallbackInfo2(0, 2, 1, 0, "bundlename");
620     workSchedulerService_->groupObserver_->OnAppGroupChanged(appGroupCallbackInfo2);
621     EXPECT_NE(workSchedulerService_, nullptr);
622     WS_HILOGI("====== WorkSchedulerServiceTest.WorkBundleGroupChangeCallback_001 end ====== ");
623 }
624 
625 HWTEST_F(WorkSchedulerServiceTest, WorkSchedulerConnection_001, TestSize.Level1)
626 {
627     WS_HILOGI("====== WorkSchedulerServiceTest.WorkSchedulerConnection_001 begin ====== ");
628     auto workinfo = std::make_shared<WorkInfo>();
629     WorkSchedulerConnection conection(workinfo);
630     AppExecFwk::ElementName element;
631     conection.StopWork();
632     conection.OnAbilityDisconnectDone(element, 0);
633     EXPECT_EQ(conection.proxy_, nullptr);
634     WS_HILOGI("====== WorkSchedulerServiceTest.WorkSchedulerConnection_001 end ====== ");
635 }
636 
637 HWTEST_F(WorkSchedulerServiceTest, SchedulerBgTaskSubscriber_001, TestSize.Level1)
638 {
639     WS_HILOGI("====== WorkSchedulerServiceTest.SchedulerBgTaskSubscriber_001 begin ====== ");
640     SchedulerBgTaskSubscriber subscriber;
641     subscriber.OnProcEfficiencyResourcesApply(nullptr);
642     subscriber.OnProcEfficiencyResourcesReset(nullptr);
643     subscriber.OnAppEfficiencyResourcesApply(nullptr);
644     subscriber.OnAppEfficiencyResourcesReset(nullptr);
645 
646     auto resourceInfo = std::make_shared<BackgroundTaskMgr::ResourceCallbackInfo>(0, 0, 0xFFFF, "name");
647     subscriber.OnProcEfficiencyResourcesApply(resourceInfo);
648     subscriber.OnProcEfficiencyResourcesReset(resourceInfo);
649     subscriber.OnAppEfficiencyResourcesApply(resourceInfo);
650     subscriber.OnAppEfficiencyResourcesReset(resourceInfo);
651     WS_HILOGI("====== WorkSchedulerServiceTest.SchedulerBgTaskSubscriber_001 end ====== ");
652 }
653 
654 HWTEST_F(WorkSchedulerServiceTest, WorkQueueEventHandler_001, TestSize.Level1)
655 {
656     WS_HILOGI("====== WorkSchedulerServiceTest.WorkQueueEventHandler_001 begin ====== ");
657     WorkQueueEventHandler handler(nullptr, nullptr);
658     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(0);
659     handler.ProcessEvent(event);
660     event = AppExecFwk::InnerEvent::Get(1);
661     handler.ProcessEvent(event);
662     event = AppExecFwk::InnerEvent::Get(2);
663     handler.ProcessEvent(event);
664     WS_HILOGI("====== WorkSchedulerServiceTest.WorkQueueEventHandler_001 end ====== ");
665 }
666 
667 HWTEST_F(WorkSchedulerServiceTest, BatteryLevelListener_001, TestSize.Level1)
668 {
669     WS_HILOGI("====== WorkSchedulerServiceTest.BatteryLevelListener_001 begin ====== ");
670 
671     BatteryLevelListener batteryLevelListener(workSchedulerService_->workQueueManager_, workSchedulerService_);
672 
673     batteryLevelListener.Start();
674     EXPECT_NE(batteryLevelListener.commonEventSubscriber, nullptr);
675 
676     EventFwk::CommonEventData data;
677     batteryLevelListener.commonEventSubscriber->OnReceiveEvent(data);
678 
679     EventFwk::Want want;
680     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
681     data.SetWant(want);
682     batteryLevelListener.commonEventSubscriber->OnReceiveEvent(data);
683 
684     want.SetParam(PowerMgr::BatteryInfo::COMMON_EVENT_KEY_CAPACITY, 20);
685     data.SetWant(want);
686     batteryLevelListener.commonEventSubscriber->OnReceiveEvent(data);
687     batteryLevelListener.Stop();
688 
689     WS_HILOGI("====== WorkSchedulerServiceTest.BatteryLevelListener_001 end ====== ");
690 }
691 
692 HWTEST_F(WorkSchedulerServiceTest, BatteryStatusListener_001, TestSize.Level1)
693 {
694     WS_HILOGI("====== WorkSchedulerServiceTest.BatteryStatusListener_001 begin ====== ");
695     BatteryStatusListener batteryStatusListener(workSchedulerService_->workQueueManager_);
696 
697     batteryStatusListener.Start();
698     EXPECT_NE(batteryStatusListener.commonEventSubscriber, nullptr);
699 
700     EventFwk::CommonEventData data;
701     batteryStatusListener.commonEventSubscriber->OnReceiveEvent(data);
702 
703     EventFwk::Want want;
704     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
705     data.SetWant(want);
706     batteryStatusListener.commonEventSubscriber->OnReceiveEvent(data);
707 
708     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
709     data.SetWant(want);
710     batteryStatusListener.commonEventSubscriber->OnReceiveEvent(data);
711     batteryStatusListener.Stop();
712 
713     WS_HILOGI("====== WorkSchedulerServiceTest.BatteryStatusListener_001 end ====== ");
714 }
715 
716 HWTEST_F(WorkSchedulerServiceTest, ChargerListener_001, TestSize.Level1)
717 {
718     WS_HILOGI("====== WorkSchedulerServiceTest.ChargerListener_001 begin ====== ");
719     ChargerListener chargerListener(workSchedulerService_->workQueueManager_);
720 
721     chargerListener.Start();
722     EXPECT_NE(chargerListener.commonEventSubscriber, nullptr);
723 
724     EventFwk::CommonEventData data;
725     chargerListener.commonEventSubscriber->OnReceiveEvent(data);
726 
727     EventFwk::Want want;
728     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
729     data.SetWant(want);
730     data.SetCode(static_cast<uint32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_AC));
731     chargerListener.commonEventSubscriber->OnReceiveEvent(data);
732 
733     data.SetCode(static_cast<uint32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB));
734     chargerListener.commonEventSubscriber->OnReceiveEvent(data);
735 
736 
737     data.SetCode(static_cast<uint32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_WIRELESS));
738     chargerListener.commonEventSubscriber->OnReceiveEvent(data);
739 
740     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
741     data.SetWant(want);
742     data.SetCode(static_cast<uint32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_NONE));
743     chargerListener.commonEventSubscriber->OnReceiveEvent(data);
744     chargerListener.Stop();
745 
746     WS_HILOGI("====== WorkSchedulerServiceTest.ChargerListener_001 end ====== ");
747 }
748 
749 HWTEST_F(WorkSchedulerServiceTest, ListenerStop_001, TestSize.Level1)
750 {
751     WS_HILOGI("====== ListenerStop_001 begin====== ");
752     for (auto pair : workSchedulerService_->workQueueManager_->listenerMap_)
753     {
754         pair.second->Stop();
755     }
756     WS_HILOGI("====== ListenerStop_001 end ====== ");
757 }
758 
759 HWTEST_F(WorkSchedulerServiceTest, WorkSchedServiceStub_001, TestSize.Level1)
760 {
761     MyWorkSchedulerService s;
762     MessageParcel data, reply;
763     MessageOption option;
764     const int size = 11;
765     for (int i = 0; i < size; i++) {
766         s.HandleRequest(i, data, reply, option);
767         WorkInfo info;
768         info.Marshalling(data);
769         s.HandleRequest(i, data, reply, option);
770     }
771     s.OnRemoteRequest(0, data, reply, option);
772 }
773 
774 /**
775  * @tc.name: SendEvent_001
776  * @tc.desc: Test WorkSchedulerService SendEvent.
777  * @tc.type: FUNC
778  * @tc.require: I9J0A7
779  */
780 HWTEST_F(WorkSchedulerServiceTest, SendEvent_001, TestSize.Level1)
781 {
782     int32_t initDelay = 2 * 1000;
783     workSchedulerService_->GetHandler()->
784         SendEvent(AppExecFwk::InnerEvent::Get(WorkEventHandler::SERVICE_INIT_MSG, 0), initDelay);
785     EXPECT_TRUE(workSchedulerService_->ready_);
786 }
787 
788 /**
789  * @tc.name: GetJsonFromFile_001
790  * @tc.desc: Test WorkSchedulerService GetJsonFromFile.
791  * @tc.type: FUNC
792  * @tc.require: I9J0A7
793  */
794 HWTEST_F(WorkSchedulerServiceTest, GetJsonFromFile_001, TestSize.Level1)
795 {
796     Json::Value root;
797     const char* path = "/a/b/c";
798     bool ret = workSchedulerService_->GetJsonFromFile(path, root);
799     EXPECT_FALSE(ret);
800 }
801 
802 /**
803  * @tc.name: GetAppIndexAndBundleNameByUid_001
804  * @tc.desc: Test WorkSchedulerService GetAppIndexAndBundleNameByUid.
805  * @tc.type: FUNC
806  * @tc.require: I9TL53
807  */
808 HWTEST_F(WorkSchedulerServiceTest, GetAppIndexAndBundleNameByUid_001, TestSize.Level1)
809 {
810     int32_t appIndex;
811     std::string bundleName;
812     int32_t uid = 1;
813     bool ret = workSchedulerService_->GetAppIndexAndBundleNameByUid(uid, appIndex, bundleName);
814     EXPECT_FALSE(ret);
815 }
816 
817 /**
818  * @tc.name: LoadSa_001
819  * @tc.desc: Test WorkSchedulerService LoadSa.
820  * @tc.type: FUNC
821  * @tc.require: IAHY0B
822  */
823 HWTEST_F(WorkSchedulerServiceTest, LoadSa_001, TestSize.Level1)
824 {
825     workSchedulerService_->ready_ = false;
826     workSchedulerService_->LoadSa();
827 
828     workSchedulerService_->ready_ = true;
829     workSchedulerService_->saMap_.clear();
830     workSchedulerService_->LoadSa();
831 
832     int32_t saId1 = 401;
833     workSchedulerService_->saMap_.emplace(saId1, true);
834     workSchedulerService_->LoadSa();
835 
836     workSchedulerService_->saMap_.emplace(saId1, false);
837     workSchedulerService_->LoadSa();
838 
839     int32_t saId2 = 5300;
840     workSchedulerService_->saMap_.emplace(saId2, true);
841     workSchedulerService_->LoadSa();
842 
843     workSchedulerService_->saMap_.emplace(saId2, false);
844     workSchedulerService_->LoadSa();
845 }
846 
847 /**
848  * @tc.name: CheckExtensionInfos_001
849  * @tc.desc: Test WorkSchedulerService CheckExtensionInfos.
850  * @tc.type: FUNC
851  * @tc.require: IAJSVG
852  */
853 HWTEST_F(WorkSchedulerServiceTest, CheckExtensionInfos_001, TestSize.Level1)
854 {
855     int32_t uid = 1;
856     WorkInfo workInfo = WorkInfo();
857     workInfo.SetWorkId(1);
858     workInfo.SetElement("bundleName", "abilityName");
859     bool ret = workSchedulerService_->CheckExtensionInfos(workInfo, uid);
860     EXPECT_TRUE(ret);
861 }
862 
863 /**
864  * @tc.name: InitBgTaskSubscriber_001
865  * @tc.desc: Test WorkSchedulerService InitBgTaskSubscriber.
866  * @tc.type: FUNC
867  * @tc.require: IAJSVG
868  */
869 HWTEST_F(WorkSchedulerServiceTest, InitBgTaskSubscriber_001, TestSize.Level1)
870 {
871     bool ret = workSchedulerService_->InitBgTaskSubscriber();
872     EXPECT_TRUE(ret);
873 }
874 
875 /**
876  * @tc.name: AllowDump_001
877  * @tc.desc: Test WorkSchedulerService AllowDump.
878  * @tc.type: FUNC
879  * @tc.require: IAJSVG
880  */
881 HWTEST_F(WorkSchedulerServiceTest, AllowDump_001, TestSize.Level1)
882 {
883     bool ret = workSchedulerService_->AllowDump();
884     EXPECT_FALSE(ret);
885 }
886 
887 /**
888  * @tc.name: UpdateWorkBeforeRealStart_001
889  * @tc.desc: Test WorkSchedulerService UpdateWorkBeforeRealStart.
890  * @tc.type: FUNC
891  * @tc.require: IAJSVG
892  */
893 HWTEST_F(WorkSchedulerServiceTest, UpdateWorkBeforeRealStart_001, TestSize.Level1)
894 {
895     std::shared_ptr<WorkStatus> workStatus = nullptr;
896     workSchedulerService_->UpdateWorkBeforeRealStart(workStatus);
897 
898     WorkInfo workInfo = WorkInfo();
899     workStatus = std::make_shared<WorkStatus>(workInfo, 1);
900     workStatus->conditionMap_.clear();
901     workSchedulerService_->UpdateWorkBeforeRealStart(workStatus);
902 
903     std::shared_ptr<Condition> repeatCycle = std::make_shared<Condition>();
904     repeatCycle->boolVal = true;
905     workStatus->conditionMap_.emplace(WorkCondition::Type::TIMER, repeatCycle);
906     workSchedulerService_->UpdateWorkBeforeRealStart(workStatus);
907 }
908 
909 /**
910  * @tc.name: CheckEffiResApplyInfo_001
911  * @tc.desc: Test WorkSchedulerService CheckEffiResApplyInfo.
912  * @tc.type: FUNC
913  * @tc.require: IAJSVG
914  */
915 HWTEST_F(WorkSchedulerServiceTest, CheckEffiResApplyInfo_001, TestSize.Level1)
916 {
917     bool ret = workSchedulerService_->CheckEffiResApplyInfo(1);
918     EXPECT_FALSE(ret);
919 }
920 
921 /**
922  * @tc.name: PauseRunningWorks_001
923  * @tc.desc: Test WorkSchedulerService PauseRunningWorks.
924  * @tc.type: FUNC
925  * @tc.require: IAJSVG
926  */
927 HWTEST_F(WorkSchedulerServiceTest, PauseRunningWorks_001, TestSize.Level1)
928 {
929     workSchedulerService_->TriggerWorkIfConditionReady();
930     workSchedulerService_->PauseRunningWorks(1);
931     workSchedulerService_->ResumePausedWorks(1);
932 }
933 
934 /**
935  * @tc.name: OnAddSystemAbility_001
936  * @tc.desc: Test WorkSchedulerService OnAddSystemAbility.
937  * @tc.type: FUNC
938  * @tc.require: IAJSVG
939  */
940 HWTEST_F(WorkSchedulerServiceTest, OnAddSystemAbility_001, TestSize.Level1)
941 {
942     std::string deviceId;
943     int32_t DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID = 1914;
944     int32_t DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID = 1907;
945     workSchedulerService_->OnAddSystemAbility(DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID, deviceId);
946     workSchedulerService_->OnRemoveSystemAbility(DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID, deviceId);
947 
948     workSchedulerService_->OnAddSystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID, deviceId);
949     workSchedulerService_->OnRemoveSystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID, deviceId);
950 }
951 
952 /**
953  * @tc.name: IsDebugApp_001
954  * @tc.desc: Test WorkSchedulerService IsDebugApp.
955  * @tc.type: FUNC
956  * @tc.require: IAJSVG
957  */
958 HWTEST_F(WorkSchedulerServiceTest, IsDebugApp_001, TestSize.Level1)
959 {
960     bool ret = workSchedulerService_->IsDebugApp("bundleName");
961     EXPECT_FALSE(ret);
962 }
963 
964 /**
965  * @tc.name: IsPreinstalledBundle_001
966  * @tc.desc: Test WorkSchedulerService IsPreinstalledBundle.
967  * @tc.type: FUNC
968  * @tc.require: IAJSVG
969  */
970 HWTEST_F(WorkSchedulerServiceTest, IsPreinstalledBundle_001, TestSize.Level1)
971 {
972     workSchedulerService_->preinstalledBundles_.clear();
973     std::string bundleName = "com.demo.bundle";
974     bool ret = workSchedulerService_->IsPreinstalledBundle(bundleName);
975     EXPECT_FALSE(ret);
976 }
977 
978 /**
979  * @tc.name: IsPreinstalledBundle_002
980  * @tc.desc: Test WorkSchedulerService IsPreinstalledBundle.
981  * @tc.type: FUNC
982  * @tc.require: IAJSVG
983  */
984 HWTEST_F(WorkSchedulerServiceTest, IsPreinstalledBundle_002, TestSize.Level1)
985 {
986     workSchedulerService_->preinstalledBundles_.clear();
987     std::string bundleName = "com.demo.bundle";
988     workSchedulerService_->preinstalledBundles_.insert(bundleName);
989     bool ret = workSchedulerService_->IsPreinstalledBundle(bundleName);
990     EXPECT_TRUE(ret);
991 }
992 
993 /**
994  * @tc.name: IsPreinstalledBundle_003
995  * @tc.desc: Test WorkSchedulerService IsPreinstalledBundle.
996  * @tc.type: FUNC
997  * @tc.require: IAJSVG
998  */
999 HWTEST_F(WorkSchedulerServiceTest, IsPreinstalledBundle_003, TestSize.Level1)
1000 {
1001     workSchedulerService_->preinstalledBundles_.clear();
1002     std::string bundleName = "com.demo.bundle";
1003     workSchedulerService_->preinstalledBundles_.insert("com.demo.bundle1");
1004     bool ret = workSchedulerService_->IsPreinstalledBundle(bundleName);
1005     EXPECT_FALSE(ret);
1006 }
1007 
1008 /**
1009  * @tc.name: LoadMinRepeatTimeFromFile_001
1010  * @tc.desc: Test WorkSchedulerService LoadMinRepeatTimeFromFile.
1011  * @tc.type: FUNC
1012  * @tc.require: IBC6F3
1013  */
1014 HWTEST_F(WorkSchedulerServiceTest, LoadMinRepeatTimeFromFile_001, TestSize.Level1)
1015 {
1016     workSchedulerService_->specialMap_.clear();
1017     const char* path = nullptr;
1018     workSchedulerService_->LoadMinRepeatTimeFromFile(path);
1019     EXPECT_TRUE(workSchedulerService_->specialMap_.empty());
1020 }
1021 
1022 /**
1023  * @tc.name: LoadMinRepeatTimeFromFile_002
1024  * @tc.desc: Test WorkSchedulerService LoadMinRepeatTimeFromFile.
1025  * @tc.type: FUNC
1026  * @tc.require: IBC6F3
1027  */
1028 HWTEST_F(WorkSchedulerServiceTest, LoadMinRepeatTimeFromFile_002, TestSize.Level1)
1029 {
1030     workSchedulerService_->specialMap_.clear();
1031     const char *path = "/a/b/c";
1032     workSchedulerService_->LoadMinRepeatTimeFromFile(path);
1033     EXPECT_TRUE(workSchedulerService_->specialMap_.empty());
1034 }
1035 }
1036 }