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,const int32_t & userId)74 virtual int32_t PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
75 const OHOS::sptr<IRemoteObject> &commonEventListener, const int32_t &userId)
76 {
77 return ERR_COMMON;
78 }
79
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const OHOS::sptr<IRemoteObject> & commonEventListener,const int32_t instanceKey)80 virtual int32_t SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo,
81 const OHOS::sptr<IRemoteObject> &commonEventListener, const int32_t instanceKey)
82 {
83 return ERR_COMMON;
84 }
85
UnsubscribeCommonEvent(const OHOS::sptr<IRemoteObject> & commonEventListener)86 virtual int32_t UnsubscribeCommonEvent(const OHOS::sptr<IRemoteObject> &commonEventListener)
87 {
88 return ERR_COMMON;
89 }
90
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)91 virtual bool DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
92 std::vector<std::string> &state)
93 {
94 return false;
95 }
96
~CommonEventStubTest()97 virtual ~CommonEventStubTest()
98 {}
99
FinishReceiver(const OHOS::sptr<IRemoteObject> & proxy,const int32_t & code,const std::string & receiverData,const bool & abortEvent)100 virtual bool FinishReceiver(const OHOS::sptr<IRemoteObject> &proxy, const int32_t &code,
101 const std::string &receiverData, const bool &abortEvent)
102 {
103 return false;
104 }
105 };
106
107 class SubscriberTest : public CommonEventSubscriber {
108 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)109 explicit SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
110 {}
111
~SubscriberTest()112 ~SubscriberTest()
113 {}
114
OnReceiveEvent(const CommonEventData & data)115 virtual void OnReceiveEvent(const CommonEventData &data)
116 {}
117 };
118
SetUpTestCase(void)119 void CommonEventUnSubscribeTest::SetUpTestCase(void)
120 {}
121
TearDownTestCase(void)122 void CommonEventUnSubscribeTest::TearDownTestCase(void)
123 {}
124
SetUp(void)125 void CommonEventUnSubscribeTest::SetUp(void)
126 {}
127
TearDown(void)128 void CommonEventUnSubscribeTest::TearDown(void)
129 {}
130
SetMatchingSkillsWithEvent(const std::string & event)131 void CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent(const std::string &event)
132 {
133 matchingSkills_.AddEvent(event);
134 }
135
SetMatchingSkillsWithEntity(const std::string & entity)136 void CommonEventUnSubscribeTest::SetMatchingSkillsWithEntity(const std::string &entity)
137 {
138 matchingSkills_.AddEntity(entity);
139 }
140
SetMatchingSkillsWithScheme(const std::string & scheme)141 void CommonEventUnSubscribeTest::SetMatchingSkillsWithScheme(const std::string &scheme)
142 {
143 matchingSkills_.AddScheme(scheme);
144 }
145
146 /*
147 * Feature: CommonEventManager
148 * Function: UnSubscribeCommonEvent
149 * SubFunction: NA
150 * FunctionPoints: normal
151 * EnvConditions: system running normally
152 * CaseDescription: Unsubscribe normally and verify UnSubscribeCommonEvent function return value.
153 */
154 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_001, TestSize.Level1)
155 {
156 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
157 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
158 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
159 std::shared_ptr<CommonEventManager> helper;
160 EXPECT_EQ(true, helper->UnSubscribeCommonEvent(subscriber));
161 }
162
163 /*
164 * Feature: CommonEvent
165 * Function: UnSubscribeCommonEvent
166 * SubFunction: NA
167 * FunctionPoints: normal
168 * EnvConditions: system running normally
169 * CaseDescription: Verify UnSubscribeCommonEvent function return value with eventListener not exist.
170 */
171 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_002, TestSize.Level1)
172 {
173 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
174 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
175 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
176 EXPECT_EQ(ERR_OK, CommonEvent::GetInstance()->UnSubscribeCommonEvent(subscriber));
177 }
178
179 /*
180 * Feature: CommonEvent
181 * Function: UnSubscribeCommonEvent
182 * SubFunction: NA
183 * FunctionPoints: normal
184 * EnvConditions: system running normally
185 * CaseDescription: Verify UnSubscribeCommonEvent function return value with eventListener existed.
186 */
187 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_003, TestSize.Level1)
188 {
189 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
190 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
191 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
192 CommonEvent::GetInstance()->SubscribeCommonEvent(subscriber);
193 EXPECT_EQ(ERR_OK, CommonEvent::GetInstance()->UnSubscribeCommonEvent(subscriber));
194 }
195
196 /*
197 * Feature: InnerCommonEventManager
198 * Function: UnsubscribeCommonEvent
199 * SubFunction: NA
200 * FunctionPoints: normal
201 * EnvConditions: system running normally
202 * CaseDescription: Verify UnSubscribeCommonEvent function return value.
203 */
204 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_004, TestSize.Level1)
205 {
206 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
207 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
208 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
209 sptr<IRemoteObject> commonEventListener = new CommonEventListener(subscriber);
210 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
211 EXPECT_EQ(true, innerCommonEventManager->UnsubscribeCommonEvent(commonEventListener));
212 }
213
214 /*
215 * Feature: CommonEventSubscriberManager
216 * Function: RemoveSubscriber
217 * SubFunction: NA
218 * FunctionPoints: normal
219 * EnvConditions: system running normally
220 * CaseDescription: Verify RemoveSubscriber function return value.
221 */
222 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_005, TestSize.Level1)
223 {
224 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
225 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
226 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
227 sptr<IRemoteObject> commonEventListener = new CommonEventListener(subscriber);
228 EXPECT_EQ(
229 ERR_OK, DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->RemoveSubscriber(commonEventListener));
230 }
231
232 /*
233 * Feature: CommonEvent
234 * Function: UnSubscribeCommonEvent
235 * SubFunction: NA
236 * FunctionPoints: exception
237 * EnvConditions: system running normally
238 * CaseDescription: When the subscriber does not exist, verify UnSubscribeCommonEvent function return value.
239 */
240 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_006, TestSize.Level1)
241 {
242 std::shared_ptr<DreivedSubscriber> subscriber = nullptr;
243 EXPECT_NE(ERR_OK, CommonEvent::GetInstance()->UnSubscribeCommonEvent(subscriber));
244 }
245
246 /*
247 * Feature: InnerCommonEventManager
248 * Function: UnsubscribeCommonEvent
249 * SubFunction: NA
250 * FunctionPoints: exception
251 * EnvConditions: system running normally
252 * CaseDescription: When the CommonEventListener does not exist,
253 * verify UnsubscribeCommonEvent function return value.
254 */
255 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_009, TestSize.Level1)
256 {
257 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
258 sptr<IRemoteObject> sp(nullptr);
259 EXPECT_EQ(false, innerCommonEventManager->UnsubscribeCommonEvent(sp));
260 }
261
262 /*
263 * Feature: CommonEventSubscriberManager
264 * Function: RemoveSubscriber
265 * SubFunction: NA
266 * FunctionPoints: exception
267 * EnvConditions: system running normally
268 * CaseDescription: When the CommonEventListener does not exist,
269 * verify RemoveSubscriber function return value.
270 */
271 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_010, TestSize.Level1)
272 {
273 EXPECT_EQ(
274 ERR_INVALID_VALUE, DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->RemoveSubscriber(nullptr));
275 }
276
277 /*
278 * Feature: CommonEventSubscriberManager
279 * Function: RemoveSubscriber
280 * SubFunction: NA
281 * FunctionPoints: exception
282 * EnvConditions: system running normally
283 * CaseDescription: When the death_ is not null,
284 * verify RemoveSubscriber function return value.
285 */
286 HWTEST_F(CommonEventUnSubscribeTest, UnSubscribe_011, TestSize.Level1)
287 {
288 CommonEventUnSubscribeTest::SetMatchingSkillsWithEvent("event");
289 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
290 // make a subscriber object
291 std::shared_ptr<SubscriberTest> subscriberTest = std::make_shared<SubscriberTest>(subscribeInfo);
292 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberTest);
293 DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->death_ = nullptr;
294 EXPECT_EQ(
295 ERR_OK, DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->RemoveSubscriber(commonEventListener));
296 }