1 /*
2 * Copyright (c) 2022-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 <gtest/gtest.h>
17 #include <numeric>
18 #include "iremote_broker.h"
19 #include "subscriber_death_recipient.h"
20
21 namespace OHOS {
22 namespace EventFwk {
23 using namespace testing::ext;
24
25 class SubscriberDeathRecipientTest : public testing::Test {
26 public:
SubscriberDeathRecipientTest()27 SubscriberDeathRecipientTest()
28 {}
~SubscriberDeathRecipientTest()29 ~SubscriberDeathRecipientTest()
30 {}
31
32 static void SetUpTestCase(void);
33 static void TearDownTestCase(void);
34 void SetUp();
35 void TearDown();
36 };
37
SetUpTestCase(void)38 void SubscriberDeathRecipientTest::SetUpTestCase(void)
39 {}
40
TearDownTestCase(void)41 void SubscriberDeathRecipientTest::TearDownTestCase(void)
42 {}
43
SetUp(void)44 void SubscriberDeathRecipientTest::SetUp(void)
45 {}
46
TearDown(void)47 void SubscriberDeathRecipientTest::TearDown(void)
48 {}
49
50 class MockIRemoteObject : public IRemoteObject {
51 public:
MockIRemoteObject()52 MockIRemoteObject() : IRemoteObject(u"mock_i_remote_object") {}
53
~MockIRemoteObject()54 ~MockIRemoteObject() {}
55
GetObjectRefCount()56 int32_t GetObjectRefCount() override
57 {
58 return 0;
59 }
60
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)61 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
62 {
63 return 0;
64 }
65
IsProxyObject() const66 bool IsProxyObject() const override
67 {
68 return true;
69 }
70
CheckObjectLegality() const71 bool CheckObjectLegality() const override
72 {
73 return true;
74 }
75
AddDeathRecipient(const sptr<DeathRecipient> & recipient)76 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override
77 {
78 return true;
79 }
80
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)81 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override
82 {
83 return true;
84 }
85
Marshalling(Parcel & parcel) const86 bool Marshalling(Parcel &parcel) const override
87 {
88 return true;
89 }
90
AsInterface()91 sptr<IRemoteBroker> AsInterface() override
92 {
93 return nullptr;
94 }
95
Dump(int fd,const std::vector<std::u16string> & args)96 int Dump(int fd, const std::vector<std::u16string> &args) override
97 {
98 return 0;
99 }
100
GetObjectDescriptor() const101 std::u16string GetObjectDescriptor() const
102 {
103 std::u16string descriptor = std::u16string();
104 return descriptor;
105 }
106 };
107
108 /**
109 * @tc.name: SubscriberDeathRecipient_0100
110 * @tc.desc: test OnRemoteDied function and remote is nullptr.
111 * @tc.type: FUNC
112 */
HWTEST_F(SubscriberDeathRecipientTest,SubscriberDeathRecipient_0100,Level1)113 HWTEST_F(SubscriberDeathRecipientTest, SubscriberDeathRecipient_0100, Level1)
114 {
115 GTEST_LOG_(INFO) << "SubscriberDeathRecipient_0100 start";
116 std::shared_ptr<SubscriberDeathRecipient> subscriberDeathRecipient = std::make_shared<SubscriberDeathRecipient>();
117 ASSERT_NE(nullptr, subscriberDeathRecipient);
118 const wptr<MockIRemoteObject> remote = nullptr;
119 subscriberDeathRecipient->OnRemoteDied(remote);
120 GTEST_LOG_(INFO) << "SubscriberDeathRecipient_0100 end";
121 }
122
123 /**
124 * @tc.name: SubscriberDeathRecipient_0200
125 * @tc.desc: test OnRemoteDied function and remote is not nullptr.
126 * @tc.type: FUNC
127 */
HWTEST_F(SubscriberDeathRecipientTest,SubscriberDeathRecipient_0200,Level1)128 HWTEST_F(SubscriberDeathRecipientTest, SubscriberDeathRecipient_0200, Level1)
129 {
130 GTEST_LOG_(INFO) << "SubscriberDeathRecipient_0200 start";
131 SubscriberDeathRecipient subscriberDeathRecipient;
132 sptr<MockIRemoteObject> sptrDeath = new (std::nothrow) MockIRemoteObject();
133 ASSERT_NE(nullptr, sptrDeath);
134 const wptr<MockIRemoteObject> remote = sptrDeath;
135 subscriberDeathRecipient.OnRemoteDied(remote);
136 GTEST_LOG_(INFO) << "SubscriberDeathRecipient_0200 end";
137 }
138 } // namespace EventFwk
139 } // namespace OHOS