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