1 /* 2 * Copyright (c) 2021-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 "battery_client_death_recipient_test.h" 17 18 #include <gtest/gtest.h> 19 #include <iostream> 20 #include <string> 21 22 #include "if_system_ability_manager.h" 23 #include "iremote_broker.h" 24 #include "iservice_registry.h" 25 #include "system_ability_definition.h" 26 27 #include "battery_log.h" 28 #include "battery_srv_client.h" 29 #include "test_utils.h" 30 31 using namespace testing::ext; 32 using namespace OHOS::PowerMgr; 33 using namespace OHOS; 34 using namespace std; 35 36 namespace { 37 /** 38 * @tc.name: BatteryClientDeathRecipient001 39 * @tc.desc: test OnRemoteDied function 40 * @tc.type: FUNC 41 */ 42 HWTEST_F(BatteryClientDeathRecipientTest, BatteryClientDeathRecipient001, TestSize.Level1) 43 { 44 BATTERY_HILOGI(LABEL_TEST, "BatteryClientDeathRecipient001 function start!"); 45 auto& batterySrvClient = BatterySrvClient::GetInstance(); 46 EXPECT_NE(batterySrvClient.Connect(), nullptr); 47 48 wptr<IRemoteObject> remoteObj = nullptr; 49 std::shared_ptr<IRemoteObject::DeathRecipient> deathRecipient = 50 std::make_shared<BatterySrvClient::BatterySrvDeathRecipient>(batterySrvClient); 51 EXPECT_NE(deathRecipient, nullptr); 52 deathRecipient->OnRemoteDied(remoteObj); 53 EXPECT_NE(batterySrvClient.proxy_, nullptr); 54 BATTERY_HILOGI(LABEL_TEST, "BatteryClientDeathRecipient001 function end!"); 55 } 56 57 /** 58 * @tc.name: BatteryClientDeathRecipient002 59 * @tc.desc: test OnRemoteDied function 60 * @tc.type: FUNC 61 */ 62 HWTEST_F(BatteryClientDeathRecipientTest, BatteryClientDeathRecipient002, TestSize.Level1) 63 { 64 BATTERY_HILOGI(LABEL_TEST, "BatteryClientDeathRecipient002 function start!"); 65 auto& batterySrvClient = BatterySrvClient::GetInstance(); 66 EXPECT_NE(batterySrvClient.Connect(), nullptr); 67 68 sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 69 ASSERT_NE(sysMgr, nullptr); 70 wptr<IRemoteObject> remoteObj = sysMgr->CheckSystemAbility(POWER_MANAGER_BATT_SERVICE_ID); 71 ASSERT_NE(remoteObj, nullptr); 72 73 { 74 std::shared_ptr<IRemoteObject::DeathRecipient> deathRecipient = 75 std::make_shared<BatterySrvClient::BatterySrvDeathRecipient>(batterySrvClient); 76 EXPECT_NE(deathRecipient, nullptr); 77 deathRecipient->OnRemoteDied(remoteObj); 78 deathRecipient = nullptr; 79 } 80 81 ASSERT_EQ(batterySrvClient.proxy_, nullptr); 82 BATTERY_HILOGI(LABEL_TEST, "BatteryClientDeathRecipient002 function end!"); 83 } 84 } // namespace 85