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 "hdi_service_test.h"
17 #include "battery_log.h"
18 #include "battery_thread_test.h"
19 #include "power_supply_provider.h"
20 #include <chrono>
21 #include <condition_variable>
22 #include <csignal>
23 #include <cstring>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <fstream>
27 #include <iostream>
28 #include <memory>
29 #include <mutex>
30 #include <streambuf>
31 #include <sys/stat.h>
32 #include <thread>
33 #include <vector>
34
35 using namespace testing::ext;
36 using namespace OHOS;
37 using namespace OHOS::HDI::Battery;
38 using namespace OHOS::HDI::Battery::V2_0;
39 using namespace std;
40
41 namespace HdiServiceTest {
42 const std::string SYSTEM_BATTERY_PATH = "/sys/class/power_supply";
43 const std::string MOCK_BATTERY_PATH = "/data/service/el0/battery/";
44 static std::vector<std::string> g_filenodeName;
45 static std::map<std::string, std::string> g_nodeInfo;
46 const int STR_TO_LONG_LEN = 10;
47 const int NUM_ZERO = 0;
48 constexpr int32_t ERROR = -1;
49 constexpr int32_t MAX_BUFF_SIZE = 128;
50 constexpr int32_t MAX_SYSFS_SIZE = 64;
51 std::unique_ptr<PowerSupplyProvider> giver_ = nullptr;
52
SetUpTestCase(void)53 void HdiServiceTest::SetUpTestCase(void)
54 {
55 giver_ = std::make_unique<PowerSupplyProvider>();
56 if (giver_ == nullptr) {
57 BATTERY_HILOGI(LABEL_TEST, "Failed to get PowerSupplyProvider");
58 }
59 }
60
TearDownTestCase(void)61 void HdiServiceTest::TearDownTestCase(void) {}
62
SetUp(void)63 void HdiServiceTest::SetUp(void) {}
64
TearDown(void)65 void HdiServiceTest::TearDown(void) {}
66
67 struct StringEnumMap {
68 const char* str;
69 int32_t enumVal;
70 };
71
CreateFile(std::string path, std::string content)72 std::string CreateFile(std::string path, std::string content)
73 {
74 std::ofstream stream(path.c_str());
75 if (!stream.is_open()) {
76 BATTERY_HILOGI(LABEL_TEST, "Cannot create file");
77 return nullptr;
78 }
79 stream << content.c_str() << std::endl;
80 stream.close();
81 return path;
82 }
83
CheckSubfolderNode(const std::string& path)84 static void CheckSubfolderNode(const std::string& path)
85 {
86 DIR* dir = nullptr;
87 struct dirent* entry = nullptr;
88 std::string batteryPath = SYSTEM_BATTERY_PATH + "/" + path;
89
90 dir = opendir(batteryPath.c_str());
91 if (dir == nullptr) {
92 BATTERY_HILOGI(LABEL_TEST, "subfolder file is not exist.");
93 return;
94 }
95
96 while (true) {
97 entry = readdir(dir);
98 if (entry == nullptr) {
99 break;
100 }
101
102 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
103 continue;
104 }
105
106 if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) {
107 continue;
108 }
109
110 if ((strcmp(entry->d_name, "type") == 0) && (g_nodeInfo["type"] == "") &&
111 (strcasecmp(path.c_str(), "battery") != 0)) {
112 g_nodeInfo["type"] = path;
113 }
114
115 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
116 if ((strcmp(entry->d_name, iter->first.c_str()) == 0) && (g_nodeInfo[iter->first] == "")) {
117 g_nodeInfo[iter->first] = path;
118 }
119 }
120 }
121 closedir(dir);
122 }
123
TraversalBaseNode()124 static void TraversalBaseNode()
125 {
126 g_nodeInfo.insert(std::make_pair("type", ""));
127 g_nodeInfo.insert(std::make_pair("online", ""));
128 g_nodeInfo.insert(std::make_pair("current_max", ""));
129 g_nodeInfo.insert(std::make_pair("voltage_max", ""));
130 g_nodeInfo.insert(std::make_pair("capacity", ""));
131 g_nodeInfo.insert(std::make_pair("voltage_now", ""));
132 g_nodeInfo.insert(std::make_pair("temp", ""));
133 g_nodeInfo.insert(std::make_pair("health", ""));
134 g_nodeInfo.insert(std::make_pair("status", ""));
135 g_nodeInfo.insert(std::make_pair("present", ""));
136 g_nodeInfo.insert(std::make_pair("charge_counter", ""));
137 g_nodeInfo.insert(std::make_pair("technology", ""));
138 g_nodeInfo.insert(std::make_pair("charge_full", ""));
139 g_nodeInfo.insert(std::make_pair("current_avg", ""));
140 g_nodeInfo.insert(std::make_pair("current_now", ""));
141 g_nodeInfo.insert(std::make_pair("charge_now", ""));
142
143 auto iter = g_filenodeName.begin();
144 while (iter != g_filenodeName.end()) {
145 if (*iter == "battery") {
146 CheckSubfolderNode(*iter);
147 iter = g_filenodeName.erase(iter);
148 } else {
149 iter++;
150 }
151 }
152
153 iter = g_filenodeName.begin();
154 while (iter != g_filenodeName.end()) {
155 if (*iter == "Battery") {
156 CheckSubfolderNode(*iter);
157 iter = g_filenodeName.erase(iter);
158 } else {
159 iter++;
160 }
161 }
162
163 for (auto it = g_filenodeName.begin(); it != g_filenodeName.end(); ++it) {
164 CheckSubfolderNode(*it);
165 }
166 }
167
InitBaseSysfs(void)168 static int32_t InitBaseSysfs(void)
169 {
170 DIR* dir = nullptr;
171 struct dirent* entry = nullptr;
172 int32_t index = 0;
173
174 dir = opendir(SYSTEM_BATTERY_PATH.c_str());
175 if (dir == nullptr) {
176 BATTERY_HILOGE(LABEL_TEST, "cannot open POWER_SUPPLY_BASE_PATH");
177 return HDF_ERR_IO;
178 }
179
180 while (true) {
181 entry = readdir(dir);
182 if (entry == nullptr) {
183 break;
184 }
185
186 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
187 continue;
188 }
189
190 if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) {
191 BATTERY_HILOGI(LABEL_TEST, "init sysfs info of %{public}s", entry->d_name);
192 if (index >= MAX_SYSFS_SIZE) {
193 BATTERY_HILOGE(LABEL_TEST, "too many plugged types");
194 break;
195 }
196 g_filenodeName.emplace_back(entry->d_name);
197 index++;
198 }
199 }
200
201 TraversalBaseNode();
202 BATTERY_HILOGI(LABEL_TEST, "index is %{public}d", index);
203 closedir(dir);
204
205 return HDF_SUCCESS;
206 }
207
ReadTemperatureSysfs()208 static int32_t ReadTemperatureSysfs()
209 {
210 int strlen = 10;
211 char buf[128] = {0};
212 int32_t readSize;
213 InitBaseSysfs();
214 std::string tempNode = "battery";
215 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
216 if (iter->first == "temp") {
217 tempNode = iter->second;
218 break;
219 }
220 }
221 std::string sysBattTemPath = SYSTEM_BATTERY_PATH + "/" + tempNode + "/" + "temp";
222
223 int fd = open(sysBattTemPath.c_str(), O_RDONLY);
224 if (fd < NUM_ZERO) {
225 BATTERY_HILOGE(LABEL_TEST, "failed to open TemperatureSysfs");
226 return HDF_FAILURE;
227 }
228
229 readSize = read(fd, buf, sizeof(buf) - 1);
230 if (readSize < NUM_ZERO) {
231 BATTERY_HILOGE(LABEL_TEST, "failed to read TemperatureSysfs");
232 close(fd);
233 return HDF_FAILURE;
234 }
235
236 buf[readSize] = '\0';
237 int32_t battTemperature = strtol(buf, nullptr, strlen);
238 if (battTemperature < NUM_ZERO) {
239 BATTERY_HILOGE(LABEL_TEST, "read system file temperature is %{public}d", battTemperature);
240 }
241 BATTERY_HILOGE(LABEL_TEST, "read system file temperature is %{public}d", battTemperature);
242 close(fd);
243 return battTemperature;
244 }
245
ReadVoltageSysfs()246 static int32_t ReadVoltageSysfs()
247 {
248 int strlen = 10;
249 char buf[128] = {0};
250 int32_t readSize;
251 std::string voltageNode = "battery";
252 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
253 if (iter->first == "voltage_now") {
254 voltageNode = iter->second;
255 break;
256 }
257 }
258 std::string sysBattVolPath = SYSTEM_BATTERY_PATH + "/" + voltageNode + "/" + "voltage_now";
259
260 int fd = open(sysBattVolPath.c_str(), O_RDONLY);
261 if (fd < NUM_ZERO) {
262 BATTERY_HILOGE(LABEL_TEST, "failed to open VoltageSysfs");
263 return HDF_FAILURE;
264 }
265
266 readSize = read(fd, buf, sizeof(buf) - 1);
267 if (readSize < HDF_SUCCESS) {
268 BATTERY_HILOGE(LABEL_TEST, "failed to read VoltageSysfs");
269 close(fd);
270 return HDF_FAILURE;
271 }
272
273 buf[readSize] = '\0';
274 int32_t battVoltage = strtol(buf, nullptr, strlen);
275 if (battVoltage < NUM_ZERO) {
276 BATTERY_HILOGE(LABEL_TEST, "read system file voltage is %{public}d", battVoltage);
277 }
278 BATTERY_HILOGE(LABEL_TEST, "read system file voltage is %{public}d", battVoltage);
279 close(fd);
280 return battVoltage;
281 }
282
ReadCapacitySysfs()283 static int32_t ReadCapacitySysfs()
284 {
285 int strlen = 10;
286 char buf[128] = {0};
287 int32_t readSize;
288 std::string capacityNode = "battery";
289 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
290 if (iter->first == "capacity") {
291 capacityNode = iter->second;
292 break;
293 }
294 }
295 std::string sysBattCapPath = SYSTEM_BATTERY_PATH + "/" + capacityNode + "/" + "capacity";
296
297 int fd = open(sysBattCapPath.c_str(), O_RDONLY);
298 if (fd < NUM_ZERO) {
299 BATTERY_HILOGE(LABEL_TEST, "failed to open CapacitySysfs");
300 return HDF_FAILURE;
301 }
302
303 readSize = read(fd, buf, sizeof(buf) - 1);
304 if (readSize < NUM_ZERO) {
305 BATTERY_HILOGE(LABEL_TEST, "failed to read CapacitySysfs");
306 close(fd);
307 return HDF_FAILURE;
308 }
309
310 buf[readSize] = '\0';
311 int32_t battCapacity = strtol(buf, nullptr, strlen);
312 if (battCapacity < NUM_ZERO) {
313 BATTERY_HILOGE(LABEL_TEST, "read system file capacity is %{public}d", battCapacity);
314 }
315 BATTERY_HILOGE(LABEL_TEST, "read system file capacity is %{public}d", battCapacity);
316 close(fd);
317 return battCapacity;
318 }
319
ReadTotalEnergySysfs()320 static int32_t ReadTotalEnergySysfs()
321 {
322 int strlen = 10;
323 char buf[128] = {0};
324 int32_t readSize;
325 InitBaseSysfs();
326 std::string totalEnergyNode = "battery";
327 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
328 if (iter->first == "charge_full") {
329 totalEnergyNode = iter->second;
330 break;
331 }
332 }
333 std::string sysBattTotalEnergyPath = SYSTEM_BATTERY_PATH + "/" + totalEnergyNode + "/" + "charge_full";
334
335 int fd = open(sysBattTotalEnergyPath.c_str(), O_RDONLY);
336 if (fd < NUM_ZERO) {
337 BATTERY_HILOGE(LABEL_TEST, "failed to open TotalEnergySysfs");
338 return HDF_FAILURE;
339 }
340
341 readSize = read(fd, buf, sizeof(buf) - 1);
342 if (readSize < NUM_ZERO) {
343 BATTERY_HILOGE(LABEL_TEST, "failed to read TotalEnergySysfs");
344 close(fd);
345 return HDF_FAILURE;
346 }
347
348 buf[readSize] = '\0';
349 int32_t totalEnergy = strtol(buf, nullptr, strlen);
350 if (totalEnergy < NUM_ZERO) {
351 BATTERY_HILOGE(LABEL_TEST, "read system file totalEnergy is %{public}d", totalEnergy);
352 }
353 BATTERY_HILOGE(LABEL_TEST, "read system file totalEnergy is %{public}d", totalEnergy);
354 close(fd);
355 return totalEnergy;
356 }
357
ReadCurrentAverageSysfs()358 static int32_t ReadCurrentAverageSysfs()
359 {
360 int strlen = 10;
361 char buf[128] = {0};
362 int32_t readSize;
363 InitBaseSysfs();
364 std::string currentAvgNode = "battery";
365 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
366 if (iter->first == "current_avg") {
367 currentAvgNode = iter->second;
368 break;
369 }
370 }
371 std::string sysBattCurrentAvgPath = SYSTEM_BATTERY_PATH + "/" + currentAvgNode + "/" + "current_avg";
372
373 int fd = open(sysBattCurrentAvgPath.c_str(), O_RDONLY);
374 if (fd < NUM_ZERO) {
375 BATTERY_HILOGE(LABEL_TEST, "failed to open CurrentAverageSysfs");
376 return HDF_FAILURE;
377 }
378
379 readSize = read(fd, buf, sizeof(buf) - 1);
380 if (readSize < NUM_ZERO) {
381 BATTERY_HILOGE(LABEL_TEST, "failed to read CurrentAverageSysfs");
382 close(fd);
383 return HDF_FAILURE;
384 }
385
386 buf[readSize] = '\0';
387 int32_t currentAvg = strtol(buf, nullptr, strlen);
388 if (currentAvg < NUM_ZERO) {
389 BATTERY_HILOGE(LABEL_TEST, "read system file currentAvg is %{public}d", currentAvg);
390 }
391 BATTERY_HILOGE(LABEL_TEST, "read system file currentAvg is %{public}d", currentAvg);
392 close(fd);
393 return currentAvg;
394 }
395
ReadCurrentNowSysfs()396 static int32_t ReadCurrentNowSysfs()
397 {
398 int strlen = 10;
399 char buf[128] = {0};
400 int32_t readSize;
401 InitBaseSysfs();
402 std::string currentNowNode = "battery";
403 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
404 if (iter->first == "current_now") {
405 currentNowNode = iter->second;
406 break;
407 }
408 }
409 std::string sysBattCurrentNowPath = SYSTEM_BATTERY_PATH + "/" + currentNowNode + "/" + "current_now";
410
411 int fd = open(sysBattCurrentNowPath.c_str(), O_RDONLY);
412 if (fd < NUM_ZERO) {
413 BATTERY_HILOGE(LABEL_TEST, "failed to open CurrentNowSysfs");
414 return HDF_FAILURE;
415 }
416
417 readSize = read(fd, buf, sizeof(buf) - 1);
418 if (readSize < NUM_ZERO) {
419 BATTERY_HILOGE(LABEL_TEST, "failed to read CurrentNowSysfs");
420 close(fd);
421 return HDF_FAILURE;
422 }
423
424 buf[readSize] = '\0';
425 int32_t currentNow = strtol(buf, nullptr, strlen);
426 if (currentNow < NUM_ZERO) {
427 BATTERY_HILOGE(LABEL_TEST, "read system file currentNow is %{public}d", currentNow);
428 }
429 BATTERY_HILOGE(LABEL_TEST, "read system file currentNow is %{public}d", currentNow);
430 close(fd);
431 return currentNow;
432 }
433
ReadRemainEnergySysfs()434 static int32_t ReadRemainEnergySysfs()
435 {
436 int strlen = 10;
437 char buf[128] = {0};
438 int32_t readSize;
439 InitBaseSysfs();
440 std::string chargeNowNode = "battery";
441 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
442 if (iter->first == "charge_now") {
443 chargeNowNode = iter->second;
444 break;
445 }
446 }
447 std::string sysBattChargeNowPath = SYSTEM_BATTERY_PATH + "/" + chargeNowNode + "/" + "charge_now";
448
449 int fd = open(sysBattChargeNowPath.c_str(), O_RDONLY);
450 if (fd < NUM_ZERO) {
451 BATTERY_HILOGE(LABEL_TEST, "failed to open RemainEnergySysfs");
452 return HDF_FAILURE;
453 }
454
455 readSize = read(fd, buf, sizeof(buf) - 1);
456 if (readSize < NUM_ZERO) {
457 BATTERY_HILOGE(LABEL_TEST, "failed to read RemainEnergySysfs");
458 close(fd);
459 return HDF_FAILURE;
460 }
461
462 buf[readSize] = '\0';
463 int32_t chargeNow = strtol(buf, nullptr, strlen);
464 if (chargeNow < NUM_ZERO) {
465 BATTERY_HILOGE(LABEL_TEST, "read system file chargeNow is %{public}d", chargeNow);
466 }
467 BATTERY_HILOGE(LABEL_TEST, "read system file chargeNow is %{public}d", chargeNow);
468 close(fd);
469 return chargeNow;
470 }
471
Trim(char* str)472 static void Trim(char* str)
473 {
474 if (str == nullptr) {
475 return;
476 }
477 str[strcspn(str, "\n")] = 0;
478 }
479
HealthStateEnumConverter(const char* str)480 static int32_t HealthStateEnumConverter(const char* str)
481 {
482 struct StringEnumMap healthStateEnumMap[] = {
483 {"Good", PowerSupplyProvider::BATTERY_HEALTH_GOOD },
484 {"Cold", PowerSupplyProvider::BATTERY_HEALTH_COLD },
485 {"Warm", PowerSupplyProvider::BATTERY_HEALTH_GOOD }, // JEITA specification
486 {"Cool", PowerSupplyProvider::BATTERY_HEALTH_GOOD }, // JEITA specification
487 {"Hot", PowerSupplyProvider::BATTERY_HEALTH_OVERHEAT }, // JEITA specification
488 {"Overheat", PowerSupplyProvider::BATTERY_HEALTH_OVERHEAT },
489 {"Over voltage", PowerSupplyProvider::BATTERY_HEALTH_OVERVOLTAGE},
490 {"Dead", PowerSupplyProvider::BATTERY_HEALTH_DEAD },
491 {"Unknown", PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN },
492 {"Unspecified failure", PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN },
493 {NULL, PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN },
494 };
495
496 for (int i = 0; healthStateEnumMap[i].str; ++i) {
497 if (strcmp(str, healthStateEnumMap[i].str) == 0) {
498 return healthStateEnumMap[i].enumVal;
499 }
500 }
501
502 return PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN;
503 }
504
ReadHealthStateSysfs()505 static int32_t ReadHealthStateSysfs()
506 {
507 char buf[128] = {0};
508 int32_t readSize;
509 std::string healthNode = "battery";
510 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
511 if (iter->first == "health") {
512 healthNode = iter->second;
513 break;
514 }
515 }
516 std::string sysHealthStatePath = SYSTEM_BATTERY_PATH + "/" + healthNode + "/" + "health";
517
518 int fd = open(sysHealthStatePath.c_str(), O_RDONLY);
519 if (fd < NUM_ZERO) {
520 BATTERY_HILOGE(LABEL_TEST, "failed to open HealthStateSysfs");
521 return HDF_FAILURE;
522 }
523
524 readSize = read(fd, buf, sizeof(buf) - 1);
525 if (readSize < NUM_ZERO) {
526 BATTERY_HILOGE(LABEL_TEST, "failed to read HealthStateSysfs");
527 close(fd);
528 return HDF_FAILURE;
529 }
530
531 Trim(buf);
532
533 int32_t battHealthState = HealthStateEnumConverter(buf);
534 BATTERY_HILOGE(LABEL_TEST, "read system file healthState is %{public}d", battHealthState);
535 close(fd);
536 return battHealthState;
537 }
538
PluggedTypeEnumConverter(const char* str)539 static int32_t PluggedTypeEnumConverter(const char* str)
540 {
541 struct StringEnumMap pluggedTypeEnumMap[] = {
542 {"USB", PowerSupplyProvider::PLUGGED_TYPE_USB },
543 {"USB_PD_DRP", PowerSupplyProvider::PLUGGED_TYPE_USB },
544 {"Wireless", PowerSupplyProvider::PLUGGED_TYPE_WIRELESS},
545 {"Mains", PowerSupplyProvider::PLUGGED_TYPE_AC },
546 {"UPS", PowerSupplyProvider::PLUGGED_TYPE_AC },
547 {"USB_ACA", PowerSupplyProvider::PLUGGED_TYPE_AC },
548 {"USB_C", PowerSupplyProvider::PLUGGED_TYPE_AC },
549 {"USB_CDP", PowerSupplyProvider::PLUGGED_TYPE_AC },
550 {"USB_DCP", PowerSupplyProvider::PLUGGED_TYPE_AC },
551 {"USB_HVDCP", PowerSupplyProvider::PLUGGED_TYPE_AC },
552 {"USB_PD", PowerSupplyProvider::PLUGGED_TYPE_AC },
553 {"Unknown", PowerSupplyProvider::PLUGGED_TYPE_BUTT },
554 {NULL, PowerSupplyProvider::PLUGGED_TYPE_BUTT },
555 };
556
557 for (int i = 0; pluggedTypeEnumMap[i].str; ++i) {
558 if (strcmp(str, pluggedTypeEnumMap[i].str) == 0) {
559 return pluggedTypeEnumMap[i].enumVal;
560 }
561 }
562
563 return PowerSupplyProvider::PLUGGED_TYPE_BUTT;
564 }
565
ReadSysfsFile(const char* path, char* buf, size_t size)566 int32_t ReadSysfsFile(const char* path, char* buf, size_t size)
567 {
568 int fd = open(path, O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
569 if (fd < NUM_ZERO) {
570 BATTERY_HILOGE(LABEL_TEST, "failed to open file");
571 return HDF_ERR_IO;
572 }
573
574 int32_t readSize = read(fd, buf, size - 1);
575 if (readSize < NUM_ZERO) {
576 BATTERY_HILOGE(LABEL_TEST, "failed to read file");
577 close(fd);
578 return HDF_ERR_IO;
579 }
580
581 buf[readSize] = '\0';
582 Trim(buf);
583 close(fd);
584
585 return HDF_SUCCESS;
586 }
587
ReadPluggedTypeSysfs()588 static int32_t ReadPluggedTypeSysfs()
589 {
590 std::string node = "USB";
591 int32_t online = ERROR;
592 char buf[MAX_BUFF_SIZE] = {0};
593
594 auto iter = g_filenodeName.begin();
595 while (iter != g_filenodeName.end()) {
596 if (strcasecmp(iter->c_str(), "battery") == 0) {
597 continue;
598 }
599 std::string onlinePath = SYSTEM_BATTERY_PATH + "/" + *iter + "/" + "online";
600 if (ReadSysfsFile(onlinePath.c_str(), buf, MAX_BUFF_SIZE) != HDF_SUCCESS) {
601 BATTERY_HILOGW(LABEL_TEST, "read online path failed in loop");
602 }
603 online = strtol(buf, nullptr, STR_TO_LONG_LEN);
604 if (online) {
605 node = *iter;
606 break;
607 }
608 iter++;
609 }
610 if (online == ERROR) {
611 return ERROR;
612 }
613 std::string typePath = SYSTEM_BATTERY_PATH + "/" + node + "/" + "type";
614 if (ReadSysfsFile(typePath.c_str(), buf, MAX_BUFF_SIZE) != HDF_SUCCESS) {
615 BATTERY_HILOGI(LABEL_TEST, "read type path failed");
616 return ERROR;
617 }
618 Trim(buf);
619 return PluggedTypeEnumConverter(buf);
620 }
621
ChargeStateEnumConverter(const char* str)622 int32_t ChargeStateEnumConverter(const char* str)
623 {
624 struct StringEnumMap chargeStateEnumMap[] = {
625 {"Discharging", PowerSupplyProvider::CHARGE_STATE_NONE },
626 {"Charging", PowerSupplyProvider::CHARGE_STATE_ENABLE },
627 {"Full", PowerSupplyProvider::CHARGE_STATE_FULL },
628 {"Not charging", PowerSupplyProvider::CHARGE_STATE_DISABLE },
629 {"Unknown", PowerSupplyProvider::CHARGE_STATE_RESERVED},
630 {NULL, PowerSupplyProvider::CHARGE_STATE_RESERVED},
631 };
632
633 for (int i = 0; chargeStateEnumMap[i].str; ++i) {
634 if (strcmp(str, chargeStateEnumMap[i].str) == 0) {
635 return chargeStateEnumMap[i].enumVal;
636 }
637 }
638 return PowerSupplyProvider::CHARGE_STATE_RESERVED;
639 }
640
ReadChargeStateSysfs()641 static int32_t ReadChargeStateSysfs()
642 {
643 char buf[128] = {0};
644 int32_t readSize;
645 std::string statusNode = "battery";
646 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
647 if (iter->first == "status") {
648 statusNode = iter->second;
649 break;
650 }
651 }
652 std::string sysChargeStatePath = SYSTEM_BATTERY_PATH + "/" + statusNode + "/" + "status";
653
654 int fd = open(sysChargeStatePath.c_str(), O_RDONLY);
655 if (fd < NUM_ZERO) {
656 BATTERY_HILOGE(LABEL_TEST, "failed to open ChargeStateSysfs");
657 return HDF_FAILURE;
658 }
659
660 readSize = read(fd, buf, sizeof(buf) - 1);
661 if (readSize < NUM_ZERO) {
662 BATTERY_HILOGE(LABEL_TEST, "failed to read ChargeStateSysfs");
663 close(fd);
664 return HDF_FAILURE;
665 }
666
667 Trim(buf);
668 int32_t battChargeState = ChargeStateEnumConverter(buf);
669 BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battChargeState);
670 close(fd);
671
672 return battChargeState;
673 }
674
ReadChargeCounterSysfs()675 static int32_t ReadChargeCounterSysfs()
676 {
677 int strlen = 10;
678 char buf[128] = {0};
679 int32_t readSize;
680 std::string counterNode = "battery";
681 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
682 if (iter->first == "charge_counter") {
683 counterNode = iter->second;
684 break;
685 }
686 }
687 std::string sysChargeCounterPath = SYSTEM_BATTERY_PATH + "/" + counterNode + "/" + "charge_counter";
688
689 int fd = open(sysChargeCounterPath.c_str(), O_RDONLY);
690 if (fd < NUM_ZERO) {
691 BATTERY_HILOGE(LABEL_TEST, "failed to open ChargeCounterSysfs");
692 return HDF_FAILURE;
693 }
694
695 readSize = read(fd, buf, sizeof(buf) - 1);
696 if (readSize < NUM_ZERO) {
697 BATTERY_HILOGE(LABEL_TEST, "failed to read ChargeCounterSysfs");
698 close(fd);
699 return HDF_FAILURE;
700 }
701
702 buf[readSize] = '\0';
703 int32_t battChargeCounter = strtol(buf, nullptr, strlen);
704 if (battChargeCounter < 0) {
705 BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battChargeCounter);
706 }
707 BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battChargeCounter);
708 close(fd);
709
710 return battChargeCounter;
711 }
712
ReadPresentSysfs()713 static int32_t ReadPresentSysfs()
714 {
715 int strlen = 10;
716 char buf[128] = {0};
717 int32_t readSize;
718 std::string presentNode = "battery";
719 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
720 if (iter->first == "present") {
721 presentNode = iter->second;
722 break;
723 }
724 }
725 std::string sysPresentPath = SYSTEM_BATTERY_PATH + "/" + presentNode + "/" + "present";
726
727 int fd = open(sysPresentPath.c_str(), O_RDONLY);
728 if (fd < NUM_ZERO) {
729 BATTERY_HILOGE(LABEL_TEST, "failed to open PresentSysfs");
730 return HDF_FAILURE;
731 }
732
733 readSize = read(fd, buf, sizeof(buf) - 1);
734 if (readSize < NUM_ZERO) {
735 BATTERY_HILOGE(LABEL_TEST, "failed to read PresentSysfs");
736 close(fd);
737 return HDF_FAILURE;
738 }
739
740 buf[readSize] = '\0';
741 int32_t battPresent = strtol(buf, nullptr, strlen);
742 if (battPresent < 0) {
743 BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battPresent);
744 }
745 BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battPresent);
746 close(fd);
747 return battPresent;
748 }
749
ReadTechnologySysfs(std::string& battTechnology)750 static std::string ReadTechnologySysfs(std::string& battTechnology)
751 {
752 char buf[128] = {0};
753 int32_t readSize;
754 std::string technologyNode = "battery";
755 for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
756 if (iter->first == "technology") {
757 technologyNode = iter->second;
758 break;
759 }
760 }
761 std::string sysTechnologyPath = SYSTEM_BATTERY_PATH + "/" + technologyNode + "/" + "technology";
762
763 int fd = open(sysTechnologyPath.c_str(), O_RDONLY);
764 if (fd < NUM_ZERO) {
765 BATTERY_HILOGE(LABEL_TEST, "failed to open TechnologySysfs");
766 return "";
767 }
768
769 readSize = read(fd, buf, sizeof(buf) - 1);
770 if (readSize < NUM_ZERO) {
771 BATTERY_HILOGE(LABEL_TEST, "failed to read TechnologySysfs");
772 close(fd);
773 return "";
774 }
775 buf[readSize] = '\0';
776 Trim(buf);
777
778 battTechnology.assign(buf, strlen(buf));
779 BATTERY_HILOGE(LABEL_TEST, "read system file technology is %{public}s.", battTechnology.c_str());
780 close(fd);
781 return battTechnology;
782 }
783
IsNotMock()784 static bool IsNotMock()
785 {
786 bool rootExist = access(SYSTEM_BATTERY_PATH.c_str(), F_OK) == 0;
787 bool lowerExist = access((SYSTEM_BATTERY_PATH + "/battery").c_str(), F_OK) == 0;
788 bool upperExist = access((SYSTEM_BATTERY_PATH + "/Battery").c_str(), F_OK) == 0;
789 return rootExist && (lowerExist || upperExist);
790 }
791
792 /**
793 * @tc.name: ProviderIsNotNull
794 * @tc.desc: Test functions of PowerSupplyProvider
795 * @tc.type: FUNC
796 */
HWTEST_F(HdiServiceTest, ProviderIsNotNull, TestSize.Level1)797 HWTEST_F(HdiServiceTest, ProviderIsNotNull, TestSize.Level1)
798 {
799 ASSERT_TRUE(giver_ != nullptr);
800 if (!IsNotMock()) {
801 giver_->SetSysFilePath(MOCK_BATTERY_PATH);
802 BATTERY_HILOGI(LABEL_TEST, "Is mock test");
803 }
804 giver_->InitPowerSupplySysfs();
805 }
806
807 /**
808 * @tc.name: HdiService001
809 * @tc.desc: Test functions of ParseTemperature
810 * @tc.type: FUNC
811 */
HWTEST_F(HdiServiceTest, HdiService001, TestSize.Level1)812 HWTEST_F(HdiServiceTest, HdiService001, TestSize.Level1)
813 {
814 int32_t temperature = 0;
815 if (IsNotMock()) {
816 giver_->ParseTemperature(&temperature);
817 int32_t sysfsTemp = ReadTemperatureSysfs();
818 BATTERY_HILOGI(
819 LABEL_TEST, "Not Mock HdiService001::temperature=%{public}d, t=%{public}d", temperature, sysfsTemp);
820 ASSERT_TRUE(temperature == sysfsTemp);
821 } else {
822 CreateFile(MOCK_BATTERY_PATH + "battery/temp", "567");
823 giver_->ParseTemperature(&temperature);
824 BATTERY_HILOGI(LABEL_TEST, "HdiService001::temperature=%{public}d.", temperature);
825 ASSERT_TRUE(temperature == 567);
826 }
827 }
828
829 /**
830 * @tc.name: HdiService002
831 * @tc.desc: Test functions of ParseVoltage
832 * @tc.type: FUNC
833 */
HWTEST_F(HdiServiceTest, HdiService002, TestSize.Level1)834 HWTEST_F(HdiServiceTest, HdiService002, TestSize.Level1)
835 {
836 int32_t voltage = 0;
837 if (IsNotMock()) {
838 giver_->ParseVoltage(&voltage);
839 int32_t sysfsVoltage = ReadVoltageSysfs();
840 BATTERY_HILOGI(LABEL_TEST, "Not Mock HdiService002::voltage=%{public}d, v=%{public}d", voltage, sysfsVoltage);
841 ASSERT_TRUE(voltage == sysfsVoltage);
842 } else {
843 CreateFile(MOCK_BATTERY_PATH + "battery/voltage_avg", "4123456");
844 CreateFile(MOCK_BATTERY_PATH + "battery/voltage_now", "4123456");
845 giver_->ParseVoltage(&voltage);
846 BATTERY_HILOGI(LABEL_TEST, "Not Mock HdiService002::voltage=%{public}d", voltage);
847 ASSERT_TRUE(voltage == 4123456);
848 }
849 }
850
851 /**
852 * @tc.name: HdiService003
853 * @tc.desc: Test functions of ParseCapacity
854 * @tc.type: FUNC
855 */
HWTEST_F(HdiServiceTest, HdiService003, TestSize.Level1)856 HWTEST_F(HdiServiceTest, HdiService003, TestSize.Level1)
857 {
858 int32_t capacity = -1;
859 if (IsNotMock()) {
860 giver_->ParseCapacity(&capacity);
861 int32_t sysfsCapacity = ReadCapacitySysfs();
862 BATTERY_HILOGI(
863 LABEL_TEST, "Not Mcok HdiService003::capacity=%{public}d, l=%{public}d", capacity, sysfsCapacity);
864 ASSERT_TRUE(capacity == sysfsCapacity);
865 } else {
866 CreateFile(MOCK_BATTERY_PATH + "battery/capacity", "11");
867 giver_->ParseCapacity(&capacity);
868 BATTERY_HILOGI(LABEL_TEST, "HdiService003::capacity=%{public}d", capacity);
869 ASSERT_TRUE(capacity == 11);
870 }
871 }
872
873 /**
874 * @tc.name: HdiService004
875 * @tc.desc: Test functions of ParseHealthState
876 * @tc.type: FUNC
877 */
HWTEST_F(HdiServiceTest, HdiService004, TestSize.Level1)878 HWTEST_F(HdiServiceTest, HdiService004, TestSize.Level1)
879 {
880 int32_t healthState = -1;
881 if (IsNotMock()) {
882 giver_->ParseHealthState(&healthState);
883 int32_t sysfsHealthState = ReadHealthStateSysfs();
884 BATTERY_HILOGI(
885 LABEL_TEST, "Not Mock HdiService004::healthState=%{public}d, h=%{public}d", healthState, sysfsHealthState);
886 ASSERT_TRUE(healthState == sysfsHealthState);
887 } else {
888 CreateFile(MOCK_BATTERY_PATH + "battery/health", "Good");
889 giver_->ParseHealthState(&healthState);
890 BATTERY_HILOGI(LABEL_TEST, "HdiService004::healthState=%{public}d.", healthState);
891 ASSERT_TRUE(PowerSupplyProvider::BatteryHealthState(healthState) ==
892 PowerSupplyProvider::BatteryHealthState::BATTERY_HEALTH_GOOD);
893 }
894 }
895
896 /**
897 * @tc.name: HdiService005
898 * @tc.desc: Test functions of ParsePluggedType
899 * @tc.type: FUNC
900 */
HWTEST_F(HdiServiceTest, HdiService005, TestSize.Level1)901 HWTEST_F(HdiServiceTest, HdiService005, TestSize.Level1)
902 {
903 int32_t pluggedType = PowerSupplyProvider::PLUGGED_TYPE_NONE;
904 if (IsNotMock()) {
905 giver_->ParsePluggedType(&pluggedType);
906 int32_t sysfsPluggedType = ReadPluggedTypeSysfs();
907 BATTERY_HILOGI(
908 LABEL_TEST, "Not Mock HdiService005::pluggedType=%{public}d, p=%{public}d", pluggedType, sysfsPluggedType);
909 ASSERT_TRUE(pluggedType == sysfsPluggedType);
910 } else {
911 CreateFile(MOCK_BATTERY_PATH + "ohos_charger/online", "1");
912 CreateFile(MOCK_BATTERY_PATH + "ohos_charger/type", "Wireless");
913 giver_->ParsePluggedType(&pluggedType);
914 BATTERY_HILOGI(LABEL_TEST, "HdiService005::pluggedType=%{public}d.", pluggedType);
915 ASSERT_TRUE(PowerSupplyProvider::BatteryPluggedType(pluggedType) ==
916 PowerSupplyProvider::BatteryPluggedType::PLUGGED_TYPE_WIRELESS);
917 }
918 }
919
920 /**
921 * @tc.name: HdiService006
922 * @tc.desc: Test functions of ParseChargeState
923 * @tc.type: FUNC
924 */
HWTEST_F(HdiServiceTest, HdiService006, TestSize.Level1)925 HWTEST_F(HdiServiceTest, HdiService006, TestSize.Level1)
926 {
927 int32_t chargeState = PowerSupplyProvider::CHARGE_STATE_RESERVED;
928 if (IsNotMock()) {
929 giver_->ParseChargeState(&chargeState);
930 int32_t sysfsChargeState = ReadChargeStateSysfs();
931 BATTERY_HILOGI(
932 LABEL_TEST, "Not Mock HdiService006::chargeState=%{public}d, cs=%{public}d", chargeState, sysfsChargeState);
933 ASSERT_TRUE(chargeState == sysfsChargeState);
934 } else {
935 CreateFile(MOCK_BATTERY_PATH + "battery/status", "Not charging");
936 giver_->ParseChargeState(&chargeState);
937 BATTERY_HILOGI(LABEL_TEST, "HdiService006::chargeState=%{public}d.", chargeState);
938 ASSERT_TRUE(PowerSupplyProvider::BatteryChargeState(chargeState) ==
939 PowerSupplyProvider::BatteryChargeState::CHARGE_STATE_DISABLE);
940 }
941 }
942
943 /**
944 * @tc.name: HdiService007
945 * @tc.desc: Test functions of ParseChargeCounter
946 * @tc.type: FUNC
947 */
HWTEST_F(HdiServiceTest, HdiService007, TestSize.Level1)948 HWTEST_F(HdiServiceTest, HdiService007, TestSize.Level1)
949 {
950 int32_t chargeCounter = -1;
951 if (IsNotMock()) {
952 giver_->ParseChargeCounter(&chargeCounter);
953 int32_t sysfsChargeCounter = ReadChargeCounterSysfs();
954 BATTERY_HILOGI(LABEL_TEST, "Not Mcok HdiService007::chargeCounter=%{public}d, cc=%{public}d", chargeCounter,
955 sysfsChargeCounter);
956 ASSERT_TRUE(chargeCounter == sysfsChargeCounter);
957 } else {
958 CreateFile(MOCK_BATTERY_PATH + "battery/charge_counter", "12345");
959 giver_->ParseChargeCounter(&chargeCounter);
960 BATTERY_HILOGI(LABEL_TEST, "HdiService007::chargeCounter=%{public}d.", chargeCounter);
961 ASSERT_TRUE(chargeCounter == 12345);
962 }
963 }
964
965 /**
966 * @tc.name: HdiService008
967 * @tc.desc: Test functions of ParsePresent
968 * @tc.type: FUNC
969 */
HWTEST_F(HdiServiceTest, HdiService008, TestSize.Level1)970 HWTEST_F(HdiServiceTest, HdiService008, TestSize.Level1)
971 {
972 int8_t present = -1;
973 if (IsNotMock()) {
974 giver_->ParsePresent(&present);
975 int32_t sysfsPresent = ReadPresentSysfs();
976 BATTERY_HILOGI(LABEL_TEST, "Not Mock HdiService008::present=%{public}d, p=%{public}d", present, sysfsPresent);
977 ASSERT_TRUE(present == sysfsPresent);
978 } else {
979 CreateFile(MOCK_BATTERY_PATH + "battery/present", "1");
980 giver_->ParsePresent(&present);
981 BATTERY_HILOGI(LABEL_TEST, "HdiService008::present=%{public}d.", present);
982 ASSERT_TRUE(present == 1);
983 }
984 }
985
986 /**
987 * @tc.name: HdiService009
988 * @tc.desc: Test functions to get value of technology
989 * @tc.type: FUNC
990 */
HWTEST_F(HdiServiceTest, HdiService009, TestSize.Level1)991 HWTEST_F(HdiServiceTest, HdiService009, TestSize.Level1)
992 {
993 std::string technology = "invalid";
994 if (IsNotMock()) {
995 giver_->ParseTechnology(technology);
996 std::string sysfsTechnology = "";
997 ReadTechnologySysfs(sysfsTechnology);
998 BATTERY_HILOGI(LABEL_TEST, "HdiService009::technology=%{public}s, ty=%{public}s", technology.c_str(),
999 sysfsTechnology.c_str());
1000 ASSERT_TRUE(technology == sysfsTechnology);
1001 } else {
1002 CreateFile(MOCK_BATTERY_PATH + "ohos-fgu/technology", "Li");
1003 giver_->ParseTechnology(technology);
1004 BATTERY_HILOGI(LABEL_TEST, "HdiService009::technology=%{public}s.", technology.c_str());
1005 ASSERT_TRUE(technology == "Li");
1006 }
1007 }
1008
1009 /**
1010 * @tc.name: HdiService010
1011 * @tc.desc: Test functions to get fd of socket
1012 * @tc.type: FUNC
1013 */
HWTEST_F(HdiServiceTest, HdiService010, TestSize.Level1)1014 HWTEST_F(HdiServiceTest, HdiService010, TestSize.Level1)
1015 {
1016 using namespace OHOS::HDI::Battery::V2_0;
1017
1018 BatteryThread bt;
1019 auto fd = OpenUeventSocketTest(bt);
1020 BATTERY_HILOGI(LABEL_TEST, "HdiService010::fd=%{public}d.", fd);
1021
1022 ASSERT_TRUE(fd > 0);
1023 close(fd);
1024 }
1025
1026 /**
1027 * @tc.name: HdiService011
1028 * @tc.desc: Test functions UpdateEpollInterval when charge-online
1029 * @tc.type: FUNC
1030 */
HWTEST_F(HdiServiceTest, HdiService011, TestSize.Level1)1031 HWTEST_F(HdiServiceTest, HdiService011, TestSize.Level1)
1032 {
1033 const int32_t CHARGE_STATE_ENABLE = 1;
1034 BatteryThread bt;
1035
1036 UpdateEpollIntervalTest(CHARGE_STATE_ENABLE, bt);
1037 auto epollInterval = GetEpollIntervalTest(bt);
1038 BATTERY_HILOGI(LABEL_TEST, "HdiService011::epollInterval=%{public}d.", epollInterval);
1039
1040 ASSERT_TRUE(epollInterval == 2000);
1041 }
1042
1043 /**
1044 * @tc.name: HdiService012
1045 * @tc.desc: Test functions UpdateEpollInterval when charge-offline
1046 * @tc.type: FUNC
1047 */
HWTEST_F(HdiServiceTest, HdiService012, TestSize.Level1)1048 HWTEST_F(HdiServiceTest, HdiService012, TestSize.Level1)
1049 {
1050 const int32_t CHARGE_STATE_NONE = 0;
1051 BatteryThread bt;
1052
1053 UpdateEpollIntervalTest(CHARGE_STATE_NONE, bt);
1054 auto epollInterval = GetEpollIntervalTest(bt);
1055 BATTERY_HILOGI(LABEL_TEST, "HdiService012::epollInterval=%{public}d.", epollInterval);
1056
1057 ASSERT_TRUE(epollInterval == -1);
1058 }
1059
1060 /**
1061 * @tc.name: HdiService013
1062 * @tc.desc: Test functions Init
1063 * @tc.type: FUNC
1064 */
HWTEST_F(HdiServiceTest, HdiService013, TestSize.Level1)1065 HWTEST_F(HdiServiceTest, HdiService013, TestSize.Level1)
1066 {
1067 void* service = nullptr;
1068 BatteryThread bt;
1069
1070 InitTest(service, bt);
1071 BATTERY_HILOGI(LABEL_TEST, "HdiService013::InitTest success");
1072 auto epollFd = GetEpollFdTest(bt);
1073 BATTERY_HILOGI(LABEL_TEST, "HdiService013::epollFd=%{public}d", epollFd);
1074
1075 ASSERT_TRUE(epollFd > 0);
1076 }
1077
1078 /**
1079 * @tc.name: HdiService023
1080 * @tc.desc: Test functions of ParseTotalEnergy
1081 * @tc.type: FUNC
1082 */
HWTEST_F(HdiServiceTest, HdiService023, TestSize.Level1)1083 HWTEST_F(HdiServiceTest, HdiService023, TestSize.Level1)
1084 {
1085 int32_t totalEnergy = 0;
1086 if (IsNotMock()) {
1087 giver_->ParseTotalEnergy(&totalEnergy);
1088 int32_t sysfsTotalEnergy = ReadTotalEnergySysfs();
1089 BATTERY_HILOGI(
1090 LABEL_TEST, "Not Mock HdiService023::totalEnergy=%{public}d, t=%{public}d", totalEnergy, sysfsTotalEnergy);
1091 ASSERT_TRUE(totalEnergy == sysfsTotalEnergy);
1092 } else {
1093 CreateFile(MOCK_BATTERY_PATH + "battery/charge_full", "4000000");
1094 giver_->ParseTotalEnergy(&totalEnergy);
1095 BATTERY_HILOGI(LABEL_TEST, "HdiService023::totalEnergy=%{public}d.", totalEnergy);
1096 ASSERT_TRUE(totalEnergy == 4000000);
1097 }
1098 }
1099
1100 /**
1101 * @tc.name: HdiService024
1102 * @tc.desc: Test functions of ParseCurrentAverage
1103 * @tc.type: FUNC
1104 */
HWTEST_F(HdiServiceTest, HdiService024, TestSize.Level1)1105 HWTEST_F(HdiServiceTest, HdiService024, TestSize.Level1)
1106 {
1107 int32_t currentAvg = HDF_FAILURE;
1108 if (IsNotMock()) {
1109 giver_->ParseCurrentAverage(¤tAvg);
1110 int32_t sysfsCurrentAvg = ReadCurrentAverageSysfs();
1111 BATTERY_HILOGI(
1112 LABEL_TEST, "Not Mock HdiService024::currentAvg=%{public}d, t=%{public}d", currentAvg, sysfsCurrentAvg);
1113 ASSERT_TRUE(currentAvg == sysfsCurrentAvg);
1114 } else {
1115 CreateFile(MOCK_BATTERY_PATH + "battery/current_avg", "1000");
1116 giver_->ParseCurrentAverage(¤tAvg);
1117 BATTERY_HILOGI(LABEL_TEST, "HdiService024::currentAvg=%{public}d.", currentAvg);
1118 ASSERT_TRUE(currentAvg == 1000);
1119 }
1120 }
1121
1122 /**
1123 * @tc.name: HdiService025
1124 * @tc.desc: Test functions of ParseCurrentNow
1125 * @tc.type: FUNC
1126 */
HWTEST_F(HdiServiceTest, HdiService025, TestSize.Level1)1127 HWTEST_F(HdiServiceTest, HdiService025, TestSize.Level1)
1128 {
1129 int32_t currentNow = 0;
1130 if (IsNotMock()) {
1131 giver_->ParseCurrentNow(¤tNow);
1132 int32_t sysfsCurrentNow = ReadCurrentNowSysfs();
1133 BATTERY_HILOGI(
1134 LABEL_TEST, "Not Mock HdiService025::currentNow=%{public}d, t=%{public}d", currentNow, sysfsCurrentNow);
1135 ASSERT_TRUE(currentNow == sysfsCurrentNow);
1136 } else {
1137 CreateFile(MOCK_BATTERY_PATH + "battery/current_now", "1000");
1138 giver_->ParseCurrentNow(¤tNow);
1139 BATTERY_HILOGI(LABEL_TEST, "HdiService025::currentNow=%{public}d.", currentNow);
1140 ASSERT_TRUE(currentNow == 1000);
1141 }
1142 }
1143
1144 /**
1145 * @tc.name: HdiService026
1146 * @tc.desc: Test functions of ParseChargeNow
1147 * @tc.type: FUNC
1148 */
HWTEST_F(HdiServiceTest, HdiService026, TestSize.Level1)1149 HWTEST_F(HdiServiceTest, HdiService026, TestSize.Level1)
1150 {
1151 int32_t chargeNow = 0;
1152 if (IsNotMock()) {
1153 giver_->ParseRemainEnergy(&chargeNow);
1154 int32_t sysfsChargeNow = ReadRemainEnergySysfs();
1155 BATTERY_HILOGI(
1156 LABEL_TEST, "Not Mock HdiService026::chargeNow=%{public}d, t=%{public}d", chargeNow, sysfsChargeNow);
1157 ASSERT_TRUE(chargeNow == sysfsChargeNow);
1158 } else {
1159 CreateFile(MOCK_BATTERY_PATH + "battery/charge_now", "1000");
1160 giver_->ParseRemainEnergy(&chargeNow);
1161 BATTERY_HILOGI(LABEL_TEST, "HdiService026::chargeNow=%{public}d.", chargeNow);
1162 ASSERT_TRUE(chargeNow == 1000);
1163 }
1164 }
1165 } // namespace HdiServiceTest
1166