• 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 "battery_callback_test.h"
17 #include "battery_callback.h"
18 #include "battery_service.h"
19 #include "power_common.h"
20 #include <memory>
21 
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 sptr<BatteryService> g_service;
28 }
29 
HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V1_2::BatteryInfo & event)30 int32_t HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V1_2::BatteryInfo& event)
31 {
32     return ERR_OK;
33 }
34 
SetUpTestCase()35 void BatteryCallbackTest::SetUpTestCase()
36 {
37     g_service = DelayedSpSingleton<BatteryService>::GetInstance();
38 }
39 
TearDownTestCase()40 void BatteryCallbackTest::TearDownTestCase()
41 {
42     g_service = nullptr;
43 }
44 
45 /**
46  * @tc.name: BatteryCallback001
47  * @tc.desc: Update BatteryInfo, the eventCb_ is valid
48  * @tc.type: FUNC
49  * @tc.require: issueI5YZR1
50  */
51 HWTEST_F(BatteryCallbackTest, BatteryCallback001, TestSize.Level1)
52 {
53     sptr<HDI::Battery::V1_2::IBatteryInterface> iBatteryInterface;
54     iBatteryInterface = HDI::Battery::V1_2::IBatteryInterface::Get();
55     sptr<HDI::Battery::V1_2::IBatteryCallback> callback = new BatteryCallback();
56     EXPECT_EQ(iBatteryInterface->Register(callback), HDF_SUCCESS);
57 
58     BatteryCallback::BatteryEventCallback eventCb = std::bind(&HandleBatteryCallbackEvent, std::placeholders::_1);
59     EXPECT_EQ(BatteryCallback::RegisterBatteryEvent(eventCb), HDF_SUCCESS);
60     HDI::Battery::V1_2::BatteryInfo event;
61     iBatteryInterface->GetBatteryInfo(event);
62     EXPECT_NE(callback->Update(event), HDF_FAILURE);
63 }
64 
65 /**
66  * @tc.name: BatteryCallback002
67  * @tc.desc: Update BatteryInfo, the eventCb_ is invalid
68  * @tc.type: FUNC
69  */
70 HWTEST_F(BatteryCallbackTest, BatteryCallback002, TestSize.Level1)
71 {
72     BatteryCallback::BatteryEventCallback eventCb = nullptr;
73     EXPECT_EQ(BatteryCallback::RegisterBatteryEvent(eventCb), HDF_SUCCESS);
74     HDI::Battery::V1_2::BatteryInfo event;
75     sptr<HDI::Battery::V1_2::IBatteryCallback> callback = new BatteryCallback();
76     EXPECT_EQ(callback->Update(event), HDF_FAILURE);
77 }
78 } // namespace PowerMgr
79 } // namespace OHOS
80