1 /* 2 * Copyright (c) 2021-2023 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_HILOGD(LABEL_TEST, "BatteryClientDeathRecipient001 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_HILOGD(LABEL_TEST, "BatteryClientDeathRecipient001 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_HILOGD(LABEL_TEST, "BatteryClientDeathRecipient002 start."); 65 auto& batterySrvClient = BatterySrvClient::GetInstance(); 66 EXPECT_NE(batterySrvClient.Connect(), nullptr); 67 68 sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 69 if (sysMgr == nullptr) { 70 BATTERY_HILOGD(LABEL_TEST, "Failed to get Registry"); 71 return; 72 } 73 wptr<IRemoteObject> remoteObj = sysMgr->CheckSystemAbility(POWER_MANAGER_BATT_SERVICE_ID); 74 if (remoteObj == nullptr) { 75 BATTERY_HILOGD(LABEL_TEST, "GetSystemAbility failed"); 76 return; 77 } 78 79 { 80 std::shared_ptr<IRemoteObject::DeathRecipient> deathRecipient = 81 std::make_shared<BatterySrvClient::BatterySrvDeathRecipient>(batterySrvClient); 82 EXPECT_NE(deathRecipient, nullptr); 83 deathRecipient->OnRemoteDied(remoteObj); 84 deathRecipient = nullptr; 85 } 86 87 ASSERT_EQ(batterySrvClient.proxy_, nullptr); 88 BATTERY_HILOGD(LABEL_TEST, "BatteryClientDeathRecipient002 end."); 89 } 90 } // namespace 91