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