diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index d105927bb7a2a119aa440da7f6387ec6fd8f87a1..2e24633c8d145be5584c85343a44fd83e4a482c8 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -635,9 +635,39 @@ ohos_unittest("ThermalServiceTest") { external_deps = deps_ex } +# fan fault detect test +ohos_unittest("FanFaultDetectTest") { + module_out_path = module_output_path + defines = [ "THERMAL_GTEST" ] + sources = [ "src/fan_fault_detect_test.cpp" ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + "${utils_path}:coverage_flags", + ] + + deps = [ + "${thermal_inner_api}:thermalsrv_client", + "${thermal_manager_path}/services:thermalservice", + "${thermal_service_zidl}:thermalmgr_proxy", + "${thermal_service_zidl}:thermalmgr_stub", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "hisysevent:libhisysevent", + "hisysevent:libhisyseventmanager", + ] + + external_deps += deps_ex +} + group("unittest") { testonly = true deps = [ + ":FanFaultDetectTest", ":ThermalActionHubTest", ":ThermalActionReportTest", ":ThermalActionTest", diff --git a/test/unittest/include/fan_fault_detect_test.h b/test/unittest/include/fan_fault_detect_test.h new file mode 100644 index 0000000000000000000000000000000000000000..0d76fbace58b1916c2ec6a0bab65c44749b5455a --- /dev/null +++ b/test/unittest/include/fan_fault_detect_test.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FAN_FAULT_DETECT_TEST_H +#define FAN_FAULT_DETECT_TEST_H + +#include +#include "fan_fault_detect.h" + +namespace OHOS { +namespace PowerMgr { +class FanFaultDetectTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + void GetFaultId(int64_t &faultId, const FanSensorInfo& report); +private: + void InitFanFaultInfoMap(const std::shared_ptr& fanFaultDetect); +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // FAN_FAULT_DETECT_TEST_H diff --git a/test/unittest/src/fan_fault_detect_test.cpp b/test/unittest/src/fan_fault_detect_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6823a8c70d418b0cfdcf05cf3c6b00997de85311 --- /dev/null +++ b/test/unittest/src/fan_fault_detect_test.cpp @@ -0,0 +1,263 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fan_fault_detect_test.h" + +#include "hisysevent.h" +#include "hisysevent_listener.h" +#include "hisysevent_manager.h" +#include "hisysevent_record.h" +#include "thermal_log.h" + +using namespace testing::ext; +using namespace OHOS::HiviewDFX; +using namespace OHOS::PowerMgr; + +namespace { +const std::string DOMAIN = "THERMAL"; +const std::string EVENT = "FAN_FAULT"; +const std::string TAG = "ID"; +const std::string FAN = "fan"; +const std::string GPU = "gpu"; +const std::string SOC = "soc"; +const int32_t FAN_SLOW_THERHOLD = 500; +const int32_t FAN_FAST_THERHOLD = 1500; +const int32_t TEMP_HIGH_THERHOLD = 50000; +const int32_t TEMP_LOW_THERHOLD = 30000; +const int32_t FAN_SLOW_SPEED = 400; +const int32_t FAN_FAST_SPEED = 1600; +const int32_t TEMP_HIGH = 60000; +const int32_t TEMP_LOW = 20000; + +class Watcher : public HiSysEventListener { +public: + explicit Watcher(std::function)> func) + { + func_ = func; + } + + virtual ~Watcher() {} + + void OnEvent(std::shared_ptr sysEvent) final + { + if (sysEvent == nullptr || func_ == nullptr) { + return; + } + func_(sysEvent); + } + + void OnServiceDied() final {} + +private: + std::function)> func_; +}; +} + +void FanFaultDetectTest::SetUpTestCase() {} + +void FanFaultDetectTest::TearDownTestCase() {} + +void FanFaultDetectTest::SetUp() {} + +void FanFaultDetectTest::TearDown() {} + +void FanFaultDetectTest::InitFanFaultInfoMap(const std::shared_ptr& fanFaultDetect) +{ + FanSensorInfo fanSlowSensorInfo; + fanSlowSensorInfo.insert(std::make_pair(FAN, FAN_SLOW_THERHOLD)); + fanSlowSensorInfo.insert(std::make_pair(SOC, TEMP_HIGH_THERHOLD)); + fanSlowSensorInfo.insert(std::make_pair(GPU, TEMP_HIGH_THERHOLD)); + + FanSensorInfo fanFastSensorInfo; + fanFastSensorInfo.insert(std::make_pair(FAN, FAN_FAST_THERHOLD)); + fanFastSensorInfo.insert(std::make_pair(SOC, TEMP_LOW_THERHOLD)); + fanFastSensorInfo.insert(std::make_pair(GPU, TEMP_LOW_THERHOLD)); + + FanFaultInfoMap fanFaultInfoMap; + fanFaultInfoMap.insert(std::make_pair(FAN_FAULT_TOO_SLOW, fanSlowSensorInfo)); + fanFaultInfoMap.insert(std::make_pair(FAN_FAULT_TOO_FAST, fanFastSensorInfo)); + + fanFaultDetect->SetFaultInfoMap(fanFaultInfoMap); +} + +void FanFaultDetectTest::GetFaultId(int64_t& faultId, const FanSensorInfo& report) +{ + std::shared_ptr fanFaultDetect = std::make_shared(); + EXPECT_NE(fanFaultDetect, nullptr); + InitFanFaultInfoMap(fanFaultDetect); + + auto watcher = std::make_shared([&faultId] (std::shared_ptr sysEvent) { + if (sysEvent == nullptr) { + return; + } + sysEvent->GetParamValue(TAG, faultId); + }); + + OHOS::HiviewDFX::ListenerRule listenerRule(DOMAIN, EVENT, OHOS::HiviewDFX::RuleType::WHOLE_WORD); + std::vector sysRules; + sysRules.emplace_back(listenerRule); + auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules); + EXPECT_TRUE(ret == SUCCESS); + + fanFaultDetect->OnFanSensorInfoChanged(report); + sleep(1); + + ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(watcher); + EXPECT_TRUE(ret == SUCCESS); +} + +/** + * @tc.name: FanFaultDetectTest001 + * @tc.desc: test class FanFaultDetectTest function + * @tc.type: FUNC + */ +HWTEST_F(FanFaultDetectTest, FanFaultDetectTest001, TestSize.Level0) +{ + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest001 start"); + int64_t faultId = FAN_FAULT_OK; + FanSensorInfo report; + report.insert(std::make_pair(FAN, FAN_SLOW_SPEED)); + report.insert(std::make_pair(SOC, TEMP_HIGH)); + report.insert(std::make_pair(GPU, TEMP_HIGH)); + GetFaultId(faultId, report); + EXPECT_TRUE(faultId == FAN_FAULT_TOO_SLOW); + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest001 end"); +} + +/** + * @tc.name: FanFaultDetectTest002 + * @tc.desc: test class FanFaultDetectTest function + * @tc.type: FUNC + */ +HWTEST_F(FanFaultDetectTest, FanFaultDetectTest002, TestSize.Level0) +{ + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest002 start"); + int64_t faultId = FAN_FAULT_OK; + FanSensorInfo report; + report.insert(std::make_pair(FAN, FAN_SLOW_SPEED)); + report.insert(std::make_pair(SOC, TEMP_HIGH)); + report.insert(std::make_pair(GPU, TEMP_LOW)); + GetFaultId(faultId, report); + EXPECT_TRUE(faultId == FAN_FAULT_TOO_SLOW); + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest002 end"); +} + +/** + * @tc.name: FanFaultDetectTest003 + * @tc.desc: test class FanFaultDetectTest function + * @tc.type: FUNC + */ +HWTEST_F(FanFaultDetectTest, FanFaultDetectTest003, TestSize.Level0) +{ + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest003 start"); + int64_t faultId = FAN_FAULT_OK; + FanSensorInfo report; + report.insert(std::make_pair(FAN, FAN_SLOW_SPEED)); + report.insert(std::make_pair(SOC, TEMP_LOW)); + report.insert(std::make_pair(GPU, TEMP_HIGH)); + GetFaultId(faultId, report); + EXPECT_TRUE(faultId == FAN_FAULT_TOO_SLOW); + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest003 end"); +} + +/** + * @tc.name: FanFaultDetectTest004 + * @tc.desc: test class FanFaultDetectTest function + * @tc.type: FUNC + */ +HWTEST_F(FanFaultDetectTest, FanFaultDetectTest004, TestSize.Level0) +{ + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest004 start"); + int64_t faultId = FAN_FAULT_OK; + FanSensorInfo report; + report.insert(std::make_pair(FAN, FAN_FAST_SPEED)); + report.insert(std::make_pair(SOC, TEMP_LOW)); + report.insert(std::make_pair(GPU, TEMP_LOW)); + GetFaultId(faultId, report); + EXPECT_TRUE(faultId == FAN_FAULT_TOO_FAST); + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest004 end"); +} + +/** + * @tc.name: FanFaultDetectTest005 + * @tc.desc: test class FanFaultDetectTest function + * @tc.type: FUNC + */ +HWTEST_F(FanFaultDetectTest, FanFaultDetectTest005, TestSize.Level0) +{ + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest005 start"); + int64_t faultId = FAN_FAULT_OK; + FanSensorInfo report; + report.insert(std::make_pair(FAN, FAN_SLOW_SPEED)); + report.insert(std::make_pair(SOC, TEMP_LOW)); + report.insert(std::make_pair(GPU, TEMP_LOW)); + GetFaultId(faultId, report); + EXPECT_TRUE(faultId == FAN_FAULT_OK); + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest005 end"); +} + +/** + * @tc.name: FanFaultDetectTest006 + * @tc.desc: test class FanFaultDetectTest function + * @tc.type: FUNC + */ +HWTEST_F(FanFaultDetectTest, FanFaultDetectTest006, TestSize.Level0) +{ + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest006 start"); + int64_t faultId = FAN_FAULT_OK; + FanSensorInfo report; + report.insert(std::make_pair(FAN, FAN_FAST_SPEED)); + report.insert(std::make_pair(SOC, TEMP_HIGH)); + report.insert(std::make_pair(GPU, TEMP_LOW)); + GetFaultId(faultId, report); + EXPECT_TRUE(faultId == FAN_FAULT_OK); + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest006 end"); +} + +/** + * @tc.name: FanFaultDetectTest007 + * @tc.desc: test class FanFaultDetectTest function + * @tc.type: FUNC + */ +HWTEST_F(FanFaultDetectTest, FanFaultDetectTest007, TestSize.Level0) +{ + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest007 start"); + int64_t faultId = FAN_FAULT_OK; + FanSensorInfo report; + report.insert(std::make_pair(FAN, FAN_FAST_SPEED)); + report.insert(std::make_pair(SOC, TEMP_LOW)); + report.insert(std::make_pair(GPU, TEMP_HIGH)); + GetFaultId(faultId, report); + EXPECT_TRUE(faultId == FAN_FAULT_OK); + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest007 end"); +} + +/** + * @tc.name: FanFaultDetectTest008 + * @tc.desc: test class FanFaultDetectTest function + * @tc.type: FUNC + */ +HWTEST_F(FanFaultDetectTest, FanFaultDetectTest008, TestSize.Level0) +{ + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest008 start"); + int64_t faultId = FAN_FAULT_OK; + FanSensorInfo report; + report.insert(std::make_pair(FAN, FAN_FAST_SPEED)); + report.insert(std::make_pair(SOC, TEMP_HIGH)); + report.insert(std::make_pair(GPU, TEMP_HIGH)); + GetFaultId(faultId, report); + EXPECT_TRUE(faultId == FAN_FAULT_OK); + THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest008 end"); +}