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 #define private public
19 #include "common_event.h"
20 #include "common_event_manager.h"
21 #include "common_event_sticky_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 
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::EventFwk;
30 using namespace OHOS::AppExecFwk;
31 
32 class CommonEventSubscriberManagerTest : public testing::Test {
33 public:
CommonEventSubscriberManagerTest()34     CommonEventSubscriberManagerTest()
35     {}
~CommonEventSubscriberManagerTest()36     ~CommonEventSubscriberManagerTest()
37     {}
38 
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     void SetUp();
42     void TearDown();
43 };
44 
45 class DreivedSubscriber : public CommonEventSubscriber {
46 public:
DreivedSubscriber(const CommonEventSubscribeInfo & sp)47     explicit DreivedSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
48     {}
49 
~DreivedSubscriber()50     ~DreivedSubscriber()
51     {}
52 
OnReceiveEvent(const CommonEventData & data)53     virtual void OnReceiveEvent(const CommonEventData &data)
54     {}
55 };
56 
SetUpTestCase(void)57 void CommonEventSubscriberManagerTest::SetUpTestCase(void)
58 {}
59 
TearDownTestCase(void)60 void CommonEventSubscriberManagerTest::TearDownTestCase(void)
61 {}
62 
SetUp(void)63 void CommonEventSubscriberManagerTest::SetUp(void)
64 {}
65 
TearDown(void)66 void CommonEventSubscriberManagerTest::TearDown(void)
67 {}
68 
69 /**
70  * @tc.name: CommonEventSubscriberManager_0100
71  * @tc.desc: test RemoveSubscriber function and commonEventListener is nullptr.
72  * @tc.type: FUNC
73  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0100,Level1)74 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0100, Level1)
75 {
76     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0100 start";
77     CommonEventSubscriberManager commonEventSubscriberManager;
78     sptr<IRemoteObject> commonEventListener = nullptr;
79     EXPECT_EQ(ERR_INVALID_VALUE, commonEventSubscriberManager.RemoveSubscriber(commonEventListener));
80     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0100 end";
81 }
82 
83 /**
84  * @tc.name: CommonEventSubscriberManager_0200
85  * @tc.desc: test RemoveSubscriber function and death_ is nullptr.
86  * @tc.type: FUNC
87  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0200,Level1)88 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0200, Level1)
89 {
90     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0200 start";
91     CommonEventSubscriberManager commonEventSubscriberManager;
92     // set commonEventListener
93     MatchingSkills matchingSkills_;
94     CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
95     std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
96     sptr<IRemoteObject> commonEventListener = new CommonEventListener(subscriber);
97     commonEventSubscriberManager.death_ = nullptr;
98     EXPECT_EQ(ERR_OK, commonEventSubscriberManager.RemoveSubscriber(commonEventListener));
99     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0200 end";
100 }
101 
102 /**
103  * @tc.name: CommonEventSubscriberManager_0300
104  * @tc.desc: test DumpDetailed function and record is nullptr.
105  * @tc.type: FUNC
106  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0300,Level1)107 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0300, Level1)
108 {
109     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0300 start";
110     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
111         std::make_shared<CommonEventSubscriberManager>();
112     ASSERT_NE(nullptr, commonEventSubscriberManager);
113     std::string title = "aa";
114     SubscriberRecordPtr record = nullptr;
115     std::string format = "aa";
116     std::string dumpInfo = "aa";
117     commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
118     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0300 end";
119 }
120 
121 /**
122  * @tc.name: CommonEventSubscriberManager_0400
123  * @tc.desc: test DumpDetailed function and record->eventSubscribeInfo is nullptr.
124  * @tc.type: FUNC
125  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0400,Level1)126 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0400, Level1)
127 {
128     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0400 start";
129     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
130         std::make_shared<CommonEventSubscriberManager>();
131     ASSERT_NE(nullptr, commonEventSubscriberManager);
132     std::string title = "aa";
133     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
134     record->eventSubscribeInfo = nullptr;
135     std::string format = "aa";
136     std::string dumpInfo = "aa";
137     commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
138     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0400 end";
139 }
140 
141 /**
142  * @tc.name: CommonEventSubscriberManager_0500
143  * @tc.desc: test DumpDetailed function and userId is UNDEFINED_USER.
144  * @tc.type: FUNC
145  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0500,Level1)146 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0500, Level1)
147 {
148     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0500 start";
149     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
150         std::make_shared<CommonEventSubscriberManager>();
151     ASSERT_NE(nullptr, commonEventSubscriberManager);
152     std::string title = "aa";
153     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
154     MatchingSkills matchingSkills_;
155     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
156     int32_t userId = UNDEFINED_USER;
157     record->eventSubscribeInfo->SetUserId(userId);
158     std::string format = "aa";
159     std::string dumpInfo = "aa";
160     commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
161     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0500 end";
162 }
163 
164 /**
165  * @tc.name: CommonEventSubscriberManager_0501
166  * @tc.desc: test DumpDetailed function and userId is ALL_USER.
167  * @tc.type: FUNC
168  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0501,Level1)169 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0501, Level1)
170 {
171     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0501 start";
172     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
173         std::make_shared<CommonEventSubscriberManager>();
174     ASSERT_NE(nullptr, commonEventSubscriberManager);
175     std::string title = "aa";
176     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
177     MatchingSkills matchingSkills_;
178     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
179     int32_t userId = ALL_USER;
180     record->eventSubscribeInfo->SetUserId(userId);
181 
182     MatchingSkills matchSkills;
183     std::string event = "event.unit.test";
184     matchSkills.AddEvent(event);
185     EXPECT_EQ(1, matchSkills.CountEvent());
186     std::string entity = "event.unit.test";
187     matchSkills.AddEntity(entity);
188     EXPECT_EQ(1, matchSkills.CountEntities());
189     std::string shceme = "event.unit.test";
190     matchSkills.AddScheme(shceme);
191     EXPECT_EQ(1, matchSkills.CountSchemes());
192     std::string format = "aa";
193     std::string dumpInfo = "aa";
194     commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
195     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0501 end";
196 }
197 
198 /**
199  * @tc.name: CommonEventSubscriberManager_0600
200  * @tc.desc: test DumpDetailed function and userId is 100.
201  * @tc.type: FUNC
202  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0600,Level1)203 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0600, Level1)
204 {
205     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0600 start";
206     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
207         std::make_shared<CommonEventSubscriberManager>();
208     ASSERT_NE(nullptr, commonEventSubscriberManager);
209     std::string title = "aa";
210     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
211     MatchingSkills matchingSkills_;
212     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
213     int32_t userId = 100;
214     record->eventSubscribeInfo->SetUserId(userId);
215     std::string format = "aa";
216     std::string dumpInfo = "aa";
217     commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
218     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0600 end";
219 }
220 
221 /**
222  * @tc.name: CommonEventSubscriberManager_0700
223  * @tc.desc: test InsertSubscriberRecordLocked function and record is nullptr.
224  * @tc.type: FUNC
225  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0700,Level1)226 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0700, Level1)
227 {
228     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0700 start";
229     CommonEventSubscriberManager commonEventSubscriberManager;
230     std::string event = "aa";
231     std::vector<std::string> events;
232     events.emplace_back(event);
233     SubscriberRecordPtr record = nullptr;
234     EXPECT_EQ(false, commonEventSubscriberManager.InsertSubscriberRecordLocked(events, record));
235     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0700 end";
236 }
237 
238 /**
239  * @tc.name: CommonEventSubscriberManager_0800
240  * @tc.desc: test InsertSubscriberRecordLocked function.
241  * @tc.type: FUNC
242  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0800,Level1)243 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0800, Level1)
244 {
245     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0800 start";
246     CommonEventSubscriberManager commonEventSubscriberManager;
247     std::string event = "aa";
248     std::vector<std::string> events;
249     events.emplace_back(event);
250     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
251     MatchingSkills matchingSkills_;
252     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
253     std::set<SubscriberRecordPtr> mults;
254     mults.insert(record);
255     commonEventSubscriberManager.eventSubscribers_.emplace(event, mults);
256     EXPECT_EQ(true, commonEventSubscriberManager.InsertSubscriberRecordLocked(events, record));
257     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0800 end";
258 }
259 
260 /**
261  * @tc.name: CommonEventSubscriberManager_0900
262  * @tc.desc: test RemoveSubscriberRecordLocked function and commonEventListener is nullptr.
263  * @tc.type: FUNC
264  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0900,Level1)265 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0900, Level1)
266 {
267     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0900 start";
268     CommonEventSubscriberManager commonEventSubscriberManager;
269     sptr<IRemoteObject> commonEventListener = nullptr;
270     EXPECT_EQ(ERR_INVALID_VALUE, commonEventSubscriberManager.RemoveSubscriberRecordLocked(commonEventListener));
271     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0900 end";
272 }
273 
274 /**
275  * @tc.name: CommonEventSubscriberManager_1000
276  * @tc.desc: test CheckSubscriberByUserId function.
277  * @tc.type: FUNC
278  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1000,Level1)279 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1000, Level1)
280 {
281     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1000 start";
282     CommonEventSubscriberManager commonEventSubscriberManager;
283     int32_t subscriberUserId = UNDEFINED_USER;
284     bool isSystemApp = true;
285     int32_t userId = UNDEFINED_USER;
286     EXPECT_EQ(true, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
287     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1000 end";
288 }
289 
290 /**
291  * @tc.name: CommonEventSubscriberManager_1100
292  * @tc.desc: test CheckSubscriberByUserId function.
293  * @tc.type: FUNC
294  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1100,Level1)295 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1100, Level1)
296 {
297     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1100 start";
298     CommonEventSubscriberManager commonEventSubscriberManager;
299     int32_t subscriberUserId = UNDEFINED_USER;
300     bool isSystemApp = true;
301     int32_t userId = ALL_USER;
302     EXPECT_EQ(true, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
303     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1100 end";
304 }
305 
306 /**
307  * @tc.name: CommonEventSubscriberManager_1200
308  * @tc.desc: test CheckSubscriberByUserId function.
309  * @tc.type: FUNC
310  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1200,Level1)311 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1200, Level1)
312 {
313     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1200 start";
314     CommonEventSubscriberManager commonEventSubscriberManager;
315     int32_t subscriberUserId = 100;
316     bool isSystemApp = false;
317     int32_t userId = 100;
318     EXPECT_EQ(true, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
319     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1200 end";
320 }
321 
322 /**
323  * @tc.name: CommonEventSubscriberManager_1300
324  * @tc.desc: test CheckSubscriberByUserId function.
325  * @tc.type: FUNC
326  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1300,Level1)327 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1300, Level1)
328 {
329     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1300 start";
330     CommonEventSubscriberManager commonEventSubscriberManager;
331     int32_t subscriberUserId = 100;
332     bool isSystemApp = true;
333     int32_t userId = 99;
334     EXPECT_EQ(false, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
335     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1300 end";
336 }
337 
338 /**
339  * @tc.name: CommonEventSubscriberManager_1400
340  * @tc.desc: test CheckSubscriberByUserId function.
341  * @tc.type: FUNC
342  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1400,Level1)343 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1400, Level1)
344 {
345     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1400 start";
346     CommonEventSubscriberManager commonEventSubscriberManager;
347     int32_t subscriberUserId = 98;
348     bool isSystemApp = true;
349     int32_t userId = 98;
350     EXPECT_EQ(true, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
351     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1400 end";
352 }
353 
354 /**
355  * @tc.name: CommonEventSubscriberManager_1500
356  * @tc.desc: test CheckSubscriberByUserId function.
357  * @tc.type: FUNC
358  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1500,Level1)359 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1500, Level1)
360 {
361     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1500 start";
362     CommonEventSubscriberManager commonEventSubscriberManager;
363     int32_t subscriberUserId = 101;
364     bool isSystemApp = false;
365     int32_t userId = 99;
366     EXPECT_EQ(false, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
367     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1500 end";
368 }
369 
370 /**
371  * @tc.name: CommonEventSubscriberManager_1600
372  * @tc.desc: test GetSubscriberRecordsByEvent function.
373  * @tc.type: FUNC
374  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1600,Level1)375 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1600, Level1)
376 {
377     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1600 start";
378     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
379         std::make_shared<CommonEventSubscriberManager>();
380     ASSERT_NE(nullptr, commonEventSubscriberManager);
381     std::string event = "";
382     int32_t userId = ALL_USER;
383     std::vector<SubscriberRecordPtr> records;
384     commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
385     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1600 end";
386 }
387 
388 /**
389  * @tc.name: CommonEventSubscriberManager_1700
390  * @tc.desc: test GetSubscriberRecordsByEvent function.
391  * @tc.type: FUNC
392  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1700,Level1)393 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1700, Level1)
394 {
395     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1700 start";
396     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
397         std::make_shared<CommonEventSubscriberManager>();
398     ASSERT_NE(nullptr, commonEventSubscriberManager);
399     std::string event = "";
400     int32_t userId = 100;
401     std::vector<SubscriberRecordPtr> records;
402     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
403     MatchingSkills matchingSkills_;
404     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
405     record->eventSubscribeInfo->SetUserId(userId);
406     commonEventSubscriberManager->subscribers_.emplace_back(record);
407     commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
408     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1700 end";
409 }
410 
411 /**
412  * @tc.name: CommonEventSubscriberManager_1800
413  * @tc.desc: test GetSubscriberRecordsByEvent function.
414  * @tc.type: FUNC
415  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1800,Level1)416 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1800, Level1)
417 {
418     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1800 start";
419     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
420         std::make_shared<CommonEventSubscriberManager>();
421     ASSERT_NE(nullptr, commonEventSubscriberManager);
422     std::string event = "";
423     int32_t userId = 100;
424     std::vector<SubscriberRecordPtr> records;
425     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
426     MatchingSkills matchingSkills_;
427     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
428     int32_t userIds = 90;
429     record->eventSubscribeInfo->SetUserId(userIds);
430     commonEventSubscriberManager->subscribers_.emplace_back(record);
431     commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
432     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1800 end";
433 }
434 
435 /**
436  * @tc.name: CommonEventSubscriberManager_1900
437  * @tc.desc: test GetSubscriberRecordsByEvent function.
438  * @tc.type: FUNC
439  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1900,Level1)440 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1900, Level1)
441 {
442     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1900 start";
443     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
444         std::make_shared<CommonEventSubscriberManager>();
445     ASSERT_NE(nullptr, commonEventSubscriberManager);
446     std::string event = "aa";
447     int32_t userId = 99;
448     std::vector<SubscriberRecordPtr> records;
449     commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
450     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1900 end";
451 }
452 
453 /**
454  * @tc.name: CommonEventSubscriberManager_2000
455  * @tc.desc: test GetSubscriberRecordsByEvent function.
456  * @tc.type: FUNC
457  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2000,Level1)458 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2000, Level1)
459 {
460     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2000 start";
461     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
462         std::make_shared<CommonEventSubscriberManager>();
463     ASSERT_NE(nullptr, commonEventSubscriberManager);
464     std::string event = "aa";
465     int32_t userId = 99;
466     std::vector<SubscriberRecordPtr> records;
467     std::set<SubscriberRecordPtr> sub;
468     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
469     sub.insert(record);
470     MatchingSkills matchingSkills_;
471     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
472     int32_t userIds = -1;
473     record->eventSubscribeInfo->SetUserId(userIds);
474     commonEventSubscriberManager->eventSubscribers_.emplace(event, sub);
475     commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
476     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2000 end";
477 }
478 
479 /**
480  * @tc.name: CommonEventSubscriberManager_2100
481  * @tc.desc: test GetSubscriberRecordsByEvent function.
482  * @tc.type: FUNC
483  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2100,Level1)484 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2100, Level1)
485 {
486     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2100 start";
487     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
488         std::make_shared<CommonEventSubscriberManager>();
489     ASSERT_NE(nullptr, commonEventSubscriberManager);
490     std::string event = "aa";
491     int32_t userId = 99;
492     std::vector<SubscriberRecordPtr> records;
493     std::set<SubscriberRecordPtr> sub;
494     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
495     sub.insert(record);
496     MatchingSkills matchingSkills_;
497     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
498     int32_t userIds = 101;
499     record->eventSubscribeInfo->SetUserId(userIds);
500     commonEventSubscriberManager->eventSubscribers_.emplace(event, sub);
501     commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
502     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2100 end";
503 }
504 
505 /**
506  * @tc.name: CommonEventSubscriberManager_2200
507  * @tc.desc: test RemoveFrozenEventsBySubscriber function.
508  * @tc.type: FUNC
509  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2200,Level1)510 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2200, Level1)
511 {
512     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2200 start";
513     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
514         std::make_shared<CommonEventSubscriberManager>();
515     ASSERT_NE(nullptr, commonEventSubscriberManager);
516     SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
517     uid_t uids = 1;
518     subscriberRecord->eventRecordInfo.uid = uids;
519     // set frozenEvents_
520     FrozenRecords frozenRecord;
521     commonEventSubscriberManager->frozenEvents_.emplace(uids, frozenRecord);
522     commonEventSubscriberManager->RemoveFrozenEventsBySubscriber(subscriberRecord);
523     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2200 end";
524 }
525 
526 /**
527  * @tc.name: CommonEventSubscriberManager_2300
528  * @tc.desc: test DumpState function when records.size()>0.
529  * @tc.type: FUNC
530  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2300,Level1)531 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2300, Level1)
532 {
533     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2300 start";
534     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
535         std::make_shared<CommonEventSubscriberManager>();
536     ASSERT_NE(nullptr, commonEventSubscriberManager);
537     std::string event = "";
538     int32_t userId = 100;
539     std::vector<SubscriberRecordPtr> records;
540     SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
541     MatchingSkills matchingSkills_;
542     record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
543     record->eventSubscribeInfo->SetUserId(userId);
544     commonEventSubscriberManager->subscribers_.emplace_back(record);
545     commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
546     EXPECT_EQ(1, records.size());
547     std::vector<std::string> state;
548     commonEventSubscriberManager->DumpState(event, userId, state);
549     commonEventSubscriberManager->UpdateAllFreezeInfos(true, 1);
550     commonEventSubscriberManager->UpdateAllFreezeInfos(false, 1);
551     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2300 end";
552 }
553 
554 /**
555  * @tc.name: CommonEventSubscriberManager_2400
556  * @tc.desc: test DumpState function when record is nullptr..
557  * @tc.type: FUNC
558  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2400,Level1)559 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2400, Level1)
560 {
561     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2400 start";
562     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
563         std::make_shared<CommonEventSubscriberManager>();
564     ASSERT_NE(nullptr, commonEventSubscriberManager);
565     SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
566     CommonEventRecord eventRecord;
567     commonEventSubscriberManager->InsertFrozenEvents(subscriberRecord, eventRecord);
568     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2400 end";
569 }
570 
571 /**
572  * @tc.name: CommonEventSubscriberManager_2500
573  * @tc.desc: test DumpState function when record not nullptr..
574  * @tc.type: FUNC
575  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2500,Level1)576 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2500, Level1)
577 {
578     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2500 start";
579     std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
580         std::make_shared<CommonEventSubscriberManager>();
581     ASSERT_NE(nullptr, commonEventSubscriberManager);
582     SubscriberRecordPtr subscriberRecord = nullptr;
583     CommonEventRecord eventRecord;
584     commonEventSubscriberManager->InsertFrozenEvents(subscriberRecord, eventRecord);
585     GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2500 end";
586 }
587 
588 /**
589  * @tc.name: CommonEventStickyManager_0100
590  * @tc.desc: test UpdateStickyEventLocked function.
591  * @tc.type: FUNC
592  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0100,Level1)593 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0100, Level1)
594 {
595     GTEST_LOG_(INFO) << "CommonEventStickyManager_0100 start";
596     CommonEventStickyManager commonEventStickyManager;
597     std::string event = "";
598     std::shared_ptr<CommonEventRecord> record = nullptr;
599     EXPECT_EQ(ERR_INVALID_VALUE, commonEventStickyManager.UpdateStickyEventLocked(event, record));
600     GTEST_LOG_(INFO) << "CommonEventStickyManager_0100 end";
601 }
602 
603 /**
604  * @tc.name: CommonEventStickyManager_0200
605  * @tc.desc: test UpdateStickyEventLocked function.
606  * @tc.type: FUNC
607  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0200,Level1)608 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0200, Level1)
609 {
610     GTEST_LOG_(INFO) << "CommonEventStickyManager_0200 start";
611     CommonEventStickyManager commonEventStickyManager;
612     std::string event = "aa";
613     std::shared_ptr<CommonEventRecord> record = nullptr;
614     EXPECT_EQ(ERR_INVALID_VALUE, commonEventStickyManager.UpdateStickyEventLocked(event, record));
615     GTEST_LOG_(INFO) << "CommonEventStickyManager_0200 end";
616 }
617 
618 /**
619  * @tc.name: CommonEventStickyManager_0300
620  * @tc.desc: test GetStickyCommonEventRecords function.
621  * @tc.type: FUNC
622  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0300,Level1)623 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0300, Level1)
624 {
625     GTEST_LOG_(INFO) << "CommonEventStickyManager_0300 start";
626     std::shared_ptr<CommonEventStickyManager> commonEventStickyManager =
627         std::make_shared<CommonEventStickyManager>();
628     ASSERT_NE(nullptr, commonEventStickyManager);
629     std::string event = "";
630     int32_t userId = ALL_USER;
631     std::vector<std::shared_ptr<CommonEventRecord>> records;
632     // set commonEventRecords_
633     std::shared_ptr<CommonEventRecord> comm = std::make_shared<CommonEventRecord>();
634     comm->userId = ALL_USER;
635     commonEventStickyManager->commonEventRecords_.emplace(event, comm);
636     commonEventStickyManager->GetStickyCommonEventRecords(event, userId, records);
637     GTEST_LOG_(INFO) << "CommonEventStickyManager_0300 end";
638 }
639 
640 /**
641  * @tc.name: CommonEventStickyManager_0400
642  * @tc.desc: test GetStickyCommonEventRecords function.
643  * @tc.type: FUNC
644  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0400,Level1)645 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0400, Level1)
646 {
647     GTEST_LOG_(INFO) << "CommonEventStickyManager_0400 start";
648     std::shared_ptr<CommonEventStickyManager> commonEventStickyManager =
649         std::make_shared<CommonEventStickyManager>();
650     ASSERT_NE(nullptr, commonEventStickyManager);
651     std::string event = "";
652     int32_t userId = 100;
653     std::vector<std::shared_ptr<CommonEventRecord>> records;
654     // set commonEventRecords_
655     std::shared_ptr<CommonEventRecord> comm = std::make_shared<CommonEventRecord>();
656     comm->userId = 101;
657     commonEventStickyManager->commonEventRecords_.emplace(event, comm);
658     commonEventStickyManager->GetStickyCommonEventRecords(event, userId, records);
659     GTEST_LOG_(INFO) << "CommonEventStickyManager_0400 end";
660 }
661 
662 /**
663  * @tc.name: CommonEventStickyManager_0500
664  * @tc.desc: test GetStickyCommonEventRecords function.
665  * @tc.type: FUNC
666  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0500,Level1)667 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0500, Level1)
668 {
669     GTEST_LOG_(INFO) << "CommonEventStickyManager_0500 start";
670     std::shared_ptr<CommonEventStickyManager> commonEventStickyManager =
671         std::make_shared<CommonEventStickyManager>();
672     ASSERT_NE(nullptr, commonEventStickyManager);
673     std::string event = "aa";
674     int32_t userId = 100;
675     std::vector<std::shared_ptr<CommonEventRecord>> records;
676     // set commonEventRecords_
677     std::shared_ptr<CommonEventRecord> comm = std::make_shared<CommonEventRecord>();
678     comm->userId = 101;
679     commonEventStickyManager->commonEventRecords_.emplace(event, comm);
680     commonEventStickyManager->GetStickyCommonEventRecords(event, userId, records);
681     GTEST_LOG_(INFO) << "CommonEventStickyManager_0500 end";
682 }
683 
684 /**
685  * @tc.name: CommonEventStickyManager_0600
686  * @tc.desc: test GetStickyCommonEventRecords function.
687  * @tc.type: FUNC
688  */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0600,Level1)689 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0600, Level1)
690 {
691     GTEST_LOG_(INFO) << "CommonEventStickyManager_0600 start";
692     std::shared_ptr<CommonEventStickyManager> commonEventStickyManager =
693         std::make_shared<CommonEventStickyManager>();
694     ASSERT_NE(nullptr, commonEventStickyManager);
695     std::string event = "aa";
696     int32_t userId = ALL_USER;
697     std::vector<std::shared_ptr<CommonEventRecord>> records;
698     // set commonEventRecords_
699     std::shared_ptr<CommonEventRecord> comm = std::make_shared<CommonEventRecord>();
700     comm->userId = ALL_USER;
701     commonEventStickyManager->commonEventRecords_.emplace(event, comm);
702     commonEventStickyManager->GetStickyCommonEventRecords(event, userId, records);
703     GTEST_LOG_(INFO) << "CommonEventStickyManager_0600 end";
704 }
705 
706