1 /*
2 * Copyright (c) 2023 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 "fan_fault_detect_test.h"
17
18 #include "hisysevent.h"
19 #include "hisysevent_listener.h"
20 #include "hisysevent_manager.h"
21 #include "hisysevent_record.h"
22 #include "thermal_log.h"
23
24 using namespace testing::ext;
25 using namespace OHOS::HiviewDFX;
26 using namespace OHOS::PowerMgr;
27
28 namespace {
29 const std::string DOMAIN = "THERMAL";
30 const std::string EVENT = "FAN_FAULT";
31 const std::string TAG = "ID";
32 const std::string FAN = "fan";
33 const std::string GPU = "gpu";
34 const std::string SOC = "soc";
35 const int32_t FAN_SLOW_THERHOLD = 500;
36 const int32_t FAN_FAST_THERHOLD = 1500;
37 const int32_t TEMP_HIGH_THERHOLD = 50000;
38 const int32_t TEMP_LOW_THERHOLD = 30000;
39 const int32_t FAN_SLOW_SPEED = 400;
40 const int32_t FAN_FAST_SPEED = 1600;
41 const int32_t TEMP_HIGH = 60000;
42 const int32_t TEMP_LOW = 20000;
43
44 class Watcher : public HiSysEventListener {
45 public:
Watcher(std::function<void (std::shared_ptr<HiSysEventRecord>)> func)46 explicit Watcher(std::function<void(std::shared_ptr<HiSysEventRecord>)> func) : func_(func) {}
~Watcher()47 virtual ~Watcher() {}
48
OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)49 void OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent) final
50 {
51 if (sysEvent == nullptr || func_ == nullptr) {
52 return;
53 }
54 func_(sysEvent);
55 }
56
OnServiceDied()57 void OnServiceDied() final {}
58
59 private:
60 std::function<void(std::shared_ptr<HiSysEventRecord>)> func_;
61 };
62 } // namespace
63
SetUpTestCase()64 void FanFaultDetectTest::SetUpTestCase() {}
65
TearDownTestCase()66 void FanFaultDetectTest::TearDownTestCase() {}
67
SetUp()68 void FanFaultDetectTest::SetUp() {}
69
TearDown()70 void FanFaultDetectTest::TearDown() {}
71
InitFanFaultInfoMap(const std::shared_ptr<FanFaultDetect> & fanFaultDetect)72 void FanFaultDetectTest::InitFanFaultInfoMap(const std::shared_ptr<FanFaultDetect>& fanFaultDetect)
73 {
74 FanSensorInfo fanSlowSensorInfo;
75 fanSlowSensorInfo.insert(std::make_pair(FAN, FAN_SLOW_THERHOLD));
76 fanSlowSensorInfo.insert(std::make_pair(SOC, TEMP_HIGH_THERHOLD));
77 fanSlowSensorInfo.insert(std::make_pair(GPU, TEMP_HIGH_THERHOLD));
78
79 FanSensorInfo fanFastSensorInfo;
80 fanFastSensorInfo.insert(std::make_pair(FAN, FAN_FAST_THERHOLD));
81 fanFastSensorInfo.insert(std::make_pair(SOC, TEMP_LOW_THERHOLD));
82 fanFastSensorInfo.insert(std::make_pair(GPU, TEMP_LOW_THERHOLD));
83
84 FanFaultInfoMap fanFaultInfoMap;
85 fanFaultInfoMap.insert(std::make_pair(FAN_FAULT_TOO_SLOW, fanSlowSensorInfo));
86 fanFaultInfoMap.insert(std::make_pair(FAN_FAULT_TOO_FAST, fanFastSensorInfo));
87
88 fanFaultDetect->SetFaultInfoMap(fanFaultInfoMap);
89 }
90
GetFaultId(int64_t & faultId,const FanSensorInfo & report)91 void FanFaultDetectTest::GetFaultId(int64_t& faultId, const FanSensorInfo& report)
92 {
93 std::shared_ptr<FanFaultDetect> fanFaultDetect = std::make_shared<FanFaultDetect>();
94 EXPECT_NE(fanFaultDetect, nullptr);
95 InitFanFaultInfoMap(fanFaultDetect);
96
97 auto watcher = std::make_shared<Watcher>([&faultId] (std::shared_ptr<HiSysEventRecord> sysEvent) {
98 if (sysEvent == nullptr) {
99 return;
100 }
101 sysEvent->GetParamValue(TAG, faultId);
102 });
103
104 OHOS::HiviewDFX::ListenerRule listenerRule(DOMAIN, EVENT, OHOS::HiviewDFX::RuleType::WHOLE_WORD);
105 std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
106 sysRules.emplace_back(listenerRule);
107 auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
108 EXPECT_TRUE(ret == SUCCESS);
109
110 fanFaultDetect->OnFanSensorInfoChanged(report);
111 sleep(1);
112
113 ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(watcher);
114 EXPECT_TRUE(ret == SUCCESS);
115 }
116
117 namespace {
118 /**
119 * @tc.name: FanFaultDetectTest001
120 * @tc.desc: test class FanFaultDetectTest function
121 * @tc.type: FUNC
122 */
123 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest001, TestSize.Level0)
124 {
125 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest001 start");
126 int64_t faultId = FAN_FAULT_OK;
127 FanSensorInfo report;
128 report.insert(std::make_pair(FAN, FAN_SLOW_SPEED));
129 report.insert(std::make_pair(SOC, TEMP_HIGH));
130 report.insert(std::make_pair(GPU, TEMP_HIGH));
131 GetFaultId(faultId, report);
132 EXPECT_TRUE(faultId == FAN_FAULT_TOO_SLOW);
133 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest001 end");
134 }
135
136 /**
137 * @tc.name: FanFaultDetectTest002
138 * @tc.desc: test class FanFaultDetectTest function
139 * @tc.type: FUNC
140 */
141 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest002, TestSize.Level0)
142 {
143 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest002 start");
144 int64_t faultId = FAN_FAULT_OK;
145 FanSensorInfo report;
146 report.insert(std::make_pair(FAN, FAN_SLOW_SPEED));
147 report.insert(std::make_pair(SOC, TEMP_HIGH));
148 report.insert(std::make_pair(GPU, TEMP_LOW));
149 GetFaultId(faultId, report);
150 EXPECT_TRUE(faultId == FAN_FAULT_TOO_SLOW);
151 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest002 end");
152 }
153
154 /**
155 * @tc.name: FanFaultDetectTest003
156 * @tc.desc: test class FanFaultDetectTest function
157 * @tc.type: FUNC
158 */
159 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest003, TestSize.Level0)
160 {
161 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest003 start");
162 int64_t faultId = FAN_FAULT_OK;
163 FanSensorInfo report;
164 report.insert(std::make_pair(FAN, FAN_SLOW_SPEED));
165 report.insert(std::make_pair(SOC, TEMP_LOW));
166 report.insert(std::make_pair(GPU, TEMP_HIGH));
167 GetFaultId(faultId, report);
168 EXPECT_TRUE(faultId == FAN_FAULT_TOO_SLOW);
169 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest003 end");
170 }
171
172 /**
173 * @tc.name: FanFaultDetectTest004
174 * @tc.desc: test class FanFaultDetectTest function
175 * @tc.type: FUNC
176 */
177 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest004, TestSize.Level0)
178 {
179 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest004 start");
180 int64_t faultId = FAN_FAULT_OK;
181 FanSensorInfo report;
182 report.insert(std::make_pair(FAN, FAN_FAST_SPEED));
183 report.insert(std::make_pair(SOC, TEMP_LOW));
184 report.insert(std::make_pair(GPU, TEMP_LOW));
185 GetFaultId(faultId, report);
186 EXPECT_TRUE(faultId == FAN_FAULT_TOO_FAST);
187 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest004 end");
188 }
189
190 /**
191 * @tc.name: FanFaultDetectTest005
192 * @tc.desc: test class FanFaultDetectTest function
193 * @tc.type: FUNC
194 */
195 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest005, TestSize.Level0)
196 {
197 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest005 start");
198 int64_t faultId = FAN_FAULT_OK;
199 FanSensorInfo report;
200 report.insert(std::make_pair(FAN, FAN_SLOW_SPEED));
201 report.insert(std::make_pair(SOC, TEMP_LOW));
202 report.insert(std::make_pair(GPU, TEMP_LOW));
203 GetFaultId(faultId, report);
204 EXPECT_TRUE(faultId == FAN_FAULT_OK);
205 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest005 end");
206 }
207
208 /**
209 * @tc.name: FanFaultDetectTest006
210 * @tc.desc: test class FanFaultDetectTest function
211 * @tc.type: FUNC
212 */
213 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest006, TestSize.Level0)
214 {
215 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest006 start");
216 int64_t faultId = FAN_FAULT_OK;
217 FanSensorInfo report;
218 report.insert(std::make_pair(FAN, FAN_FAST_SPEED));
219 report.insert(std::make_pair(SOC, TEMP_HIGH));
220 report.insert(std::make_pair(GPU, TEMP_LOW));
221 GetFaultId(faultId, report);
222 EXPECT_TRUE(faultId == FAN_FAULT_OK);
223 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest006 end");
224 }
225
226 /**
227 * @tc.name: FanFaultDetectTest007
228 * @tc.desc: test class FanFaultDetectTest function
229 * @tc.type: FUNC
230 */
231 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest007, TestSize.Level0)
232 {
233 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest007 start");
234 int64_t faultId = FAN_FAULT_OK;
235 FanSensorInfo report;
236 report.insert(std::make_pair(FAN, FAN_FAST_SPEED));
237 report.insert(std::make_pair(SOC, TEMP_LOW));
238 report.insert(std::make_pair(GPU, TEMP_HIGH));
239 GetFaultId(faultId, report);
240 EXPECT_TRUE(faultId == FAN_FAULT_OK);
241 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest007 end");
242 }
243
244 /**
245 * @tc.name: FanFaultDetectTest008
246 * @tc.desc: test class FanFaultDetectTest function
247 * @tc.type: FUNC
248 */
249 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest008, TestSize.Level0)
250 {
251 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest008 start");
252 int64_t faultId = FAN_FAULT_OK;
253 FanSensorInfo report;
254 report.insert(std::make_pair(FAN, FAN_FAST_SPEED));
255 report.insert(std::make_pair(SOC, TEMP_HIGH));
256 report.insert(std::make_pair(GPU, TEMP_HIGH));
257 GetFaultId(faultId, report);
258 EXPECT_TRUE(faultId == FAN_FAULT_OK);
259 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest008 end");
260 }
261 } // namespace
262