1 /*
2 * Copyright (c) 2024-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 "policy/cpu_policy.h"
20 #include "work_policy_manager.h"
21 #include "work_scheduler_service.h"
22
23
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace WorkScheduler {
28 class CpuPolicyTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
TearDownTestCase()31 static void TearDownTestCase() {};
SetUp()32 void SetUp() {};
TearDown()33 void TearDown() {};
34 static std::shared_ptr<WorkPolicyManager> workPolicyManager_;
35 static std::shared_ptr<CpuPolicy> cpuPolicy_;
36 };
37
38 std::shared_ptr<CpuPolicy> CpuPolicyTest::cpuPolicy_ = nullptr;
39 std::shared_ptr<WorkPolicyManager> CpuPolicyTest::workPolicyManager_ = nullptr;
40
SetUpTestCase()41 void CpuPolicyTest::SetUpTestCase()
42 {
43 std::shared_ptr<WorkSchedulerService> workSchedulerService_ = std::make_shared<WorkSchedulerService>();
44 workPolicyManager_ = std::make_shared<WorkPolicyManager>(workSchedulerService_);
45 cpuPolicy_ = std::make_shared<CpuPolicy>(workPolicyManager_);
46 }
47
48 /**
49 * @tc.name: getCpuUsage_001
50 * @tc.desc: Test CpuPolicy GetCpuUsage.
51 * @tc.type: FUNC
52 * @tc.require: I974IQ
53 */
54 HWTEST_F(CpuPolicyTest, getCpuUsage_001, TestSize.Level1)
55 {
56 workPolicyManager_->SetCpuUsageByDump(15);
57 int32_t cpuUsage = cpuPolicy_->GetCpuUsage();
58 EXPECT_EQ(cpuUsage, 15);
59 }
60
61 /**
62 * @tc.name: getCpuUsage_002
63 * @tc.desc: Test CpuPolicy GetCpuUsage.
64 * @tc.type: FUNC
65 * @tc.require: I974IQ
66 */
67 HWTEST_F(CpuPolicyTest, getCpuUsage_002, TestSize.Level1)
68 {
69 workPolicyManager_->SetCpuUsageByDump(120);
70 int32_t cpuUsage = cpuPolicy_->GetCpuUsage();
71 EXPECT_TRUE(cpuUsage >= 0 && cpuUsage <= 100);
72 }
73
74 /**
75 * @tc.name: getCpuUsage_003
76 * @tc.desc: Test CpuPolicy GetCpuUsage.
77 * @tc.type: FUNC
78 * @tc.require: I974IQ
79 */
80 HWTEST_F(CpuPolicyTest, getCpuUsage_003, TestSize.Level1)
81 {
82 int32_t cpuUsage = cpuPolicy_->GetCpuUsage();
83 EXPECT_TRUE(cpuUsage >= 0 && cpuUsage <= 100);
84 }
85
86 /**
87 * @tc.name: getPolicyMaxRunning_001
88 * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
89 * @tc.type: FUNC
90 * @tc.require: I974IQ
91 */
92 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_001, TestSize.Level1)
93 {
94 WorkSchedSystemPolicy systemPolicy;
95 workPolicyManager_->SetCpuUsageByDump(15);
96 int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy);
97 EXPECT_EQ(maxRunning, 3);
98 }
99
100 /**
101 * @tc.name: getPolicyMaxRunning_002
102 * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
103 * @tc.type: FUNC
104 * @tc.require: I974IQ
105 */
106 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_002, TestSize.Level1)
107 {
108 WorkSchedSystemPolicy systemPolicy;
109 workPolicyManager_->SetCpuUsageByDump(45);
110 int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy);
111 EXPECT_EQ(maxRunning, 2);
112 }
113
114 /**
115 * @tc.name: getPolicyMaxRunning_003
116 * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
117 * @tc.type: FUNC
118 * @tc.require: I974IQ
119 */
120 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_003, TestSize.Level1)
121 {
122 WorkSchedSystemPolicy systemPolicy;
123 workPolicyManager_->SetCpuUsageByDump(55);
124 int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy);
125 EXPECT_EQ(maxRunning, 1);
126 }
127
128 /**
129 * @tc.name: getPolicyMaxRunning_004
130 * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
131 * @tc.type: FUNC
132 * @tc.require: I974IQ
133 */
134 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_004, TestSize.Level1)
135 {
136 WorkSchedSystemPolicy systemPolicy;
137 workPolicyManager_->SetCpuUsageByDump(65);
138 int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy);
139 EXPECT_EQ(maxRunning, 0);
140 }
141
142 /**
143 * @tc.name: getPolicyMaxRunning_005
144 * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
145 * @tc.type: FUNC
146 * @tc.require: I974IQ
147 */
148 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_005, TestSize.Level1)
149 {
150 WorkSchedSystemPolicy systemPolicy;
151 workPolicyManager_->SetCpuUsageByDump(120);
152 int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy);
153 EXPECT_TRUE(maxRunning >= 0 && maxRunning <= 3);
154 }
155
156 /**
157 * @tc.name: getPolicyMaxRunning_006
158 * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
159 * @tc.type: FUNC
160 * @tc.require: I974IQ
161 */
162 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_006, TestSize.Level1)
163 {
164 WorkSchedSystemPolicy systemPolicy;
165 int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy);
166 EXPECT_TRUE(maxRunning >= 0 && maxRunning <= 3);
167 }
168 }
169 }