• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef BATTERY_SUBSCRIBER_TEST_H
17 #define BATTERY_SUBSCRIBER_TEST_H
18 
19 #include <gtest/gtest.h>
20 #include <ipc_skeleton.h>
21 
22 #include "battery_service.h"
23 #include "battery_log.h"
24 #include "iservice_registry.h"
25 #include "if_system_ability_manager.h"
26 #include "system_ability_definition.h"
27 #include "ipc_skeleton.h"
28 #include "string_ex.h"
29 #include "sys_param.h"
30 
31 namespace OHOS {
32 namespace PowerMgr {
33 class BatterySubscriberTest : public testing::Test {
34 };
35 
36 const std::string KEY_CAPACITY = ToString(BatteryInfo::COMMON_EVENT_CODE_CAPACITY);
37 const std::string KEY_VOLTAGE = ToString(BatteryInfo::COMMON_EVENT_CODE_VOLTAGE);
38 const std::string KEY_TEMPERATURE = ToString(BatteryInfo::COMMON_EVENT_CODE_TEMPERATURE);
39 const std::string KEY_HEALTH_STATE = ToString(BatteryInfo::COMMON_EVENT_CODE_HEALTH_STATE);
40 const std::string KEY_PLUGGED_TYPE = ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_TYPE);
41 const std::string KEY_PLUGGED_MAX_CURRENT = ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_MAX_CURRENT);
42 const std::string KEY_PLUGGED_MAX_VOLTAGE = ToString(BatteryInfo::COMMON_EVENT_CODE_PLUGGED_MAX_VOLTAGE);
43 const std::string KEY_CHARGE_STATE = ToString(BatteryInfo::COMMON_EVENT_CODE_CHARGE_STATE);
44 const std::string KEY_CHARGE_COUNTER = ToString(BatteryInfo::COMMON_EVENT_CODE_CHARGE_COUNTER);
45 const std::string KEY_PRESENT = ToString(BatteryInfo::COMMON_EVENT_CODE_PRESENT);
46 const std::string KEY_TECHNOLOGY = ToString(BatteryInfo::COMMON_EVENT_CODE_TECHNOLOGY);
47 
48 class SubscriberTest : public EventFwk::CommonEventSubscriber {
49 public:
SubscriberTest(const EventFwk::CommonEventSubscribeInfo & sp)50     explicit SubscriberTest(const EventFwk::CommonEventSubscribeInfo& sp) : EventFwk::CommonEventSubscriber(sp)
51     {}
52 
OnReceiveEvent(const EventFwk::CommonEventData & data)53     void OnReceiveEvent(const EventFwk::CommonEventData& data)
54     {
55         BATTERY_HILOGD(LABEL_TEST, "OnReceiveEvent enter.");
56         std::string action = data.GetWant().GetAction();
57         BATTERY_HILOGD(LABEL_TEST, "BatteryService=== start. action=%{public}s", action.c_str());
58         if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED) {
59             int defaultCapacity = -1;
60             int capacity = data.GetWant().GetIntParam(KEY_CAPACITY, defaultCapacity);
61             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest capacity = %{public}d", capacity);
62 
63             int defaultVoltage = -1;
64             int voltage = data.GetWant().GetIntParam(KEY_VOLTAGE, defaultVoltage);
65             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest voltage = %{public}d", voltage);
66 
67             int defaultTemperature = -1;
68             int temperature = data.GetWant().GetIntParam(KEY_TEMPERATURE, defaultTemperature);
69             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest temperature = %{public}d", temperature);
70 
71             int defaultHealthState = -1;
72             int healthState = data.GetWant().GetIntParam(KEY_HEALTH_STATE, defaultHealthState);
73             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest healthState = %{public}d", healthState);
74 
75             int defaultPluggedType = -1;
76             int pluggedType = data.GetWant().GetIntParam(KEY_PLUGGED_TYPE, defaultPluggedType);
77             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest pluggedType = %{public}d", pluggedType);
78 
79             int defaultMaxCurrent = -1;
80             int maxCurrent = data.GetWant().GetIntParam(KEY_PLUGGED_MAX_CURRENT, defaultMaxCurrent);
81             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest maxCurrent = %{public}d", maxCurrent);
82 
83             int defaultMaxVoltage = -1;
84             int maxVoltage = data.GetWant().GetIntParam(KEY_PLUGGED_MAX_VOLTAGE, defaultMaxVoltage);
85             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest maxVoltage = %{public}d", maxVoltage);
86 
87             int defaultChargeState = -1;
88             int chargeState = data.GetWant().GetIntParam(KEY_CHARGE_STATE, defaultChargeState);
89             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest chargeState = %{public}d", chargeState);
90 
91             int defaultChargeCounter = -1;
92             int chargeCounter = data.GetWant().GetIntParam(KEY_CHARGE_COUNTER, defaultChargeCounter);
93             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest chargeCounter = %{public}d", chargeCounter);
94 
95             bool defaultPresent = false;
96             bool isPresent = data.GetWant().GetBoolParam(KEY_PRESENT, defaultPresent);
97             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest isPresent = %{public}d", isPresent);
98 
99             std::string defaultTechnology = "";
100             std::string technology = data.GetWant().GetStringParam(KEY_TECHNOLOGY);
101             BATTERY_HILOGD(LABEL_TEST, "SubscriberTest technology = %{public}s", technology.c_str());
102         }
103     }
104 };
105 } // namespace PowerMgr
106 } // namespace OHOS
107 #endif // BATTERY_SUBSCRIBER_TEST_H
108