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/thermal_policy.h"
20 #include "work_policy_manager.h"
21 #include "work_scheduler_service.h"
22 #include "thermal_mgr_client.h"
23 
24 using namespace testing::ext;
25 
26 namespace {
27 OHOS::PowerMgr::ThermalLevel thermalLevel_;
MockProcess(OHOS::PowerMgr::ThermalLevel thermlLevel)28 void MockProcess(OHOS::PowerMgr::ThermalLevel thermlLevel)
29 {
30     thermalLevel_ = thermlLevel;
31 }
32 }
33 
34 namespace OHOS {
35 namespace PowerMgr {
GetThermalLevel()36 ThermalLevel ThermalMgrClient::GetThermalLevel()
37 {
38     return thermalLevel_;
39 }
40 }
41 namespace WorkScheduler {
42 class ThermalPolicyTest : public testing::Test {
43 public:
44     static void SetUpTestCase();
TearDownTestCase()45     static void TearDownTestCase() {};
SetUp()46     void SetUp() {};
TearDown()47     void TearDown() {};
48     static std::shared_ptr<WorkPolicyManager> workPolicyManager_;
49     static std::shared_ptr<ThermalPolicy> thermalPolicy_;
50 };
51 
52 std::shared_ptr<ThermalPolicy> ThermalPolicyTest::thermalPolicy_ = nullptr;
53 std::shared_ptr<WorkPolicyManager> ThermalPolicyTest::workPolicyManager_ = nullptr;
54 
SetUpTestCase()55 void ThermalPolicyTest::SetUpTestCase()
56 {
57     std::shared_ptr<WorkSchedulerService> workSchedulerService_ = std::make_shared<WorkSchedulerService>();
58     workSchedulerService_->Start();
59     workPolicyManager_ = std::make_shared<WorkPolicyManager>(workSchedulerService_);
60     thermalPolicy_ = std::make_shared<ThermalPolicy>(workPolicyManager_);
61 }
62 
63 /**
64  * @tc.name: GetPolicyMaxRunning_001
65  * @tc.desc: Test ThermalPolicy GetPolicyMaxRunning.
66  * @tc.type: FUNC
67  * @tc.require: I974IQ
68  */
HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_001, TestSize.Level1)69 HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_001, TestSize.Level1)
70 {
71     MockProcess(PowerMgr::ThermalLevel::WARM);
72     int32_t ret = thermalPolicy_->GetPolicyMaxRunning();
73     EXPECT_EQ(ret, 0);
74 }
75 
76 /**
77  * @tc.name: GetPolicyMaxRunning_002
78  * @tc.desc: Test ThermalPolicy GetPolicyMaxRunning.
79  * @tc.type: FUNC
80  * @tc.require: I974IQ
81  */
HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_002, TestSize.Level1)82 HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_002, TestSize.Level1)
83 {
84     MockProcess(PowerMgr::ThermalLevel::NORMAL);
85     int32_t ret = thermalPolicy_->GetPolicyMaxRunning();
86     EXPECT_EQ(ret, 1);
87 }
88 
89 /**
90  * @tc.name: GetPolicyMaxRunning_003
91  * @tc.desc: Test ThermalPolicy GetPolicyMaxRunning.
92  * @tc.type: FUNC
93  * @tc.require: I974IQ
94  */
HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_003, TestSize.Level1)95 HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_003, TestSize.Level1)
96 {
97     MockProcess(PowerMgr::ThermalLevel::COOL);
98     int32_t ret = thermalPolicy_->GetPolicyMaxRunning();
99     EXPECT_EQ(ret, 3);
100 }
101 
102 /**
103  * @tc.name: GetPolicyName_001
104  * @tc.desc: Test ThermalPolicy GetPolicyName.
105  * @tc.type: FUNC
106  * @tc.require: I974IQ
107  */
HWTEST_F(ThermalPolicyTest, GetPolicyName_001, TestSize.Level1)108 HWTEST_F(ThermalPolicyTest, GetPolicyName_001, TestSize.Level1)
109 {
110     std::string ret = thermalPolicy_->GetPolicyName();
111     EXPECT_EQ(ret, "THERMAL_POLICY");
112 }
113 }
114 }
115