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