• 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 #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_BEGIN);
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(1));
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     auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
320     decisionMaker->lastRequestTime_ = TimeProvider::GetCurrentTime();
321     EXPECT_EQ(decisionMaker->Decide(nullptr, nullptr), ERR_BGTASK_NO_MEMORY);
322 
323     auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
324     decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime() - ALLOW_REQUEST_TIME_BG - 1;
325     EXPECT_EQ(decisionMaker->Decide(keyInfo, nullptr), ERR_BGTASK_NOT_IN_PRESET_TIME);
326     decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime();
327     EXPECT_EQ(decisionMaker->Decide(keyInfo, nullptr), ERR_BGTASK_NO_MEMORY);
328 
329     auto keyInfo2 = std::make_shared<KeyInfo>("bundleName2", 2);
330     auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName2", 2, timerManager);
331     auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1);
332     auto delayInfo2 = std::make_shared<DelaySuspendInfoEx>(2);
333     auto delayInfo3 = std::make_shared<DelaySuspendInfoEx>(3);
334     pkgDelaySuspendInfo->requestList_.push_back(delayInfo1);
335     pkgDelaySuspendInfo->requestList_.push_back(delayInfo2);
336     pkgDelaySuspendInfo->requestList_.push_back(delayInfo3);
337     decisionMaker->pkgDelaySuspendInfoMap_[keyInfo2] = pkgDelaySuspendInfo;
338     EXPECT_EQ(decisionMaker->Decide(keyInfo2, nullptr), ERR_BGTASK_EXCEEDS_THRESHOLD);
339 
340     EXPECT_EQ(decisionMaker->Decide(keyInfo2, delayInfo1), ERR_BGTASK_EXCEEDS_THRESHOLD);
341     decisionMaker->pkgDelaySuspendInfoMap_.clear();
342     deviceInfoManeger->isScreenOn_ = true;
343     EXPECT_EQ(decisionMaker->Decide(keyInfo, delayInfo1), ERR_OK);
344     decisionMaker->pkgDelaySuspendInfoMap_.clear();
345     deviceInfoManeger->isScreenOn_ = false;
346     EXPECT_EQ(decisionMaker->Decide(keyInfo, delayInfo1), ERR_OK);
347 }
348 
349 /**
350  * @tc.name: DecisionMakerTest_002
351  * @tc.desc: test DecisionMaker class misc method.
352  * @tc.type: FUNC
353  * @tc.require: issueI4QT3W issueI4QU0V
354  */
355 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_002, TestSize.Level1)
356 {
357     auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
358     auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
359     auto timerManager = std::make_shared<TimerManager>(bgtaskService);
360     auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
361 
362     decisionMaker->RemoveRequest(nullptr, -1);
363     auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
364     decisionMaker->RemoveRequest(keyInfo, -1);
365 
366     auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
367     auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1);
368     pkgDelaySuspendInfo->requestList_.push_back(delayInfo1);
369     decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
370     decisionMaker->RemoveRequest(keyInfo, -1);
371     decisionMaker->RemoveRequest(keyInfo, 1);
372 
373     decisionMaker->pkgDelaySuspendInfoMap_.clear();
374     EXPECT_EQ(decisionMaker->GetRemainingDelayTime(nullptr, -1), -1);
375     EXPECT_EQ(decisionMaker->GetRemainingDelayTime(nullptr, -1), -1);
376     decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
377     EXPECT_EQ(decisionMaker->GetRemainingDelayTime(keyInfo, -1), 0);
378 
379     EXPECT_EQ(decisionMaker->GetQuota(nullptr), -1);
380     decisionMaker->pkgDelaySuspendInfoMap_.clear();
381     EXPECT_EQ(decisionMaker->GetQuota(keyInfo), INIT_QUOTA);
382     pkgDelaySuspendInfo->quota_ = -1;
383     decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
384     EXPECT_EQ(decisionMaker->GetQuota(keyInfo), 0);
385     EXPECT_FALSE(decisionMaker->IsFrontApp("pkgName", 1));
386 
387     decisionMaker->requestId_ = INT_MAX;
388     EXPECT_EQ(decisionMaker->NewDelaySuspendRequestId(), 1);
389     EXPECT_EQ(decisionMaker->NewDelaySuspendRequestId(), TEST_NUM_TWO);
390 
391     decisionMaker->lastRequestTime_ = TimeProvider::GetCurrentTime() - 1;
392     decisionMaker->ResetDayQuotaLocked();
393     decisionMaker->lastRequestTime_ = TimeProvider::GetCurrentTime() - QUOTA_UPDATE - 1;
394     decisionMaker->pkgDelaySuspendInfoMap_.clear();
395     decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
396     auto keyInfo2 = std::make_shared<KeyInfo>("bundleName2", TEST_NUM_TWO);
397     auto pkgDelaySuspendInfo2 = std::make_shared<PkgDelaySuspendInfo>(
398         "bundleName2", TEST_NUM_TWO, timerManager);
399     decisionMaker->pkgDelaySuspendInfoMap_[keyInfo2] = pkgDelaySuspendInfo2;
400     decisionMaker->ResetDayQuotaLocked();
401 
402     EventInfo eventInfo = EventInfo();
403     eventInfo.eventId_ = 0;
404     decisionMaker->OnInputEvent(eventInfo);
405     eventInfo.eventId_ = EVENT_SCREEN_ON;
406     decisionMaker->OnInputEvent(eventInfo);
407     eventInfo.eventId_ = EVENT_SCREEN_OFF;
408     decisionMaker->OnInputEvent(eventInfo);
409     eventInfo.eventId_ = EVENT_SCREEN_UNLOCK;
410     decisionMaker->OnInputEvent(eventInfo);
411     EXPECT_TRUE(true);
412 }
413 
414 /**
415  * @tc.name: DeviceInfoManagerTest_001
416  * @tc.desc: test DeviceInfoManager class.
417  * @tc.type: FUNC
418  * @tc.require: issueI4QT3W issueI4QU0V
419  */
420 HWTEST_F(BgTaskMiscUnitTest, DeviceInfoManagerTest_001, TestSize.Level1)
421 {
422     auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
423     deviceInfoManeger->isDump_ = true;
424     EventInfo eventInfo = EventInfo();
425     std::vector<std::string> args;
426     args.emplace_back("test");
427     eventInfo.SetStringArgs(args);
428     deviceInfoManeger->OnInputEvent(eventInfo);
429     args.clear();
430     args.emplace_back("dump");
431     eventInfo.SetStringArgs(args);
432     deviceInfoManeger->isDump_ = false;
433     eventInfo.eventId_ = EVENT_SCREEN_ON;
434     deviceInfoManeger->OnInputEvent(eventInfo);
435     eventInfo.eventId_ = EVENT_SCREEN_OFF;
436     deviceInfoManeger->OnInputEvent(eventInfo);
437     eventInfo.eventId_ = EVENT_SCREEN_UNLOCK;
438     deviceInfoManeger->OnInputEvent(eventInfo);
439     eventInfo.eventId_ = EVENT_BATTERY_LOW;
440     deviceInfoManeger->OnInputEvent(eventInfo);
441     eventInfo.eventId_ = EVENT_BATTERY_OKAY;
442     deviceInfoManeger->OnInputEvent(eventInfo);
443     eventInfo.eventId_ = EVENT_MAX;
444     deviceInfoManeger->OnInputEvent(eventInfo);
445     EXPECT_TRUE(true);
446 }
447 
448 /**
449  * @tc.name: InputManagerTest_001
450  * @tc.desc: test InputManager class.
451  * @tc.type: FUNC
452  * @tc.require: issueI4QT3W issueI4QU0V
453  */
454 HWTEST_F(BgTaskMiscUnitTest, InputManagerTest_001, TestSize.Level1)
455 {
456     auto inputManager = std::make_shared<InputManager>();
457     auto deviceInfoManeger1 = std::make_shared<DeviceInfoManager>();
458     inputManager->RegisterEventListener(nullptr);
459     inputManager->RegisterEventListener(deviceInfoManeger1);
460     auto eventInfo = std::make_shared<EventInfo>();
461     inputManager->SendEventInfo(nullptr);
462     inputManager->SendEventInfo(eventInfo);
463     auto deviceInfoManeger2 = std::make_shared<DeviceInfoManager>();
464     inputManager->UnRegisterEventListener(nullptr);
465     inputManager->UnRegisterEventListener(deviceInfoManeger2);
466     EXPECT_EQ(inputManager->listenerList_.size(), 1);
467     inputManager->UnRegisterEventListener(deviceInfoManeger1);
468     EXPECT_EQ(inputManager->listenerList_.size(), 0);
469     EXPECT_TRUE(true);
470 }
471 
472 /**
473  * @tc.name: PkgDelaySuspendInfoTest_001
474  * @tc.desc: test PkgDelaySuspendInfo class.
475  * @tc.type: FUNC
476  * @tc.require: issueI4QT3W issueI4QU0V
477  */
478 HWTEST_F(BgTaskMiscUnitTest, PkgDelaySuspendInfoTest_001, TestSize.Level1)
479 {
480     auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
481     auto timerManager = std::make_shared<TimerManager>(bgtaskService);
482     auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
483     pkgDelaySuspendInfo->isCounting_ = true;
484     pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime() + MIN_ALLOW_QUOTA_TIME;
485     EXPECT_EQ(pkgDelaySuspendInfo->IsAllowRequest(), ERR_OK);
486     pkgDelaySuspendInfo->quota_ = 0;
487     pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime();
488     EXPECT_EQ(pkgDelaySuspendInfo->IsAllowRequest(), ERR_BGTASK_TIME_INSUFFICIENT);
489     auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1, 1);
490     auto delayInfo2 = std::make_shared<DelaySuspendInfoEx>(1, 2);
491     pkgDelaySuspendInfo->AddRequest(nullptr, 1);
492     pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
493     pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
494     pkgDelaySuspendInfo->AddRequest(delayInfo2, 1);
495     EXPECT_EQ(pkgDelaySuspendInfo->IsAllowRequest(), ERR_BGTASK_EXCEEDS_THRESHOLD);
496     pkgDelaySuspendInfo->RemoveRequest(1);
497     pkgDelaySuspendInfo->RemoveRequest(1);
498     pkgDelaySuspendInfo->RemoveRequest(2);
499 
500     pkgDelaySuspendInfo->requestList_.clear();
501     EXPECT_EQ(pkgDelaySuspendInfo->GetRemainDelayTime(-1), 0);
502     delayInfo1->actualDelayTime_ = 1;
503     auto delayInfo3 = std::make_shared<DelaySuspendInfoEx>(1, 1, 1);
504     pkgDelaySuspendInfo->requestList_.emplace_back(delayInfo3);
505     EXPECT_EQ(pkgDelaySuspendInfo->GetRemainDelayTime(-1), 0);
506     EXPECT_EQ(pkgDelaySuspendInfo->GetRemainDelayTime(1), 1);
507 }
508 
509 /**
510  * @tc.name: PkgDelaySuspendInfoTest_002
511  * @tc.desc: test PkgDelaySuspendInfo class.
512  * @tc.type: FUNC
513  * @tc.require: issueI4QT3W issueI4QU0V
514  */
515 HWTEST_F(BgTaskMiscUnitTest, PkgDelaySuspendInfoTest_002, TestSize.Level1)
516 {
517     auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
518     auto timerManager = std::make_shared<TimerManager>(bgtaskService);
519     auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
520     auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1, 1);
521 
522     pkgDelaySuspendInfo->requestList_.clear();
523     pkgDelaySuspendInfo->StartAccounting(1);
524     pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
525     pkgDelaySuspendInfo->StartAccounting(2);
526     pkgDelaySuspendInfo->StartAccounting(-1);
527     pkgDelaySuspendInfo->isCounting_ = false;
528     pkgDelaySuspendInfo->baseTime_ = 0;
529     pkgDelaySuspendInfo->StartAccounting(1);
530     pkgDelaySuspendInfo->isCounting_ = true;
531     pkgDelaySuspendInfo->baseTime_ = 1;
532     pkgDelaySuspendInfo->StartAccounting(1);
533 
534     pkgDelaySuspendInfo->requestList_.clear();
535     pkgDelaySuspendInfo->StopAccounting(1);
536     pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
537     pkgDelaySuspendInfo->StopAccounting(-1);
538     pkgDelaySuspendInfo->baseTime_ = 0;
539     pkgDelaySuspendInfo->StopAccounting(1);
540     pkgDelaySuspendInfo->baseTime_ = 1;
541     pkgDelaySuspendInfo->StopAccounting(1);
542     pkgDelaySuspendInfo->requestList_.clear();
543     pkgDelaySuspendInfo->StopAccountingAll();
544     pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
545     pkgDelaySuspendInfo->baseTime_ = 0;
546     pkgDelaySuspendInfo->StopAccounting(1);
547     pkgDelaySuspendInfo->baseTime_ = 1;
548     pkgDelaySuspendInfo->StopAccounting(1);
549 
550     pkgDelaySuspendInfo->isCounting_ = true;
551     pkgDelaySuspendInfo->quota_ = 0;
552     pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime() - 1;
553     pkgDelaySuspendInfo->UpdateQuota(false);
554     pkgDelaySuspendInfo->quota_ = 0;
555     pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime() + 1;
556     pkgDelaySuspendInfo->UpdateQuota(true);
557     EXPECT_TRUE(true);
558 }
559 
560 /**
561  * @tc.name: SuspendControllerTest_001
562  * @tc.desc: test SuspendController.
563  * @tc.type: FUNC
564  * @tc.require: issueI4QT3W issueI4QU0V
565  */
566 HWTEST_F(BgTaskMiscUnitTest, SuspendControllerTest_001, TestSize.Level1)
567 {
568     SuspendController suspendController = SuspendController();
569     auto keyInfo = std::make_shared<KeyInfo>("bundleName", 1);
570     suspendController.RequestSuspendDelay(nullptr);
571     suspendController.RequestSuspendDelay(keyInfo);
572     suspendController.CancelSuspendDelay(nullptr);
573     suspendController.CancelSuspendDelay(keyInfo);
574     EXPECT_TRUE(true);
575 }
576 
577 /**
578  * @tc.name: WatchdogTest_001
579  * @tc.desc: test Watchdog class.
580  * @tc.type: FUNC
581  * @tc.require: issueI4QT3W issueI4QU0V
582  */
583 HWTEST_F(BgTaskMiscUnitTest, WatchdogTest_001, TestSize.Level1)
584 {
585     auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
586     auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
587     auto timerManager = std::make_shared<TimerManager>(bgtaskService);
588     auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
589     auto watchdog = std::make_shared<Watchdog>(bgtaskService, decisionMaker);
590     EXPECT_TRUE(watchdog->KillApplicationByUid("bundleName", 1));
591     EXPECT_TRUE(watchdog->KillApplicationByUid("bundleName", 1));
592 }
593 
594 /**
595  * @tc.name: ConfigChangeObserver_001
596  * @tc.desc: test Watchdog class.
597  * @tc.type: FUNC
598  * @tc.require: issueI4QT3W issueI4QU0V
599  */
600 HWTEST_F(BgTaskMiscUnitTest, ConfigChangeObserver_001, TestSize.Level1)
601 {
602     sptr<ConfigChangeObserver> configChangeObserver1 = sptr<ConfigChangeObserver>(
603         new ConfigChangeObserver(nullptr, nullptr));
604     EXPECT_FALSE(configChangeObserver1->CheckParamValid());
605     AppExecFwk::Configuration configuration;
606     configChangeObserver1->OnConfigurationUpdated(configuration);
607     auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(nullptr);
608     auto bgContinuousTaskMgr = std::make_shared<BgContinuousTaskMgr>();
609     sptr<ConfigChangeObserver> configChangeObserver2 = sptr<ConfigChangeObserver>(
610         new ConfigChangeObserver(handler, nullptr));
611     EXPECT_FALSE(configChangeObserver2->CheckParamValid());
612     sptr<ConfigChangeObserver> configChangeObserver3 = sptr<ConfigChangeObserver>(
613         new ConfigChangeObserver(handler, bgContinuousTaskMgr));
614     EXPECT_TRUE(configChangeObserver3->CheckParamValid());
615     configChangeObserver3->OnConfigurationUpdated(configuration);
616 }
617 
618 /**
619  * @tc.name: DataStorageHelper_001
620  * @tc.desc: test Watchdog class.
621  * @tc.type: FUNC
622  * @tc.require: issueI4QT3W issueI4QU0V
623  */
624 HWTEST_F(BgTaskMiscUnitTest, DataStorageHelper_001, TestSize.Level1)
625 {
626     std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap1;
627     auto continuousTaskRecord = std::make_shared<ContinuousTaskRecord>();
628     continuousTaskInfosMap1.emplace("key", continuousTaskRecord);
629     DelayedSingleton<DataStorageHelper>::GetInstance()->RefreshTaskRecord(continuousTaskInfosMap1);
630     std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap2;
631     EXPECT_EQ(DelayedSingleton<DataStorageHelper>::GetInstance()->RestoreTaskRecord(continuousTaskInfosMap2),
632         ERR_OK);
633     EXPECT_EQ(DelayedSingleton<DataStorageHelper>::GetInstance()->SaveJsonValueToFile("", ""),
634         ERR_BGTASK_CREATE_FILE_ERR);
635     nlohmann::json json1;
636     EXPECT_EQ(DelayedSingleton<DataStorageHelper>::GetInstance()->ParseJsonValueFromFile(json1, ""),
637         ERR_BGTASK_DATA_STORAGE_ERR);
638     EXPECT_FALSE(DelayedSingleton<DataStorageHelper>::GetInstance()->CreateNodeFile(""));
639     std::string fullPath;
640     EXPECT_FALSE(DelayedSingleton<DataStorageHelper>::GetInstance()->ConvertFullPath("", fullPath));
641 }
642 
643 /**
644  * @tc.name: DecisionMakerTest_003
645  * @tc.desc: test Watchdog class.
646  * @tc.type: FUNC
647  * @tc.require: issueI4QT3W issueI4QU0V
648  */
649 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_003, TestSize.Level1)
650 {
651     auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
652     auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
653     auto timerManager = std::make_shared<TimerManager>(bgtaskService);
654     auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
655     auto applicationStateObserver = sptr<DecisionMaker::ApplicationStateObserver>(
656         new (std::nothrow) DecisionMaker::ApplicationStateObserver(*decisionMaker));
657 
658     AppExecFwk::AppStateData appStateData;
659     appStateData.uid = 1;
660     appStateData.bundleName = "bundleName1";
661     appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND);
662     applicationStateObserver->OnForegroundApplicationChanged(appStateData);
663     appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOCUS);
664     applicationStateObserver->OnForegroundApplicationChanged(appStateData);
665 
666     auto keyInfo1 = std::make_shared<KeyInfo>("bundleName1", 1);
667     auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
668     auto delayInfo = std::make_shared<DelaySuspendInfoEx>(1);
669     pkgDelaySuspendInfo->requestList_.push_back(delayInfo);
670     decisionMaker->pkgDelaySuspendInfoMap_[keyInfo1] = pkgDelaySuspendInfo;
671     auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
672     decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime() - ALLOW_REQUEST_TIME_BG - 1;
673     appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND);
674     applicationStateObserver->OnForegroundApplicationChanged(appStateData);
675     appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOCUS);
676     applicationStateObserver->OnForegroundApplicationChanged(appStateData);
677 
678     decisionMaker->pkgDelaySuspendInfoMap_.clear();
679     appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_BACKGROUND);
680     applicationStateObserver->OnForegroundApplicationChanged(appStateData);
681     decisionMaker->pkgDelaySuspendInfoMap_[keyInfo1] = pkgDelaySuspendInfo;
682     applicationStateObserver->OnForegroundApplicationChanged(appStateData);
683     EXPECT_EQ((int32_t)decisionMaker->pkgDelaySuspendInfoMap_.size(), 1);
684 }
685 
686 /**
687  * @tc.name: DelaySuspendInfoEx_001
688  * @tc.desc: test DelaySuspendInfoEx.
689  * @tc.type: FUNC
690  * @tc.require: issueI4QT3W issueI4QU0V
691  */
692 HWTEST_F(BgTaskMiscUnitTest, DelaySuspendInfoEx_001, TestSize.Level1)
693 {
694     auto delayInfo = std::make_shared<DelaySuspendInfoEx>(1);
695     delayInfo->baseTime_ = 1;
696     delayInfo->StartAccounting();
697     delayInfo->baseTime_ = 0;
698     delayInfo->StopAccounting();
699     EXPECT_EQ(delayInfo->spendTime_, 0);
700 }
701 }
702 }
703