1 /*
2 * Copyright (c) 2025 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 <gtest/gtest.h>
18
19 #include "work_queue_manager.h"
20 #include "work_policy_manager.h"
21 #include "work_scheduler_service.h"
22 #include "work_condition.h"
23 #include "work_sched_hilog.h"
24 #include "work_info.h"
25
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace WorkScheduler {
30 class MockWorkPolicyManager : public WorkPolicyManager {
31 public:
32 using WorkPolicyManager::WorkPolicyManager;
33 ~MockWorkPolicyManager() = default;
34
CheckWorkToRun()35 void CheckWorkToRun(){};
36 };
37 class WorkQueueManagerTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
TearDownTestCase()40 static void TearDownTestCase() {}
SetUp()41 void SetUp() {}
TearDown()42 void TearDown() {}
43 static std::shared_ptr<WorkQueueManager> workQueueManager_;
44 };
45
46 std::shared_ptr<WorkQueueManager> WorkQueueManagerTest::workQueueManager_ = nullptr;
47
SetUpTestCase()48 void WorkQueueManagerTest::SetUpTestCase()
49 {
50 std::shared_ptr<WorkSchedulerService> workSchedulerService_ = DelayedSingleton<WorkSchedulerService>::GetInstance();
51 std::shared_ptr<WorkPolicyManager> workPolicyManager_ =
52 std::make_shared<MockWorkPolicyManager>(workSchedulerService_);
53 workSchedulerService_->workPolicyManager_ = workPolicyManager_;
54 workQueueManager_ = std::make_shared<WorkQueueManager>(workSchedulerService_);
55 }
56
57 /**
58 * @tc.name: ClearTimeOutWorkStatus_001
59 * @tc.desc: Test WorkQueueManagerTest ClearTimeOutWorkStatus.
60 * @tc.type: FUNC
61 * @tc.require: https://gitee.com/openharmony/resourceschedule_work_scheduler/issues/IC8IR5
62 */
63 HWTEST_F(WorkQueueManagerTest, ClearTimeOutWorkStatus_001, TestSize.Level1)
64 {
65 workQueueManager_->queueMap_.clear();
66 WorkInfo workinfo;
67 workinfo.SetWorkId(10000);
68 workinfo.RequestBatteryStatus(WorkCondition::BatteryStatus::BATTERY_STATUS_LOW);
69 workinfo.RequestBatteryLevel(80);
70 int32_t uid = 10000;
71 std::shared_ptr<WorkStatus> workStatus = std::make_shared<WorkStatus>(workinfo, uid);
72 workQueueManager_->AddWork(workStatus);
73 workQueueManager_->ClearTimeOutWorkStatus();
74 EXPECT_EQ(workQueueManager_->queueMap_.size(), 2);
75 }
76
77 /**
78 * @tc.name: ClearTimeOutWorkStatus_002
79 * @tc.desc: Test WorkQueueManagerTest ClearTimeOutWorkStatus.
80 * @tc.type: FUNC
81 * @tc.require: https://gitee.com/openharmony/resourceschedule_work_scheduler/issues/IC8IR5
82 */
83 HWTEST_F(WorkQueueManagerTest, ClearTimeOutWorkStatus_002, TestSize.Level1)
84 {
85 workQueueManager_->queueMap_.clear();
86 WorkInfo workinfo;
87 workinfo.SetWorkId(10000);
88 workinfo.RequestBatteryStatus(WorkCondition::BatteryStatus::BATTERY_STATUS_LOW);
89 workinfo.RequestDeepIdle(WorkCondition::DeepIdle::DEEP_IDLE_OUT);
90 workinfo.RequestBatteryLevel(80);
91 int32_t uid = 10000;
92 std::shared_ptr<WorkStatus> workStatus = std::make_shared<WorkStatus>(workinfo, uid);
93 workStatus->SetTimeout(true);
94 workQueueManager_->AddWork(workStatus);
95 workQueueManager_->ClearTimeOutWorkStatus();
96 EXPECT_EQ(workQueueManager_->queueMap_.size(), 3);
97 }
98 }
99 }