1 /*
2 * Copyright (c) 2024 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include "battersrvclient_mock.h"
19 #include "battery_status.h"
20 #include "utils_log.h"
21
22 namespace OHOS::FileManagement::CloudSync::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26 using ChargeState = PowerMgr::BatteryChargeState;
27 using PluggedType = PowerMgr::BatteryPluggedType;
28 constexpr int32_t STOP_CAPACITY_LIMIT = 10;
29 constexpr int32_t PAUSE_CAPACITY_LIMIT = 15;
30
31 class BatteryStatusMock final : public BatteryStatus {
32 public:
BatteryStatusMock()33 BatteryStatusMock() : BatteryStatus() {}
34 MOCK_METHOD0(GetCapacity, int32_t());
35 };
36
37 class BatteryStatusTest : public testing::Test {
38 public:
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43 shared_ptr<BatteryStatusMock> batteryStatus_ = nullptr;
44 static inline shared_ptr<BatterySrvClientMock> dfsBatterySrvClient_ = nullptr;
45 };
46
SetUpTestCase(void)47 void BatteryStatusTest::SetUpTestCase(void)
48 {
49 dfsBatterySrvClient_ = make_shared<BatterySrvClientMock>();
50 BatterySrvClientMock::dfsBatterySrvClient = dfsBatterySrvClient_;
51 GTEST_LOG_(INFO) << "SetUpTestCase";
52 }
53
TearDownTestCase(void)54 void BatteryStatusTest::TearDownTestCase(void)
55 {
56 BatterySrvClientMock::dfsBatterySrvClient = nullptr;
57 dfsBatterySrvClient_ = nullptr;
58 GTEST_LOG_(INFO) << "TearDownTestCase";
59 }
60
SetUp(void)61 void BatteryStatusTest::SetUp(void)
62 {
63 GTEST_LOG_(INFO) << "SetUp";
64 batteryStatus_ = make_shared<BatteryStatusMock>();
65 }
66
TearDown(void)67 void BatteryStatusTest::TearDown(void)
68 {
69 GTEST_LOG_(INFO) << "TearDown";
70 batteryStatus_ = nullptr;
71 }
72
73 /**
74 * @tc.name: IsAllowUploadTest001
75 * @tc.desc: Verify the IsAllowUpload function
76 * @tc.type: FUNC
77 * @tc.require: I6JPKG
78 */
79 HWTEST_F(BatteryStatusTest, IsAllowUploadTest001, TestSize.Level1)
80 {
81 GTEST_LOG_(INFO) << "IsAllowUploadTest001 Start";
82 try {
83 batteryStatus_->SetChargingStatus(true);
84 bool ret = batteryStatus_->IsAllowUpload(true);
85 EXPECT_EQ(ret, true);
86 } catch (...) {
87 EXPECT_TRUE(false);
88 GTEST_LOG_(INFO) << "IsAllowUploadTest001 FAILED";
89 }
90 GTEST_LOG_(INFO) << "IsAllowUploadTest001 End";
91 }
92
93 /**
94 * @tc.name: IsAllowUploadTest002
95 * @tc.desc: Verify the IsAllowUpload function
96 * @tc.type: FUNC
97 * @tc.require: I6JPKG
98 */
99 HWTEST_F(BatteryStatusTest, IsAllowUploadTest002, TestSize.Level1)
100 {
101 GTEST_LOG_(INFO) << "IsAllowUploadTest002 Start";
102 try {
103 batteryStatus_->SetChargingStatus(false);
104 bool ret = batteryStatus_->IsAllowUpload(true);
105 EXPECT_EQ(ret, false);
106 } catch (...) {
107 EXPECT_TRUE(false);
108 GTEST_LOG_(INFO) << "IsAllowUploadTest002 FAILED";
109 }
110 GTEST_LOG_(INFO) << "IsAllowUploadTest002 End";
111 }
112
113 /**
114 * @tc.name: IsAllowUploadTest003
115 * @tc.desc: Verify the IsAllowUpload function
116 * @tc.type: FUNC
117 * @tc.require: I6JPKG
118 */
119 HWTEST_F(BatteryStatusTest, IsAllowUploadTest003, TestSize.Level1)
120 {
121 GTEST_LOG_(INFO) << "IsAllowUploadTest003 Start";
122 EXPECT_CALL(*dfsBatterySrvClient_, GetCapacity()).WillOnce(Return(STOP_CAPACITY_LIMIT - 1));
123 batteryStatus_->SetChargingStatus(false);
124 bool ret = batteryStatus_->IsAllowUpload(false);
125 EXPECT_FALSE(ret);
126 GTEST_LOG_(INFO) << "IsAllowUploadTest003 End";
127 }
128
129 /**
130 * @tc.name: IsAllowUploadTest004
131 * @tc.desc: Verify the IsAllowUpload function
132 * @tc.type: FUNC
133 * @tc.require: I6JPKG
134 */
135 HWTEST_F(BatteryStatusTest, IsAllowUploadTest004, TestSize.Level1)
136 {
137 GTEST_LOG_(INFO) << "IsAllowUploadTest004 Start";
138 EXPECT_CALL(*dfsBatterySrvClient_, GetCapacity()).WillOnce(Return(PAUSE_CAPACITY_LIMIT - 1));
139 batteryStatus_->SetChargingStatus(false);
140 bool ret = batteryStatus_->IsAllowUpload(false);
141 EXPECT_FALSE(ret);
142 GTEST_LOG_(INFO) << "IsAllowUploadTest004 End";
143 }
144
145 /**
146 * @tc.name: IsAllowUploadTest005
147 * @tc.desc: Verify the IsAllowUpload function
148 * @tc.type: FUNC
149 * @tc.require: I6JPKG
150 */
151 HWTEST_F(BatteryStatusTest, IsAllowUploadTest005, TestSize.Level1)
152 {
153 GTEST_LOG_(INFO) << "IsAllowUploadTest005 Start";
154 EXPECT_CALL(*dfsBatterySrvClient_, GetCapacity()).WillOnce(Return(PAUSE_CAPACITY_LIMIT - 1));
155 batteryStatus_->SetChargingStatus(false);
156 bool ret = batteryStatus_->IsAllowUpload(true);
157 EXPECT_TRUE(ret);
158 GTEST_LOG_(INFO) << "IsAllowUploadTest005 End";
159 }
160
161 /**
162 * @tc.name: IsAllowUploadTest006
163 * @tc.desc: Verify the IsAllowUpload function
164 * @tc.type: FUNC
165 * @tc.require: I6JPKG
166 */
167 HWTEST_F(BatteryStatusTest, IsAllowUploadTest006, TestSize.Level1)
168 {
169 GTEST_LOG_(INFO) << "IsAllowUploadTest006 Start";
170 EXPECT_CALL(*dfsBatterySrvClient_, GetCapacity()).WillOnce(Return(PAUSE_CAPACITY_LIMIT + 1));
171 batteryStatus_->SetChargingStatus(false);
172 bool ret = batteryStatus_->IsAllowUpload(true);
173 EXPECT_TRUE(ret);
174 GTEST_LOG_(INFO) << "IsAllowUploadTest006 End";
175 }
176
177 /**
178 * @tc.name: IsBatteryCapcityOkayTest001
179 * @tc.desc: Verify the IsBatteryCapcityOkay function
180 * @tc.type: FUNC
181 * @tc.require: I6JPKG
182 */
183 HWTEST_F(BatteryStatusTest, IsBatteryCapcityOkayTest001, TestSize.Level1)
184 {
185 GTEST_LOG_(INFO) << "IsBatteryCapcityOkayTest001 Start";
186 try {
187 batteryStatus_->SetChargingStatus(true);
188 bool ret = batteryStatus_->IsBatteryCapcityOkay();
189 EXPECT_EQ(ret, true);
190 } catch (...) {
191 EXPECT_TRUE(false);
192 GTEST_LOG_(INFO) << "IsBatteryCapcityOkayTest001 FAILED";
193 }
194 GTEST_LOG_(INFO) << "IsBatteryCapcityOkayTest001 End";
195 }
196
197 /**
198 * @tc.name: IsBatteryCapcityOkayTest002
199 * @tc.desc: Verify the IsBatteryCapcityOkay function
200 * @tc.type: FUNC
201 * @tc.require: I6JPKG
202 */
203 HWTEST_F(BatteryStatusTest, IsBatteryCapcityOkayTest002, TestSize.Level1)
204 {
205 GTEST_LOG_(INFO) << "IsBatteryCapcityOkayTest Start";
206 try {
207 EXPECT_CALL(*dfsBatterySrvClient_, GetCapacity()).WillOnce(Return(STOP_CAPACITY_LIMIT + 1));
208 batteryStatus_->SetChargingStatus(false);
209 bool ret = batteryStatus_->IsBatteryCapcityOkay();
210 EXPECT_EQ(ret, true);
211 } catch (...) {
212 EXPECT_TRUE(false);
213 GTEST_LOG_(INFO) << "IsBatteryCapcityOkayTest002 FAILED";
214 }
215 GTEST_LOG_(INFO) << "IsBatteryCapcityOkayTest002 End";
216 }
217
218 /**
219 * @tc.name: IsBatteryCapcityOkayTest003
220 * @tc.desc: Verify the IsBatteryCapcityOkay function
221 * @tc.type: FUNC
222 * @tc.require: I6JPKG
223 */
224 HWTEST_F(BatteryStatusTest, IsBatteryCapcityOkayTest003, TestSize.Level1)
225 {
226 GTEST_LOG_(INFO) << "IsBatteryCapcityOkayTest003 Start";
227 try {
228 EXPECT_CALL(*dfsBatterySrvClient_, GetCapacity()).WillOnce(Return(STOP_CAPACITY_LIMIT - 1));
229 batteryStatus_->SetChargingStatus(false);
230 bool ret = batteryStatus_->IsBatteryCapcityOkay();
231 EXPECT_EQ(ret, false);
232 } catch (...) {
233 EXPECT_TRUE(false);
234 GTEST_LOG_(INFO) << "IsBatteryCapcityOkayTest003 FAILED";
235 }
236 GTEST_LOG_(INFO) << "IsBatteryCapcityOkayTest003 End";
237 }
238
239 HWTEST_F(BatteryStatusTest, GetInitChargingStatus_Plugged_BUTT, TestSize.Level1)
240 {
241 EXPECT_CALL(*dfsBatterySrvClient_, GetPluggedType())
242 .WillOnce(Return(PluggedType::PLUGGED_TYPE_BUTT));
243
244 batteryStatus_->GetInitChargingStatus();
245 EXPECT_FALSE(batteryStatus_->IsCharging());
246 }
247
248 HWTEST_F(BatteryStatusTest, GetInitChargingStatus_Plugged_AC, TestSize.Level1)
249 {
250 EXPECT_CALL(*dfsBatterySrvClient_, GetPluggedType())
251 .WillOnce(Return(PluggedType::PLUGGED_TYPE_AC));
252
253 batteryStatus_->GetInitChargingStatus();
254 EXPECT_TRUE(batteryStatus_->IsCharging());
255 }
256
257 HWTEST_F(BatteryStatusTest, GetInitChargingStatus_Plugged_USB, TestSize.Level1)
258 {
259 EXPECT_CALL(*dfsBatterySrvClient_, GetPluggedType())
260 .WillOnce(Return(PluggedType::PLUGGED_TYPE_USB));
261
262 batteryStatus_->GetInitChargingStatus();
263 EXPECT_TRUE(batteryStatus_->IsCharging());
264 }
265
266 HWTEST_F(BatteryStatusTest, GetInitChargingStatus_NotCharging_NotPlugged, TestSize.Level1)
267 {
268 EXPECT_CALL(*dfsBatterySrvClient_, GetPluggedType())
269 .WillOnce(Return(PluggedType::PLUGGED_TYPE_NONE));
270
271 batteryStatus_->GetInitChargingStatus();
272 EXPECT_FALSE(batteryStatus_->IsCharging());
273 }
274 } // namespace OHOS::FileManagement::CloudSync::Test