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