• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 
18 #ifdef GTEST
19 #define private   public
20 #define protected public
21 #endif
22 #include "battery_service.h"
23 #include "battery_callback.h"
24 #include "battery_log.h"
25 
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace PowerMgr {
30 namespace {
31 sptr<BatteryService> g_service;
32 }
33 
HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo & event)34 int32_t HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo& event)
35 {
36     return ERR_OK;
37 }
38 
SetUpTestCase()39 void BatteryCallbackTest::SetUpTestCase()
40 {
41     g_service = DelayedSpSingleton<BatteryService>::GetInstance();
42 }
43 
TearDownTestCase()44 void BatteryCallbackTest::TearDownTestCase()
45 {
46     g_service = nullptr;
47 }
48 
49 /**
50  * @tc.name: BatteryCallback001
51  * @tc.desc: Update BatteryInfo, the eventCb_ is valid
52  * @tc.type: FUNC
53  * @tc.require: issueI5YZR1
54  */
55 HWTEST_F(BatteryCallbackTest, BatteryCallback001, TestSize.Level1)
56 {
57     BATTERY_HILOGI(LABEL_TEST, "BatteryCallback001 function start!");
58     sptr<HDI::Battery::V2_0::IBatteryInterface> iBatteryInterface;
59     iBatteryInterface = HDI::Battery::V2_0::IBatteryInterface::Get();
60     sptr<HDI::Battery::V2_0::IBatteryCallback> callback = new BatteryCallback();
61     EXPECT_EQ(iBatteryInterface->Register(callback), HDF_SUCCESS);
62 
63     BatteryCallback::BatteryEventCallback eventCb = std::bind(&HandleBatteryCallbackEvent, std::placeholders::_1);
64     EXPECT_EQ(BatteryCallback::RegisterBatteryEvent(eventCb), HDF_SUCCESS);
65     HDI::Battery::V2_0::BatteryInfo event;
66     iBatteryInterface->GetBatteryInfo(event);
67     EXPECT_NE(callback->Update(event), HDF_FAILURE);
68     BATTERY_HILOGI(LABEL_TEST, "BatteryCallback001 function end!");
69 }
70 
71 /**
72  * @tc.name: BatteryCallback002
73  * @tc.desc: Update BatteryInfo, the eventCb_ is invalid
74  * @tc.type: FUNC
75  */
76 HWTEST_F(BatteryCallbackTest, BatteryCallback002, TestSize.Level1)
77 {
78     BATTERY_HILOGI(LABEL_TEST, "BatteryCallback002 function start!");
79     BatteryCallback::BatteryEventCallback eventCb = nullptr;
80     EXPECT_EQ(BatteryCallback::RegisterBatteryEvent(eventCb), HDF_SUCCESS);
81     HDI::Battery::V2_0::BatteryInfo event;
82     sptr<HDI::Battery::V2_0::IBatteryCallback> callback = new BatteryCallback();
83     EXPECT_EQ(callback->Update(event), HDF_FAILURE);
84     BATTERY_HILOGI(LABEL_TEST, "BatteryCallback002 function end!");
85 }
86 } // namespace PowerMgr
87 } // namespace OHOS
88