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