• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <functional>
17 #include <chrono>
18 #include <thread>
19 
20 #include "gtest/gtest.h"
21 
22 #define private public
23 
24 #include "bgtaskmgr_inner_errors.h"
25 #include "background_task_subscriber.h"
26 #include "bg_efficiency_resources_mgr.h"
27 #include "resource_type.h"
28 #include "system_ability_definition.h"
29 #include "time_provider.h"
30 
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace BackgroundTaskMgr {
35 static constexpr int32_t SLEEP_TIME = 2000;
36 static constexpr int32_t REMAIN_TIME = 1000;
37 static constexpr int32_t WAIT_TIME = 1000;
38 static constexpr uint32_t MAX_RESOURCES_TYPE_NUM = 7;
39 static constexpr char MOCK_EFFICIENCY_RESOURCES_MGR_NAME[] = "MockEfficiencyResourcesMgr";
40 
41 class BgEfficiencyResourcesMgrTest : public testing::Test {
42 public:
43     static void SetUpTestCase();
44     static void TearDownTestCase();
45     void SetUp();
46     void TearDown();
SleepFor(int32_t sleepTime)47     inline void SleepFor(int32_t sleepTime)
48     {
49         std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
50     }
51 
52     static std::shared_ptr<BgEfficiencyResourcesMgr> bgEfficiencyResourcesMgr_;
53 };
54 
55 std::shared_ptr<BgEfficiencyResourcesMgr> BgEfficiencyResourcesMgrTest::bgEfficiencyResourcesMgr_ = nullptr;
56 
SetUpTestCase()57 void BgEfficiencyResourcesMgrTest::SetUpTestCase()
58 {
59     bgEfficiencyResourcesMgr_ = DelayedSingleton<BgEfficiencyResourcesMgr>::GetInstance();
60     bgEfficiencyResourcesMgr_->subscriberMgr_ = DelayedSingleton<ResourcesSubscriberMgr>::GetInstance();
61     std::shared_ptr<AppExecFwk::EventRunner> runner =
62         AppExecFwk::EventRunner::Create(MOCK_EFFICIENCY_RESOURCES_MGR_NAME);
63     bgEfficiencyResourcesMgr_->handler_ =
64         std::make_shared<AppExecFwk::EventHandler>(runner);
65     bgEfficiencyResourcesMgr_->isSysReady_.store(true);
66 }
67 
TearDownTestCase()68 void BgEfficiencyResourcesMgrTest::TearDownTestCase() {}
69 
SetUp()70 void BgEfficiencyResourcesMgrTest::SetUp() {}
71 
TearDown()72 void BgEfficiencyResourcesMgrTest::TearDown()
73 {
74     std::vector<std::string> dumpOption;
75     dumpOption.emplace_back("-E");
76     dumpOption.emplace_back("--reset_all");
77     std::vector<std::string> dumpInfo;
78     bgEfficiencyResourcesMgr_->ShellDump(dumpOption, dumpInfo);
79     SleepFor(WAIT_TIME);
80 }
81 
82 class TestBackgroundTaskSubscriber : public BackgroundTaskMgr::BackgroundTaskSubscriber {
83 public:
OnAppEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)84     void OnAppEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) override {}
85 
OnAppEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)86     void OnAppEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) override {}
87 
OnProcEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)88     void OnProcEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) override {}
89 
OnProcEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)90     void OnProcEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> &resourceInfo) override {}
91 };
92 
93 /**
94  * @tc.name: AppEfficiencyResources_001
95  * @tc.desc: apply efficiency resources using ApplyEfficiencyResources function.
96  * @tc.type: FUNC
97  * @tc.require: issuesI5OD7X
98  */
99 HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_001, TestSize.Level1)
100 {
101     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
102         nullptr), (int32_t)ERR_BGTASK_RESOURCES_EXCEEDS_MAX);
103     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo();
104     resourceInfo->isApply_ = true;
105     EXPECT_NE(resourceInfo, nullptr);
106     resourceInfo->resourceNumber_ = 1 << MAX_RESOURCES_TYPE_NUM;
107     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
108         resourceInfo), (int32_t)ERR_BGTASK_RESOURCES_EXCEEDS_MAX);
109     resourceInfo->resourceNumber_ = 1;
110     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
111         resourceInfo), (int32_t)ERR_BGTASK_RESOURCES_EXCEEDS_MAX);
112     resourceInfo->isPersist_ = true;
113     resourceInfo->reason_ = "apply";
114     resourceInfo->timeOut_ = 0;
115     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
116         resourceInfo), (int32_t)ERR_OK);
117 
118     resourceInfo->isPersist_ = false;
119     resourceInfo->timeOut_ = 0;
120     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
121         resourceInfo), (int32_t)ERR_BGTASK_RESOURCES_EXCEEDS_MAX);
122     resourceInfo->timeOut_ = 10;
123     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
124         resourceInfo), (int32_t)ERR_OK);
125     resourceInfo->isPersist_ = true;
126     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
127         resourceInfo), (int32_t)ERR_OK);
128     bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources();
129 }
130 
131 /**
132  * @tc.name: AppEfficiencyResources_002
133  * @tc.desc: apply and reset resources for process and app respectively using ApplyEfficiencyResources function.
134  * @tc.type: FUNC
135  * @tc.require: issuesI5OD7X
136  */
137 HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_002, TestSize.Level1)
138 {
139     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 0, "apply",
140         true, false);
141     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
142         resourceInfo), (int32_t)ERR_OK);
143     SleepFor(WAIT_TIME);
144     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 1);
145     resourceInfo->isApply_ = false;
146     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
147         resourceInfo), (int32_t)ERR_OK);
148     SleepFor(WAIT_TIME);
149     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 0);
150 
151     resourceInfo->isProcess_ = true;
152     resourceInfo->isApply_ = true;
153     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
154         resourceInfo), (int32_t)ERR_OK);
155     SleepFor(WAIT_TIME);
156     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 1);
157     resourceInfo->isApply_ = false;
158     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
159         resourceInfo), (int32_t)ERR_OK);
160     SleepFor(WAIT_TIME);
161     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 0);
162     bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources();
163 }
164 
165 /**
166  * @tc.name: AppEfficiencyResources_003
167  * @tc.desc: apply transient efficiency and reset ahead.
168  * @tc.require: issuesI5OD7X
169  * @tc.type: FUNC
170  */
171 HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_003, TestSize.Level1)
172 {
173     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 0, "apply",
174         true, true);
175     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
176         resourceInfo), (int32_t)ERR_OK);
177     SleepFor(WAIT_TIME);
178     EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->procResourceApplyMap_.size()), 1);
179     resourceInfo->isPersist_ = false;
180     resourceInfo->timeOut_ = SLEEP_TIME;
181     resourceInfo->isProcess_ = false;
182     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
183         resourceInfo), (int32_t)ERR_OK);
184     SleepFor(WAIT_TIME);
185     EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->appResourceApplyMap_.size()), 1);
186     SleepFor(SLEEP_TIME + REMAIN_TIME);
187     EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->procResourceApplyMap_.size()), 1);
188     EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->appResourceApplyMap_.size()), 0);
189     bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources();
190 }
191 
192 /**
193  * @tc.name: AppEfficiencyResources_004
194  * @tc.desc: reset resources record of process using app ApplyEfficiencyResources function.
195  * @tc.type: FUNC
196  * @tc.require: issuesI5OD7X
197  */
198 HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_004, TestSize.Level1)
199 {
200     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 0, "apply",
201         true, false);
202     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
203         resourceInfo), (int32_t)ERR_OK);
204     SleepFor(WAIT_TIME);
205     EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->appResourceApplyMap_.size()), 1);
206     resourceInfo->isProcess_ = true;
207     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
208         resourceInfo), (int32_t)ERR_OK);
209     SleepFor(WAIT_TIME);
210     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 1);
211 
212     resourceInfo->isProcess_ = false;
213     resourceInfo->isApply_ = false;
214     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
215         resourceInfo), (int32_t)ERR_OK);
216     SleepFor(WAIT_TIME);
217     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 0);
218     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 0);
219     bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources();
220 }
221 
222 /**
223  * @tc.name: AppEfficiencyResources_005
224  * @tc.desc: repeatedly apply unpersisted resources.
225  * @tc.type: FUNC
226  * @tc.require: issuesI5OD7X
227  */
228 HWTEST_F(BgEfficiencyResourcesMgrTest, AppEfficiencyResources_005, TestSize.Level1)
229 {
230     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 2000, "apply",
231         false, true);
232     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
233         resourceInfo), (int32_t)ERR_OK);
234     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
235         resourceInfo), (int32_t)ERR_OK);
236     SleepFor(WAIT_TIME);
237     EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->procResourceApplyMap_.size()), 1);
238     bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources();
239     SleepFor(WAIT_TIME);
240     EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->procResourceApplyMap_.size()), 0);
241 
242     resourceInfo->resourceNumber_ = ResourceType::CPU
243         | ResourceType::TIMER | ResourceType::COMMON_EVENT;
244     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
245         resourceInfo), (int32_t)ERR_OK);
246     resourceInfo->resourceNumber_ = ResourceType::CPU;
247     resourceInfo->timeOut_ = 0;
248     resourceInfo->isPersist_ = true;
249     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
250         resourceInfo), (int32_t)ERR_OK);
251 }
252 
253 /**
254  * @tc.name: ResetAllEfficiencyResources_001
255  * @tc.desc: reset all efficiency resources using ResetAllEfficiencyResources function.
256  * @tc.type: FUNC
257  * @tc.require: issuesI5OD7X
258  */
259 HWTEST_F(BgEfficiencyResourcesMgrTest, ResetAllEfficiencyResources_001, TestSize.Level1)
260 {
261     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 0, "apply",
262         true, false);
263     SleepFor(WAIT_TIME);
264     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 0);
265     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 0);
266     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
267         resourceInfo), (int32_t)ERR_OK);
268     resourceInfo->resourceNumber_ = 1 << 1;
269     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
270         resourceInfo), (int32_t)ERR_OK);
271     SleepFor(WAIT_TIME);
272     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 1);
273     resourceInfo->isProcess_ = true;
274     resourceInfo->resourceNumber_ = 1;
275     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
276         resourceInfo), (int32_t)ERR_OK);
277     resourceInfo->resourceNumber_ = 1 << 1;
278     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
279         resourceInfo), (int32_t)ERR_OK);
280     SleepFor(WAIT_TIME);
281     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 1);
282     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources(), (int32_t)ERR_OK);
283     SleepFor(WAIT_TIME);
284     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 0);
285     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 0);
286 }
287 
288 /**
289  * @tc.name: SubscribeEfficiencyResources_001
290  * @tc.desc: subscribe efficiency resources callback test.
291  * @tc.type: FUNC
292  * @tc.require: issuesI5OD7X
293  */
294 HWTEST_F(BgEfficiencyResourcesMgrTest, SubscribeEfficiencyResources_001, TestSize.Level1)
295 {
296     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->AddSubscriber(
297         nullptr), (int32_t)ERR_BGTASK_INVALID_PARAM);
298     SleepFor(SLEEP_TIME);
299     auto subscriber =  std::make_shared<TestBackgroundTaskSubscriber>();
300     EXPECT_NE(subscriber, nullptr);
301     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->AddSubscriber(
302         subscriber->GetImpl()), (int32_t)ERR_OK);
303     SleepFor(SLEEP_TIME);
304     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->AddSubscriber(
305         subscriber->GetImpl()), (int32_t)ERR_BGTASK_OBJECT_EXISTS);
306 
307     auto subscriberImpl = subscriber->GetImpl();
308     auto resourceInfo = std::make_shared<ResourceCallbackInfo>(0, 0, 1, "");
309     subscriberImpl->OnAppEfficiencyResourcesApply(resourceInfo);
310     subscriberImpl->OnAppEfficiencyResourcesReset(resourceInfo);
311     subscriberImpl->OnProcEfficiencyResourcesApply(resourceInfo);
312     subscriberImpl->OnProcEfficiencyResourcesReset(resourceInfo);
313     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->RemoveSubscriber(subscriber->GetImpl()), (int32_t)ERR_OK);
314 }
315 
316 /**
317  * @tc.name: SubscribeEfficiencyResources_002
318  * @tc.desc: unsubscribe efficiency resources callback test.
319  * @tc.type: FUNC
320  * @tc.require: issuesI5OD7X
321  */
322 HWTEST_F(BgEfficiencyResourcesMgrTest, SubscribeEfficiencyResources_002, TestSize.Level1)
323 {
324     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->RemoveSubscriber(nullptr),
325         (int32_t)ERR_BGTASK_INVALID_PARAM);
326     SleepFor(SLEEP_TIME);
327     auto subscriber =  std::make_shared<TestBackgroundTaskSubscriber>();
328     EXPECT_NE(subscriber, nullptr);
329     bgEfficiencyResourcesMgr_->AddSubscriber(subscriber->GetImpl());
330     SleepFor(SLEEP_TIME);
331     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->RemoveSubscriber(subscriber->GetImpl()), (int32_t)ERR_OK);
332 }
333 
334 /**
335  * @tc.name: EfficiencyResourcesCallback_001
336  * @tc.desc: efficiency resources callback test.
337  * @tc.type: FUNC
338  * @tc.require: issuesI5OD7X
339  */
340 HWTEST_F(BgEfficiencyResourcesMgrTest, EfficiencyResourcesCallback_001, TestSize.Level1)
341 {
342     auto subscriber =  std::make_shared<BackgroundTaskSubscriber>();
343     EXPECT_NE(subscriber, nullptr);
344     auto resourceInfo = std::make_shared<ResourceCallbackInfo>(0, 0, 0, "");
345     subscriber->OnAppEfficiencyResourcesApply(resourceInfo);
346     subscriber->OnAppEfficiencyResourcesReset(resourceInfo);
347     subscriber->OnProcEfficiencyResourcesApply(resourceInfo);
348     subscriber->OnProcEfficiencyResourcesReset(resourceInfo);
349 
350     auto subscriberImpl = subscriber->GetImpl();
351     EXPECT_NE(subscriberImpl, nullptr);
352     subscriberImpl->OnAppEfficiencyResourcesApply(resourceInfo);
353     subscriberImpl->OnAppEfficiencyResourcesReset(resourceInfo);
354     subscriberImpl->OnProcEfficiencyResourcesApply(resourceInfo);
355     subscriberImpl->OnProcEfficiencyResourcesReset(resourceInfo);
356 }
357 
358 /**
359  * @tc.name: Marshalling_001
360  * @tc.desc: marshalling resource callback info and efficiency resources info.
361  * @tc.type: FUNC
362  * @tc.require: issuesI5OD7X
363  */
364 HWTEST_F(BgEfficiencyResourcesMgrTest, Marshalling_001, TestSize.Level1)
365 {
366     auto callbackInfo = std::make_shared<ResourceCallbackInfo>();
367     MessageParcel data;
368     EXPECT_TRUE(callbackInfo->Marshalling(data));
369     std::shared_ptr<ResourceCallbackInfo> callbackInfoPtr (ResourceCallbackInfo::Unmarshalling(data));
370     EXPECT_TRUE(callbackInfoPtr != nullptr);
371     auto resourceInfo = std::make_shared<EfficiencyResourceInfo>(1, true,
372         0, "apply", true, false);
373     MessageParcel out;
374     EXPECT_TRUE(resourceInfo->Marshalling(out));
375     std::shared_ptr<EfficiencyResourceInfo> resourceInfoPtr (EfficiencyResourceInfo::Unmarshalling(out));
376     EXPECT_TRUE(resourceInfoPtr != nullptr);
377 }
378 
379 /**
380  * @tc.name: Init_001
381  * @tc.desc: cover init function.
382  * @tc.type: FUNC
383  */
384 HWTEST_F(BgEfficiencyResourcesMgrTest, Init_001, TestSize.Level1)
385 {
386     std::shared_ptr<AppExecFwk::EventRunner> runner = nullptr;
387     bgEfficiencyResourcesMgr_->Init(runner);
388     runner = AppExecFwk::EventRunner::Create(MOCK_EFFICIENCY_RESOURCES_MGR_NAME);
389     bgEfficiencyResourcesMgr_->Init(runner);
390     EXPECT_TRUE(true);
391 }
392 
393 /**
394  * @tc.name: SystemAbility_001
395  * @tc.desc: cover system ability callback function.
396  * @tc.type: FUNC
397  */
398 HWTEST_F(BgEfficiencyResourcesMgrTest, SystemAbility_001, TestSize.Level1)
399 {
400     bgEfficiencyResourcesMgr_->OnRemoveSystemAbility(-1, "");
401     bgEfficiencyResourcesMgr_->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, "");
402     bgEfficiencyResourcesMgr_->OnRemoveSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, "");
403     bgEfficiencyResourcesMgr_->OnAddSystemAbility(-1, "");
404     bgEfficiencyResourcesMgr_->OnAddSystemAbility(APP_MGR_SERVICE_ID, "");
405     bgEfficiencyResourcesMgr_->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, "");
406 
407     bgEfficiencyResourcesMgr_->isSysReady_.store(true);
408     bgEfficiencyResourcesMgr_->CheckAlivedApp(0);
409     bgEfficiencyResourcesMgr_->Clear();
410     bgEfficiencyResourcesMgr_->appStateObserver_ = nullptr;
411     bgEfficiencyResourcesMgr_->Clear();
412     bgEfficiencyResourcesMgr_->CheckAlivedApp(0);
413     EXPECT_TRUE(true);
414 }
415 
416 /**
417  * @tc.name: EraseRecord_001
418  * @tc.desc: cover EraseRecordIf and RecoverDelayedTask function.
419  * @tc.type: FUNC
420  */
421 HWTEST_F(BgEfficiencyResourcesMgrTest, EraseRecord_001, TestSize.Level1)
422 {
423     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(ResourceType::CPU, true,
424         0, "apply", true, false);
425     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
426         resourceInfo), (int32_t)ERR_OK);
427     SleepFor(WAIT_TIME);
428     EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->appResourceApplyMap_.size()), 1);
429 
430     resourceInfo->isProcess_ = true;
431     resourceInfo->isPersist_ = false;
432     resourceInfo->timeOut_ = 2000;
433     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
434         resourceInfo), (int32_t)ERR_OK);
435     SleepFor(WAIT_TIME);
436     EXPECT_EQ((int32_t)(bgEfficiencyResourcesMgr_->procResourceApplyMap_.size()), 1);
437     bgEfficiencyResourcesMgr_->HandlePersistenceData();
438 
439     std::unordered_map<int32_t, std::shared_ptr<ResourceApplicationRecord>> infoMap {};
__anonac7af7d50102(const auto &iter) 440     auto returnFalse = [](const auto &iter)  { return iter.first < 0; };
441     auto appRecord = std::make_shared<ResourceApplicationRecord>();
442     infoMap.emplace(0, appRecord);
443     bgEfficiencyResourcesMgr_->EraseRecordIf(infoMap, returnFalse);
444 
445     appRecord->resourceNumber_ = ResourceType::CPU | ResourceType::COMMON_EVENT;
446     appRecord->resourceUnitList_.emplace_back(0, true, 0, "CPU");
447     appRecord->resourceUnitList_.emplace_back(1, false, 0, "COMMON_EVENT");
448     bgEfficiencyResourcesMgr_->RecoverDelayedTask(false, infoMap);
449     EXPECT_TRUE(true);
450 }
451 
452 /**
453  * @tc.name: DeathCallback_001
454  * @tc.desc: cover the condition of application death.
455  * @tc.type: FUNC
456  */
457 HWTEST_F(BgEfficiencyResourcesMgrTest, DeathCallback_001, TestSize.Level1)
458 {
459     bgEfficiencyResourcesMgr_->isSysReady_.store(false);
460     bgEfficiencyResourcesMgr_->RemoveAppRecord(0, "", false);
461     bgEfficiencyResourcesMgr_->RemoveProcessRecord(0, 0, "");
462     bgEfficiencyResourcesMgr_->isSysReady_.store(true);
463     bgEfficiencyResourcesMgr_->RemoveAppRecord(0, "", false);
464     bgEfficiencyResourcesMgr_->RemoveAppRecord(0, "", true);
465     bgEfficiencyResourcesMgr_->RemoveProcessRecord(-1, 0, "");
466     bgEfficiencyResourcesMgr_->RemoveProcessRecord(0, 0, "");
467     EXPECT_TRUE(true);
468 }
469 
470 /**
471  * @tc.name: SysUnload_001
472  * @tc.desc: cover the condition when sysload_ is false.
473  * @tc.type: FUNC
474  */
475 HWTEST_F(BgEfficiencyResourcesMgrTest, SysUnload_001, TestSize.Level1)
476 {
477     bgEfficiencyResourcesMgr_->isSysReady_.store(false);
478     bgEfficiencyResourcesMgr_->RemoveAppRecord(0, "", false);
479     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(1, true, 0, "apply",
480         true, false);
481     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(resourceInfo),
482         (int32_t)ERR_BGTASK_SYS_NOT_READY);
483     bgEfficiencyResourcesMgr_->ResetAllEfficiencyResources();
484     std::vector<std::string> dumpInfo {};
485     bgEfficiencyResourcesMgr_->ShellDump({"-E", "--reset_all"}, dumpInfo);
486 
487     bgEfficiencyResourcesMgr_->isSysReady_.store(true);
488     bgEfficiencyResourcesMgr_->RemoveAppRecord(0, "", false);
489     bgEfficiencyResourcesMgr_->RemoveAppRecord(0, "", true);
490     EXPECT_TRUE(true);
491 }
492 
493 /**
494  * @tc.name: Dump_001
495  * @tc.desc: cover the condition when ShellDump function is called.
496  * @tc.type: FUNC
497  */
498 HWTEST_F(BgEfficiencyResourcesMgrTest, Dump_001, TestSize.Level1)
499 {
500     std::vector<std::string> dumpInfo {};
501     bgEfficiencyResourcesMgr_->ShellDump({"-E", "--all"}, dumpInfo);
502     bgEfficiencyResourcesMgr_->ShellDump({"-E", "--resetapp", "0"}, dumpInfo);
503     std::vector<std::shared_ptr<ResourceCallbackInfo>> appList {};
504     std::vector<std::shared_ptr<ResourceCallbackInfo>> procList {};
505     bgEfficiencyResourcesMgr_->GetEfficiencyResourcesInfos(appList, procList);
506     EXPECT_EQ((int32_t)appList.size(), 0);
507     EXPECT_EQ((int32_t)procList.size(), 0);
508 
509     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(
510         ResourceType::CPU | ResourceType::WORK_SCHEDULER, true, 0, "apply", true, true);
511     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
512         resourceInfo), (int32_t)ERR_OK);
513     resourceInfo->isProcess_ = false;
514     resourceInfo->isPersist_ = false;
515     resourceInfo->timeOut_ = 2000;
516     resourceInfo->resourceNumber_ = ResourceType::CPU;
517     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
518         resourceInfo), (int32_t)ERR_OK);
519     SleepFor(WAIT_TIME);
520     bgEfficiencyResourcesMgr_->ShellDump({"-E", "--all"}, dumpInfo);
521     bgEfficiencyResourcesMgr_->GetEfficiencyResourcesInfos(appList, procList);
522     EXPECT_EQ((int32_t)appList.size(), 1);
523     EXPECT_EQ((int32_t)procList.size(), 1);
524     bgEfficiencyResourcesMgr_->ShellDump({"-E", "--resetapp", "0", "1"}, dumpInfo);
525     bgEfficiencyResourcesMgr_->ShellDump({"-E", "--resetproc", "0", "1"}, dumpInfo);
526     bgEfficiencyResourcesMgr_->ShellDump({"-E", "--reset_proc", "0", "0"}, dumpInfo);
527 
528     bgEfficiencyResourcesMgr_->ShellDump({"-E", "--reset_all"}, dumpInfo);
529     resourceInfo= new (std::nothrow) EfficiencyResourceInfo(
530         ResourceType::CPU, true, 0, "apply", true, true);
531     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
532         resourceInfo), (int32_t)ERR_OK);
533     bgEfficiencyResourcesMgr_->ShellDump({"-E", "--all"}, dumpInfo);
534 }
535 
536 /**
537  * @tc.name: BoundaryCondition_001
538  * @tc.desc: cover the boundary condition.
539  * @tc.type: FUNC
540  */
541 HWTEST_F(BgEfficiencyResourcesMgrTest, BoundaryCondition_001, TestSize.Level1)
542 {
543     sptr<EfficiencyResourceInfo> resourceInfo = new (std::nothrow) EfficiencyResourceInfo(0, true, 0, "apply",
544         true, false);
545     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
546         resourceInfo), (int32_t)ERR_BGTASK_RESOURCES_EXCEEDS_MAX);
547     std::string bundleName {""};
548     EXPECT_FALSE(bgEfficiencyResourcesMgr_->IsCallingInfoLegal(-1, 0, bundleName));
549     EXPECT_FALSE(bgEfficiencyResourcesMgr_->IsCallingInfoLegal(0, -1, bundleName));
550     EXPECT_TRUE(bgEfficiencyResourcesMgr_->CheckRunningResourcesApply(0, "bundleName"));
551     std::list<PersistTime> resourceUnitList {};
552     bgEfficiencyResourcesMgr_->RemoveListRecord(resourceUnitList, 0);
553 
554     resourceInfo->resourceNumber_ = ResourceType::CPU | ResourceType::WORK_SCHEDULER | ResourceType::COMMON_EVENT;
555     resourceInfo->isProcess_ = true;
556     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
557         resourceInfo), (int32_t)ERR_OK);
558     bgEfficiencyResourcesMgr_->RemoveRelativeProcessRecord(0, 0);
559     resourceInfo->isApply_ = false;
560     resourceInfo->resourceNumber_ = ResourceType::WORK_SCHEDULER;
561     resourceInfo->isProcess_ = false;
562     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
563         resourceInfo), (int32_t)ERR_OK);
564     resourceInfo->resourceNumber_ = ResourceType::TIMER;
565     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
566         resourceInfo), (int32_t)ERR_OK);
567     resourceInfo->isProcess_ = true;
568     resourceInfo->resourceNumber_ = ResourceType::CPU;
569     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
570         resourceInfo), (int32_t)ERR_OK);
571     resourceInfo->resourceNumber_ = ResourceType::COMMON_EVENT;
572     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->ApplyEfficiencyResources(
573         resourceInfo), (int32_t)ERR_OK);
574     SleepFor(WAIT_TIME);
575     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->appResourceApplyMap_.size(), 1);
576     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 0);
577 }
578 
579 /**
580  * @tc.name: ResetTimeOutResource_001
581  * @tc.desc: cover the ResetTimeOutResource and RemoveRelativeProcessRecord function.
582  * @tc.type: FUNC
583  */
584 HWTEST_F(BgEfficiencyResourcesMgrTest, ResetTimeOutResource_001, TestSize.Level1)
585 {
586     auto &procMap = bgEfficiencyResourcesMgr_->procResourceApplyMap_;
587     auto procRecord = std::make_shared<ResourceApplicationRecord>();
588     procMap.emplace(0, procRecord);
589 
590     procRecord->resourceNumber_ = ResourceType::CPU;
591     procRecord->resourceUnitList_.emplace_back(0, true, 0, "CPU");
592     bgEfficiencyResourcesMgr_->ResetTimeOutResource(0, true);
593 
594     procRecord->resourceNumber_ = ResourceType::CPU | ResourceType::COMMON_EVENT
595         | ResourceType::TIMER;
596     procRecord->resourceUnitList_.emplace_back(1, false, 0, "COMMON_EVENT");
597     procRecord->resourceUnitList_.emplace_back(2, false, TimeProvider::GetCurrentTime() + WAIT_TIME, "TIMER");
598     bgEfficiencyResourcesMgr_->RemoveRelativeProcessRecord(1, 1);
599     bgEfficiencyResourcesMgr_->RemoveRelativeProcessRecord(1, 64);
600     bgEfficiencyResourcesMgr_->ResetTimeOutResource(0, true);
601     bgEfficiencyResourcesMgr_->RemoveRelativeProcessRecord(0, 0);
602     EXPECT_EQ((int32_t)bgEfficiencyResourcesMgr_->procResourceApplyMap_.size(), 1);
603 }
604 }  // namespace BackgroundTaskMgr
605 }  // namespace OHOS
606