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 // redefine private and protected since testcase need to invoke and test private function
17 #include "errors.h"
18 #define private public
19 #define protected public
20 #include "common_event.h"
21 #include "common_event_manager.h"
22 #include "common_event_stub.h"
23 #include "common_event_subscriber_manager.h"
24 #include "inner_common_event_manager.h"
25 #undef private
26 #undef protected
27
28 #include <gtest/gtest.h>
29
30 using namespace testing::ext;
31 using namespace OHOS;
32 using namespace OHOS::EventFwk;
33 using OHOS::Parcel;
34
35 namespace {
36 constexpr int32_t ERR_COMMON = -1;
37 }
38
39 class CommonEventUnSubscribeTest : public testing::Test {
40 public:
CommonEventUnSubscribeTest()41 CommonEventUnSubscribeTest()
42 {}
~CommonEventUnSubscribeTest()43 ~CommonEventUnSubscribeTest()
44 {}
45 static void SetUpTestCase(void);
46 static void TearDownTestCase(void);
47 void SetUp();
48 void TearDown();
49
50 public:
51 MatchingSkills matchingSkills_;
52 void SetMatchingSkillsWithEvent(const std::string &event);
53 void SetMatchingSkillsWithEntity(const std::string &entity);
54 void SetMatchingSkillsWithScheme(const std::string &scheme);
55 };
56
57 class DreivedSubscriber : public CommonEventSubscriber {
58 public:
DreivedSubscriber(const CommonEventSubscribeInfo & sp)59 explicit DreivedSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
60 {}
61
~DreivedSubscriber()62 ~DreivedSubscriber()
63 {}
64
OnReceiveEvent(const CommonEventData & data)65 virtual void OnReceiveEvent(const CommonEventData &data)
66 {}
67 };
68
69 class CommonEventStubTest : public CommonEventStub {
70 public:
CommonEventStubTest()71 CommonEventStubTest()
72 {}
73
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,const OHOS::sptr<IRemoteObject> & commonEventListener,int32_t userId,int32_t & funcResult)74 virtual ErrCode PublishCommonEvent(const CommonEventData& event, const CommonEventPublishInfo& publishInfo,
75 const OHOS::sptr<IRemoteObject>& commonEventListener, int32_t userId, int32_t& funcResult)
76 {
77 return ERR_COMMON;
78 }
79
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const OHOS::sptr<IRemoteObject> & commonEventListener,int32_t instanceKey,int32_t & funcResult)80 virtual ErrCode SubscribeCommonEvent(const CommonEventSubscribeInfo& subscribeInfo,
81 const OHOS::sptr<IRemoteObject>& commonEventListener, int32_t instanceKey, int32_t& funcResult)
82 {
83 return ERR_COMMON;
84 }
85
UnsubscribeCommonEvent(const OHOS::sptr<IRemoteObject> & commonEventListener,int32_t & funcResult)86 virtual ErrCode UnsubscribeCommonEvent(const OHOS::sptr<IRemoteObject>& commonEventListener, int32_t& funcResult)
87 {
88 return ERR_COMMON;
89 }
90
DumpState(uint8_t dumpType,const std::string & event,int32_t userId,std::vector<std::string> & state,bool & funcResult)91 virtual ErrCode DumpState(uint8_t dumpType, const std::string& event, int32_t userId,
92 std::vector<std::string>& state, bool& funcResult)
93 {
94 funcResult = false;
95 return ERR_OK;
96 }
97
~CommonEventStubTest()98 virtual ~CommonEventStubTest()
99 {}
100
FinishReceiver(const OHOS::sptr<IRemoteObject> & proxy,int32_t code,const std::string & receiverData,bool abortEvent,bool & funcResult)101 virtual ErrCode FinishReceiver(const OHOS::sptr<IRemoteObject>& proxy, int32_t code,
102 const std::string& receiverData, bool abortEvent, bool& funcResult)
103 {
104 funcResult = false;
105 return ERR_OK;
106 }
107 };
108
109 class SubscriberTest : public CommonEventSubscriber {
110 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)111 explicit SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
112 {}
113
~SubscriberTest()114 ~SubscriberTest()
115 {}
116
OnReceiveEvent(const CommonEventData & data)117 virtual void OnReceiveEvent(const CommonEventData &data)
118 {}
119 };
120
SetUpTestCase(void)121 void CommonEventUnSubscribeTest::SetUpTestCase(void)
122 {}
123
TearDownTestCase(void)124 void CommonEventUnSubscribeTest::TearDownTestCase(void)
125 {}
126
SetUp(void)127 void CommonEventUnSubscribeTest::SetUp(void)
128 {}
129
TearDown(void)130 void CommonEventUnSubscribeTest::TearDown(void)
131 {}
132
SetMatchingSkillsWithEvent(const std::string & event)133 void CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent(const std::string &event)
134 {
135 matchingSkills_.AddEvent(event);
136 }
137
SetMatchingSkillsWithEntity(const std::string & entity)138 void CommonEventUnSubscribeTest::SetMatchingSkillsWithEntity(const std::string &entity)
139 {
140 matchingSkills_.AddEntity(entity);
141 }
142
SetMatchingSkillsWithScheme(const std::string & scheme)143 void CommonEventUnSubscribeTest::SetMatchingSkillsWithScheme(const std::string &scheme)
144 {
145 matchingSkills_.AddScheme(scheme);
146 }
147
148 /*
149 * Feature: CommonEventManager
150 * Function: UnSubscribeCommonEvent
151 * SubFunction: NA
152 * FunctionPoints: normal
153 * EnvConditions: system running normally
154 * CaseDescription: Unsubscribe normally and verify UnSubscribeCommonEvent function return value.
155 */
156 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_001, TestSize.Level0)
157 {
158 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
159 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
160 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
161 std::shared_ptr<CommonEventManager> helper;
162 EXPECT_EQ(true, helper->UnSubscribeCommonEvent(subscriber));
163 }
164
165 /*
166 * Feature: CommonEvent
167 * Function: UnSubscribeCommonEvent
168 * SubFunction: NA
169 * FunctionPoints: normal
170 * EnvConditions: system running normally
171 * CaseDescription: Verify UnSubscribeCommonEvent function return value with eventListener not exist.
172 */
173 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_002, TestSize.Level0)
174 {
175 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
176 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
177 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
178 EXPECT_EQ(ERR_OK, CommonEvent::GetInstance()->UnSubscribeCommonEvent(subscriber));
179 }
180
181 /*
182 * Feature: CommonEvent
183 * Function: UnSubscribeCommonEvent
184 * SubFunction: NA
185 * FunctionPoints: normal
186 * EnvConditions: system running normally
187 * CaseDescription: Verify UnSubscribeCommonEvent function return value with eventListener existed.
188 */
189 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_003, TestSize.Level0)
190 {
191 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
192 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
193 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
194 CommonEvent::GetInstance()->SubscribeCommonEvent(subscriber);
195 EXPECT_EQ(ERR_OK, CommonEvent::GetInstance()->UnSubscribeCommonEvent(subscriber));
196 }
197
198 /*
199 * Feature: InnerCommonEventManager
200 * Function: UnsubscribeCommonEvent
201 * SubFunction: NA
202 * FunctionPoints: normal
203 * EnvConditions: system running normally
204 * CaseDescription: Verify UnSubscribeCommonEvent function return value.
205 */
206 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_004, TestSize.Level0)
207 {
208 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
209 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
210 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
211 sptr<IRemoteObject> commonEventListener = new CommonEventListener(subscriber);
212 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
213 EXPECT_EQ(true, innerCommonEventManager->UnsubscribeCommonEvent(commonEventListener));
214 }
215
216 /*
217 * Feature: CommonEventSubscriberManager
218 * Function: RemoveSubscriber
219 * SubFunction: NA
220 * FunctionPoints: normal
221 * EnvConditions: system running normally
222 * CaseDescription: Verify RemoveSubscriber function return value.
223 */
224 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_005, TestSize.Level0)
225 {
226 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
227 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
228 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
229 sptr<IRemoteObject> commonEventListener = new CommonEventListener(subscriber);
230 EXPECT_EQ(
231 ERR_OK, DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->RemoveSubscriber(commonEventListener));
232 }
233
234 /*
235 * Feature: CommonEvent
236 * Function: UnSubscribeCommonEvent
237 * SubFunction: NA
238 * FunctionPoints: exception
239 * EnvConditions: system running normally
240 * CaseDescription: When the subscriber does not exist, verify UnSubscribeCommonEvent function return value.
241 */
242 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_006, TestSize.Level0)
243 {
244 std::shared_ptr<DreivedSubscriber> subscriber = nullptr;
245 EXPECT_NE(ERR_OK, CommonEvent::GetInstance()->UnSubscribeCommonEvent(subscriber));
246 }
247
248 /*
249 * Feature: InnerCommonEventManager
250 * Function: UnsubscribeCommonEvent
251 * SubFunction: NA
252 * FunctionPoints: exception
253 * EnvConditions: system running normally
254 * CaseDescription: When the CommonEventListener does not exist,
255 * verify UnsubscribeCommonEvent function return value.
256 */
257 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_009, TestSize.Level0)
258 {
259 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
260 sptr<IRemoteObject> sp(nullptr);
261 EXPECT_EQ(false, innerCommonEventManager->UnsubscribeCommonEvent(sp));
262 }
263
264 /*
265 * Feature: CommonEventSubscriberManager
266 * Function: RemoveSubscriber
267 * SubFunction: NA
268 * FunctionPoints: exception
269 * EnvConditions: system running normally
270 * CaseDescription: When the CommonEventListener does not exist,
271 * verify RemoveSubscriber function return value.
272 */
273 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_010, TestSize.Level0)
274 {
275 EXPECT_EQ(
276 ERR_INVALID_VALUE, DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->RemoveSubscriber(nullptr));
277 }
278
279 /*
280 * Feature: CommonEventSubscriberManager
281 * Function: RemoveSubscriber
282 * SubFunction: NA
283 * FunctionPoints: exception
284 * EnvConditions: system running normally
285 * CaseDescription: When the death_ is not null,
286 * verify RemoveSubscriber function return value.
287 */
288 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_011, TestSize.Level0)
289 {
290 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
291 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
292 // make a subscriber object
293 std::shared_ptr<SubscriberTest> subscriberTest = std::make_shared<SubscriberTest>(subscribeInfo);
294 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberTest);
295 DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->death_ = nullptr;
296 EXPECT_EQ(
297 ERR_OK, DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->RemoveSubscriber(commonEventListener));
298 }
299
300 /*
301 * Feature: CommonEvent
302 * Function: NewUnSubscribeCommonEventSync
303 * SubFunction: NA
304 * FunctionPoints: normal
305 * EnvConditions: system running normally
306 * CaseDescription: Verify UnSubscribeCommonEvent function return value with eventListener existed.
307 */
308 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_012, TestSize.Level0)
309 {
310 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
311 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
312 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
313 CommonEvent::GetInstance()->SubscribeCommonEvent(subscriber);
314 EXPECT_EQ(ERR_OK, CommonEvent::GetInstance()->UnSubscribeCommonEventSync(subscriber));
315 }
316
317 /*
318 * Feature: CommonEventManager
319 * Function: NewUnSubscribeCommonEventSync
320 * SubFunction: NA
321 * FunctionPoints: normal
322 * EnvConditions: system running normally
323 * CaseDescription: Unsubscribe normally and verify UnSubscribeCommonEvent function return value.
324 */
325 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_013, TestSize.Level0)
326 {
327 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
328 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
329 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
330 std::shared_ptr<CommonEventManager> helper;
331 EXPECT_EQ(ERR_OK, helper->NewUnSubscribeCommonEventSync(subscriber));
332 }