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 <gtest/gtest.h> 17 18#define private public 19#define protected public 20#include "thermal_dfx.h" 21#undef private 22#undef protected 23#include "file_ex.h" 24#include "thermal_log.h" 25 26using namespace OHOS::HDI; 27using namespace OHOS::HDI::Thermal::V1_1; 28using namespace testing::ext; 29 30class HdfThermalLogTest : public testing::Test { 31public: 32 static bool CheckThread(const std::string &threadName); 33 static void TearDownTestCase(); 34}; 35 36bool HdfThermalLogTest::CheckThread(const std::string &threadName) 37{ 38 std::string file = "/data/local/tmp/psTp"; 39 std::string cmd = "ps -T -p " + std::to_string(getpid()) + " > " + file; 40 system(cmd.c_str()); 41 std::string content; 42 OHOS::LoadStringFromFile(file, content); 43 return (std::string::npos != content.find(threadName)); 44} 45 46void HdfThermalLogTest::TearDownTestCase() 47{ 48 system("rm -rf /data/local/tmp/psTp"); 49} 50 51namespace { 52constexpr int32_t DEFAULT_WIDTH = 20; 53constexpr int32_t DEFAULT_INTERVAL = 5000; 54constexpr int32_t MIN_INTERVAL = 100; 55} // namespace 56 57namespace { 58/** 59 * @tc.name: HdfThermalLogTest001 60 * @tc.desc: Tests that the created thread is running properly 61 * @tc.type: FUNC 62 */ 63HWTEST_F(HdfThermalLogTest, HdfThermalLogTest001, TestSize.Level1) 64{ 65 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest001: start."); 66 auto &hdfLog = ThermalDfx::GetInstance(); 67 hdfLog.Init(); 68 hdfLog.DoWork(); 69 // thermal log off skipped tests 70 if (!hdfLog.enable_) { 71 ThermalDfx::DestroyInstance(); 72 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest001: thermal log off skipped tests."); 73 return; 74 } 75 ThermalDfx::DestroyInstance(); 76 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest001: end."); 77} 78 79/** 80 * @tc.name: HdfThermalLogTest002 81 * @tc.desc: Tests that the GetIntParameter Limiting minimum 82 * @tc.type: FUNC 83 */ 84HWTEST_F(HdfThermalLogTest, HdfThermalLogTest002, TestSize.Level1) 85{ 86 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest002: start."); 87 auto &hdfLog = ThermalDfx::GetInstance(); 88 uint32_t def = 888; 89 // The obtained value is less than the default value. Return the default value 90 uint32_t minVal = hdfLog.width_ + 1; 91 uint32_t width = hdfLog.GetIntParameter("persist.thermal.log.width", def, minVal); 92 ASSERT_EQ(def, width); 93 94 // The value obtained is greater than the value obtained by default 95 minVal = hdfLog.width_ - 1; 96 width = hdfLog.GetIntParameter("persist.thermal.log.width", def, minVal); 97 ASSERT_EQ(hdfLog.width_, width); 98 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest002: end."); 99} 100 101/** 102 * @tc.name: HdfThermalLogTest003 103 * @tc.desc: Tests that the WidthWatchCallback Limiting minimum 104 * @tc.type: FUNC 105 */ 106HWTEST_F(HdfThermalLogTest, HdfThermalLogTest003, TestSize.Level1) 107{ 108 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest003: start."); 109 auto &hdfLog = ThermalDfx::GetInstance(); 110 int32_t minVal = -5; 111 int32_t maxVal = DEFAULT_WIDTH + 10; 112 for (int32_t i = minVal; i < maxVal; ++i) { 113 std::string value = std::to_string(i); 114 hdfLog.WidthWatchCallback(value); 115 if (i <= DEFAULT_WIDTH) { 116 ASSERT_EQ(hdfLog.width_.load(), static_cast<uint8_t>(DEFAULT_WIDTH)); 117 } else { 118 ASSERT_EQ(hdfLog.width_.load(), static_cast<uint8_t>(i)); 119 } 120 } 121 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest003: end."); 122} 123 124/** 125 * @tc.name: HdfThermalLogTest004 126 * @tc.desc: Tests that the WidthWatchCallback abnormal value 127 * @tc.type: FUNC 128 */ 129HWTEST_F(HdfThermalLogTest, HdfThermalLogTest004, TestSize.Level1) 130{ 131 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest004: start."); 132 auto &hdfLog = ThermalDfx::GetInstance(); 133 std::vector<std::string> abnormal = {"", "abc", "123abc", "890,0"}; 134 for (auto& it : abnormal) { 135 hdfLog.WidthWatchCallback(it); 136 ASSERT_EQ(hdfLog.width_.load(), static_cast<uint8_t>(DEFAULT_WIDTH)) << 137 "HdfThermalLogTest004 failed value = " << it; 138 } 139 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest004: end."); 140} 141 142/** 143 * @tc.name: HdfThermalLogTest005 144 * @tc.desc: Tests that the IntervalWatchCallback Limiting minimum 145 * @tc.type: FUNC 146 */ 147HWTEST_F(HdfThermalLogTest, HdfThermalLogTest005, TestSize.Level1) 148{ 149 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest005: start."); 150 auto &hdfLog = ThermalDfx::GetInstance(); 151 int32_t minVal = -5; 152 int32_t maxVal = DEFAULT_INTERVAL + 10; 153 for (int32_t i = minVal; i < maxVal; ++i) { 154 std::string value = std::to_string(i); 155 hdfLog.IntervalWatchCallback(value); 156 if (i <= MIN_INTERVAL) { 157 ASSERT_EQ(hdfLog.interval_.load(), static_cast<uint32_t>(MIN_INTERVAL)); 158 } else { 159 ASSERT_EQ(hdfLog.interval_.load(), static_cast<uint32_t>(i)); 160 } 161 } 162 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest005: end."); 163} 164 165/** 166 * @tc.name: HdfThermalLogTest006 167 * @tc.desc: Tests that the IntervalWatchCallback abnormal value 168 * @tc.type: FUNC 169 */ 170HWTEST_F(HdfThermalLogTest, HdfThermalLogTest006, TestSize.Level1) 171{ 172 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest006: start."); 173 auto &hdfLog = ThermalDfx::GetInstance(); 174 std::vector<std::string> abnormal = {"", "abc", "123abc", "890,0"}; 175 for (auto& it : abnormal) { 176 hdfLog.IntervalWatchCallback(it); 177 ASSERT_EQ(hdfLog.interval_.load(), static_cast<uint32_t>(DEFAULT_INTERVAL)) << 178 "HdfThermalLogTest006 failed value = " << it; 179 } 180 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest006: end."); 181} 182 183/** 184 * @tc.name: HdfThermalLogTest007 185 * @tc.desc: Tests that the EnableWatchCallback The thread starts and stops normally 186 * @tc.type: FUNC 187 */ 188HWTEST_F(HdfThermalLogTest, HdfThermalLogTest007, TestSize.Level1) 189{ 190 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest007: start."); 191 auto &hdfLog = ThermalDfx::GetInstance(); 192 hdfLog.Init(); 193 // thermal log off skipped tests 194 if (!hdfLog.enable_) { 195 ThermalDfx::DestroyInstance(); 196 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest007: thermal log off skipped tests."); 197 return; 198 } 199 // Stop 200 hdfLog.EnableWatchCallback("false"); 201 ASSERT_EQ(hdfLog.enable_, false); 202 // Run 203 hdfLog.EnableWatchCallback("true"); 204 ASSERT_EQ(hdfLog.enable_, true); 205 ThermalDfx::DestroyInstance(); 206 THERMAL_HILOGD(LABEL_TEST, "HdfThermalLogTest007: end."); 207} 208} // namespace 209