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 "battery_srv_proxy_mock_test.h"
17 #include "battery_srv_proxy.h"
18 #include "ipc_object_stub.h"
19 #include "mock_remote_object.h"
20 #include <hisysevent.h>
21
22 using namespace testing::ext;
23 using namespace OHOS::HiviewDFX;
24 using namespace OHOS::PowerMgr;
25 using namespace OHOS;
26 using namespace std;
27
28 namespace {
29 std::shared_ptr<BatterySrvProxy> g_proxy;
30 sptr<IRemoteObject> remoteObj;
31 }
32
SetUpTestCase()33 void BatteryProxyMockTest::SetUpTestCase()
34 {
35 #ifdef ENABLE_REMOTE_INTERFACE
36 remoteObj = new MockRemoteObject();
37 #else
38 remoteObj = new IPCObjectStub();
39 #endif
40 g_proxy = std::make_shared<BatterySrvProxy>(remoteObj);
41 }
42
TearDownTestCase()43 void BatteryProxyMockTest::TearDownTestCase()
44 {
45 remoteObj = nullptr;
46 g_proxy = nullptr;
47 }
48
SetUp()49 void BatteryProxyMockTest::SetUp() {}
50
TearDown()51 void BatteryProxyMockTest::TearDown() {}
52
53 namespace {
54 /**
55 * @tc.name: BatteryProxyMockTest_001
56 * @tc.desc: test BatterySrvProxy::GetCapacity() when an exception is raised
57 * @tc.type: FUNC
58 * @tc.require: issueI5X13X
59 */
60 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_001, TestSize.Level0)
61 {
62 int32_t capacity = g_proxy->GetCapacity();
63 EXPECT_TRUE(capacity == INVALID_BATT_INT_VALUE);
64 }
65
66 /**
67 * @tc.name: BatteryProxyMockTest_002
68 * @tc.desc: test BatterySrvProxy::GetChargingStatus() when an exception is raised
69 * @tc.type: FUNC
70 * @tc.require: issueI5X13X
71 */
72 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_002, TestSize.Level0)
73 {
74 BatteryChargeState chargeState = g_proxy->GetChargingStatus();
75 EXPECT_TRUE(chargeState >= BatteryChargeState::CHARGE_STATE_NONE &&
76 chargeState <= BatteryChargeState::CHARGE_STATE_BUTT); // the enum value range of BatteryChargeState
77 }
78
79 /**
80 * @tc.name: BatteryProxyMockTest_003
81 * @tc.desc: test BatterySrvProxy::GetHealthStatus() when an exception is raised
82 * @tc.type: FUNC
83 * @tc.require: issueI5X13X
84 */
85 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_003, TestSize.Level0)
86 {
87 BatteryHealthState healthState = g_proxy->GetHealthStatus();
88 EXPECT_TRUE(healthState >= BatteryHealthState::HEALTH_STATE_UNKNOWN &&
89 healthState <= BatteryHealthState::HEALTH_STATE_BUTT); // the enum value range of BatteryHealthState
90 }
91
92 /**
93 * @tc.name: BatteryProxyMockTest_004
94 * @tc.desc: test BatterySrvProxy::GetPluggedType() when an exception is raised
95 * @tc.type: FUNC
96 * @tc.require: issueI5X13X
97 */
98 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_004, TestSize.Level0)
99 {
100 BatteryPluggedType pluggedType = g_proxy->GetPluggedType();
101 EXPECT_TRUE(pluggedType >= BatteryPluggedType::PLUGGED_TYPE_NONE &&
102 pluggedType <= BatteryPluggedType::PLUGGED_TYPE_BUTT); // the enum value range of BatteryPluggedType
103 }
104
105 /**
106 * @tc.name: BatteryProxyMockTest_005
107 * @tc.desc: test BatterySrvProxy::GetPresent() when an exception is raised
108 * @tc.type: FUNC
109 * @tc.require: issueI5X13X
110 */
111 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_005, TestSize.Level0)
112 {
113 bool isPresent = g_proxy->GetPresent();
114 EXPECT_TRUE(isPresent == INVALID_BATT_BOOL_VALUE);
115 }
116
117 /**
118 * @tc.name: BatteryProxyMockTest_006
119 * @tc.desc: test BatterySrvProxy::GetTchnology() when an exception is raised
120 * @tc.type: FUNC
121 * @tc.require: issueI5X13X
122 */
123 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_006, TestSize.Level0)
124 {
125 std::string technology = g_proxy->GetTechnology();
126 EXPECT_TRUE(technology == INVALID_STRING_VALUE);
127 }
128
129 /**
130 * @tc.name: BatteryProxyMockTest_007
131 * @tc.desc: test BatterySrvProxy::GetTotalEnergy() when an exception is raised
132 * @tc.type: FUNC
133 * @tc.require: issueI5X13X
134 */
135 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_007, TestSize.Level0)
136 {
137 int32_t totalEnergy = g_proxy->GetTotalEnergy();
138 EXPECT_TRUE(totalEnergy == INVALID_BATT_INT_VALUE);
139 }
140
141 /**
142 * @tc.name: BatteryProxyMockTest_008
143 * @tc.desc: test BatterySrvProxy::GetCurrentAverage() when an exception is raised
144 * @tc.type: FUNC
145 * @tc.require: issueI5X13X
146 */
147 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_008, TestSize.Level0)
148 {
149 int32_t currentAverage = g_proxy->GetCurrentAverage();
150 EXPECT_TRUE(currentAverage == INVALID_BATT_INT_VALUE);
151 }
152
153 /**
154 * @tc.name: BatteryProxyMockTest_009
155 * @tc.desc: test BatterySrvProxy::GetNowCurrent() when an exception is raised
156 * @tc.type: FUNC
157 * @tc.require: issueI5X13X
158 */
159 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_009, TestSize.Level0)
160 {
161 int32_t nowCurrent = g_proxy->GetNowCurrent();
162 EXPECT_TRUE(nowCurrent == INVALID_BATT_INT_VALUE);
163 }
164
165 /**
166 * @tc.name: BatteryProxyMockTest_010
167 * @tc.desc: test BatterySrvProxy::GetRemainEnergy() when an exception is raised
168 * @tc.type: FUNC
169 * @tc.require: issueI5X13X
170 */
171 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_010, TestSize.Level0)
172 {
173 int32_t remainEnergy = g_proxy->GetRemainEnergy();
174 EXPECT_TRUE(remainEnergy == INVALID_BATT_INT_VALUE);
175 }
176
177 /**
178 * @tc.name: BatteryProxyMockTest_011
179 * @tc.desc: test BatterySrvProxy::GetBatteryTemperature() when an exception is raised
180 * @tc.type: FUNC
181 * @tc.require: issueI5X13X
182 */
183 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_011, TestSize.Level0)
184 {
185 int32_t temperature = g_proxy->GetBatteryTemperature();
186 EXPECT_TRUE(temperature == INVALID_BATT_TEMP_VALUE);
187 }
188
189 /**
190 * @tc.name: BatteryProxyMockTest_012
191 * @tc.desc: test BatterySrvProxy::GetCapacityLevel() when an exception is raised
192 * @tc.type: FUNC
193 * @tc.require: issueI5X13X
194 */
195 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_012, TestSize.Level0)
196 {
197 BatteryCapacityLevel batteryLevel = g_proxy->GetCapacityLevel();
198 EXPECT_TRUE(batteryLevel >= BatteryCapacityLevel::LEVEL_NONE &&
199 batteryLevel <= BatteryCapacityLevel::LEVEL_RESERVED); // the enum value range of BatteryCapacityLevel
200 }
201
202 /**
203 * @tc.name: BatteryProxyMockTest_013
204 * @tc.desc: test BatterySrvProxy::GetRemainingChargeTime() when an exception is raised
205 * @tc.type: FUNC
206 * @tc.require: issueI5X13X
207 */
208 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_013, TestSize.Level0)
209 {
210 int64_t remainChargeTime = g_proxy->GetRemainingChargeTime();
211 EXPECT_TRUE(remainChargeTime == INVALID_REMAINING_CHARGE_TIME_VALUE);
212 }
213
214 /**
215 * @tc.name: BatteryProxyMockTest_014
216 * @tc.desc: test BatterySrvProxy::GetVoltage() when an exception is raised
217 * @tc.type: FUNC
218 * @tc.require: issueI5X13X
219 */
220 HWTEST_F(BatteryProxyMockTest, BatteryProxyMockTest_014, TestSize.Level0)
221 {
222 int32_t voltage = g_proxy->GetVoltage();
223 EXPECT_TRUE(voltage == INVALID_BATT_INT_VALUE);
224 }
225 } // namespace
226