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->OnProcessDiedEfficiencyRes(processData);
100 AppExecFwk::AbilityStateData abilityStateData = AppExecFwk::AbilityStateData();
101 appStateObserver->OnAbilityStateChanged(abilityStateData);
102 auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(nullptr);
103 appStateObserver->SetEventHandler(handler);
104 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED);
105 appStateObserver->OnAbilityStateChanged(abilityStateData);
106 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_CREATE);
107 appStateObserver->OnAbilityStateChanged(abilityStateData);
108 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED);
109 appStateObserver->OnAbilityStateChanged(abilityStateData);
110 AppExecFwk::AppStateData appStateData = AppExecFwk::AppStateData();
111 appStateObserver->OnAppStopped(appStateData);
112 appStateData.uid = 1;
113 appStateData.bundleName = "bundleName";
114 appStateObserver->OnAppStopped(appStateData);
115 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_TERMINATED);
116 appStateObserver->OnAppStopped(appStateData);
117 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_END);
118 appStateObserver->OnAppStopped(appStateData);
119 appStateObserver->OnAppStopped(appStateData);
120 }
121
122 /**
123 * @tc.name: BundleManagerHelperTest_001
124 * @tc.desc: test BundleManagerHelper class.
125 * @tc.type: FUNC
126 * @tc.require: issueI4QT3W issueI4QU0V
127 */
128 HWTEST_F(BgTaskMiscUnitTest, BundleManagerHelperTest_001, TestSize.Level1)
129 {
130 EXPECT_EQ(BundleManagerHelper::GetInstance()->GetClientBundleName(1), "");
131 EXPECT_FALSE(BundleManagerHelper::GetInstance()->CheckPermission("permission"));
132 EXPECT_FALSE(BundleManagerHelper::GetInstance()->IsSystemApp(1LLU));
133 AppExecFwk::BundleInfo bundleInfo;
134 EXPECT_FALSE(BundleManagerHelper::GetInstance()->GetBundleInfo("bundleName",
135 AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo));
136 AppExecFwk::ApplicationInfo applicationInfo;
137 EXPECT_FALSE(BundleManagerHelper::GetInstance()->GetApplicationInfo("bundleName",
138 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, 100, applicationInfo));
139 BundleManagerHelper::GetInstance()->OnRemoteDied(nullptr);
140 BundleManagerHelper::GetInstance()->bundleMgr_ = nullptr;
141 BundleManagerHelper::GetInstance()->OnRemoteDied(nullptr);
142 AAFwk::Want want;
143 want.SetAction("action.system.home");
144 want.AddEntity("entity.system.home");
145 want.SetElementName("", "bundleName", "", "");
146 AppExecFwk::AbilityInfo abilityInfo;
147 BundleManagerHelper::GetInstance()->QueryAbilityInfo(want,
148 AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION, 0, abilityInfo);
149 BundleManagerHelper::GetInstance()->bundleMgr_ = nullptr;
150 BundleManagerHelper::GetInstance()->QueryAbilityInfo(want,
151 AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION, 0, abilityInfo);
152 }
153
154 /**
155 * @tc.name: SystemEventObserverTest_001
156 * @tc.desc: test SystemEventObserver class.
157 * @tc.type: FUNC
158 * @tc.require: issueI4QT3W issueI4QU0V
159 */
160 HWTEST_F(BgTaskMiscUnitTest, SystemEventObserver_001, TestSize.Level1)
161 {
162 EventFwk::MatchingSkills matchingSkills;
163 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
164 EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
165 auto systemEventListener = std::make_shared<SystemEventObserver>(commonEventSubscribeInfo);
166 EXPECT_TRUE(systemEventListener->Subscribe());
167 EXPECT_TRUE(systemEventListener->Unsubscribe());
168
169 EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
170 systemEventListener->OnReceiveEvent(eventData);
171
172 auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(nullptr);
173 systemEventListener->SetEventHandler(handler);
174 systemEventListener->OnReceiveEventContinuousTask(eventData);
175 auto bgContinuousTaskMgr = std::make_shared<BgContinuousTaskMgr>();
176 systemEventListener->SetBgContinuousTaskMgr(bgContinuousTaskMgr);
177 systemEventListener->OnReceiveEventContinuousTask(eventData);
178 AAFwk::Want want = AAFwk::Want();
179 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
180 eventData.SetWant(want);
181 systemEventListener->OnReceiveEventContinuousTask(eventData);
182 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED);
183 eventData.SetWant(want);
184 systemEventListener->OnReceiveEventContinuousTask(eventData);
185 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
186 eventData.SetWant(want);
187 systemEventListener->OnReceiveEventContinuousTask(eventData);
188
189 EventFwk::CommonEventData eventData2 = EventFwk::CommonEventData();
190 AAFwk::Want want2 = AAFwk::Want();
191 want2.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
192 eventData2.SetWant(want2);
193 systemEventListener->OnReceiveEventEfficiencyRes(eventData2);
194 EXPECT_TRUE(true);
195 }
196
197 /**
198 * @tc.name: ContinuousTaskRecordTest_001
199 * @tc.desc: test ContinuousTaskRecord class.
200 * @tc.type: FUNC
201 * @tc.require: issueI4QT3W issueI4QU0V
202 */
203 HWTEST_F(BgTaskMiscUnitTest, ContinuousTaskRecordTest_001, TestSize.Level1)
204 {
205 ContinuousTaskRecord record = ContinuousTaskRecord();
206 std::string str1 = record.ParseToJsonStr();
207 EXPECT_NE(str1, "");
208 record.wantAgentInfo_ = std::make_shared<WantAgentInfo>();
209 std::string str2 = record.ParseToJsonStr();
210 EXPECT_NE(str2, "");
211 ContinuousTaskRecord record2 = ContinuousTaskRecord();
212 nlohmann::json json2 = nlohmann::json::parse("", nullptr, false);
213 EXPECT_FALSE(record2.ParseFromJson(json2));
214 nlohmann::json json3;
215 json3["bundleName"] = "bundleName";
216 EXPECT_FALSE(record2.ParseFromJson(json3));
217 nlohmann::json json4 = nlohmann::json::parse(str1, nullptr, false);
218 ContinuousTaskRecord record3 = ContinuousTaskRecord();
219 EXPECT_TRUE(record3.ParseFromJson(json4));
220 nlohmann::json json5 = nlohmann::json::parse(str2, nullptr, false);
221 ContinuousTaskRecord record4 = ContinuousTaskRecord();
222 EXPECT_TRUE(record4.ParseFromJson(json5));
223 }
224
225 /**
226 * @tc.name: NotificationToolsTest_001
227 * @tc.desc: test NotificationTools class.
228 * @tc.type: FUNC
229 * @tc.require: issueI4QT3W issueI4QU0V
230 */
231 HWTEST_F(BgTaskMiscUnitTest, NotificationToolsTest_001, TestSize.Level1)
232 {
233 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
234 auto taskRecord = std::make_shared<ContinuousTaskRecord>();
235 NotificationTools::GetInstance()->PublishNotification(taskRecord, "appName", "prompt", 1);
236 SetPublishContinuousTaskNotificationFlag(1);
237 EXPECT_EQ(NotificationTools::GetInstance()->PublishNotification(taskRecord, "appName", "prompt", 1),
238 ERR_BGTASK_NOTIFICATION_ERR);
239 NotificationTools::GetInstance()->CancelNotification("label", 0);
240 SetCancelContinuousTaskNotificationFlag(1);
241 EXPECT_EQ(NotificationTools::GetInstance()->CancelNotification("label", 0), ERR_BGTASK_NOTIFICATION_ERR);
242 std::set<std::string> notificationLabels;
243 NotificationTools::GetInstance()->GetAllActiveNotificationsLabels(notificationLabels);
244
245 std::map<std::string, std::pair<std::string, std::string>> newPromptInfos;
246 newPromptInfos.emplace("label", std::make_pair<std::string, std::string>("test1", "test2"));
247 SetGetAllActiveNotificationsFlag(TEST_NUM_ONE);
248 SetPublishContinuousTaskNotificationFlag(0);
249 SetPublishContinuousTaskNotificationFlag(0);
250 NotificationTools::GetInstance()->RefreshContinuousNotifications(newPromptInfos, 0);
251 SetGetAllActiveNotificationsFlag(TEST_NUM_TWO);
252 SetPublishContinuousTaskNotificationFlag(1);
253 NotificationTools::GetInstance()->RefreshContinuousNotifications(newPromptInfos, 0);
254 #endif
255 }
256
257 /**
258 * @tc.name: TaskNotificationSubscriber_001
259 * @tc.desc: test TaskNotificationSubscriber class.
260 * @tc.type: FUNC
261 * @tc.require: issueI4QT3W issueI4QU0V
262 */
263 HWTEST_F(BgTaskMiscUnitTest, TaskNotificationSubscriber_001, TestSize.Level1)
264 {
265 auto subscriber = std::make_shared<TaskNotificationSubscriber>();
266 subscriber->OnCanceled(nullptr, nullptr, 1);
267 auto notificationMap = std::make_shared<Notification::NotificationSortingMap>();
268 auto notificationRequest = sptr<Notification::NotificationRequest>(new Notification::NotificationRequest());
269 auto notification = std::make_shared<Notification::Notification>(notificationRequest);
270 subscriber->OnCanceled(notification, notificationMap, 1);
271 BgContinuousTaskMgr::GetInstance()->bgTaskUid_ = BGTASKMGR_UID;
272
273 notification->request_->creatorUid_ = BGTASKMGR_UID;
274 subscriber->OnCanceled(notification, notificationMap, 1);
275 notification->request_->label_ = "label";
276 subscriber->OnCanceled(notification, notificationMap, 1);
277 notification->request_->label_ = "bgmode_1";
278 subscriber->OnCanceled(notification, notificationMap, 1);
279 notification->request_->label_ = "bgmode_1_1";
280 subscriber->OnCanceled(notification, notificationMap, 1);
281 subscriber->OnCanceled(notification, notificationMap,
282 Notification::NotificationConstant::APP_CANCEL_REASON_DELETE);
283
284 std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<AAFwk::WantParams>();
285 extraInfo->SetParam("abilityName", AAFwk::String::Box("abilityName"));
286 notification->request_->additionalParams_ = extraInfo;
287 subscriber->OnCanceled(notification, notificationMap,
288 Notification::NotificationConstant::USER_STOPPED_REASON_DELETE);
289
290 std::shared_ptr<ContinuousTaskRecord> continuousTaskRecord = std::make_shared<ContinuousTaskRecord>();
291 BgContinuousTaskMgr::GetInstance()->continuousTaskInfosMap_["1_abilityName"] = continuousTaskRecord;
292 subscriber->OnCanceled(notification, notificationMap,
293 Notification::NotificationConstant::USER_STOPPED_REASON_DELETE);
294 EXPECT_TRUE(true);
295 }
296
297 /**
298 * @tc.name: DecisionMakerTest_001
299 * @tc.desc: test DecisionMaker class decide method.
300 * @tc.type: FUNC
301 * @tc.require: issueI4QT3W issueI4QU0V
302 */
303 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_001, TestSize.Level1)
304 {
305 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
306 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
307 auto timerManager = std::make_shared<TimerManager>(bgtaskService,
308 AppExecFwk::EventRunner::Create("tdd_test_handler"));
309 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
310 decisionMaker->lastRequestTime_ = TimeProvider::GetCurrentTime();
311 EXPECT_EQ(decisionMaker->Decide(nullptr, nullptr), ERR_BGTASK_NO_MEMORY);
312
313 auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
314 auto delayInfo = std::make_shared<DelaySuspendInfoEx>(1);
315 decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime() - ALLOW_REQUEST_TIME_BG - 1;
316 EXPECT_EQ(decisionMaker->Decide(keyInfo, delayInfo), ERR_BGTASK_NOT_IN_PRESET_TIME);
317 decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime();
318 EXPECT_EQ(decisionMaker->Decide(keyInfo, nullptr), ERR_BGTASK_NO_MEMORY);
319
320 auto keyInfo2 = std::make_shared<KeyInfo>("bundleName2", 2);
321 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName2", 2, timerManager);
322 auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1);
323 auto delayInfo2 = std::make_shared<DelaySuspendInfoEx>(2);
324 auto delayInfo3 = std::make_shared<DelaySuspendInfoEx>(3);
325 pkgDelaySuspendInfo->requestList_.push_back(delayInfo1);
326 pkgDelaySuspendInfo->requestList_.push_back(delayInfo2);
327 pkgDelaySuspendInfo->requestList_.push_back(delayInfo3);
328 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo2] = pkgDelaySuspendInfo;
329 EXPECT_EQ(decisionMaker->Decide(keyInfo2, delayInfo1), ERR_BGTASK_EXCEEDS_THRESHOLD);
330 decisionMaker->pkgDelaySuspendInfoMap_.clear();
331 deviceInfoManeger->isScreenOn_ = true;
332 EXPECT_EQ(decisionMaker->Decide(keyInfo, delayInfo1), ERR_OK);
333 decisionMaker->pkgDelaySuspendInfoMap_.clear();
334 deviceInfoManeger->isScreenOn_ = false;
335 EXPECT_EQ(decisionMaker->Decide(keyInfo, delayInfo1), ERR_OK);
336 }
337
338 /**
339 * @tc.name: DecisionMakerTest_002
340 * @tc.desc: test DecisionMaker class misc method.
341 * @tc.type: FUNC
342 * @tc.require: issueI4QT3W issueI4QU0V
343 */
344 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_002, TestSize.Level1)
345 {
346 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
347 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
348 auto timerManager = std::make_shared<TimerManager>(bgtaskService,
349 AppExecFwk::EventRunner::Create("tdd_test_handler"));
350 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
351
352 decisionMaker->RemoveRequest(nullptr, -1);
353 auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
354 decisionMaker->RemoveRequest(keyInfo, -1);
355
356 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
357 auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1);
358 pkgDelaySuspendInfo->requestList_.push_back(delayInfo1);
359 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
360 decisionMaker->RemoveRequest(keyInfo, -1);
361 decisionMaker->RemoveRequest(keyInfo, 1);
362
363 decisionMaker->pkgDelaySuspendInfoMap_.clear();
364 EXPECT_EQ(decisionMaker->GetRemainingDelayTime(nullptr, -1), -1);
365 EXPECT_EQ(decisionMaker->GetRemainingDelayTime(nullptr, -1), -1);
366 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
367 EXPECT_EQ(decisionMaker->GetRemainingDelayTime(keyInfo, -1), 0);
368
369 EXPECT_EQ(decisionMaker->GetQuota(nullptr), -1);
370 decisionMaker->pkgDelaySuspendInfoMap_.clear();
371 EXPECT_EQ(decisionMaker->GetQuota(keyInfo), INIT_QUOTA);
372 pkgDelaySuspendInfo->quota_ = -1;
373 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
374 EXPECT_EQ(decisionMaker->GetQuota(keyInfo), 0);
375 EXPECT_FALSE(decisionMaker->IsFrontApp("pkgName", 1));
376
377 decisionMaker->requestId_ = INT_MAX;
378 EXPECT_EQ(decisionMaker->NewDelaySuspendRequestId(), 1);
379 EXPECT_EQ(decisionMaker->NewDelaySuspendRequestId(), TEST_NUM_TWO);
380
381 decisionMaker->lastRequestTime_ = TimeProvider::GetCurrentTime() - 1;
382 decisionMaker->ResetDayQuotaLocked();
383 decisionMaker->lastRequestTime_ = TimeProvider::GetCurrentTime() - QUOTA_UPDATE - 1;
384 decisionMaker->pkgDelaySuspendInfoMap_.clear();
385 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo] = pkgDelaySuspendInfo;
386 auto keyInfo2 = std::make_shared<KeyInfo>("bundleName2", TEST_NUM_TWO);
387 auto pkgDelaySuspendInfo2 = std::make_shared<PkgDelaySuspendInfo>("bundleName2", TEST_NUM_TWO, timerManager);
388 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo2] = pkgDelaySuspendInfo2;
389 decisionMaker->ResetDayQuotaLocked();
390
391 EventInfo eventInfo = EventInfo();
392 eventInfo.eventId_ = 0;
393 decisionMaker->OnInputEvent(eventInfo);
394 eventInfo.eventId_ = EVENT_SCREEN_ON;
395 decisionMaker->OnInputEvent(eventInfo);
396 eventInfo.eventId_ = EVENT_SCREEN_OFF;
397 decisionMaker->OnInputEvent(eventInfo);
398 eventInfo.eventId_ = EVENT_SCREEN_UNLOCK;
399 decisionMaker->OnInputEvent(eventInfo);
400 EXPECT_TRUE(true);
401 }
402
403 /**
404 * @tc.name: DeviceInfoManagerTest_001
405 * @tc.desc: test DeviceInfoManager class.
406 * @tc.type: FUNC
407 * @tc.require: issueI4QT3W issueI4QU0V
408 */
409 HWTEST_F(BgTaskMiscUnitTest, DeviceInfoManagerTest_001, TestSize.Level1)
410 {
411 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
412 deviceInfoManeger->isDump_ = true;
413 EventInfo eventInfo = EventInfo();
414 std::vector<std::string> args;
415 args.emplace_back("test");
416 eventInfo.SetStringArgs(args);
417 deviceInfoManeger->OnInputEvent(eventInfo);
418 args.clear();
419 args.emplace_back("dump");
420 eventInfo.SetStringArgs(args);
421 deviceInfoManeger->isDump_ = false;
422 eventInfo.eventId_ = EVENT_SCREEN_ON;
423 deviceInfoManeger->OnInputEvent(eventInfo);
424 eventInfo.eventId_ = EVENT_SCREEN_OFF;
425 deviceInfoManeger->OnInputEvent(eventInfo);
426 eventInfo.eventId_ = EVENT_SCREEN_UNLOCK;
427 deviceInfoManeger->OnInputEvent(eventInfo);
428 eventInfo.eventId_ = EVENT_BATTERY_LOW;
429 deviceInfoManeger->OnInputEvent(eventInfo);
430 eventInfo.eventId_ = EVENT_BATTERY_OKAY;
431 deviceInfoManeger->OnInputEvent(eventInfo);
432 eventInfo.eventId_ = EVENT_MAX;
433 deviceInfoManeger->OnInputEvent(eventInfo);
434 EXPECT_TRUE(true);
435 }
436
437 /**
438 * @tc.name: InputManagerTest_001
439 * @tc.desc: test InputManager class.
440 * @tc.type: FUNC
441 * @tc.require: issueI4QT3W issueI4QU0V
442 */
443 HWTEST_F(BgTaskMiscUnitTest, InputManagerTest_001, TestSize.Level1)
444 {
445 auto inputManager = std::make_shared<InputManager>(AppExecFwk::EventRunner::Create("tdd_test_handler"));
446 auto deviceInfoManeger1 = std::make_shared<DeviceInfoManager>();
447 inputManager->RegisterEventListener(nullptr);
448 inputManager->RegisterEventListener(deviceInfoManeger1);
449 auto eventInfo = std::make_shared<EventInfo>();
450 inputManager->SendEventInfo(nullptr);
451 inputManager->SendEventInfo(eventInfo);
452 auto deviceInfoManeger2 = std::make_shared<DeviceInfoManager>();
453 inputManager->UnRegisterEventListener(nullptr);
454 inputManager->UnRegisterEventListener(deviceInfoManeger2);
455 EXPECT_EQ(inputManager->listenerList_.size(), 1);
456 inputManager->UnRegisterEventListener(deviceInfoManeger1);
457 EXPECT_EQ(inputManager->listenerList_.size(), 0);
458 EXPECT_TRUE(true);
459 }
460
461 /**
462 * @tc.name: PkgDelaySuspendInfoTest_001
463 * @tc.desc: test PkgDelaySuspendInfo class.
464 * @tc.type: FUNC
465 * @tc.require: issueI4QT3W issueI4QU0V
466 */
467 HWTEST_F(BgTaskMiscUnitTest, PkgDelaySuspendInfoTest_001, TestSize.Level1)
468 {
469 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
470 auto timerManager = std::make_shared<TimerManager>(bgtaskService,
471 AppExecFwk::EventRunner::Create("tdd_test_handler"));
472 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
473 pkgDelaySuspendInfo->isCounting_ = true;
474 pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime() + MIN_ALLOW_QUOTA_TIME;
475 EXPECT_EQ(pkgDelaySuspendInfo->IsAllowRequest(), ERR_OK);
476 pkgDelaySuspendInfo->quota_ = 0;
477 pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime();
478 EXPECT_EQ(pkgDelaySuspendInfo->IsAllowRequest(), ERR_BGTASK_TIME_INSUFFICIENT);
479 auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1, 1);
480 auto delayInfo2 = std::make_shared<DelaySuspendInfoEx>(1, 2);
481 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
482 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
483 pkgDelaySuspendInfo->AddRequest(delayInfo2, 1);
484 EXPECT_EQ(pkgDelaySuspendInfo->IsAllowRequest(), ERR_BGTASK_EXCEEDS_THRESHOLD);
485 pkgDelaySuspendInfo->RemoveRequest(1);
486 pkgDelaySuspendInfo->RemoveRequest(1);
487 pkgDelaySuspendInfo->RemoveRequest(2);
488
489 pkgDelaySuspendInfo->requestList_.clear();
490 EXPECT_EQ(pkgDelaySuspendInfo->GetRemainDelayTime(-1), 0);
491 delayInfo1->actualDelayTime_ = 1;
492 auto delayInfo3 = std::make_shared<DelaySuspendInfoEx>(1, 1, 1);
493 pkgDelaySuspendInfo->requestList_.emplace_back(delayInfo3);
494 EXPECT_EQ(pkgDelaySuspendInfo->GetRemainDelayTime(-1), 0);
495 EXPECT_EQ(pkgDelaySuspendInfo->GetRemainDelayTime(1), 1);
496 }
497
498 /**
499 * @tc.name: PkgDelaySuspendInfoTest_002
500 * @tc.desc: test PkgDelaySuspendInfo class.
501 * @tc.type: FUNC
502 * @tc.require: issueI4QT3W issueI4QU0V
503 */
504 HWTEST_F(BgTaskMiscUnitTest, PkgDelaySuspendInfoTest_002, TestSize.Level1)
505 {
506 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
507 auto timerManager =
508 std::make_shared<TimerManager>(bgtaskService, AppExecFwk::EventRunner::Create("tdd_test_handler"));
509 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
510 auto delayInfo1 = std::make_shared<DelaySuspendInfoEx>(1, 1);
511
512 pkgDelaySuspendInfo->requestList_.clear();
513 pkgDelaySuspendInfo->StartAccounting(1);
514 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
515 pkgDelaySuspendInfo->StartAccounting(2);
516 pkgDelaySuspendInfo->StartAccounting(-1);
517 pkgDelaySuspendInfo->isCounting_ = false;
518 pkgDelaySuspendInfo->baseTime_ = 0;
519 pkgDelaySuspendInfo->StartAccounting(1);
520 pkgDelaySuspendInfo->isCounting_ = true;
521 pkgDelaySuspendInfo->baseTime_ = 1;
522 pkgDelaySuspendInfo->StartAccounting(1);
523
524 pkgDelaySuspendInfo->requestList_.clear();
525 pkgDelaySuspendInfo->StopAccounting(1);
526 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
527 pkgDelaySuspendInfo->StopAccounting(-1);
528 pkgDelaySuspendInfo->baseTime_ = 0;
529 pkgDelaySuspendInfo->StopAccounting(1);
530 pkgDelaySuspendInfo->baseTime_ = 1;
531 pkgDelaySuspendInfo->StopAccounting(1);
532 pkgDelaySuspendInfo->requestList_.clear();
533 pkgDelaySuspendInfo->StopAccountingAll();
534 pkgDelaySuspendInfo->AddRequest(delayInfo1, 1);
535 pkgDelaySuspendInfo->baseTime_ = 0;
536 pkgDelaySuspendInfo->StopAccounting(1);
537 pkgDelaySuspendInfo->baseTime_ = 1;
538 pkgDelaySuspendInfo->StopAccounting(1);
539
540 pkgDelaySuspendInfo->isCounting_ = true;
541 pkgDelaySuspendInfo->quota_ = 0;
542 pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime() - 1;
543 pkgDelaySuspendInfo->UpdateQuota(false);
544 pkgDelaySuspendInfo->quota_ = 0;
545 pkgDelaySuspendInfo->baseTime_ = (int32_t)TimeProvider::GetCurrentTime() + 1;
546 pkgDelaySuspendInfo->UpdateQuota(true);
547 EXPECT_TRUE(true);
548 }
549
550 /**
551 * @tc.name: SuspendControllerTest_001
552 * @tc.desc: test SuspendController.
553 * @tc.type: FUNC
554 * @tc.require: issueI4QT3W issueI4QU0V
555 */
556 HWTEST_F(BgTaskMiscUnitTest, SuspendControllerTest_001, TestSize.Level1)
557 {
558 SuspendController suspendController = SuspendController();
559 auto keyInfo = std::make_shared<KeyInfo>("bundleName", 1);
560 suspendController.RequestSuspendDelay(nullptr);
561 suspendController.RequestSuspendDelay(keyInfo);
562 suspendController.CancelSuspendDelay(nullptr);
563 suspendController.CancelSuspendDelay(keyInfo);
564 EXPECT_TRUE(true);
565 }
566
567 /**
568 * @tc.name: WatchdogTest_001
569 * @tc.desc: test Watchdog class.
570 * @tc.type: FUNC
571 * @tc.require: issueI4QT3W issueI4QU0V
572 */
573 HWTEST_F(BgTaskMiscUnitTest, WatchdogTest_001, TestSize.Level1)
574 {
575 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
576 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
577 auto timerManager =
578 std::make_shared<TimerManager>(bgtaskService, AppExecFwk::EventRunner::Create("tdd_test_handler"));
579 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
580 auto watchdog = std::make_shared<Watchdog>(bgtaskService, decisionMaker,
581 AppExecFwk::EventRunner::Create("tdd_test_handler"));
582 EXPECT_TRUE(watchdog->KillApplicationByUid("bundleName", 1));
583 EXPECT_TRUE(watchdog->KillApplicationByUid("bundleName", 1));
584 }
585
586 /**
587 * @tc.name: ConfigChangeObserver_001
588 * @tc.desc: test ConfigChangeObserver class.
589 * @tc.type: FUNC
590 * @tc.require: issueI4QT3W issueI4QU0V
591 */
592 HWTEST_F(BgTaskMiscUnitTest, ConfigChangeObserver_001, TestSize.Level1)
593 {
594 // Given
595 sptr<ConfigChangeObserver> configChangeObserver1 = sptr<ConfigChangeObserver>(
596 new ConfigChangeObserver(nullptr, nullptr));
597
598 // When & Then
599 EXPECT_FALSE(configChangeObserver1->CheckExpired());
600 }
601
602 /**
603 * @tc.name: ConfigChangeObserver_002
604 * @tc.desc: test ConfigChangeObserver class.
605 * @tc.type: FUNC
606 * @tc.require: issueI4QT3W issueI4QU0V
607 */
608 HWTEST_F(BgTaskMiscUnitTest, ConfigChangeObserver_002, TestSize.Level1)
609 {
610 // Given
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
616 // When & Then
617 EXPECT_FALSE(configChangeObserver2->CheckExpired());
618 }
619
620 /**
621 * @tc.name: ConfigChangeObserver_003
622 * @tc.desc: test ConfigChangeObserver class.
623 * @tc.type: FUNC
624 * @tc.require: issueI4QT3W issueI4QU0V
625 */
626 HWTEST_F(BgTaskMiscUnitTest, ConfigChangeObserver_003, TestSize.Level1)
627 {
628 // Given
629 auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(nullptr);
630 auto bgContinuousTaskMgr = std::make_shared<BgContinuousTaskMgr>();
631 sptr<ConfigChangeObserver> configChangeObserver3 = sptr<ConfigChangeObserver>(
632 new ConfigChangeObserver(handler, bgContinuousTaskMgr));
633
634 // When & Then
635 EXPECT_TRUE(configChangeObserver3->CheckExpired());
636
637 AppExecFwk::Configuration configuration;
638 configChangeObserver3->OnConfigurationUpdated(configuration);
639 SUCCEED();
640 }
641
642 /**
643 * @tc.name: DataStorageHelper_001
644 * @tc.desc: test Watchdog class.
645 * @tc.type: FUNC
646 * @tc.require: issueI4QT3W issueI4QU0V
647 */
648 HWTEST_F(BgTaskMiscUnitTest, DataStorageHelper_001, TestSize.Level1)
649 {
650 std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap1;
651 auto continuousTaskRecord = std::make_shared<ContinuousTaskRecord>();
652 continuousTaskInfosMap1.emplace("key", continuousTaskRecord);
653 DelayedSingleton<DataStorageHelper>::GetInstance()->RefreshTaskRecord(continuousTaskInfosMap1);
654 std::unordered_map<std::string, std::shared_ptr<ContinuousTaskRecord>> continuousTaskInfosMap2;
655 EXPECT_EQ(DelayedSingleton<DataStorageHelper>::GetInstance()->RestoreTaskRecord(continuousTaskInfosMap2),
656 ERR_OK);
657 EXPECT_EQ(DelayedSingleton<DataStorageHelper>::GetInstance()->SaveJsonValueToFile("", ""),
658 ERR_BGTASK_CREATE_FILE_ERR);
659 nlohmann::json json1;
660 EXPECT_EQ(DelayedSingleton<DataStorageHelper>::GetInstance()->ParseJsonValueFromFile(json1, ""),
661 ERR_BGTASK_DATA_STORAGE_ERR);
662 EXPECT_FALSE(DelayedSingleton<DataStorageHelper>::GetInstance()->CreateNodeFile(""));
663 std::string fullPath;
664 EXPECT_FALSE(DelayedSingleton<DataStorageHelper>::GetInstance()->ConvertFullPath("", fullPath));
665 }
666
667 /**
668 * @tc.name: DataStorageHelper_002
669 * @tc.desc: test ParseFastSuspendDozeTime.
670 * @tc.type: FUNC
671 * @tc.require: issue#I99360
672 */
673 HWTEST_F(BgTaskMiscUnitTest, DataStorageHelper_002, TestSize.Level1)
674 {
675 int time = -1;
676 // 文件路径错误
677 std::string file("");
678 DelayedSingleton<DataStorageHelper>::GetInstance()->ParseFastSuspendDozeTime(file, time);
679 EXPECT_EQ(time, -1);
680 // 文件路径正确
681 file = "/etc/efficiency_manager/suspend_manager_config.json";
682 DelayedSingleton<DataStorageHelper>::GetInstance()->ParseFastSuspendDozeTime(file, time);
683 SUCCEED();
684 }
685
686 /**
687 * @tc.name: DecisionMakerTest_003
688 * @tc.desc: test Watchdog class.
689 * @tc.type: FUNC
690 * @tc.require: issueI4QT3W issueI4QU0V
691 */
692 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_003, TestSize.Level1)
693 {
694 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
695 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
696 auto timerManager =
697 std::make_shared<TimerManager>(bgtaskService, AppExecFwk::EventRunner::Create("tdd_test_handler"));
698 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
699 auto applicationStateObserver = sptr<DecisionMaker::ApplicationStateObserver>(
700 new (std::nothrow) DecisionMaker::ApplicationStateObserver(*decisionMaker));
701
702 AppExecFwk::AppStateData appStateData;
703 appStateData.uid = 1;
704 appStateData.bundleName = "bundleName1";
705 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND);
706 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
707 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOCUS);
708 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
709
710 auto keyInfo1 = std::make_shared<KeyInfo>("bundleName1", 1);
711 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
712 auto delayInfo = std::make_shared<DelaySuspendInfoEx>(1);
713 pkgDelaySuspendInfo->requestList_.push_back(delayInfo);
714 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo1] = pkgDelaySuspendInfo;
715 auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
716 decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime() - ALLOW_REQUEST_TIME_BG - 1;
717 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND);
718 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
719 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOCUS);
720 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
721
722 decisionMaker->pkgDelaySuspendInfoMap_.clear();
723 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_BACKGROUND);
724 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
725 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo1] = pkgDelaySuspendInfo;
726 applicationStateObserver->OnForegroundApplicationChanged(appStateData);
727 EXPECT_EQ((int32_t)decisionMaker->pkgDelaySuspendInfoMap_.size(), 1);
728 }
729
730 /**
731 * @tc.name: DecisionMakerTest_004
732 * @tc.desc: test PauseTransientTaskTimeForInner.
733 * @tc.type: FUNC
734 * @tc.require: issueI936BL
735 */
736 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_004, TestSize.Level1)
737 {
738 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
739 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
740 auto timerManager = std::make_shared<TimerManager>(bgtaskService,
741 AppExecFwk::EventRunner::Create("tdd_test_handler"));
742 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
743
744 decisionMaker->pkgBgDurationMap_.clear();
745 std::string name = "bundleName1";
746 int32_t uid = 1;
747 EXPECT_EQ(decisionMaker->PauseTransientTaskTimeForInner(uid, name), ERR_BGTASK_FOREGROUND);
748
749 auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
750 decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime() - ALLOW_REQUEST_TIME_BG - 1;
751 decisionMaker->pkgDelaySuspendInfoMap_.clear();
752 EXPECT_EQ(decisionMaker->PauseTransientTaskTimeForInner(uid, name), ERR_BGTASK_NOREQUEST_TASK);
753
754 auto keyInfo1 = std::make_shared<KeyInfo>("bundleName1", 1);
755 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
756 auto delayInfo = std::make_shared<DelaySuspendInfoEx>(1);
757 pkgDelaySuspendInfo->requestList_.push_back(delayInfo);
758 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo1] = pkgDelaySuspendInfo;
759 EXPECT_EQ(decisionMaker->PauseTransientTaskTimeForInner(uid, name), ERR_OK);
760 }
761
762 /**
763 * @tc.name: DecisionMakerTest_005
764 * @tc.desc: test StartTransientTaskTimeForInner.
765 * @tc.type: FUNC
766 * @tc.require: issueI936BL
767 */
768 HWTEST_F(BgTaskMiscUnitTest, DecisionMakerTest_005, TestSize.Level1)
769 {
770 auto deviceInfoManeger = std::make_shared<DeviceInfoManager>();
771 auto bgtaskService = sptr<BackgroundTaskMgrService>(new BackgroundTaskMgrService());
772 auto timerManager = std::make_shared<TimerManager>(bgtaskService,
773 AppExecFwk::EventRunner::Create("tdd_test_handler"));
774 auto decisionMaker = std::make_shared<DecisionMaker>(timerManager, deviceInfoManeger);
775
776 decisionMaker->pkgBgDurationMap_.clear();
777 std::string name = "bundleName1";
778 int32_t uid = 1;
779 EXPECT_EQ(decisionMaker->StartTransientTaskTimeForInner(uid, name), ERR_BGTASK_FOREGROUND);
780
781 auto keyInfo = std::make_shared<KeyInfo>("bundleName1", 1);
782 decisionMaker->pkgBgDurationMap_[keyInfo] = TimeProvider::GetCurrentTime() - ALLOW_REQUEST_TIME_BG - 1;
783 decisionMaker->pkgDelaySuspendInfoMap_.clear();
784 EXPECT_EQ(decisionMaker->StartTransientTaskTimeForInner(uid, name), ERR_BGTASK_NOREQUEST_TASK);
785
786 auto keyInfo1 = std::make_shared<KeyInfo>("bundleName1", 1);
787 auto pkgDelaySuspendInfo = std::make_shared<PkgDelaySuspendInfo>("bundleName1", 1, timerManager);
788 auto delayInfo = std::make_shared<DelaySuspendInfoEx>(1);
789 pkgDelaySuspendInfo->requestList_.push_back(delayInfo);
790 decisionMaker->pkgDelaySuspendInfoMap_[keyInfo1] = pkgDelaySuspendInfo;
791 EXPECT_EQ(decisionMaker->StartTransientTaskTimeForInner(uid, name), ERR_OK);
792 }
793
794 /**
795 * @tc.name: DelaySuspendInfoEx_001
796 * @tc.desc: test DelaySuspendInfoEx.
797 * @tc.type: FUNC
798 * @tc.require: issueI4QT3W issueI4QU0V
799 */
800 HWTEST_F(BgTaskMiscUnitTest, DelaySuspendInfoEx_001, TestSize.Level1)
801 {
802 auto delayInfo = std::make_shared<DelaySuspendInfoEx>(1);
803 delayInfo->baseTime_ = 1;
804 delayInfo->StartAccounting();
805 delayInfo->baseTime_ = 0;
806 delayInfo->StopAccounting();
807 EXPECT_EQ(delayInfo->spendTime_, 0);
808 }
809 }
810 }
811