• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class BatteryMgrAdapterTest : public testing::Test {
62 public:
63     static void SetUpTestCase(void);
64     static void TearDownTestCase(void);
65     void SetUp();
66     void TearDown();
67 };
68 
SetUpTestCase(void)69 void BatteryMgrAdapterTest::SetUpTestCase(void)
70 {
71     bool result = true;
72     CommonEventSubscribeInfo subscribe;
73 
74     auto callback = [] (WebBatteryInfo& info) {};
75     BatteryEventCallback eventCallback = callback;
76     g_batter = std::make_shared<NWebBatteryEventSubscriber>(subscribe, eventCallback);
77     if (g_batter == nullptr) {
78         result = false;
79     }
80     EXPECT_TRUE(result);
81     g_batterImpl = std::make_shared<BatteryMgrClientAdapterImpl>();
82     if (g_batter == nullptr) {
83         result = false;
84     }
85     EXPECT_TRUE(result);
86 }
87 
TearDownTestCase(void)88 void BatteryMgrAdapterTest::TearDownTestCase(void)
89 {}
90 
SetUp(void)91 void BatteryMgrAdapterTest::SetUp(void)
92 {}
93 
TearDown(void)94 void BatteryMgrAdapterTest::TearDown(void)
95 {}
96 
97 /**
98  * @tc.name: BatteryAdapter_OnReceiveEvent_001.
99  * @tc.desc: Test the OnReceiveEvent.
100  * @tc.type: FUNC
101  * @tc.require:
102  */
103 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_OnReceiveEvent_001, TestSize.Level1)
104 {
105     Want want;
106     want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
107     CommonEventData data(want);
108     std::string key = PowerMgr::BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE;
109     g_batter->OnReceiveEvent(data);
110 
111     want.SetParam(key, static_cast<int>(BatteryPluggedType::PLUGGED_TYPE_NONE));
112     g_batter->OnReceiveEvent(data);
113     want.SetParam(key, static_cast<int>(BatteryPluggedType::PLUGGED_TYPE_BUTT));
114     g_batter->OnReceiveEvent(data);
115 }
116 
117 /**
118  * @tc.name: BatteryAdapter_RegBatteryEvent_002.
119  * @tc.desc: Test the RegBatteryEvent.
120  * @tc.type: FUNC
121  * @tc.require:
122  */
123 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_RegBatteryEvent_002, TestSize.Level1)
124 {
__anone40660560402(WebBatteryInfo& info) 125     auto cb = [] (WebBatteryInfo& info) {};
126     BatteryEventCallback eventCallback = cb;
127     g_batterImpl->RegBatteryEvent(std::move(eventCallback));
128 }
129 
130 /**
131  * @tc.name: BatteryAdapter_StartListen_003.
132  * @tc.desc: Test the StartListen.
133  * @tc.type: FUNC
134  * @tc.require:
135  */
136 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_StartListen_003, TestSize.Level1)
137 {
138     bool result = g_batterImpl->StartListen();
139     EXPECT_TRUE(result);
140     g_subscribeCommonEventRet = false;
141     result = g_batterImpl->StartListen();
142     EXPECT_FALSE(result);
143     g_subscribeCommonEventRet = true;
144 }
145 
146 /**
147  * @tc.name: BatteryAdapter_StopListen_004.
148  * @tc.desc: Test the StopListen.
149  * @tc.type: FUNC
150  * @tc.require:
151  */
152 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_StopListen_004, TestSize.Level1)
153 {
154     g_unSubscribeCommonEventRet = false;
155     g_batterImpl->StopListen();
156     g_unSubscribeCommonEventRet = true;
157     g_batterImpl->StopListen();
158 }
159 
160 /**
161  * @tc.name: BatteryAdapter_RequestBatteryInfo_005.
162  * @tc.desc: Test the RequestBatteryInfo.
163  * @tc.type: FUNC
164  * @tc.require:
165  */
166 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_RequestBatteryInfo_005, TestSize.Level1)
167 {
168     bool result = true;
169     g_pluggedType = BatteryPluggedType::PLUGGED_TYPE_AC;
170     std::unique_ptr<WebBatteryInfo> battery = g_batterImpl->RequestBatteryInfo();
171     if (battery == nullptr) {
172         result = false;
173     }
174     EXPECT_TRUE(result);
175     g_pluggedType = BatteryPluggedType::PLUGGED_TYPE_NONE;
176     battery = g_batterImpl->RequestBatteryInfo();
177     if (battery == nullptr) {
178         result = false;
179     }
180     EXPECT_TRUE(result);
181     g_pluggedType = BatteryPluggedType::PLUGGED_TYPE_BUTT;
182     battery = g_batterImpl->RequestBatteryInfo();
183     if (battery == nullptr) {
184         result = false;
185     }
186     EXPECT_TRUE(result);
187 }
188 
189 /**
190  * @tc.name: BatteryAdapter_WebBatteryInfoImpl_006.
191  * @tc.desc: Test the WebBatteryInfoImpl.
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(BatteryMgrAdapterTest, BatteryAdapter_WebBatteryInfoImpl_006, TestSize.Level1)
196 {
197     double level = 1;
198     bool isCharging = true;
199     int disChargingTime = 1;
200     int chargingTime = 1;
201     WebBatteryInfoImpl info(level, isCharging, disChargingTime, chargingTime);
202     EXPECT_FLOAT_EQ(info.GetLevel(), level);
203     EXPECT_TRUE(info.IsCharging());
204     EXPECT_EQ(info.DisChargingTime(), disChargingTime);
205     EXPECT_EQ(info.ChargingTime(), chargingTime);
206     Want want;
207     want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
208     CommonEventData data(want);
209     g_batter->OnReceiveEvent(data);
210     g_batterImpl->commonEventSubscriber_ = nullptr;
211     g_batterImpl->StopListen();
212 }
213 }
214 }