1 /*
2 * Copyright (c) 2021-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 #include "thermal_log.h"
31
32 using namespace OHOS::HDI;
33 using namespace OHOS::HDI::Thermal::V1_0;
34 using namespace testing::ext;
35
36 namespace {
37 class ThermalCallbackMock : public IThermalCallback {
38 public:
~ThermalCallbackMock()39 virtual ~ThermalCallbackMock() {}
40 using ThermalEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
RegisterThermalEvent(const ThermalEventCallback & eventCb)41 static int32_t RegisterThermalEvent(const ThermalEventCallback &eventCb)
42 {
43 (void)eventCb;
44 return 0;
45 }
OnThermalDataEvent(const HdfThermalCallbackInfo & event)46 int32_t OnThermalDataEvent(const HdfThermalCallbackInfo &event) override
47 {
48 (void)event;
49 return 0;
50 }
51 };
52
53 sptr<IThermalInterface> g_thermalInterface = nullptr;
54 sptr<IThermalCallback> g_callback = new ThermalCallbackMock();
55 std::mutex g_mutex;
56 const uint32_t MAX_PATH = 256;
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();
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, S_IRUSR | S_IRGRP | S_IROTH);
94 if (fd < HDF_SUCCESS) {
95 THERMAL_HILOGE(LABEL_TEST, "WriteFile: failed to open file %{public}d", fd);
96 return HDF_FAILURE;
97 }
98
99 ret = read(fd, buf, size);
100 if (ret < HDF_SUCCESS) {
101 THERMAL_HILOGE(LABEL_TEST, "WriteFile: failed to read file %{public}d", 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: set cpu freq
131 * @tc.type: FUNC
132 */
133 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest002, TestSize.Level1)
134 {
135 THERMAL_HILOGD(LABEL_TEST, "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 ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
148 if (ret != HDF_SUCCESS) {
149 THERMAL_HILOGE(LABEL_TEST, "HdfThermalHdiTest002: Failed to read file ");
150 return;
151 }
152
153 std::string freq = freqValue;
154 int32_t value = HdfThermalHdiTest::ConvertInt(freq);
155 THERMAL_HILOGD(LABEL_TEST, "freq is %{public}d", value);
156 EXPECT_EQ(value, cpuFreq) << "HdfThermalHdiTest002 failed";
157 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest002: return.");
158 }
159
160 /**
161 * @tc.name: HdfThermalHdiTest003
162 * @tc.desc: set gpu freq
163 * @tc.type: FUNC
164 */
165 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest003, TestSize.Level1)
166 {
167 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest003: start.");
168 int32_t gpuFreq = 40000;
169 int32_t ret = g_thermalInterface->SetGpuFreq(gpuFreq);
170 EXPECT_EQ(0, ret);
171
172 char cpuBuf[MAX_PATH] = {0};
173 char freqValue[MAX_PATH] = {0};
174
175 if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, GPU_FREQ_PATH.c_str()) < EOK) {
176 return;
177 }
178
179 ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
180 if (ret != HDF_SUCCESS) {
181 THERMAL_HILOGE(LABEL_TEST, "HdfThermalHdiTest003: Failed to read file ");
182 return;
183 }
184
185 std::string freq = freqValue;
186 int32_t value = HdfThermalHdiTest::ConvertInt(freq);
187 THERMAL_HILOGD(LABEL_TEST, "freq is %{public}d", value);
188 EXPECT_EQ(value, gpuFreq) << "HdfThermalHdiTest003 failed";
189 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest003: return.");
190 }
191
192 /**
193 * @tc.name: HdfThermalHdiTest004
194 * @tc.desc: set battery current
195 * @tc.type: FUNC
196 */
197 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest004, TestSize.Level1)
198 {
199 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest004: start.");
200 int32_t batteryCurrent = 1000;
201 int32_t ret = g_thermalInterface->SetBatteryCurrent(batteryCurrent);
202 EXPECT_EQ(0, ret);
203
204 char cpuBuf[MAX_PATH] = {0};
205 char currentValue[MAX_PATH] = {0};
206
207 if (snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, BATTERY_CHARGER_CURRENT_PATH.c_str()) < EOK) {
208 return;
209 }
210
211 ret = HdfThermalHdiTest::ReadFile(cpuBuf, currentValue, sizeof(currentValue));
212 if (ret != HDF_SUCCESS) {
213 THERMAL_HILOGE(LABEL_TEST, "HdfThermalHdiTest004: Failed to read file ");
214 return;
215 }
216
217 std::string current = currentValue;
218 int32_t value = HdfThermalHdiTest::ConvertInt(current);
219 THERMAL_HILOGD(LABEL_TEST, "freq is %{public}d", value);
220 EXPECT_EQ(value, batteryCurrent) << "HdfThermalHdiTest004 failed";
221 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest004: return.");
222 }
223
224 /**
225 * @tc.name: HdfThermalHdiTest005
226 * @tc.desc: get thermal zone info
227 * @tc.type: FUNC
228 */
229 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest005, TestSize.Level1)
230 {
231 HdfThermalCallbackInfo event;
232 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest005: start.");
233 int32_t ret = g_thermalInterface->GetThermalZoneInfo(event);
234 EXPECT_EQ(0, ret) << "HdfThermalHdiTest005 failed";
235 for (auto iter : event.info) {
236 THERMAL_HILOGD(LABEL_TEST, "type is %{public}s", iter.type.c_str());
237 THERMAL_HILOGD(LABEL_TEST, "temp is %{public}d", iter.temp);
238 }
239 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest005: return.");
240 }
241
242 /**
243 * @tc.name: HdfThermalHdiTest006
244 * @tc.desc: register callback
245 * @tc.type: FUNC
246 */
247 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest006, TestSize.Level1)
248 {
249 HdfThermalCallbackInfo event;
250 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest006: start.");
251 int32_t ret = g_thermalInterface->Register(g_callback);
252 EXPECT_EQ(0, ret) << "HdfThermalHdiTest006 failed";
253 ret = g_thermalInterface->Unregister();
254 EXPECT_EQ(0, ret) << "HdfThermalHdiTest006 failed";
255 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest006: return.");
256 }
257 }
258