• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 "gtest/hwext/gtest-multithread.h"
18 
19 #include <vector>
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include "nativetoken_kit.h"
23 #include "notifier_mgr.h"
24 #include "plugin_mgr.h"
25 #include "res_common_util.h"
26 #include "res_sched_ipc_interface_code.h"
27 #include "res_sched_common_death_recipient.h"
28 #include "res_sched_service.h"
29 #include "res_sched_service_ability.h"
30 #include "res_sched_systemload_notifier_proxy.h"
31 #include "res_sched_systemload_notifier_stub.h"
32 #include "res_type.h"
33 #include "token_setproc.h"
34 
35 namespace OHOS {
36 namespace ResourceSchedule {
37 using namespace std;
38 using namespace testing::ext;
39 using namespace testing::mt;
40 using namespace Security::AccessToken;
41 class ResSchedServiceTest : public testing::Test {
42 public:
43     static void SetUpTestCase(void);
44     static void TearDownTestCase(void);
45     void SetUp();
46     void TearDown();
47 protected:
48     std::shared_ptr<ResSchedService> resSchedService_ = nullptr;
49     std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = nullptr;
50 };
51 
52 class TestResSchedSystemloadListener : public ResSchedSystemloadNotifierStub {
53 public:
54     TestResSchedSystemloadListener() = default;
55 
OnSystemloadLevel(int32_t level)56     void OnSystemloadLevel(int32_t level)
57     {
58         testSystemloadLevel = level;
59     }
60 
61     static int32_t testSystemloadLevel;
62 };
63 
64 int32_t TestResSchedSystemloadListener::testSystemloadLevel = 0;
65 
SetUpTestCase(void)66 void ResSchedServiceTest::SetUpTestCase(void)
67 {
68     static const char *perms[] = {
69         "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT",
70         "ohos.permission.DUMP",
71     };
72     uint64_t tokenId;
73     NativeTokenInfoParams infoInstance = {
74         .dcapsNum = 0,
75         .permsNum = 2,
76         .aclsNum = 0,
77         .dcaps = nullptr,
78         .perms = perms,
79         .acls = nullptr,
80         .processName = "ResSchedServiceTest",
81         .aplStr = "system_core",
82     };
83     tokenId = GetAccessTokenId(&infoInstance);
84     SetSelfTokenID(tokenId);
85     AccessTokenKit::ReloadNativeTokenInfo();
86 }
87 
TearDownTestCase()88 void ResSchedServiceTest::TearDownTestCase() {}
89 
SetUp()90 void ResSchedServiceTest::SetUp()
91 {
92     /**
93      * @tc.setup: initialize the member variable resSchedServiceAbility_
94      */
95     resSchedService_ = make_shared<ResSchedService>();
96     resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
97 }
98 
TearDown()99 void ResSchedServiceTest::TearDown()
100 {
101     /**
102      * @tc.teardown: clear resSchedServiceAbility_
103      */
104     resSchedService_ = nullptr;
105     resSchedServiceAbility_ = nullptr;
106 }
107 
108 /**
109  * @tc.name: ressched service dump 001
110  * @tc.desc: Verify if ressched service dump commonds is success.
111  * @tc.type: FUNC
112  * @tc.require: issueI5WWV3
113  * @tc.author:lice
114  */
115 HWTEST_F(ResSchedServiceTest, ServiceDump001, Function | MediumTest | Level0)
116 {
117     PluginMgr::GetInstance().Init();
118     std::string result;
119     resSchedService_->DumpAllInfo(result);
120     EXPECT_TRUE(!result.empty());
121 
122     result = "";
123     resSchedService_->DumpUsage(result);
124     EXPECT_TRUE(!result.empty());
125 
126     int32_t wrongFd = -1;
127     std::vector<std::u16string> argsNull;
128     int res = resSchedService_->Dump(wrongFd, argsNull);
129     EXPECT_NE(res, ERR_OK);
130 
131     int32_t correctFd = -1;
132     res = resSchedService_->Dump(correctFd, argsNull);
133 
134     std::vector<std::u16string> argsHelp = {to_utf16("-h")};
135     res = resSchedService_->Dump(correctFd, argsHelp);
136 
137     std::vector<std::u16string> argsAll = {to_utf16("-a")};
138     res = resSchedService_->Dump(correctFd, argsAll);
139 
140     std::vector<std::u16string> argsError = {to_utf16("-e")};
141     res = resSchedService_->Dump(correctFd, argsError);
142 
143     std::vector<std::u16string> argsPlugin = {to_utf16("-p")};
144     res = resSchedService_->Dump(correctFd, argsPlugin);
145 
146     std::vector<std::u16string> argsOnePlugin = {to_utf16("-p"), to_utf16("1")};
147     res = resSchedService_->Dump(correctFd, argsOnePlugin);
148 
149     std::vector<std::u16string> argsOnePlugin1 = {to_utf16("getRunningLockInfo")};
150     res = resSchedService_->Dump(correctFd, argsOnePlugin1);
151 
152     std::vector<std::u16string> argsOnePlugin2 = {to_utf16("getProcessEventInfo")};
153     res = resSchedService_->Dump(correctFd, argsOnePlugin2);
154 
155     std::vector<std::u16string> argsOnePlugin3 = {to_utf16("getProcessWindowInfo")};
156     res = resSchedService_->Dump(correctFd, argsOnePlugin3);
157 
158     std::vector<std::u16string> argsOnePlugin4 = {to_utf16("getSystemloadInfo")};
159     res = resSchedService_->Dump(correctFd, argsOnePlugin4);
160 
161     std::vector<std::u16string> argsOnePlugin5 = {to_utf16("sendDebugToExecutor")};
162     res = resSchedService_->Dump(correctFd, argsOnePlugin5);
163 }
164 
165 /**
166  * @tc.name: Ressched service ReportData 001
167  * @tc.desc: Verify if Ressched service ReportData is success.
168  * @tc.type: FUNC
169  * @tc.require: issueI5WWV3
170  * @tc.author:lice
171  */
172 HWTEST_F(ResSchedServiceTest, Report001, Function | MediumTest | Level0)
173 {
174     nlohmann::json payload;
175     EXPECT_TRUE(resSchedService_ != nullptr);
176     resSchedService_->ReportData(0, 0, payload);
177 }
178 
179 /**
180  * @tc.name: ReportSyncEvent
181  * @tc.desc: test func ReportSyncEvent.
182  * @tc.type: FUNC
183  * @tc.require: I9QN9E
184  */
185 HWTEST_F(ResSchedServiceTest, ReportSyncEvent, Function | MediumTest | Level0)
186 {
187     EXPECT_NE(resSchedService_, nullptr);
188     nlohmann::json payload({{"pid", 100}});
189     nlohmann::json reply;
190     int32_t ret = resSchedService_->ReportSyncEvent(ResType::SYNC_RES_TYPE_THAW_ONE_APP, 0, payload, reply);
191     // 事件分发失败,返回err
192     EXPECT_NE(ret, 0);
193 }
194 
ReportTask()195 static void ReportTask()
196 {
197     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
198     nlohmann::json payload;
199     EXPECT_TRUE(resSchedService_ != nullptr);
200     resSchedService_->ReportData(0, 0, payload);
201 }
202 
203 /**
204  * @tc.name: Ressched service ReportData 002
205  * @tc.desc: Test Ressched service ReportData in multithreading.
206  * @tc.type: FUNC
207  * @tc.require: issueI7G8VT
208  * @tc.author: nizihao
209  */
210 HWTEST_F(ResSchedServiceTest, Report002, Function | MediumTest | Level0)
211 {
212     SET_THREAD_NUM(10);
213     GTEST_RUN_TASK(ReportTask);
214 }
215 
216 /**
217  * @tc.name: Ressched service KillProcess 001
218  * @tc.desc: test the interface service KillProcess
219  * @tc.type: FUNC
220  * @tc.require: issueI8VZVN
221  * @tc.author:z30053169
222  */
223 HWTEST_F(ResSchedServiceTest, KillProcess001, Function | MediumTest | Level0)
224 {
225     nlohmann::json payload;
226     int32_t t = resSchedService_->KillProcess(payload);
227     EXPECT_EQ(t, -1);
228 }
229 
230 /**
231  * @tc.name: Ressched service TestResSchedSystemloadListener 001
232  * @tc.desc: test the interface service TestResSchedSystemloadListener
233  * @tc.type: FUNC
234  * @tc.require: issueI97M6C
235  * @tc.author:shanhaiyang
236  */
237 HWTEST_F(ResSchedServiceTest, TestResSchedSystemloadListener001, Function | MediumTest | Level0)
238 {
239     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
240     EXPECT_TRUE(resSchedService_ != nullptr);
241     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
242     EXPECT_TRUE(notifier != nullptr);
243     NotifierMgr::GetInstance().Init();
244     resSchedService_->RegisterSystemloadNotifier(notifier);
245     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
246     resSchedService_->OnDeviceLevelChanged(0, 2);
247     sleep(1);
248     EXPECT_TRUE(TestResSchedSystemloadListener::testSystemloadLevel == 2);
249     resSchedService_->UnRegisterSystemloadNotifier();
250     NotifierMgr::GetInstance().OnApplicationStateChange(4, IPCSkeleton::GetCallingPid());
251     TestResSchedSystemloadListener::testSystemloadLevel = 0;
252 }
253 
254 /**
255  * @tc.name: Ressched service TestResSchedSystemloadListener 002
256  * @tc.desc: test the interface service TestResSchedSystemloadListener
257  * @tc.type: FUNC
258  * @tc.require: issueI97M6C
259  * @tc.author:shanhaiyang
260  */
261 HWTEST_F(ResSchedServiceTest, TestResSchedSystemloadListener002, Function | MediumTest | Level0)
262 {
263     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
264     EXPECT_TRUE(resSchedService_ != nullptr);
265     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
266     EXPECT_TRUE(notifier != nullptr);
267     NotifierMgr::GetInstance().Init();
268     resSchedService_->RegisterSystemloadNotifier(notifier);
269     resSchedService_->OnDeviceLevelChanged(0, 2);
270     sleep(1);
271     EXPECT_TRUE(TestResSchedSystemloadListener::testSystemloadLevel == 2);
272     resSchedService_->UnRegisterSystemloadNotifier();
273     TestResSchedSystemloadListener::testSystemloadLevel = 0;
274 }
275 
276 /**
277  * @tc.name: Ressched service TestResSchedSystemloadListener 003
278  * @tc.desc: test the interface service TestResSchedSystemloadListener
279  * @tc.type: FUNC
280  * @tc.require: issueI97M6C
281  * @tc.author:shanhaiyang
282  */
283 HWTEST_F(ResSchedServiceTest, TestResSchedSystemloadListener003, Function | MediumTest | Level0)
284 {
285     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
286     EXPECT_TRUE(resSchedService_ != nullptr);
287     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
288     EXPECT_TRUE(notifier != nullptr);
289     NotifierMgr::GetInstance().Init();
290     resSchedService_->RegisterSystemloadNotifier(notifier);
291     NotifierMgr::GetInstance().OnApplicationStateChange(2, 111111);
292     resSchedService_->OnDeviceLevelChanged(0, 2);
293     sleep(1);
294     EXPECT_TRUE(TestResSchedSystemloadListener::testSystemloadLevel == 2);
295     resSchedService_->UnRegisterSystemloadNotifier();
296     NotifierMgr::GetInstance().OnApplicationStateChange(4, 111111);
297     TestResSchedSystemloadListener::testSystemloadLevel = 0;
298 }
299 
300 /**
301  * @tc.name: Ressched service TestResSchedSystemloadListener 004
302  * @tc.desc: test the interface service TestResSchedSystemloadListener
303  * @tc.type: FUNC
304  * @tc.require: issueI97M6C
305  * @tc.author:shanhaiyang
306  */
307 HWTEST_F(ResSchedServiceTest, TestResSchedSystemloadListener004, Function | MediumTest | Level0)
308 {
309     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
310     EXPECT_TRUE(resSchedService_ != nullptr);
311     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
312     EXPECT_TRUE(notifier != nullptr);
313     NotifierMgr::GetInstance().Init();
314     std::string cbType = "systemLoadChange";
315     resSchedService_->RegisterSystemloadNotifier(notifier);
316     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
317     NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
318     resSchedService_->OnDeviceLevelChanged(0, 2);
319     sleep(1);
320     EXPECT_TRUE(TestResSchedSystemloadListener::testSystemloadLevel == 0);
321     resSchedService_->UnRegisterSystemloadNotifier();
322     NotifierMgr::GetInstance().OnApplicationStateChange(4, IPCSkeleton::GetCallingPid());
323     TestResSchedSystemloadListener::testSystemloadLevel = 0;
324 }
325 
326 /**
327  * @tc.name: Ressched service RegisterSystemloadNotifier 001
328  * @tc.desc: test the interface service RegisterSystemloadNotifier
329  * @tc.type: FUNC
330  * @tc.require: issueI97M6C
331  * @tc.author:shanhaiyang
332  */
333 HWTEST_F(ResSchedServiceTest, RegisterSystemloadNotifier001, Function | MediumTest | Level0)
334 {
335     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
336     EXPECT_TRUE(resSchedService_ != nullptr);
337     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
338     EXPECT_TRUE(notifier != nullptr);
339     NotifierMgr::GetInstance().Init();
340     resSchedService_->RegisterSystemloadNotifier(notifier);
341 }
342 
343 /**
344  * @tc.name: Ressched service RegisterSystemloadNotifier 002
345  * @tc.desc: test the interface service RegisterSystemloadNotifier
346  * @tc.type: FUNC
347  * @tc.require: issueI97M6C
348  * @tc.author:shanhaiyang
349  */
350 HWTEST_F(ResSchedServiceTest, RegisterSystemloadNotifier002, Function | MediumTest | Level0)
351 {
352     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
353     EXPECT_TRUE(resSchedService_ != nullptr);
354     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
355     EXPECT_TRUE(notifier != nullptr);
356     NotifierMgr::GetInstance().Init();
357     resSchedService_->RegisterSystemloadNotifier(notifier);
358     resSchedService_->RegisterSystemloadNotifier(notifier);
359 }
360 
361 /**
362  * @tc.name: Ressched service UnRegisterSystemloadNotifier 001
363  * @tc.desc: test the interface service UnRegisterSystemloadNotifier
364  * @tc.type: FUNC
365  * @tc.require: issueI97M6C
366  * @tc.author:shanhaiyang
367  */
368 HWTEST_F(ResSchedServiceTest, UnRegisterSystemloadNotifier001, Function | MediumTest | Level0)
369 {
370     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
371     EXPECT_TRUE(resSchedService_ != nullptr);
372     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
373     EXPECT_TRUE(notifier != nullptr);
374     NotifierMgr::GetInstance().Init();
375     resSchedService_->RegisterSystemloadNotifier(notifier);
376     resSchedService_->UnRegisterSystemloadNotifier();
377 }
378 
379 /**
380  * @tc.name: Ressched service UnRegisterSystemloadNotifier 002
381  * @tc.desc: test the interface service UnRegisterSystemloadNotifier
382  * @tc.type: FUNC
383  * @tc.require: issueI97M6C
384  * @tc.author:shanhaiyang
385  */
386 HWTEST_F(ResSchedServiceTest, UnRegisterSystemloadNotifier002, Function | MediumTest | Level0)
387 {
388     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
389     EXPECT_TRUE(resSchedService_ != nullptr);
390     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
391     EXPECT_TRUE(notifier != nullptr);
392     NotifierMgr::GetInstance().Init();
393     resSchedService_->UnRegisterSystemloadNotifier();
394 }
395 
396 /**
397  * @tc.name: Ressched service GetSystemloadLevel 001
398  * @tc.desc: test the interface service GetSystemloadLevel
399  * @tc.type: FUNC
400  * @tc.require: issueI97M6C
401  * @tc.author:shanhaiyang
402  */
403 HWTEST_F(ResSchedServiceTest, GetSystemloadLevel001, Function | MediumTest | Level0)
404 {
405     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
406     EXPECT_TRUE(resSchedService_ != nullptr);
407     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
408     EXPECT_TRUE(notifier != nullptr);
409     NotifierMgr::GetInstance().Init();
410     resSchedService_->OnDeviceLevelChanged(0, 0);
411     int32_t res = resSchedService_->GetSystemloadLevel();
412     EXPECT_TRUE(res == 0);
413 }
414 
415 /**
416  * @tc.name: Ressched service GetSystemloadLevel 002
417  * @tc.desc: test the interface service GetSystemloadLevel
418  * @tc.type: FUNC
419  * @tc.require: issueI97M6C
420  * @tc.author:shanhaiyang
421  */
422 HWTEST_F(ResSchedServiceTest, GetSystemloadLevel002, Function | MediumTest | Level0)
423 {
424     std::shared_ptr<ResSchedService> resSchedService_ = make_shared<ResSchedService>();
425     EXPECT_TRUE(resSchedService_ != nullptr);
426     sptr<IRemoteObject> notifier = new (std::nothrow) TestResSchedSystemloadListener();
427     EXPECT_TRUE(notifier != nullptr);
428     NotifierMgr::GetInstance().Init();
429     resSchedService_->OnDeviceLevelChanged(0, 2);
430     resSchedService_->OnDeviceLevelChanged(1, 5);
431     int32_t res = resSchedService_->GetSystemloadLevel();
432     EXPECT_TRUE(res == 2);
433 }
434 
435 /**
436  * @tc.name: Start ResSchedServiceAbility 001
437  * @tc.desc: Verify if ResSchedServiceAbility OnStart is success.
438  * @tc.type: FUNC
439  * @tc.require: issueI5WWV3
440  * @tc.author:lice
441  */
442 HWTEST_F(ResSchedServiceTest, OnStart001, Function | MediumTest | Level0)
443 {
444     resSchedServiceAbility_->OnStart();
445     EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
446 }
447 
OnStartTask()448 static void OnStartTask()
449 {
450     std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
451     resSchedServiceAbility_->OnStart();
452     EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
453 }
454 
455 /**
456  * @tc.name: Start ResSchedServiceAbility 002
457  * @tc.desc: Test ResSchedServiceAbility OnStart in multithreading.
458  * @tc.type: FUNC
459  * @tc.require: issueI7G8VT
460  * @tc.author: nizihao
461  */
462 HWTEST_F(ResSchedServiceTest, OnStart002, Function | MediumTest | Level0)
463 {
464     SET_THREAD_NUM(10);
465     GTEST_RUN_TASK(OnStartTask);
466 }
467 
468 /**
469  * @tc.name: Stop ResSchedServiceAbility 001
470  * @tc.desc: test the interface Onstop
471  * @tc.type: FUNC
472  * @tc.require: issueI8VZVN
473  * @tc.author:z30053169
474  */
475 HWTEST_F(ResSchedServiceTest, OnStop001, Function | MediumTest | Level0)
476 {
477     resSchedServiceAbility_->OnStop();
478     SUCCEED();
479 }
480 
481 /**
482  * @tc.name: ResSchedServiceAbility ChangeAbility 001
483  * @tc.desc: Verify if add and remove system ability is success.
484  * @tc.type: FUNC
485  * @tc.require: issueI5WWV3
486  * @tc.author:lice
487  */
488 HWTEST_F(ResSchedServiceTest, ChangeAbility001, Function | MediumTest | Level0)
489 {
490     std::string deviceId;
491     resSchedServiceAbility_->OnAddSystemAbility(-1, deviceId);
492     resSchedServiceAbility_->OnRemoveSystemAbility(-1, deviceId);
493 }
494 
ChangeAbilityTask()495 static void ChangeAbilityTask()
496 {
497     std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
498     std::string deviceId;
499     resSchedServiceAbility_->OnAddSystemAbility(-1, deviceId);
500     resSchedServiceAbility_->OnRemoveSystemAbility(-1, deviceId);
501 }
502 
503 /**
504  * @tc.name: ResSchedServiceAbility ChangeAbility 002
505  * @tc.desc: Test add and remove system ability in multithreading.
506  * @tc.type: FUNC
507  * @tc.require: issueI7G8VT
508  * @tc.author: nizihao
509  */
510 HWTEST_F(ResSchedServiceTest, ChangeAbility002, Function | MediumTest | Level0)
511 {
512     SET_THREAD_NUM(10);
513     GTEST_RUN_TASK(ChangeAbilityTask);
514 }
515 
516 class TestResSchedServiceStub : public ResSchedServiceStub {
517 public:
TestResSchedServiceStub()518     TestResSchedServiceStub() : ResSchedServiceStub() {}
519 
ReportData(uint32_t restype,int64_t value,const nlohmann::json & payload)520     void ReportData(uint32_t restype, int64_t value, const nlohmann::json& payload) override
521     {
522     }
523 
ReportSyncEvent(const uint32_t resType,const int64_t value,const nlohmann::json & payload,nlohmann::json & reply)524     int32_t ReportSyncEvent(const uint32_t resType, const int64_t value, const nlohmann::json& payload,
525         nlohmann::json& reply) override
526     {
527         return 0;
528     }
529 
KillProcess(const nlohmann::json & payload)530     int32_t KillProcess(const nlohmann::json& payload) override
531     {
532         return 0;
533     }
534 
RegisterSystemloadNotifier(const sptr<IRemoteObject> & notifier)535     void RegisterSystemloadNotifier(const sptr<IRemoteObject>& notifier) override
536     {
537     }
538 
UnRegisterSystemloadNotifier()539     void UnRegisterSystemloadNotifier() override
540     {
541     }
542 
RegisterEventListener(const sptr<IRemoteObject> & listener,uint32_t eventType,uint32_t listenerGroup=ResType::EventListenerGroup::LISTENER_GROUP_COMMON)543     void RegisterEventListener(const sptr<IRemoteObject>& listener, uint32_t eventType,
544         uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON) override
545     {
546     }
547 
UnRegisterEventListener(uint32_t eventType,uint32_t listenerGroup=ResType::EventListenerGroup::LISTENER_GROUP_COMMON)548     void UnRegisterEventListener(uint32_t eventType,
549         uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON) override
550     {
551     }
552 
GetSystemloadLevel()553     int32_t GetSystemloadLevel() override
554     {
555         return 0;
556     }
557 
IsAllowedAppPreload(const std::string & bundleName,int32_t preloadMode)558     bool IsAllowedAppPreload(const std::string& bundleName, int32_t preloadMode) override
559     {
560         return true;
561     }
562 };
563 
564 /**
565  * @tc.name: ResSchedServicesStub ReportDataInner 001
566  * @tc.desc: Verify if resschedstub reportdatainner is success.
567  * @tc.type: FUNC
568  * @tc.require: issueI5WWV3
569  * @tc.author:lice
570  */
571 HWTEST_F(ResSchedServiceTest, ReportDataInner001, Function | MediumTest | Level0)
572 {
573     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
574     resSchedServiceStub_->Init();
575     MessageParcel reply;
576     MessageParcel emptyData;
577     EXPECT_TRUE(resSchedServiceStub_->ReportDataInner(emptyData, reply));
578 
579     MessageParcel reportData;
580     reportData.WriteInterfaceToken(ResSchedServiceStub::GetDescriptor());
581     reportData.WriteUint32(1);
582     reportData.WriteInt64(1);
583     reportData.WriteString("{ { \" uid \" : \" 1 \" } }");
584     SUCCEED();
585 }
586 
587 /**
588  * @tc.name: ReportSyncEventInner
589  * @tc.desc: test func ReportSyncEventInner.
590  * @tc.type: FUNC
591  * @tc.require: I9QN9E
592  */
593 HWTEST_F(ResSchedServiceTest, ReportSyncEventInner, Function | MediumTest | Level0)
594 {
595     auto serviceStub = make_shared<TestResSchedServiceStub>();
596     EXPECT_NE(serviceStub, nullptr);
597     serviceStub->Init();
598     MessageParcel data;
599     data.WriteInterfaceToken(ResSchedServiceStub::GetDescriptor());
600     data.WriteUint32(ResType::SYNC_RES_TYPE_THAW_ONE_APP);
601     data.WriteInt64(0);
602     data.WriteString(R"({"pid": 100})");
603     MessageParcel reply;
604     EXPECT_NE(serviceStub->ReportSyncEventInner(data, reply), 0);
605 }
606 
ReportDataInnerTask()607 static void ReportDataInnerTask()
608 {
609     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
610     resSchedServiceStub_->Init();
611     MessageParcel reply;
612     MessageParcel emptyData;
613     EXPECT_TRUE(resSchedServiceStub_->ReportDataInner(emptyData, reply));
614 
615     MessageParcel reportData;
616     reportData.WriteInterfaceToken(ResSchedServiceStub::GetDescriptor());
617     reportData.WriteUint32(1);
618     reportData.WriteInt64(1);
619     reportData.WriteString("{ { \" uid \" : \" 1 \" } }");
620     SUCCEED();
621 }
622 
623 /**
624  * @tc.name: ResSchedServicesStub ReportDataInner 002
625  * @tc.desc: Test resschedstub reportdatainner in multithreading.
626  * @tc.type: FUNC
627  * @tc.require: issueI7G8VT
628  * @tc.author: nizihao
629  */
630 HWTEST_F(ResSchedServiceTest, ReportDataInner002, Function | MediumTest | Level0)
631 {
632     SET_THREAD_NUM(10);
633     GTEST_RUN_TASK(ReportDataInnerTask);
634 }
635 
636 /**
637  * @tc.name: ResSchedServicesStub StringToJson 001
638  * @tc.desc: Verify if resschedstub StringToJson is success.
639  * @tc.type: FUNC
640  * @tc.require: issueI5WWV3
641  * @tc.author:lice
642  */
643 HWTEST_F(ResSchedServiceTest, StringToJson001, Function | MediumTest | Level0)
644 {
645     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
646     nlohmann::json res = resSchedServiceStub_->StringToJsonObj("");
647     EXPECT_TRUE(!res.dump().empty());
648 }
649 
StringToJsonTask()650 static void StringToJsonTask()
651 {
652     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
653     nlohmann::json res = resSchedServiceStub_->StringToJsonObj("");
654     EXPECT_TRUE(!res.dump().empty());
655 }
656 
657 /**
658  * @tc.name: ResSchedServicesStub StringToJson 002
659  * @tc.desc: Test resschedstub StringToJson in multithreading.
660  * @tc.type: FUNC
661  * @tc.require: issueI7G8VT
662  * @tc.author: nizihao
663  */
664 HWTEST_F(ResSchedServiceTest, StringToJson002, Function | MediumTest | Level0)
665 {
666     SET_THREAD_NUM(10);
667     GTEST_RUN_TASK(StringToJsonTask);
668 }
669 
670 /**
671  * @tc.name: ResSchedServicesStub RemoteRequest 001
672  * @tc.desc: Verify if resschedstub RemoteRequest is success.
673  * @tc.type: FUNC
674  * @tc.require: issueI5WWV3 issueI6D6BM
675  * @tc.author:lice
676  */
677 HWTEST_F(ResSchedServiceTest, RemoteRequest001, Function | MediumTest | Level0)
678 {
679     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
680     MessageOption option;
681     MessageParcel reply;
682     int32_t res = resSchedServiceStub_->OnRemoteRequest(
683         static_cast<uint32_t>(ResourceScheduleInterfaceCode::REPORT_DATA), reply, reply, option);
684     EXPECT_TRUE(res);
685     res = resSchedServiceStub_->OnRemoteRequest(
686         static_cast<uint32_t>(ResourceScheduleInterfaceCode::REPORT_SYNC_EVENT), reply, reply, option);
687     EXPECT_TRUE(res);
688     res = resSchedServiceStub_->OnRemoteRequest(
689         static_cast<uint32_t>(ResourceScheduleInterfaceCode::KILL_PROCESS), reply, reply, option);
690     EXPECT_TRUE(res);
691     res = resSchedServiceStub_->OnRemoteRequest(0, reply, reply, option);
692     EXPECT_TRUE(res);
693     res = resSchedServiceStub_->OnRemoteRequest(
694         static_cast<uint32_t>(ResourceScheduleInterfaceCode::REGISTER_SYSTEMLOAD_NOTIFIER), reply, reply, option);
695     EXPECT_TRUE(!res);
696     res = resSchedServiceStub_->OnRemoteRequest(
697         static_cast<uint32_t>(ResourceScheduleInterfaceCode::UNREGISTER_SYSTEMLOAD_NOTIFIER), reply, reply, option);
698     EXPECT_TRUE(!res);
699     res = resSchedServiceStub_->OnRemoteRequest(
700         static_cast<uint32_t>(ResourceScheduleInterfaceCode::GET_SYSTEMLOAD_LEVEL), reply, reply, option);
701     EXPECT_TRUE(res);
702 }
703 
RemoteRequestTask()704 static void RemoteRequestTask()
705 {
706     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
707     MessageOption option;
708     MessageParcel reply;
709     int32_t res = resSchedServiceStub_->OnRemoteRequest(
710         static_cast<uint32_t>(ResourceScheduleInterfaceCode::REPORT_DATA), reply, reply, option);
711     EXPECT_TRUE(res);
712     res = resSchedServiceStub_->OnRemoteRequest(
713         static_cast<uint32_t>(ResourceScheduleInterfaceCode::KILL_PROCESS), reply, reply, option);
714     EXPECT_TRUE(res);
715     res = resSchedServiceStub_->OnRemoteRequest(0, reply, reply, option);
716     EXPECT_TRUE(res);
717     res = resSchedServiceStub_->OnRemoteRequest(
718         static_cast<uint32_t>(ResourceScheduleInterfaceCode::REGISTER_SYSTEMLOAD_NOTIFIER), reply, reply, option);
719     EXPECT_TRUE(!res);
720     res = resSchedServiceStub_->OnRemoteRequest(
721         static_cast<uint32_t>(ResourceScheduleInterfaceCode::UNREGISTER_SYSTEMLOAD_NOTIFIER), reply, reply, option);
722     EXPECT_TRUE(!res);
723     res = resSchedServiceStub_->OnRemoteRequest(
724         static_cast<uint32_t>(ResourceScheduleInterfaceCode::GET_SYSTEMLOAD_LEVEL), reply, reply, option);
725     EXPECT_TRUE(res);
726 }
727 
728 /**
729  * @tc.name: ResSchedServicesStub RemoteRequest 002
730  * @tc.desc: Test resschedstub RemoteRequest in multithreading.
731  * @tc.type: FUNC
732  * @tc.require: issueI7G8VT
733  * @tc.author: nizihao
734  */
735 HWTEST_F(ResSchedServiceTest, RemoteRequest002, Function | MediumTest | Level0)
736 {
737     SET_THREAD_NUM(10);
738     GTEST_RUN_TASK(RemoteRequestTask);
739 }
740 
741 /**
742  * @tc.name: ResSchedServicesStub RegisterSystemloadNotifier 001
743  * @tc.desc: Verify if resschedstub RegisterSystemloadNotifier is success.
744  * @tc.type: FUNC
745  * @tc.require: issueI97M6C
746  * @tc.author:shanhaiyang
747  */
748 HWTEST_F(ResSchedServiceTest, StubRegisterSystemloadNotifier001, Function | MediumTest | Level0)
749 {
750     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
751     MessageParcel reply;
752     MessageParcel emptyData;
753     EXPECT_TRUE(resSchedServiceStub_ != nullptr);
754     resSchedServiceStub_->RegisterSystemloadNotifierInner(emptyData, reply);
755 }
756 
757 /**
758  * @tc.name: ResSchedServicesStub UnRegisterSystemloadNotifier 001
759  * @tc.desc: Verify if resschedstub UnRegisterSystemloadNotifier is success.
760  * @tc.type: FUNC
761  * @tc.require: issueI97M6C
762  * @tc.author:shanhaiyang
763  */
764 HWTEST_F(ResSchedServiceTest, StubUnRegisterSystemloadNotifier001, Function | MediumTest | Level0)
765 {
766     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
767     MessageParcel reply;
768     MessageParcel emptyData;
769     EXPECT_TRUE(resSchedServiceStub_ != nullptr);
770     resSchedServiceStub_->UnRegisterSystemloadNotifierInner(emptyData, reply);
771 }
772 
773 /**
774  * @tc.name: ResSchedServicesStub GetSystemloadLevel 001
775  * @tc.desc: Verify if resschedstub GetSystemloadLevel is success.
776  * @tc.type: FUNC
777  * @tc.require: issueI97M6C
778  * @tc.author:shanhaiyang
779  */
780 HWTEST_F(ResSchedServiceTest, StubGetSystemloadLevel001, Function | MediumTest | Level0)
781 {
782     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
783     MessageParcel reply;
784     MessageParcel emptyData;
785     EXPECT_TRUE(resSchedServiceStub_->GetSystemloadLevelInner(emptyData, reply));
786 }
787 
788 /**
789  * @tc.name: ResSchedServicesStub IsAllowedAppPreloadInner 001
790  * @tc.desc: Verify resschedstub allowedAppPreloadInner.
791  * @tc.type: FUNC
792  * @tc.require: issueI9C9JN
793  * @tc.author:xiaoshun
794  */
795 HWTEST_F(ResSchedServiceTest, IsAllowedAppPreloadInner001, Function | MediumTest | Level0)
796 {
797     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
798     resSchedServiceStub_->Init();
799     MessageParcel reply;
800     MessageParcel emptyData;
801     EXPECT_TRUE(!resSchedServiceStub_->IsAllowedAppPreloadInner(emptyData, reply));
802 }
803 
804 /**
805  * @tc.name: ResSchedServicesStub IsLimitRequest 001
806  * @tc.desc: IsLimitRequestTest
807  * @tc.type: FUNC
808  * @tc.require: issueI9U0YF
809  * @tc.author:fengyang
810  */
811 HWTEST_F(ResSchedServiceTest, IsLimitRequest001, Function | MediumTest | Level0)
812 {
813     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
814     resSchedServiceStub_->Init();
815     int32_t uid = 0;
816     EXPECT_EQ(resSchedServiceStub_->IsLimitRequest(uid), false);
817     resSchedServiceStub_->appRequestCountMap_[uid] = 300;
818     EXPECT_EQ(resSchedServiceStub_->IsLimitRequest(uid), true);
819     resSchedServiceStub_->allRequestCount_.store(800);
820     EXPECT_EQ(resSchedServiceStub_->IsLimitRequest(uid), true);
821 }
822 
823 /**
824  * @tc.name: ResSchedServicesStub PrintLimitLog 001
825  * @tc.desc: PrintLimitLog
826  * @tc.type: FUNC
827  * @tc.require: issuesIAGHOC
828  * @tc.author:fengyang
829  */
830 HWTEST_F(ResSchedServiceTest, PrintLimitLog001, Function | MediumTest | Level0)
831 {
832     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
833     resSchedServiceStub_->Init();
834     int32_t uid = 0;
835     resSchedServiceStub_->isPrintLimitLog_.store(true);
836     resSchedServiceStub_->PrintLimitLog(uid);
837 }
838 
839 /**
840  * @tc.name: ResSchedServicesStub ReportBigData 001
841  * @tc.desc: ReportBigData
842  * @tc.type: FUNC
843  * @tc.require: issuesIAGHOC
844  * @tc.author:fengyang
845  */
846 HWTEST_F(ResSchedServiceTest, ReportBigData001, Function | MediumTest | Level0)
847 {
848     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
849     resSchedServiceStub_->Init();
850     resSchedServiceStub_->isReportBigData_.store(false);
851     resSchedServiceStub_->ReportBigData();
852     resSchedServiceStub_->isReportBigData_.store(true);
853     resSchedServiceStub_->ReportBigData();
854     resSchedServiceStub_->nextReportBigDataTime_ = ResCommonUtil::GetNowMillTime();
855     resSchedServiceStub_->ReportBigData();
856 }
857 
858 /**
859  * @tc.name: ResSchedServicesStub InreaseBigDataCount 001
860  * @tc.desc: InreaseBigDataCount
861  * @tc.type: FUNC
862  * @tc.require: issuesIAGHOC
863  * @tc.author:fengyang
864  */
865 HWTEST_F(ResSchedServiceTest, InreaseBigDataCount001, Function | MediumTest | Level0)
866 {
867     auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
868     resSchedServiceStub_->Init();
869     resSchedServiceStub_->isReportBigData_.store(false);
870     resSchedServiceStub_->InreaseBigDataCount();
871     resSchedServiceStub_->isReportBigData_.store(true);
872     resSchedServiceStub_->InreaseBigDataCount();
873 }
874 
875 } // namespace ResourceSchedule
876 } // namespace OHOS
877