1 /*
2 * Copyright (c) 2022 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 <cmath>
17 #include <cstdio>
18 #include <gtest/gtest.h>
19 #include <mutex>
20 #include <fcntl.h>
21 #include <functional>
22 #include <securec.h>
23 #include <unistd.h>
24
25 #include "hdf_base.h"
26 #include "osal_time.h"
27 #include "v1_0/ithermal_interface.h"
28 #include "v1_0/ithermal_callback.h"
29 #include "v1_0/thermal_types.h"
30
31 using namespace OHOS::HDI;
32 using namespace OHOS::HDI::Thermal::V1_0;
33 using namespace testing::ext;
34
35 namespace {
36 class ThermalCallbackMock : public IThermalCallback {
37 public:
~ThermalCallbackMock()38 virtual ~ThermalCallbackMock() {}
39 using ThermalEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
RegisterThermalEvent(const ThermalEventCallback & eventCb)40 static int32_t RegisterThermalEvent(const ThermalEventCallback &eventCb)
41 {
42 (void)eventCb;
43 return 0;
44 }
OnThermalDataEvent(const HdfThermalCallbackInfo & event)45 int32_t OnThermalDataEvent(const HdfThermalCallbackInfo &event) override
46 {
47 (void)event;
48 return 0;
49 }
50 };
51
52 sptr<IThermalInterface> g_thermalInterface = nullptr;
53 sptr<IThermalCallback> g_callback = new ThermalCallbackMock();
54 std::mutex g_mutex;
55 const uint32_t MAX_PATH = 256;
56 const uint32_t WAIT_TIME = 1;
57 const std::string CPU_FREQ_PATH = "/data/service/el0/thermal/cooling/cpu/freq";
58 const std::string GPU_FREQ_PATH = "/data/service/el0/thermal/cooling/gpu/freq";
59 const std::string BATTERY_CHARGER_CURRENT_PATH = "/data/service/el0/thermal/cooling/battery/current";
60
61 class HdfThermalHdiTest : public testing::Test {
62 public:
63 static void SetUpTestCase();
64 static void TearDownTestCase();
65 void SetUp();
66 void TearDown();
67 static int32_t ReadFile(const char *path, char *buf, size_t size);
68 static int32_t ConvertInt(const std::string &value);
69 };
70
SetUpTestCase()71 void HdfThermalHdiTest::SetUpTestCase()
72 {
73 g_thermalInterface = IThermalInterface::Get(true);
74 }
75
TearDownTestCase()76 void HdfThermalHdiTest::TearDownTestCase()
77 {
78 }
79
SetUp()80 void HdfThermalHdiTest::SetUp()
81 {
82 }
83
TearDown()84 void HdfThermalHdiTest::TearDown()
85 {
86 }
87
ReadFile(const char * path,char * buf,size_t size)88 int32_t HdfThermalHdiTest::ReadFile(const char *path, char *buf, size_t size)
89 {
90 std::lock_guard<std::mutex> lck(g_mutex);
91 int32_t ret;
92
93 int32_t fd = open(path, O_RDONLY);
94 if (fd < HDF_SUCCESS) {
95 printf("WriteFile: failed to open file %d\n\r", fd);
96 return HDF_FAILURE;
97 }
98
99 ret = read(fd, buf, size);
100 if (ret < HDF_SUCCESS) {
101 printf("WriteFile: failed to read file %d\n\r", ret);
102 close(fd);
103 return HDF_FAILURE;
104 }
105
106 close(fd);
107 buf[size - 1] = '\0';
108 return HDF_SUCCESS;
109 }
110
ConvertInt(const std::string & value)111 int32_t HdfThermalHdiTest::ConvertInt(const std::string &value)
112 {
113 return std::stoi(value);
114 }
115 }
116
117 namespace {
118 /**
119 * @tc.name: HdfThermalHdiTest001
120 * @tc.desc: Get a client and check whether the client is empty.
121 * @tc.type: FUNC
122 */
123 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest001, TestSize.Level1)
124 {
125 ASSERT_NE(nullptr, g_thermalInterface);
126 }
127
128 /**
129 * @tc.name: HdfThermalHdiTest002
130 * @tc.desc: SetCpuFreq
131 * @tc.type: FUNC
132 */
133 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest002, TestSize.Level1)
134 {
135 printf("HdfThermalHdiTest002: start.");
136 int32_t cpuFreq = 1994100;
137 int32_t ret = g_thermalInterface->SetCpuFreq(cpuFreq);
138 EXPECT_EQ(0, ret);
139
140 char cpuBuf[MAX_PATH] = {0};
141 char freqValue[MAX_PATH] = {0};
142
143 if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, CPU_FREQ_PATH.c_str()) < EOK) {
144 return;
145 }
146
147 sleep(WAIT_TIME);
148 ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
149 if (ret != HDF_SUCCESS) {
150 printf("HdfThermalHdiTest002: Failed to read file ");
151 return;
152 }
153
154 std::string freq = freqValue;
155 int32_t value = HdfThermalHdiTest::ConvertInt(freq);
156 printf("freq is %d\n\r", value);
157 EXPECT_EQ(value, cpuFreq) << "HdfThermalHdiTest002 failed";
158 printf("HdfThermalHdiTest002: return.");
159 }
160
161 /**
162 * @tc.name: HdfThermalHdiTest003
163 * @tc.desc: SetGpuFreq
164 * @tc.type: FUNC
165 */
166 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest003, TestSize.Level1)
167 {
168 printf("HdfThermalHdiTest003: start.");
169 int32_t gpuFreq = 40000;
170 int32_t ret = g_thermalInterface->SetGpuFreq(gpuFreq);
171 EXPECT_EQ(0, ret);
172
173 char cpuBuf[MAX_PATH] = {0};
174 char freqValue[MAX_PATH] = {0};
175
176 if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, GPU_FREQ_PATH.c_str()) < EOK) {
177 return;
178 }
179
180 sleep(WAIT_TIME);
181 ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
182 if (ret != HDF_SUCCESS) {
183 printf("HdfThermalHdiTest003: Failed to read file ");
184 return;
185 }
186
187 std::string freq = freqValue;
188 int32_t value = HdfThermalHdiTest::ConvertInt(freq);
189 printf("freq is %d\n\r", value);
190 EXPECT_EQ(value, gpuFreq) << "HdfThermalHdiTest003 failed";
191 printf("HdfThermalHdiTest003: return.");
192 }
193
194 /**
195 * @tc.name: HdfThermalHdiTest004
196 * @tc.desc: SetBatteryCurrent
197 * @tc.type: FUNC
198 */
199 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest004, TestSize.Level1)
200 {
201 printf("HdfThermalHdiTest004: start.");
202 int32_t batteryCurrent = 1000;
203 int32_t ret = g_thermalInterface->SetBatteryCurrent(batteryCurrent);
204 EXPECT_EQ(0, ret);
205
206 char cpuBuf[MAX_PATH] = {0};
207 char currentValue[MAX_PATH] = {0};
208
209 if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, BATTERY_CHARGER_CURRENT_PATH.c_str()) < EOK) {
210 return;
211 }
212
213 sleep(WAIT_TIME);
214 ret = HdfThermalHdiTest::ReadFile(cpuBuf, currentValue, sizeof(currentValue));
215 if (ret != HDF_SUCCESS) {
216 printf("HdfThermalHdiTest004: Failed to read file ");
217 return;
218 }
219
220 std::string current = currentValue;
221 int32_t value = HdfThermalHdiTest::ConvertInt(current);
222 printf("freq is %d\n\r", value);
223 EXPECT_EQ(value, batteryCurrent) << "HdfThermalHdiTest004 failed";
224 printf("HdfThermalHdiTest004: return.");
225 }
226
227 /**
228 * @tc.name: HdfThermalHdiTest005
229 * @tc.desc: GetThermalZoneInfo
230 * @tc.type: FUNC
231 */
232 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest005, TestSize.Level1)
233 {
234 HdfThermalCallbackInfo event;
235 printf("HdfThermalHdiTest005: start.");
236 int32_t ret = g_thermalInterface->GetThermalZoneInfo(event);
237 EXPECT_EQ(0, ret) << "HdfThermalHdiTest005 failed";
238 for (auto iter : event.info) {
239 printf("type is %s\n\r", iter.type.c_str());
240 printf("temp is %d\n\r", iter.temp);
241 }
242 printf("HdfThermalHdiTest005: return.");
243 }
244
245 /**
246 * @tc.name: HdfThermalHdiTest006
247 * @tc.desc: Register
248 * @tc.type: FUNC
249 */
250 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest006, TestSize.Level1)
251 {
252 printf("HdfThermalHdiTest006: start.");
253 int32_t ret = g_thermalInterface->Register(g_callback);
254 EXPECT_EQ(0, ret) << "HdfThermalHdiTest006 failed";
255 printf("HdfThermalHdiTest006: return.");
256 }
257
258 /**
259 * @tc.name: HdfThermalHdiTest007
260 * @tc.desc: Unregister
261 * @tc.type: FUNC
262 */
263 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest007, TestSize.Level1)
264 {
265 printf("HdfThermalHdiTest007: start.");
266 int32_t ret = g_thermalInterface->Unregister();
267 EXPECT_EQ(0, ret) << "HdfThermalHdiTest007 failed";
268 printf("HdfThermalHdiTest007: return.");
269 }
270 }
271