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