1 /*
2  * Copyright (c) 2024 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     workPolicyManager_->SetCpuUsageByDump(15);
95     int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning();
96     EXPECT_EQ(maxRunning, 3);
97 }
98 
99 /**
100  * @tc.name: getPolicyMaxRunning_002
101  * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
102  * @tc.type: FUNC
103  * @tc.require: I974IQ
104  */
105 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_002, TestSize.Level1)
106 {
107     workPolicyManager_->SetCpuUsageByDump(45);
108     int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning();
109     EXPECT_EQ(maxRunning, 2);
110 }
111 
112 /**
113  * @tc.name: getPolicyMaxRunning_003
114  * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
115  * @tc.type: FUNC
116  * @tc.require: I974IQ
117  */
118 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_003, TestSize.Level1)
119 {
120     workPolicyManager_->SetCpuUsageByDump(55);
121     int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning();
122     EXPECT_EQ(maxRunning, 1);
123 }
124 
125 /**
126  * @tc.name: getPolicyMaxRunning_004
127  * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
128  * @tc.type: FUNC
129  * @tc.require: I974IQ
130  */
131 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_004, TestSize.Level1)
132 {
133     workPolicyManager_->SetCpuUsageByDump(65);
134     int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning();
135     EXPECT_EQ(maxRunning, 0);
136 }
137 
138 /**
139  * @tc.name: getPolicyMaxRunning_005
140  * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
141  * @tc.type: FUNC
142  * @tc.require: I974IQ
143  */
144 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_005, TestSize.Level1)
145 {
146     workPolicyManager_->SetCpuUsageByDump(120);
147     int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning();
148     EXPECT_TRUE(maxRunning >= 0 && maxRunning <= 3);
149 }
150 
151 /**
152  * @tc.name: getPolicyMaxRunning_006
153  * @tc.desc: Test CpuPolicy GetPolicyMaxRunning.
154  * @tc.type: FUNC
155  * @tc.require: I974IQ
156  */
157 HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_006, TestSize.Level1)
158 {
159     int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning();
160     EXPECT_TRUE(maxRunning >= 0 && maxRunning <= 3);
161 }
162 
163 /**
164  * @tc.name: GetPolicyName_001
165  * @tc.desc: Test CpuPolicy GetPolicyName.
166  * @tc.type: FUNC
167  * @tc.require: I974IQ
168  */
169 HWTEST_F(CpuPolicyTest, GetPolicyName_001, TestSize.Level1)
170 {
171     std::string policyName = cpuPolicy_->GetPolicyName();
172     EXPECT_TRUE(policyName == "CPU_POLICY");
173 }
174 }
175 }