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 #include <gtest/gtest.h>
20
21 #include "ability_state_data.h"
22 #include "app_state_data.h"
23 #include "app_state_observer.h"
24 #include "background_task_mgr_service.h"
25 #include "bg_continuous_task_mgr.h"
26 #include "bg_efficiency_resources_mgr.h"
27 #include "bundle_info.h"
28 #include "bundle_manager_helper.h"
29 #include "common_event_data.h"
30 #include "continuous_task_record.h"
31 #include "decision_maker.h"
32 #include "delay_suspend_info_ex.h"
33 #include "device_info_manager.h"
34 #include "event_info.h"
35 #include "event_handler.h"
36 #include "event_runner.h"
37 #include "input_manager.h"
38 #include "key_info.h"
39 #include "notification.h"
40 #include "notification_sorting_map.h"
41 #include "notification_tools.h"
42 #include "pkg_delay_suspend_info.h"
43 #include "process_data.h"
44 #include "singleton.h"
45 #include "string_wrapper.h"
46 #include "suspend_controller.h"
47 #include "task_notification_subscriber.h"
48 #include "time_provider.h"
49 #include "timer_manager.h"
50 #include "watchdog.h"
51
52 using namespace testing::ext;
53
54 namespace OHOS {
55 extern void SetPublishContinuousTaskNotificationFlag(int32_t flag);
56 extern void SetCancelContinuousTaskNotificationFlag(int32_t flag);
57 extern void SetGetAllActiveNotificationsFlag(int32_t flag);
58
59 namespace BackgroundTaskMgr {
60 namespace {
61 static constexpr int32_t SLEEP_TIME = 500;
62 static constexpr int32_t BGTASKMGR_UID = 3051;
63 static constexpr int32_t TEST_NUM_ONE = 1;
64 static constexpr int32_t TEST_NUM_TWO = 2;
65 static constexpr int32_t MIN_ALLOW_QUOTA_TIME = 10 * MSEC_PER_SEC; // 10s
66 }
67
68 class BgTaskMiscUnitTest : public testing::Test {
69 public:
70 static void SetUpTestCase();
71 static void TearDownTestCase();
72 void SetUp();
73 void TearDown();
SleepForFC()74 inline void SleepForFC()
75 {
76 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
77 }
78 };
79
SetUpTestCase()80 void BgTaskMiscUnitTest::SetUpTestCase() {}
81
TearDownTestCase()82 void BgTaskMiscUnitTest::TearDownTestCase() {}
83
SetUp()84 void BgTaskMiscUnitTest::SetUp() {}
85
TearDown()86 void BgTaskMiscUnitTest::TearDown() {}
87
88 /**
89 * @tc.name: AppStateObserverTest_001
90 * @tc.desc: test AppStateObserver class CheckParamValid method.
91 * @tc.type: FUNC
92 * @tc.require: issueI4QT3W issueI4QU0V
93 */
94 HWTEST_F(BgTaskMiscUnitTest, AppStateObserverTest_001, TestSize.Level1)
95 {
96 sptr<AppStateObserver> appStateObserver = sptr<AppStateObserver>(new AppStateObserver());
97 AppExecFwk::ProcessData processData = AppExecFwk::ProcessData();
98 appStateObserver->OnProcessDied(processData);
99 appStateObserver->OnProcessDiedContinuousTask(processData);
100 appStateObserver->OnProcessDiedEfficiencyRes(processData);
101 AppExecFwk::AbilityStateData abilityStateData = AppExecFwk::AbilityStateData();
102 appStateObserver->OnAbilityStateChanged(abilityStateData);
103 EXPECT_FALSE(appStateObserver->CheckParamValid());
104 auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(nullptr);
105 appStateObserver->SetEventHandler(handler);
106 EXPECT_FALSE(appStateObserver->CheckParamValid());
107 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED);
108 appStateObserver->OnAbilityStateChanged(abilityStateData);
109 auto bgContinuousTaskMgr = std::make_shared<BgContinuousTaskMgr>();
110 appStateObserver->SetBgContinuousTaskMgr(bgContinuousTaskMgr);
111 EXPECT_TRUE(appStateObserver->CheckParamValid());
112 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_CREATE);
113 appStateObserver->OnAbilityStateChanged(abilityStateData);
114 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED);
115 appStateObserver->OnAbilityStateChanged(abilityStateData);
116 AppExecFwk::AppStateData appStateData = AppExecFwk::AppStateData();
117 appStateObserver->OnApplicationStateChanged(appStateData);
118 appStateData.uid = 1;
119 appStateData.bundleName = "bundleName";
120 appStateObserver->OnApplicationStateChanged(appStateData);
121 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_TERMINATED);
122 appStateObserver->OnApplicationStateChanged(appStateData);
123 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_END);
124 appStateObserver->OnApplicationStateChanged(appStateData);
125 appStateObserver->SetBgEfficiencyResourcesMgr(std::make_shared<BgEfficiencyResourcesMgr>());
126 appStateObserver->OnApplicationStateChanged(appStateData);
127 EXPECT_TRUE(appStateObserver->Subscribe());
128 SleepForFC();
129 EXPECT_TRUE(appStateObserver->Unsubscribe());
130 SleepForFC();
131 }
132
133 /**
134 * @tc.name: BundleManagerHelperTest_001
135 * @tc.desc: test BundleManagerHelper class.
136 * @tc.type: FUNC
137 * @tc.require: issueI4QT3W issueI4QU0V
138 */
139 HWTEST_F(BgTaskMiscUnitTest, BundleManagerHelperTest_001, TestSize.Level1)
140 {
141 EXPECT_EQ(BundleManagerHelper::GetInstance()->GetClientBundleName(1), "");
142 EXPECT_FALSE(BundleManagerHelper::GetInstance()->CheckPermission("permission"));
143 EXPECT_FALSE(BundleManagerHelper::GetInstance()->IsSystemApp(1LLU));
144 AppExecFwk::BundleInfo bundleInfo;
145 EXPECT_FALSE(BundleManagerHelper::GetInstance()->GetBundleInfo("bundleName",
146 AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo));
147 AppExecFwk::ApplicationInfo applicationInfo;
148 EXPECT_FALSE(BundleManagerHelper::GetInstance()->GetApplicationInfo("bundleName",
149 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, 100, applicationInfo));
150 BundleManagerHelper::GetInstance()->OnRemoteDied(nullptr);
151 BundleManagerHelper::GetInstance()->bundleMgr_ = nullptr;
152 BundleManagerHelper::GetInstance()->OnRemoteDied(nullptr);
153 AAFwk::Want want;
154 want.SetAction("action.system.home");
155 want.AddEntity("entity.system.home");
156 want.SetElementName("", "bundleName", "", "");
157 AppExecFwk::AbilityInfo abilityInfo;
158 BundleManagerHelper::GetInstance()->QueryAbilityInfo(want,
159 AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION, 0, abilityInfo);
160 BundleManagerHelper::GetInstance()->bundleMgr_ = nullptr;
161 BundleManagerHelper::GetInstance()->QueryAbilityInfo(want,
162 AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION, 0, abilityInfo);
163 }
164
165 /**
166 * @tc.name: SystemEventObserverTest_001
167 * @tc.desc: test SystemEventObserver class.
168 * @tc.type: FUNC
169 * @tc.require: issueI4QT3W issueI4QU0V
170 */
171 HWTEST_F(BgTaskMiscUnitTest, SystemEventObserver_001, TestSize.Level1)
172 {
173 EventFwk::MatchingSkills matchingSkills;
174 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
175 EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
176 auto systemEventListener = std::make_shared<SystemEventObserver>(commonEventSubscribeInfo);
177 EXPECT_TRUE(systemEventListener->Subscribe());
178 EXPECT_TRUE(systemEventListener->Unsubscribe());
179
180 EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
181 systemEventListener->OnReceiveEvent(eventData);
182
183 auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(nullptr);
184 systemEventListener->SetEventHandler(handler);
185 systemEventListener->OnReceiveEventContinuousTask(eventData);
186 auto bgContinuousTaskMgr = std::make_shared<BgContinuousTaskMgr>();
187 systemEventListener->SetBgContinuousTaskMgr(bgContinuousTaskMgr);
188 systemEventListener->OnReceiveEventContinuousTask(eventData);
189 AAFwk::Want want = AAFwk::Want();
190 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
191 eventData.SetWant(want);
192 systemEventListener->OnReceiveEventContinuousTask(eventData);
193 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED);
194 eventData.SetWant(want);
195 systemEventListener->OnReceiveEventContinuousTask(eventData);
196 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
197 eventData.SetWant(want);
198 systemEventListener->OnReceiveEventContinuousTask(eventData);
199
200 EventFwk::CommonEventData eventData2 = EventFwk::CommonEventData();
201 AAFwk::Want want2 = AAFwk::Want();
202 want2.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
203 eventData2.SetWant(want2);
204 systemEventListener->OnReceiveEventEfficiencyRes(eventData2);
205 EXPECT_TRUE(true);
206 }
207
208 /**
209 * @tc.name: ContinuousTaskRecordTest_001
210 * @tc.desc: test ContinuousTaskRecord class.
211 * @tc.type: FUNC
212 * @tc.require: issueI4QT3W issueI4QU0V
213 */
214 HWTEST_F(BgTaskMiscUnitTest, ContinuousTaskRecordTest_001, TestSize.Level1)
215 {
216 ContinuousTaskRecord record = ContinuousTaskRecord();
217 std::string str1 = record.ParseToJsonStr();
218 EXPECT_NE(str1, "");
219 record.wantAgentInfo_ = std::make_shared<WantAgentInfo>();
220 std::string str2 = record.ParseToJsonStr();
221 EXPECT_NE(str2, "");
222 ContinuousTaskRecord record2 = ContinuousTaskRecord();
223 nlohmann::json json2 = nlohmann::json::parse("", nullptr, false);
224 EXPECT_FALSE(record2.ParseFromJson(json2));
225 nlohmann::json json3;
226 json3["bundleName"] = "bundleName";
227 EXPECT_FALSE(record2.ParseFromJson(json3));
228 nlohmann::json json4 = nlohmann::json::parse(str1, nullptr, false);
229 ContinuousTaskRecord record3 = ContinuousTaskRecord();
230 EXPECT_TRUE(record3.ParseFromJson(json4));
231 nlohmann::json json5 = nlohmann::json::parse(str2, nullptr, false);
232 ContinuousTaskRecord record4 = ContinuousTaskRecord();
233 EXPECT_TRUE(record4.ParseFromJson(json5));
234 }
235
236 /**
237 * @tc.name: NotificationToolsTest_001
238 * @tc.desc: test NotificationTools class.
239 * @tc.type: FUNC
240 * @tc.require: issueI4QT3W issueI4QU0V
241 */
242 HWTEST_F(BgTaskMiscUnitTest, NotificationToolsTest_001, TestSize.Level1)
243 {
244 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
245 auto taskRecord = std::make_shared<ContinuousTaskRecord>();
246 NotificationTools::GetInstance()->PublishNotification(taskRecord, "appName", "prompt", 1);
247 SetPublishContinuousTaskNotificationFlag(1);
248 EXPECT_EQ(NotificationTools::GetInstance()->PublishNotification(taskRecord, "appName", "prompt", 1),
249 ERR_BGTASK_NOTIFICATION_ERR);
250 NotificationTools::GetInstance()->CancelNotification("label", 0);
251 SetCancelContinuousTaskNotificationFlag(1);
252 EXPECT_EQ(NotificationTools::GetInstance()->CancelNotification("label", 0), ERR_BGTASK_NOTIFICATION_ERR);
253 std::set<std::string> notificationLabels;
254 NotificationTools::GetInstance()->GetAllActiveNotificationsLabels(notificationLabels);
255
256 std::map<std::string, std::pair<std::string, std::string>> newPromptInfos;
257 newPromptInfos.emplace("label", std::make_pair<std::string, std::string>("test1", "test2"));
258 SetGetAllActiveNotificationsFlag(TEST_NUM_ONE);
259 SetPublishContinuousTaskNotificationFlag(0);
260 SetPublishContinuousTaskNotificationFlag(0);
261 NotificationTools::GetInstance()->RefreshContinuousNotifications(newPromptInfos, 0);
262 SetGetAllActiveNotificationsFlag(TEST_NUM_TWO);
263 SetPublishContinuousTaskNotificationFlag(1);
264 NotificationTools::GetInstance()->RefreshContinuousNotifications(newPromptInfos, 0);
265 #endif
266 }
267
268 /**
269 * @tc.name: TaskNotificationSubscriber_001
270 * @tc.desc: test TaskNotificationSubscriber class.
271 * @tc.type: FUNC
272 * @tc.require: issueI4QT3W issueI4QU0V
273 */
274 HWTEST_F(BgTaskMiscUnitTest, TaskNotificationSubscriber_001, TestSize.Level1)
275 {
276 auto subscriber = std::make_shared<TaskNotificationSubscriber>();
277 subscriber->OnCanceled(nullptr, nullptr, 1);
278 auto notificationMap = std::make_shared<Notification::NotificationSortingMap>();
279 auto notificationRequest = sptr<Notification::NotificationRequest>(new Notification::NotificationRequest());
280 auto notification = std::make_shared<Notification::Notification>(notificationRequest);
281 subscriber->OnCanceled(notification, notificationMap, 1);
282 BgContinuousTaskMgr::GetInstance()->bgTaskUid_ = BGTASKMGR_UID;
283
284 notification->request_->creatorUid_ = BGTASKMGR_UID;
285 subscriber->OnCanceled(notification, notificationMap, 1);
286 notification->request_->label_ = "label";
287 subscriber->OnCanceled(notification, notificationMap, 1);
288 notification->request_->label_ = "bgmode_1";
289 subscriber->OnCanceled(notification, notificationMap, 1);
290 notification->request_->label_ = "bgmode_1_1";
291 subscriber->OnCanceled(notification, notificationMap, 1);
292 subscriber->OnCanceled(notification, notificationMap,
293 Notification::NotificationConstant::APP_CANCEL_REASON_DELETE);
294
295 std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<AAFwk::WantParams>();
296 extraInfo->SetParam("abilityName", AAFwk::String::Box("abilityName"));
297 notification->request_->additionalParams_ = extraInfo;
298 subscriber->OnCanceled(notification, notificationMap,
299 Notification::NotificationConstant::USER_STOPPED_REASON_DELETE);
300
301 std::shared_ptr<ContinuousTaskRecord> continuousTaskRecord = std::make_shared<ContinuousTaskRecord>();
302 BgContinuousTaskMgr::GetInstance()->continuousTaskInfosMap_["1_abilityName"] = continuousTaskRecord;
303 subscriber->OnCanceled(notification, notificationMap,
304 Notification::NotificationConstant::USER_STOPPED_REASON_DELETE);
305 EXPECT_TRUE(true);
306 }
307
308 /**
309 * @tc.name: DecisionMakerTest_001
310 * @tc.desc: test DecisionMaker class decide method.
311 * @tc.type: FUNC
312 * @tc.require: issueI4QT3W issueI4QU0V
313 */
314 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_001, TestSize.Level1)
315 {
316 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
317 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
318 auto timerManager = std::make_shared<TimerManager>(bgtaskService,
319 AppExecFwk::EventRunner::Create("tdd_test_handler"));
320 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
321 decisionMaker->lastRequestTime_ = TimeProvider::GetCurrentTime();
322 EXPECT_EQ(decisionMaker->Decide(nullptr, nullptr), ERR_BGTASK_NO_MEMORY);
323
324 auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
325 decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime() - ALLOW_REQUEST_TIME_BG - 1;
326 EXPECT_EQ(decisionMaker->Decide(keyInfo, nullptr), ERR_BGTASK_NOT_IN_PRESET_TIME);
327 decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime();
328 EXPECT_EQ(decisionMaker->Decide(keyInfo, nullptr), ERR_BGTASK_NO_MEMORY);
329
330 auto keyInfo2 = std::make_shared<KeyInfo>("bundleName2", 2);
331 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName2", 2, timerManager);
332 auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1);
333 auto delayInfo2 = std::make_shared<DelaySuspendInfoEx>(2);
334 auto delayInfo3 = std::make_shared<DelaySuspendInfoEx>(3);
335 pkgDelaySuspendInfo->requestList_.push_back(delayInfo1);
336 pkgDelaySuspendInfo->requestList_.push_back(delayInfo2);
337 pkgDelaySuspendInfo->requestList_.push_back(delayInfo3);
338 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo2] = pkgDelaySuspendInfo;
339 EXPECT_EQ(decisionMaker->Decide(keyInfo2, nullptr), ERR_BGTASK_EXCEEDS_THRESHOLD);
340
341 EXPECT_EQ(decisionMaker->Decide(keyInfo2, delayInfo1), ERR_BGTASK_EXCEEDS_THRESHOLD);
342 decisionMaker->pkgDelaySuspendInfoMap_.clear();
343 deviceInfoManeger->isScreenOn_ = true;
344 EXPECT_EQ(decisionMaker->Decide(keyInfo, delayInfo1), ERR_OK);
345 decisionMaker->pkgDelaySuspendInfoMap_.clear();
346 deviceInfoManeger->isScreenOn_ = false;
347 EXPECT_EQ(decisionMaker->Decide(keyInfo, delayInfo1), ERR_OK);
348 }
349
350 /**
351 * @tc.name: DecisionMakerTest_002
352 * @tc.desc: test DecisionMaker class misc method.
353 * @tc.type: FUNC
354 * @tc.require: issueI4QT3W issueI4QU0V
355 */
356 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_002, TestSize.Level1)
357 {
358 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
359 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
360 auto timerManager = std::make_shared<TimerManager>(bgtaskService,
361 AppExecFwk::EventRunner::Create("tdd_test_handler"));
362 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
363
364 decisionMaker->RemoveRequest(nullptr, -1);
365 auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
366 decisionMaker->RemoveRequest(keyInfo, -1);
367
368 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
369 auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1);
370 pkgDelaySuspendInfo->requestList_.push_back(delayInfo1);
371 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
372 decisionMaker->RemoveRequest(keyInfo, -1);
373 decisionMaker->RemoveRequest(keyInfo, 1);
374
375 decisionMaker->pkgDelaySuspendInfoMap_.clear();
376 EXPECT_EQ(decisionMaker->GetRemainingDelayTime(nullptr, -1), -1);
377 EXPECT_EQ(decisionMaker->GetRemainingDelayTime(nullptr, -1), -1);
378 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
379 EXPECT_EQ(decisionMaker->GetRemainingDelayTime(keyInfo, -1), 0);
380
381 EXPECT_EQ(decisionMaker->GetQuota(nullptr), -1);
382 decisionMaker->pkgDelaySuspendInfoMap_.clear();
383 EXPECT_EQ(decisionMaker->GetQuota(keyInfo), INIT_QUOTA);
384 pkgDelaySuspendInfo->quota_ = -1;
385 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
386 EXPECT_EQ(decisionMaker->GetQuota(keyInfo), 0);
387 EXPECT_FALSE(decisionMaker->IsFrontApp("pkgName", 1));
388
389 decisionMaker->requestId_ = INT_MAX;
390 EXPECT_EQ(decisionMaker->NewDelaySuspendRequestId(), 1);
391 EXPECT_EQ(decisionMaker->NewDelaySuspendRequestId(), TEST_NUM_TWO);
392
393 decisionMaker->lastRequestTime_ = TimeProvider::GetCurrentTime() - 1;
394 decisionMaker->ResetDayQuotaLocked();
395 decisionMaker->lastRequestTime_ = TimeProvider::GetCurrentTime() - QUOTA_UPDATE - 1;
396 decisionMaker->pkgDelaySuspendInfoMap_.clear();
397 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
398 auto keyInfo2 = std::make_shared<KeyInfo>("bundleName2", TEST_NUM_TWO);
399 auto pkgDelaySuspendInfo2 = std::make_shared<PkgDelaySuspendInfo>(
400 "bundleName2", TEST_NUM_TWO, timerManager);
401 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo2] = pkgDelaySuspendInfo2;
402 decisionMaker->ResetDayQuotaLocked();
403
404 EventInfo eventInfo = EventInfo();
405 eventInfo.eventId_ = 0;
406 decisionMaker->OnInputEvent(eventInfo);
407 eventInfo.eventId_ = EVENT_SCREEN_ON;
408 decisionMaker->OnInputEvent(eventInfo);
409 eventInfo.eventId_ = EVENT_SCREEN_OFF;
410 decisionMaker->OnInputEvent(eventInfo);
411 eventInfo.eventId_ = EVENT_SCREEN_UNLOCK;
412 decisionMaker->OnInputEvent(eventInfo);
413 EXPECT_TRUE(true);
414 }
415
416 /**
417 * @tc.name: DeviceInfoManagerTest_001
418 * @tc.desc: test DeviceInfoManager class.
419 * @tc.type: FUNC
420 * @tc.require: issueI4QT3W issueI4QU0V
421 */
422 HWTEST_F(BgTaskMiscUnitTest, DeviceInfoManagerTest_001, TestSize.Level1)
423 {
424 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
425 deviceInfoManeger->isDump_ = true;
426 EventInfo eventInfo = EventInfo();
427 std::vector<std::string> args;
428 args.emplace_back("test");
429 eventInfo.SetStringArgs(args);
430 deviceInfoManeger->OnInputEvent(eventInfo);
431 args.clear();
432 args.emplace_back("dump");
433 eventInfo.SetStringArgs(args);
434 deviceInfoManeger->isDump_ = false;
435 eventInfo.eventId_ = EVENT_SCREEN_ON;
436 deviceInfoManeger->OnInputEvent(eventInfo);
437 eventInfo.eventId_ = EVENT_SCREEN_OFF;
438 deviceInfoManeger->OnInputEvent(eventInfo);
439 eventInfo.eventId_ = EVENT_SCREEN_UNLOCK;
440 deviceInfoManeger->OnInputEvent(eventInfo);
441 eventInfo.eventId_ = EVENT_BATTERY_LOW;
442 deviceInfoManeger->OnInputEvent(eventInfo);
443 eventInfo.eventId_ = EVENT_BATTERY_OKAY;
444 deviceInfoManeger->OnInputEvent(eventInfo);
445 eventInfo.eventId_ = EVENT_MAX;
446 deviceInfoManeger->OnInputEvent(eventInfo);
447 EXPECT_TRUE(true);
448 }
449
450 /**
451 * @tc.name: InputManagerTest_001
452 * @tc.desc: test InputManager class.
453 * @tc.type: FUNC
454 * @tc.require: issueI4QT3W issueI4QU0V
455 */
456 HWTEST_F(BgTaskMiscUnitTest, InputManagerTest_001, TestSize.Level1)
457 {
458 auto inputManager = std::make_shared<InputManager>(AppExecFwk::EventRunner::Create("tdd_test_handler"));
459 auto deviceInfoManeger1 = std::make_shared<DeviceInfoManager>();
460 inputManager->RegisterEventListener(nullptr);
461 inputManager->RegisterEventListener(deviceInfoManeger1);
462 auto eventInfo = std::make_shared<EventInfo>();
463 inputManager->SendEventInfo(nullptr);
464 inputManager->SendEventInfo(eventInfo);
465 auto deviceInfoManeger2 = std::make_shared<DeviceInfoManager>();
466 inputManager->UnRegisterEventListener(nullptr);
467 inputManager->UnRegisterEventListener(deviceInfoManeger2);
468 EXPECT_EQ(inputManager->listenerList_.size(), 1);
469 inputManager->UnRegisterEventListener(deviceInfoManeger1);
470 EXPECT_EQ(inputManager->listenerList_.size(), 0);
471 EXPECT_TRUE(true);
472 }
473
474 /**
475 * @tc.name: PkgDelaySuspendInfoTest_001
476 * @tc.desc: test PkgDelaySuspendInfo class.
477 * @tc.type: FUNC
478 * @tc.require: issueI4QT3W issueI4QU0V
479 */
480 HWTEST_F(BgTaskMiscUnitTest, PkgDelaySuspendInfoTest_001, TestSize.Level1)
481 {
482 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
483 auto timerManager = std::make_shared<TimerManager>(bgtaskService,
484 AppExecFwk::EventRunner::Create("tdd_test_handler"));
485 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
486 pkgDelaySuspendInfo->isCounting_ = true;
487 pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime() + MIN_ALLOW_QUOTA_TIME;
488 EXPECT_EQ(pkgDelaySuspendInfo->IsAllowRequest(), ERR_OK);
489 pkgDelaySuspendInfo->quota_ = 0;
490 pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime();
491 EXPECT_EQ(pkgDelaySuspendInfo->IsAllowRequest(), ERR_BGTASK_TIME_INSUFFICIENT);
492 auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1, 1);
493 auto delayInfo2 = std::make_shared<DelaySuspendInfoEx>(1, 2);
494 pkgDelaySuspendInfo->AddRequest(nullptr, 1);
495 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
496 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
497 pkgDelaySuspendInfo->AddRequest(delayInfo2, 1);
498 EXPECT_EQ(pkgDelaySuspendInfo->IsAllowRequest(), ERR_BGTASK_EXCEEDS_THRESHOLD);
499 pkgDelaySuspendInfo->RemoveRequest(1);
500 pkgDelaySuspendInfo->RemoveRequest(1);
501 pkgDelaySuspendInfo->RemoveRequest(2);
502
503 pkgDelaySuspendInfo->requestList_.clear();
504 EXPECT_EQ(pkgDelaySuspendInfo->GetRemainDelayTime(-1), 0);
505 delayInfo1->actualDelayTime_ = 1;
506 auto delayInfo3 = std::make_shared<DelaySuspendInfoEx>(1, 1, 1);
507 pkgDelaySuspendInfo->requestList_.emplace_back(delayInfo3);
508 EXPECT_EQ(pkgDelaySuspendInfo->GetRemainDelayTime(-1), 0);
509 EXPECT_EQ(pkgDelaySuspendInfo->GetRemainDelayTime(1), 1);
510 }
511
512 /**
513 * @tc.name: PkgDelaySuspendInfoTest_002
514 * @tc.desc: test PkgDelaySuspendInfo class.
515 * @tc.type: FUNC
516 * @tc.require: issueI4QT3W issueI4QU0V
517 */
518 HWTEST_F(BgTaskMiscUnitTest, PkgDelaySuspendInfoTest_002, TestSize.Level1)
519 {
520 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
521 auto timerManager = std::make_shared<TimerManager>(bgtaskService, AppExecFwk::EventRunner::Create("tdd_test_handler"));
522 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
523 auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1, 1);
524
525 pkgDelaySuspendInfo->requestList_.clear();
526 pkgDelaySuspendInfo->StartAccounting(1);
527 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
528 pkgDelaySuspendInfo->StartAccounting(2);
529 pkgDelaySuspendInfo->StartAccounting(-1);
530 pkgDelaySuspendInfo->isCounting_ = false;
531 pkgDelaySuspendInfo->baseTime_ = 0;
532 pkgDelaySuspendInfo->StartAccounting(1);
533 pkgDelaySuspendInfo->isCounting_ = true;
534 pkgDelaySuspendInfo->baseTime_ = 1;
535 pkgDelaySuspendInfo->StartAccounting(1);
536
537 pkgDelaySuspendInfo->requestList_.clear();
538 pkgDelaySuspendInfo->StopAccounting(1);
539 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
540 pkgDelaySuspendInfo->StopAccounting(-1);
541 pkgDelaySuspendInfo->baseTime_ = 0;
542 pkgDelaySuspendInfo->StopAccounting(1);
543 pkgDelaySuspendInfo->baseTime_ = 1;
544 pkgDelaySuspendInfo->StopAccounting(1);
545 pkgDelaySuspendInfo->requestList_.clear();
546 pkgDelaySuspendInfo->StopAccountingAll();
547 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
548 pkgDelaySuspendInfo->baseTime_ = 0;
549 pkgDelaySuspendInfo->StopAccounting(1);
550 pkgDelaySuspendInfo->baseTime_ = 1;
551 pkgDelaySuspendInfo->StopAccounting(1);
552
553 pkgDelaySuspendInfo->isCounting_ = true;
554 pkgDelaySuspendInfo->quota_ = 0;
555 pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime() - 1;
556 pkgDelaySuspendInfo->UpdateQuota(false);
557 pkgDelaySuspendInfo->quota_ = 0;
558 pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime() + 1;
559 pkgDelaySuspendInfo->UpdateQuota(true);
560 EXPECT_TRUE(true);
561 }
562
563 /**
564 * @tc.name: SuspendControllerTest_001
565 * @tc.desc: test SuspendController.
566 * @tc.type: FUNC
567 * @tc.require: issueI4QT3W issueI4QU0V
568 */
569 HWTEST_F(BgTaskMiscUnitTest, SuspendControllerTest_001, TestSize.Level1)
570 {
571 SuspendController suspendController = SuspendController();
572 auto keyInfo = std::make_shared<KeyInfo>("bundleName", 1);
573 suspendController.RequestSuspendDelay(nullptr);
574 suspendController.RequestSuspendDelay(keyInfo);
575 suspendController.CancelSuspendDelay(nullptr);
576 suspendController.CancelSuspendDelay(keyInfo);
577 EXPECT_TRUE(true);
578 }
579
580 /**
581 * @tc.name: WatchdogTest_001
582 * @tc.desc: test Watchdog class.
583 * @tc.type: FUNC
584 * @tc.require: issueI4QT3W issueI4QU0V
585 */
586 HWTEST_F(BgTaskMiscUnitTest, WatchdogTest_001, TestSize.Level1)
587 {
588 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
589 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
590 auto timerManager = std::make_shared<TimerManager>(bgtaskService, AppExecFwk::EventRunner::Create("tdd_test_handler"));
591 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
592 auto watchdog = std::make_shared<Watchdog>(bgtaskService, decisionMaker,
593 AppExecFwk::EventRunner::Create("tdd_test_handler"));
594 EXPECT_TRUE(watchdog->KillApplicationByUid("bundleName", 1));
595 EXPECT_TRUE(watchdog->KillApplicationByUid("bundleName", 1));
596 }
597
598 /**
599 * @tc.name: ConfigChangeObserver_001
600 * @tc.desc: test Watchdog class.
601 * @tc.type: FUNC
602 * @tc.require: issueI4QT3W issueI4QU0V
603 */
604 HWTEST_F(BgTaskMiscUnitTest, ConfigChangeObserver_001, TestSize.Level1)
605 {
606 sptr<ConfigChangeObserver> configChangeObserver1 = sptr<ConfigChangeObserver>(
607 new ConfigChangeObserver(nullptr, nullptr));
608 EXPECT_FALSE(configChangeObserver1->CheckParamValid());
609 AppExecFwk::Configuration configuration;
610 configChangeObserver1->OnConfigurationUpdated(configuration);
611 auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(nullptr);
612 auto bgContinuousTaskMgr = std::make_shared<BgContinuousTaskMgr>();
613 sptr<ConfigChangeObserver> configChangeObserver2 = sptr<ConfigChangeObserver>(
614 new ConfigChangeObserver(handler, nullptr));
615 EXPECT_FALSE(configChangeObserver2->CheckParamValid());
616 sptr<ConfigChangeObserver> configChangeObserver3 = sptr<ConfigChangeObserver>(
617 new ConfigChangeObserver(handler, bgContinuousTaskMgr));
618 EXPECT_TRUE(configChangeObserver3->CheckParamValid());
619 configChangeObserver3->OnConfigurationUpdated(configuration);
620 }
621
622 /**
623 * @tc.name: DataStorageHelper_001
624 * @tc.desc: test Watchdog class.
625 * @tc.type: FUNC
626 * @tc.require: issueI4QT3W issueI4QU0V
627 */
628 HWTEST_F(BgTaskMiscUnitTest, DataStorageHelper_001, TestSize.Level1)
629 {
630 std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap1;
631 auto continuousTaskRecord = std::make_shared<ContinuousTaskRecord>();
632 continuousTaskInfosMap1.emplace("key", continuousTaskRecord);
633 DelayedSingleton<DataStorageHelper>::GetInstance()->RefreshTaskRecord(continuousTaskInfosMap1);
634 std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap2;
635 EXPECT_EQ(DelayedSingleton<DataStorageHelper>::GetInstance()->RestoreTaskRecord(continuousTaskInfosMap2),
636 ERR_OK);
637 EXPECT_EQ(DelayedSingleton<DataStorageHelper>::GetInstance()->SaveJsonValueToFile("", ""),
638 ERR_BGTASK_CREATE_FILE_ERR);
639 nlohmann::json json1;
640 EXPECT_EQ(DelayedSingleton<DataStorageHelper>::GetInstance()->ParseJsonValueFromFile(json1, ""),
641 ERR_BGTASK_DATA_STORAGE_ERR);
642 EXPECT_FALSE(DelayedSingleton<DataStorageHelper>::GetInstance()->CreateNodeFile(""));
643 std::string fullPath;
644 EXPECT_FALSE(DelayedSingleton<DataStorageHelper>::GetInstance()->ConvertFullPath("", fullPath));
645 }
646
647 /**
648 * @tc.name: DecisionMakerTest_003
649 * @tc.desc: test Watchdog class.
650 * @tc.type: FUNC
651 * @tc.require: issueI4QT3W issueI4QU0V
652 */
653 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_003, TestSize.Level1)
654 {
655 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
656 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
657 auto timerManager = std::make_shared<TimerManager>(bgtaskService, AppExecFwk::EventRunner::Create("tdd_test_handler"));
658 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
659 auto applicationStateObserver = sptr<DecisionMaker::ApplicationStateObserver>(
660 new (std::nothrow) DecisionMaker::ApplicationStateObserver(*decisionMaker));
661
662 AppExecFwk::AppStateData appStateData;
663 appStateData.uid = 1;
664 appStateData.bundleName = "bundleName1";
665 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND);
666 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
667 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOCUS);
668 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
669
670 auto keyInfo1 = std::make_shared<KeyInfo>("bundleName1", 1);
671 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
672 auto delayInfo = std::make_shared<DelaySuspendInfoEx>(1);
673 pkgDelaySuspendInfo->requestList_.push_back(delayInfo);
674 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo1] = pkgDelaySuspendInfo;
675 auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
676 decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime() - ALLOW_REQUEST_TIME_BG - 1;
677 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND);
678 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
679 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOCUS);
680 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
681
682 decisionMaker->pkgDelaySuspendInfoMap_.clear();
683 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_BACKGROUND);
684 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
685 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo1] = pkgDelaySuspendInfo;
686 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
687 EXPECT_EQ((int32_t)decisionMaker->pkgDelaySuspendInfoMap_.size(), 1);
688 }
689
690 /**
691 * @tc.name: DelaySuspendInfoEx_001
692 * @tc.desc: test DelaySuspendInfoEx.
693 * @tc.type: FUNC
694 * @tc.require: issueI4QT3W issueI4QU0V
695 */
696 HWTEST_F(BgTaskMiscUnitTest, DelaySuspendInfoEx_001, TestSize.Level1)
697 {
698 auto delayInfo = std::make_shared<DelaySuspendInfoEx>(1);
699 delayInfo->baseTime_ = 1;
700 delayInfo->StartAccounting();
701 delayInfo->baseTime_ = 0;
702 delayInfo->StopAccounting();
703 EXPECT_EQ(delayInfo->spendTime_, 0);
704 }
705 }
706 }
707