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 <cstring>
17 #include <gtest/gtest.h>
18
19 #define private public
20 #include "battery_mgr_client_adapter_impl.h"
21 #undef private
22
23 #include "battery_srv_client.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace EventFwk;
29 using namespace OHOS::EventFwk;
30 using namespace PowerMgr;
31
32 namespace OHOS {
33 namespace {
34 bool g_subscribeCommonEventRet = true;
35 bool g_unSubscribeCommonEventRet = true;
36 BatteryPluggedType g_pluggedType = BatteryPluggedType::PLUGGED_TYPE_NONE;
37 } // namespace
38
39 namespace EventFwk {
SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)40 bool CommonEventManager::SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber)
41 {
42 return g_subscribeCommonEventRet;
43 }
44
UnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)45 bool CommonEventManager::UnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber)
46 {
47 return g_unSubscribeCommonEventRet;
48 }
49 }
50 namespace PowerMgr {
GetPluggedType()51 BatteryPluggedType BatterySrvClient::GetPluggedType()
52 {
53 return g_pluggedType;
54 }
55 }
56 namespace NWeb {
57 namespace {
58 std::shared_ptr<NWeb::NWebBatteryEventSubscriber> g_batter;
59 std::shared_ptr<NWeb::BatteryMgrClientAdapterImpl> g_batterImpl;
60 } // namespace
61
62 class MockWebBatteryEventCallback : public WebBatteryEventCallback {
63 public:
64 MockWebBatteryEventCallback() = default;
BatteryInfoChanged(std::shared_ptr<WebBatteryInfo>)65 void BatteryInfoChanged(std::shared_ptr<WebBatteryInfo>) {};
66 };
67
68 class BatteryMgrAdapterTest : public testing::Test {
69 public:
70 static void SetUpTestCase(void);
71 static void TearDownTestCase(void);
72 void SetUp();
73 void TearDown();
74 };
75
SetUpTestCase(void)76 void BatteryMgrAdapterTest::SetUpTestCase(void)
77 {
78 bool result = true;
79 CommonEventSubscribeInfo subscribe;
80
81 std::shared_ptr<WebBatteryEventCallback> eventCallback = std::make_shared<MockWebBatteryEventCallback>();
82 g_batter = std::make_shared<NWebBatteryEventSubscriber>(subscribe, eventCallback);
83 if (g_batter == nullptr) {
84 result = false;
85 }
86 EXPECT_TRUE(result);
87 g_batterImpl = std::make_shared<BatteryMgrClientAdapterImpl>();
88 if (g_batter == nullptr) {
89 result = false;
90 }
91 EXPECT_TRUE(result);
92 }
93
TearDownTestCase(void)94 void BatteryMgrAdapterTest::TearDownTestCase(void)
95 {}
96
SetUp(void)97 void BatteryMgrAdapterTest::SetUp(void)
98 {}
99
TearDown(void)100 void BatteryMgrAdapterTest::TearDown(void)
101 {}
102
103 /**
104 * @tc.name: BatteryAdapter_OnReceiveEvent_001.
105 * @tc.desc: Test the OnReceiveEvent.
106 * @tc.type: FUNC
107 * @tc.require:
108 */
109 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_OnReceiveEvent_001, TestSize.Level1)
110 {
111 Want want;
112 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
113 CommonEventData data(want);
114 std::string key = PowerMgr::BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE;
115 EXPECT_NE(g_batter, nullptr);
116 g_batter->OnReceiveEvent(data);
117
118 Want want1;
119 want1.SetParam(key, static_cast<int>(BatteryPluggedType::PLUGGED_TYPE_NONE));
120 CommonEventData data1(want1);
121 g_batter->OnReceiveEvent(data1);
122 Want wantEvent;
123 wantEvent.SetParam(key, static_cast<int>(BatteryPluggedType::PLUGGED_TYPE_BUTT));
124 CommonEventData dataEvent(wantEvent);
125 g_batter->OnReceiveEvent(dataEvent);
126 }
127
128 /**
129 * @tc.name: BatteryAdapter_RegBatteryEvent_002.
130 * @tc.desc: Test the RegBatteryEvent.
131 * @tc.type: FUNC
132 * @tc.require:
133 */
134 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_RegBatteryEvent_002, TestSize.Level1)
135 {
136 std::shared_ptr<WebBatteryEventCallback> eventCallback = std::make_shared<MockWebBatteryEventCallback>();
137 EXPECT_NE(g_batterImpl, nullptr);
138 g_batterImpl->RegBatteryEvent(std::move(eventCallback));
139 g_batterImpl->RegBatteryEvent(nullptr);
140 }
141
142 /**
143 * @tc.name: BatteryAdapter_StartListen_003.
144 * @tc.desc: Test the StartListen.
145 * @tc.type: FUNC
146 * @tc.require:
147 */
148 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_StartListen_003, TestSize.Level1)
149 {
150 bool result = g_batterImpl->StartListen();
151 EXPECT_TRUE(result);
152 g_subscribeCommonEventRet = false;
153 result = g_batterImpl->StartListen();
154 EXPECT_FALSE(result);
155 g_subscribeCommonEventRet = true;
156 }
157
158 /**
159 * @tc.name: BatteryAdapter_StopListen_004.
160 * @tc.desc: Test the StopListen.
161 * @tc.type: FUNC
162 * @tc.require:
163 */
164 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_StopListen_004, TestSize.Level1)
165 {
166 g_unSubscribeCommonEventRet = false;
167 EXPECT_NE(g_batterImpl, nullptr);
168 g_batterImpl->StopListen();
169 g_unSubscribeCommonEventRet = true;
170 g_batterImpl->StopListen();
171 }
172
173 /**
174 * @tc.name: BatteryAdapter_RequestBatteryInfo_005.
175 * @tc.desc: Test the RequestBatteryInfo.
176 * @tc.type: FUNC
177 * @tc.require:
178 */
179 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_RequestBatteryInfo_005, TestSize.Level1)
180 {
181 bool result = true;
182 g_pluggedType = BatteryPluggedType::PLUGGED_TYPE_AC;
183 std::shared_ptr<WebBatteryInfo> battery = g_batterImpl->RequestBatteryInfo();
184 if (battery == nullptr) {
185 result = false;
186 }
187 EXPECT_TRUE(result);
188 g_pluggedType = BatteryPluggedType::PLUGGED_TYPE_NONE;
189 battery = g_batterImpl->RequestBatteryInfo();
190 if (battery == nullptr) {
191 result = false;
192 }
193 EXPECT_TRUE(result);
194 g_pluggedType = BatteryPluggedType::PLUGGED_TYPE_BUTT;
195 battery = g_batterImpl->RequestBatteryInfo();
196 if (battery == nullptr) {
197 result = false;
198 }
199 EXPECT_TRUE(result);
200 }
201
202 /**
203 * @tc.name: BatteryAdapter_WebBatteryInfoImpl_006.
204 * @tc.desc: Test the WebBatteryInfoImpl.
205 * @tc.type: FUNC
206 * @tc.require:
207 */
208 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_WebBatteryInfoImpl_006, TestSize.Level1)
209 {
210 double level = 1;
211 bool isCharging = true;
212 int disChargingTime = 1;
213 int chargingTime = 1;
214 WebBatteryInfoImpl info(level, isCharging, disChargingTime, chargingTime);
215 EXPECT_FLOAT_EQ(info.GetLevel(), level);
216 EXPECT_TRUE(info.IsCharging());
217 EXPECT_EQ(info.DisChargingTime(), disChargingTime);
218 EXPECT_EQ(info.ChargingTime(), chargingTime);
219 Want want;
220 want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
221 CommonEventData data(want);
222 g_batter->OnReceiveEvent(data);
223 g_batterImpl->commonEventSubscriber_ = nullptr;
224 g_batterImpl->StopListen();
225 }
226 }
227 }
228