1 /*
2 * Copyright (c) 2021-2022 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
18 #define private public
19 #define protected public
20 #include "bundle_manager_helper.h"
21 #include "common_event_subscriber_manager.h"
22 #undef private
23 #undef protected
24
25 #include "common_event_listener.h"
26 #include "common_event_subscriber.h"
27 #include "inner_common_event_manager.h"
28 #include "mock_bundle_manager.h"
29
30 using namespace testing::ext;
31 using namespace OHOS::EventFwk;
32 using namespace OHOS::AppExecFwk;
33
34 namespace {
35 const std::string EVENT = "com.ces.test.event";
36 const std::string ENTITY = "com.ces.test.entity";
37 const std::string SCHEME = "com.ces.test.scheme";
38 const std::string PERMISSION = "com.ces.test.permission";
39 const std::string DEVICEDID = "deviceId";
40 const int PRIORITY = 1;
41 } // namespace
42
43 static OHOS::sptr<OHOS::IRemoteObject> bundleObject = nullptr;
44
45 class CommonEventUnSubscribeUnitTest : public testing::Test {
46 public:
CommonEventUnSubscribeUnitTest()47 CommonEventUnSubscribeUnitTest()
48 {}
~CommonEventUnSubscribeUnitTest()49 ~CommonEventUnSubscribeUnitTest()
50 {}
51 static void SetUpTestCase(void);
52 static void TearDownTestCase(void);
53 void SetUp();
54 void TearDown();
55 };
56
57 class SubscriberTest : public CommonEventSubscriber {
58 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)59 explicit SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
60 {}
61
~SubscriberTest()62 ~SubscriberTest()
63 {}
64
OnReceiveEvent(const CommonEventData & data)65 virtual void OnReceiveEvent(const CommonEventData &data)
66 {}
67 };
68
SetUpTestCase(void)69 void CommonEventUnSubscribeUnitTest::SetUpTestCase(void)
70 {
71 bundleObject = new MockBundleMgrService();
72 OHOS::DelayedSingleton<BundleManagerHelper>::GetInstance()->sptrBundleMgr_ =
73 OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(bundleObject);
74 }
75
TearDownTestCase(void)76 void CommonEventUnSubscribeUnitTest::TearDownTestCase(void)
77 {}
78
SetUp(void)79 void CommonEventUnSubscribeUnitTest::SetUp(void)
80 {}
81
TearDown(void)82 void CommonEventUnSubscribeUnitTest::TearDown(void)
83 {}
84
85 /*
86 * @tc.number: CommonEventUnSubscribeUnitTest_0300
87 * @tc.name: exception
88 * @tc.desc: verify InnerCommonEventManager UnsubscribeCommonEvent function success
89 */
90 HWTEST_F(CommonEventUnSubscribeUnitTest, CommonEventUnSubscribeUnitTest_0300, Function | MediumTest | Level1)
91 {
92 // make subscriber info
93 MatchingSkills matchingSkills;
94 matchingSkills.AddEvent(EVENT);
95 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
96
97 // make subscriber
98 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
99
100 // make common event listener
101 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
102 OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
103
104 // UnsubscribeCommonEvent
105 InnerCommonEventManager innerCommonEventManager;
106 EXPECT_TRUE(innerCommonEventManager.UnsubscribeCommonEvent(commonEventListenerPtr));
107 }
108
109 /*
110 * @tc.number: CommonEventUnSubscribeUnitTest_0400
111 * @tc.name: exception
112 * @tc.desc: When the CommonEventListener does not exist,
113 * verify InnerCommonEventManager UnsubscribeCommonEvent function return value.
114 */
115 HWTEST_F(CommonEventUnSubscribeUnitTest, CommonEventUnSubscribeUnitTest_0400, Function | MediumTest | Level1)
116 {
117 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
118 OHOS::sptr<OHOS::IRemoteObject> sp(nullptr);
119 EXPECT_FALSE(innerCommonEventManager->UnsubscribeCommonEvent(sp));
120 }
121
122 /*
123 * @tc.number: CommonEventUnSubscribeUnitTest_0500
124 * @tc.name: exception
125 * @tc.desc: verify CommonEventSubscriberManager RemoveSubscriber success
126 */
127 HWTEST_F(CommonEventUnSubscribeUnitTest, CommonEventUnSubscribeUnitTest_0500, Function | MediumTest | Level1)
128 {
129 // make subscriber info
130 MatchingSkills matchingSkills;
131 matchingSkills.AddEvent(EVENT);
132 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
133
134 // make subscriber
135 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
136
137 // make common event listener
138 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
139 OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
140
141 // RemoveSubscriber
142 CommonEventSubscriberManager commonEventSubscriberManager;
143 int result = commonEventSubscriberManager.RemoveSubscriber(commonEventListenerPtr);
144 EXPECT_EQ(OHOS::ERR_OK, result);
145 }
146
147 /*
148 * @tc.number: CommonEventUnSubscribeUnitTest_0600
149 * @tc.name: exception
150 * @tc.desc: When the CommonEventListener does not exist,
151 * verify RemoveSubscriber fail.
152 */
153 HWTEST_F(CommonEventUnSubscribeUnitTest, CommonEventUnSubscribeUnitTest_0600, Function | MediumTest | Level1)
154 {
155 CommonEventSubscriberManager commonEventSubscriberManager;
156 EXPECT_EQ(OHOS::ERR_INVALID_VALUE, commonEventSubscriberManager.RemoveSubscriber(nullptr));
157 }
158
159 /*
160 * @tc.number: CommonEventUnSubscribeUnitTest_0700
161 * @tc.name: exception
162 * @tc.desc: When the death_ is not null, verify RemoveSubscriber fail.
163 */
164 HWTEST_F(CommonEventUnSubscribeUnitTest, CommonEventUnSubscribeUnitTest_0700, Function | MediumTest | Level1)
165 {
166 // make matching skills
167 MatchingSkills matchingSkills;
168 matchingSkills.AddEvent(EVENT);
169 matchingSkills.AddEntity(ENTITY);
170 matchingSkills.AddScheme(SCHEME);
171
172 // make subscriber info
173 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
174 subscribeInfo.SetPriority(PRIORITY);
175 subscribeInfo.SetPermission(PERMISSION);
176 subscribeInfo.SetDeviceId(DEVICEDID);
177
178 // make a subscriber
179 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
180
181 // make common event listener
182 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
183
184 // RemoveSubscriber
185 CommonEventSubscriberManager commonEventSubscriberManager;
186 commonEventSubscriberManager.death_ = nullptr;
187 EXPECT_EQ(OHOS::ERR_OK, commonEventSubscriberManager.RemoveSubscriber(commonEventListener));
188 }
189
190 /*
191 * @tc.number: CommonEventUnSubscribeUnitTest_0800
192 * @tc.name: exception
193 * @tc.desc: verify RemoveSubscriberRecordLocked success.
194 */
195 HWTEST_F(CommonEventUnSubscribeUnitTest, CommonEventUnSubscribeUnitTest_0800, Function | MediumTest | Level1)
196 {
197 // make subscriber info
198 MatchingSkills matchingSkills;
199 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
200
201 // make subscriber
202 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
203
204 // make common event listener
205 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
206 OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
207
208 // RemoveSubscriber
209 CommonEventSubscriberManager commonEventSubscriberManager;
210 EXPECT_EQ(OHOS::ERR_OK, commonEventSubscriberManager.RemoveSubscriberRecordLocked(commonEventListenerPtr));
211 }
212
213 /*
214 * @tc.number: CommonEventUnSubscribeUnitTest_0900
215 * @tc.name: exception
216 * @tc.desc: When the commonEventListener is null, verify RemoveSubscriberRecordLocked success.
217 */
218 HWTEST_F(CommonEventUnSubscribeUnitTest, CommonEventUnSubscribeUnitTest_0900, Function | MediumTest | Level1)
219 {
220 // make subscriber info
221 MatchingSkills matchingSkills;
222 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
223
224 // make subscriber
225 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
226
227 // make common event listener
228 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
229 OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
230
231 // RemoveSubscriber
232 CommonEventSubscriberManager commonEventSubscriberManager;
233 EXPECT_EQ(OHOS::ERR_INVALID_VALUE, commonEventSubscriberManager.RemoveSubscriberRecordLocked(nullptr));
234 }
235