1 /*
2 * Copyright (c) 2022-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 "power_mgr_service_death_test.h"
17
18 #include "mock_power_remote_object.h"
19 #include "power_mgr_client.h"
20 #include "power_log.h"
21
22 using namespace testing::ext;
23 using namespace OHOS::PowerMgr;
24 using namespace OHOS;
25 using namespace std;
26
27 namespace {
28 /**
29 * @tc.name: PowerMgrServiceDeathTest_001
30 * @tc.desc: test OnRemoteDied function(Input remoteObj is nullptr, function don't reset proxy)
31 * @tc.type: FUNC
32 * @tc.require: issueI650CX
33 */
HWTEST_F(PowerMgrServiceDeathTest, PowerMgrServiceDeathTest_001, TestSize.Level0)34 HWTEST_F (PowerMgrServiceDeathTest, PowerMgrServiceDeathTest_001, TestSize.Level0)
35 {
36 POWER_HILOGD(LABEL_TEST, "PowerMgrServiceDeathTest_001 start");
37 auto& powerMgrClient = PowerMgrClient::GetInstance();
38 EXPECT_EQ(powerMgrClient.Connect(), ERR_OK);
39
40 std::shared_ptr<IRemoteObject::DeathRecipient> deathRecipient =
41 std::make_shared<PowerMgrClient::PowerMgrDeathRecipient>(powerMgrClient);
42 wptr<IRemoteObject> remoteObj = nullptr;
43 EXPECT_NE(deathRecipient, nullptr);
44 deathRecipient->OnRemoteDied(remoteObj);
45 EXPECT_NE(powerMgrClient.proxy_, nullptr);
46 POWER_HILOGD(LABEL_TEST, "PowerMgrServiceDeathTest_001 end");
47 }
48
49 /**
50 * @tc.name: PowerMgrServiceDeathTest_002
51 * @tc.desc: test OnRemoteDied function(Input remoteObj is not nullptr, function don't reset proxy)
52 * @tc.type: FUNC
53 * @tc.require: issueI650CX
54 */
HWTEST_F(PowerMgrServiceDeathTest, PowerMgrServiceDeathTest_002, TestSize.Level0)55 HWTEST_F (PowerMgrServiceDeathTest, PowerMgrServiceDeathTest_002, TestSize.Level0)
56 {
57 POWER_HILOGD(LABEL_TEST, "PowerMgrServiceDeathTest_002 start");
58 auto& powerMgrClient = PowerMgrClient::GetInstance();
59 EXPECT_EQ(powerMgrClient.Connect(), ERR_OK);
60
61 std::shared_ptr<IRemoteObject::DeathRecipient> deathRecipient =
62 std::make_shared<PowerMgrClient::PowerMgrDeathRecipient>(powerMgrClient);
63 EXPECT_NE(deathRecipient, nullptr);
64 sptr<IRemoteObject> sptrRemoteObj = new MockPowerRemoteObject();
65 EXPECT_FALSE(sptrRemoteObj == nullptr);
66 deathRecipient->OnRemoteDied(sptrRemoteObj);
67 EXPECT_NE(powerMgrClient.proxy_, nullptr);
68 POWER_HILOGD(LABEL_TEST, "PowerMgrServiceDeathTest_002 end");
69 }
70 }