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 #include <string>
18
19 #include "common_event_listener.h"
20 #define private public
21 #define protected public
22 #include "bundle_manager_helper.h"
23 #include "common_event.h"
24 #include "common_event_constant.h"
25 #include "common_event_manager_service.h"
26 #include "common_event_manager.h"
27 #undef private
28 #undef protected
29 #include "common_event_subscriber.h"
30 #include "common_event_support.h"
31 #include "datetime_ex.h"
32 #include "event_log_wrapper.h"
33 #include "event_receive_stub.h"
34 #include "mock_bundle_manager.h"
35 #include "mock_ipc_skeleton.h"
36
37 using namespace testing::ext;
38
39 namespace OHOS {
40 namespace EventFwk {
41 namespace {
42 std::mutex mtx_;
43 const time_t TIME_OUT_SECONDS_LIMIT = 3;
44 static OHOS::sptr<OHOS::AppExecFwk::MockBundleMgrService> bundleObject = nullptr;
45 std::shared_ptr<EventHandler> handlerPtr;
46 constexpr int32_t ERR_OK = 0;
47 constexpr int32_t ERR_NOTIFICATION_CES_NOT_SA_SYSTEM_APP = 1500004;
48 } // namespace
49
50 class CommonEventServicesModuleTest : public CommonEventSubscriber {
51 public:
52 explicit CommonEventServicesModuleTest(const CommonEventSubscribeInfo &subscribeInfo);
~CommonEventServicesModuleTest()53 virtual ~CommonEventServicesModuleTest() {};
54 virtual void OnReceiveEvent(const CommonEventData &data);
55
56 public:
57 };
CommonEventServicesModuleTest(const CommonEventSubscribeInfo & subscribeInfo)58 CommonEventServicesModuleTest::CommonEventServicesModuleTest(const CommonEventSubscribeInfo &subscribeInfo)
59 : CommonEventSubscriber(subscribeInfo)
60 {}
61
OnReceiveEvent(const CommonEventData & data)62 void CommonEventServicesModuleTest::OnReceiveEvent(const CommonEventData &data)
63 {
64 printf("CommonEventServicesModuleTest OnReceiveEvent\n");
65 mtx_.unlock();
66 }
67
68 class cesModuleTest : public testing::Test {
69 public:
70 static void SetUpTestCase();
71 static void TearDownTestCase();
72 void SetUp();
73 void TearDown();
74 static sptr<CommonEventManagerService> commonEventManagerService_;
75 };
76
77 sptr<CommonEventManagerService> cesModuleTest::commonEventManagerService_ = nullptr;
78
SetUpTestCase()79 void cesModuleTest::SetUpTestCase()
80 {
81 commonEventManagerService_ = CommonEventManagerService::GetInstance();
82 commonEventManagerService_->Init();
83
84 bundleObject = new OHOS::AppExecFwk::MockBundleMgrService();
85 OHOS::DelayedSingleton<BundleManagerHelper>::GetInstance()->sptrBundleMgr_ =
86 OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(bundleObject);
87 }
88
TearDownTestCase()89 void cesModuleTest::TearDownTestCase()
90 {
91 if (commonEventManagerService_ != nullptr) {
92 if (commonEventManagerService_->innerCommonEventManager_ != nullptr) {
93 commonEventManagerService_->innerCommonEventManager_.reset();
94 commonEventManagerService_->innerCommonEventManager_ = nullptr;
95 }
96 if (commonEventManagerService_->commonEventSrvQueue_ != nullptr) {
97 commonEventManagerService_->commonEventSrvQueue_.reset();
98 commonEventManagerService_->commonEventSrvQueue_ = nullptr;
99 }
100 }
101 }
102
SetUp()103 void cesModuleTest::SetUp()
104 {}
105
TearDown()106 void cesModuleTest::TearDown()
107 {
108 commonEventManagerService_->innerCommonEventManager_->controlPtr_->orderedEventQueue_.clear();
109 }
110
111 /*
112 * @tc.number: CES_TC_ModuleTest_0100
113 * @tc.name: SubscribeCommonEvent
114 * @tc.desc: Verify the function when the input string is normal
115 */
116 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_0100, Function | MediumTest | Level1)
117 {
118 EVENT_LOGE("CES_TC_ModuleTest_0100 start");
119 std::string eventName = "SUBSCRIBEEVENT_MODULETEST";
120 MatchingSkills matchingSkills;
121 matchingSkills.AddEvent(eventName);
122 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
123 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
124 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
125
126 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
127 subscribeInfo, commonEventListener), ERR_OK);
128 EVENT_LOGE("CES_TC_ModuleTest_0100 end");
129 }
130
131 /*
132 * @tc.number: CES_TC_ModuleTest_0200
133 * @tc.name: SubscribeCommonEvent
134 * @tc.desc: Verify the function when the input string is number
135 */
136 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_0200, Function | MediumTest | Level1)
137 {
138 EVENT_LOGE("CES_TC_ModuleTest_0200 start");
139 std::string eventName = "1";
140 MatchingSkills matchingSkills;
141 matchingSkills.AddEvent(eventName);
142 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
143 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
144 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
145
146 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
147 subscribeInfo, commonEventListener), ERR_OK);
148 EVENT_LOGE("CES_TC_ModuleTest_0200 end");
149 }
150
151 /*
152 * @tc.number: CES_TC_ModuleTest_0300
153 * @tc.name: SubscribeCommonEvent
154 * @tc.desc: Verify the function when the input string is empty
155 */
156 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_0300, Function | MediumTest | Level1)
157 {
158 EVENT_LOGE("CES_TC_ModuleTest_0300 start");
159 std::string eventName = "";
160 MatchingSkills matchingSkills;
161 matchingSkills.AddEvent(eventName);
162 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
163 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
164 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
165
166 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
167 subscribeInfo, commonEventListener), ERR_OK);
168 EVENT_LOGE("CES_TC_ModuleTest_0300 end");
169 }
170
171 /*
172 * @tc.number: CES_TC_ModuleTest_0400
173 * @tc.name: SubscribeCommonEvent
174 * @tc.desc: Verify three subscribers subscribe to three different events
175 */
176 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_0400, Function | MediumTest | Level1)
177 {
178 EVENT_LOGE("CES_TC_ModuleTest_0400 start");
179 std::string eventName1 = "TESTEVENT1";
180 MatchingSkills matchingSkills1;
181 matchingSkills1.AddEvent(eventName1);
182 CommonEventSubscribeInfo subscribeInfo1(matchingSkills1);
183 auto subscriberPtr1 = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo1);
184 sptr<CommonEventListener> commonEventListener1 = new CommonEventListener(subscriberPtr1);
185 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
186 subscribeInfo1, commonEventListener1), ERR_OK);
187
188 std::string eventName2 = "TESTEVENT2";
189 MatchingSkills matchingSkills2;
190 matchingSkills2.AddEvent(eventName2);
191 CommonEventSubscribeInfo subscribeInfo2(matchingSkills2);
192 auto subscriberPtr2 = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo2);
193 sptr<CommonEventListener> commonEventListener2 = new CommonEventListener(subscriberPtr2);
194 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
195 subscribeInfo2, commonEventListener2), ERR_OK);
196
197 std::string eventName3 = "TESTEVENT3";
198 MatchingSkills matchingSkills3;
199 matchingSkills3.AddEvent(eventName3);
200 CommonEventSubscribeInfo subscribeInfo3(matchingSkills3);
201 auto subscriberPtr3 = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo3);
202 sptr<CommonEventListener> commonEventListener3 = new CommonEventListener(subscriberPtr3);
203 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
204 subscribeInfo3, commonEventListener3), ERR_OK);
205 EVENT_LOGE("CES_TC_ModuleTest_0400 end");
206 }
207
208 /*
209 * @tc.number: CES_TC_ModuleTest_0500
210 * @tc.name: SubscribeCommonEvent
211 * @tc.desc: Verify that the ordered common event was subsribered successfully
212 */
213 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_0500, Function | MediumTest | Level1)
214 {
215 EVENT_LOGE("CES_TC_ModuleTest_0500 start");
216 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_";
217 MatchingSkills matchingSkills;
218 matchingSkills.AddEvent(eventName);
219 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
220 subscribeInfo.SetPriority(100);
221 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
222 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
223
224 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
225 subscribeInfo, commonEventListener), ERR_OK);
226 EVENT_LOGE("CES_TC_ModuleTest_0500 end");
227 }
228
229 /*
230 * @tc.number: CES_TC_ModuleTest_0600
231 * @tc.name: SubscribeCommonEvent
232 * @tc.desc: Verify that the ordered common event was subsribered successfully
233 */
234 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_0600, Function | MediumTest | Level1)
235 {
236 EVENT_LOGE("CES_TC_ModuleTest_0600 start");
237 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_PRIORITY";
238 MatchingSkills matchingSkills;
239 matchingSkills.AddEvent(eventName);
240 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
241 subscribeInfo.SetPriority(1000);
242 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
243 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
244
245 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
246 subscribeInfo, commonEventListener), ERR_OK);
247 EVENT_LOGE("CES_TC_ModuleTest_0600 end");
248 }
249
250 /*
251 * @tc.number: CES_TC_ModuleTest_0700
252 * @tc.name: SubscribeCommonEvent
253 * @tc.desc: Verify that the ordered common event was subsribered successfully
254 */
255 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_0700, Function | MediumTest | Level1)
256 {
257 EVENT_LOGE("CES_TC_ModuleTest_0700 start");
258 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_DEVUCEID";
259 MatchingSkills matchingSkills;
260 matchingSkills.AddEvent(eventName);
261 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
262 subscribeInfo.SetDeviceId("module_test");
263 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
264 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
265
266 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
267 subscribeInfo, commonEventListener), ERR_OK);
268 EVENT_LOGE("CES_TC_ModuleTest_0700 end");
269 }
270
271 /*
272 * @tc.number: CES_TC_ModuleTest_0800
273 * @tc.name: CommonEventManagerService UnsubscribeCommonEvent
274 * @tc.desc: test Unsubscribe to common event and set the priority to 1000, Verify that ordered common event was
275 * unsubscribed successfully
276 */
277 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_0800, Function | MediumTest | Level1)
278 {
279 EVENT_LOGE("CES_TC_ModuleTest_0800 start");
280 std::string eventName = "UNSUBSCRIBEEVENT_MODULETEST_PRIORITY";
281 MatchingSkills matchingSkills;
282 matchingSkills.AddEvent(eventName);
283 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
284 subscribeInfo.SetPriority(1000);
285 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
286 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
287 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
288 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
289 EVENT_LOGE("CES_TC_ModuleTest_0800 end");
290 }
291
292 /*
293 * @tc.number: CES_TC_ModuleTest_0900
294 * @tc.name: CommonEventManagerService UnsubscribeCommonEvent
295 * @tc.desc: test Unsubscribe to common event and set the SetDeviceId to "module_test", Verify Unsubscribe CommonEvent
296 * success with setting DeviceId
297 */
298 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_0900, Function | MediumTest | Level1)
299 {
300 EVENT_LOGE("CES_TC_ModuleTest_0900 start");
301 std::string eventName = "UNSUBSCRIBEEVENT_MODULETEST_DEVUCEID";
302 MatchingSkills matchingSkills;
303 matchingSkills.AddEvent(eventName);
304 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
305 subscribeInfo.SetDeviceId("module_test");
306 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
307 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
308 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
309 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
310 EVENT_LOGE("CES_TC_ModuleTest_0900 end");
311 }
312
313 /*
314 * @tc.number: CES_TC_ModuleTest_1000
315 * @tc.name: CommonEventManagerService UnsubscribeCommonEvent
316 * @tc.desc: test UnsubscribeCommonEvent has priority, Verify that ordered common event was unsubscribed successfully
317 */
318 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1000, Function | MediumTest | Level1)
319 {
320 EVENT_LOGE("CES_TC_ModuleTest_1000 start");
321 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_";
322 MatchingSkills matchingSkills;
323 matchingSkills.AddEvent(eventName);
324 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
325 subscribeInfo.SetPriority(100);
326 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
327 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
328 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
329 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
330 EVENT_LOGE("CES_TC_ModuleTest_1000 end");
331 }
332
333 /*
334 * @tc.number: CES_TC_ModuleTest_1100
335 * @tc.name: CommonEventManagerService UnsubscribeCommonEvent
336 * @tc.desc: test UnsubscribeCommonEvent , Verify the common event was unsubscribed successfully
337 */
338 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1100, Function | MediumTest | Level1)
339 {
340 EVENT_LOGE("CES_TC_ModuleTest_1100 start");
341 std::string eventName = "UNSUBSCRIBEEVENT_MODELETEST";
342 MatchingSkills matchingSkills;
343 matchingSkills.AddEvent(eventName);
344 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
345 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
346 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
347 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
348 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
349 EVENT_LOGE("CES_TC_ModuleTest_1100 end");
350 }
351
352 /*
353 * @tc.number: CES_TC_ModuleTest_1200
354 * @tc.name: CommonEventManagerService UnsubscribeCommonEvent
355 * @tc.desc: test unsubscribe event when the input string is number
356 */
357 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1200, Function | MediumTest | Level1)
358 {
359 EVENT_LOGE("CES_TC_ModuleTest_1200 start");
360 std::string eventName = "2";
361 MatchingSkills matchingSkills;
362 matchingSkills.AddEvent(eventName);
363 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
364 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
365 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
366 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
367 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
368 EVENT_LOGE("CES_TC_ModuleTest_1200 end");
369 }
370
371 /*
372 * @tc.number: CES_TC_ModuleTest_1300
373 * @tc.name: CommonEventManagerService UnsubscribeCommonEvent
374 * @tc.desc: test unsubscribe event when the input string is empty
375 */
376 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1300, Function | MediumTest | Level1)
377 {
378 EVENT_LOGE("CES_TC_ModuleTest_1300 start");
379 std::string eventName = "";
380 MatchingSkills matchingSkills;
381 matchingSkills.AddEvent(eventName);
382 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
383 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
384 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
385 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
386 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
387 EVENT_LOGE("CES_TC_ModuleTest_1300 end");
388 }
389
390 /*
391 * @tc.number: CES_TC_ModuleTest_1400
392 * @tc.name: CommonEventManagerService UnsubscribeCommonEvent
393 * @tc.desc: test unsubscribe to three different events
394 */
395 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1400, Function | MediumTest | Level1)
396 {
397 EVENT_LOGE("CES_TC_ModuleTest_1400 start");
398 std::string eventName4 = "TESTEVENT4";
399 MatchingSkills matchingSkills4;
400 matchingSkills4.AddEvent(eventName4);
401 CommonEventSubscribeInfo subscribeInfo1(matchingSkills4);
402 auto subscriberPtr1 = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo1);
403 sptr<CommonEventListener> commonEventListener1 = new CommonEventListener(subscriberPtr1);
404 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo1, commonEventListener1), ERR_OK);
405 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener1), ERR_OK);
406
407 std::string eventName5 = "TESTEVENT5";
408 MatchingSkills matchingSkills5;
409 matchingSkills5.AddEvent(eventName5);
410 CommonEventSubscribeInfo subscribeInfo2(matchingSkills5);
411 auto subscriberPtr2 = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo2);
412 sptr<CommonEventListener> commonEventListener2 = new CommonEventListener(subscriberPtr2);
413 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo2, commonEventListener2), ERR_OK);
414 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener2), ERR_OK);
415
416 std::string eventName6 = "TESTEVENT6";
417 MatchingSkills matchingSkills6;
418 matchingSkills6.AddEvent(eventName6);
419 CommonEventSubscribeInfo subscribeInfo3(matchingSkills6);
420 auto subscriberPtr3 = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo3);
421 sptr<CommonEventListener> commonEventListener3 = new CommonEventListener(subscriberPtr3);
422 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo3, commonEventListener3), ERR_OK);
423 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener3), ERR_OK);
424
425 EVENT_LOGE("CES_TC_ModuleTest_1400 end");
426 }
427
428 /*
429 * @tc.number: CES_TC_ModuleTest_1500
430 * @tc.name: CommonEventManagerService PublishCommonEvent
431 * @tc.desc: Verify publish event successfully
432 */
433 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1500, Function | MediumTest | Level1)
434 {
435 EVENT_LOGE("CES_TC_ModuleTest_1500 start");
436 std::string eventName = "PUBLISHEVENT_MODULETEST_ACTION";
437 std::string eventAction = "PUBLISHEVENT_MODULETEST_ACTION";
438 bool stickty = false;
439 MatchingSkills matchingSkills;
440 matchingSkills.AddEvent(eventName);
441 Want testWant;
442 testWant.SetAction(eventAction);
443 CommonEventData commonEventData(testWant);
444 CommonEventPublishInfo publishInfo;
445 publishInfo.SetSticky(stickty);
446 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
447 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
448 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
449 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
450 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
451 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
452 EVENT_LOGE("CES_TC_ModuleTest_1500 end");
453 }
454
455 /*
456 * @tc.number: CES_TC_ModuleTest_1600
457 * @tc.name: CommonEventManagerService PublishCommonEvent
458 * @tc.desc: Verify publish event with entity successfully
459 */
460 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1600, Function | MediumTest | Level1)
461 {
462 EVENT_LOGE("CES_TC_ModuleTest_1600 start");
463 std::string eventName = "PUBLISHEVENT_MODULETEST_ACTION_ENTITY";
464 std::string eventAction = "PUBLISHEVENT_MODULETEST_ACTION_ENTITY";
465 std::string entity = "ADDENTITY";
466 bool stickty = false;
467 MatchingSkills matchingSkills;
468 matchingSkills.AddEvent(eventName);
469 Want testWant;
470 testWant.SetAction(eventAction);
471 testWant.AddEntity(entity);
472 CommonEventData commonEventData(testWant);
473 CommonEventPublishInfo publishInfo;
474 publishInfo.SetSticky(stickty);
475 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
476 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
477 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
478 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
479 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
480 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
481 EVENT_LOGE("CES_TC_ModuleTest_1600 end");
482 }
483
484 /*
485 * @tc.number: CES_TC_ModuleTest_1700
486 * @tc.name: CommonEventManagerService DumpState
487 * @tc.desc: Verify DumpState
488 */
489 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1700, Function | MediumTest | Level1)
490 {
491 EVENT_LOGE("CES_TC_ModuleTest_1700 start");
492 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_DUMPSTATE";
493 bool result = false;
494 std::vector<std::string> stateTest;
495 MatchingSkills matchingSkills;
496 matchingSkills.AddEvent(eventName);
497 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
498 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
499 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberPtr));
500 EXPECT_TRUE(CommonEvent::GetInstance()->DumpState(0, eventName, ALL_USER, stateTest));
501 if (stateTest.size() >= 1) {
502 result = true;
503 }
504 GTEST_LOG_(INFO) << " stateTest size = " << stateTest.size();
505 EXPECT_TRUE(result);
506 EVENT_LOGE("CES_TC_ModuleTest_1700 end");
507 }
508
509 /*
510 * @tc.number: CES_TC_ModuleTest_1800
511 * @tc.name: CommonEventManagerService DumpState
512 * @tc.desc: Verify DumpState another event that is not added
513 */
514 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1800, Function | MediumTest | Level2)
515 {
516 EVENT_LOGE("CES_TC_ModuleTest_1800 start");
517 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_DUMPSTATE_";
518 std::string eventNametest = "TEST_NAME";
519 bool result = false;
520 std::vector<std::string> stateTest;
521 MatchingSkills matchingSkills;
522 matchingSkills.AddEvent(eventName);
523 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
524 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
525 if (CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
526 if (CommonEvent::GetInstance()->DumpState(0, eventNametest, ALL_USER, stateTest)) {
527 if (stateTest.size() == 1) {
528 result = true;
529 } else {
530 EVENT_LOGE(" CES_TC_ModuleTest_1800 stateTest.size() is null ");
531 }
532 }
533 }
534
535 EXPECT_FALSE(result);
536 EVENT_LOGE("CES_TC_ModuleTest_1800 end");
537 }
538
539 /*
540 * @tc.number: CES_TC_ModuleTest_1900
541 * @tc.name: CommonEventManagerService DumpState
542 * @tc.desc: Verify DumpState with empty event
543 */
544 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_1900, Function | MediumTest | Level1)
545 {
546 EVENT_LOGE("CES_TC_ModuleTest_1900 start");
547 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_DUMPSTATE";
548 std::string eventNameInput = "";
549 bool result = false;
550 std::vector<std::string> stateTest;
551 MatchingSkills matchingSkills;
552 matchingSkills.AddEvent(eventName);
553 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
554 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
555 EXPECT_TRUE(CommonEventManager::SubscribeCommonEvent(subscriberPtr));
556 EXPECT_TRUE(CommonEvent::GetInstance()->DumpState(0, eventNameInput, ALL_USER, stateTest));
557 if (stateTest.size() >= 1) {
558 result = true;
559 }
560 GTEST_LOG_(INFO) << " stateTest size = " << stateTest.size();
561 EXPECT_TRUE(result);
562 EVENT_LOGE("CES_TC_ModuleTest_1900 end");
563 }
564
565 /*
566 * @tc.number: CES_TC_ModuleTest_2000
567 * @tc.name: OnReceiveEvent
568 * @tc.desc: Verify receive common event
569 */
570 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2000, Function | MediumTest | Level1)
571 {
572 EVENT_LOGE("CES_TC_ModuleTest_2000 start");
573 std::string eventName = "PUBLISHEVENT_MODULETEST_ACTION2000";
574 std::string eventAction = "PUBLISHEVENT_MODULETEST_ACTION2000";
575 MatchingSkills matchingSkills;
576 matchingSkills.AddEvent(eventName);
577 Want testWant;
578 testWant.SetAction(eventAction);
579 CommonEventData commonEventData(testWant);
580 CommonEventPublishInfo publishInfo;
581 publishInfo.SetOrdered(true);
582 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
583 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
584 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
585 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
586 mtx_.lock();
587 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
588 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
589 struct tm startTime = {0};
590 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
591 struct tm doingTime = {0};
592 int64_t seconds = 0;
593 while (!mtx_.try_lock()) {
594 // get current time and compare it with the start time
595 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
596 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
597 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
598 break;
599 }
600 }
601 // expect the subscriber could receive the event within 5 seconds.
602 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
603 mtx_.unlock();
604 EVENT_LOGE("CES_TC_ModuleTest_2000 end");
605 }
606
607 /*
608 * @tc.number: CES_TC_ModuleTest_2100
609 * @tc.name: OnReceiveEvent
610 * @tc.desc: Verify receive system event
611 */
612 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2100, Function | MediumTest | Level2)
613 {
614 EVENT_LOGE("CES_TC_ModuleTest_2100 start");
615 std::string eventName = "PUBLISHEVENT_MODULETEST_ACTION_ENTITY";
616 std::string eventAction = "PUBLISHEVENT_MODULETEST_ACTION_ENTITY";
617 bool resulttime = true;
618 std::string entity = "addEntity";
619 MatchingSkills matchingSkills;
620 matchingSkills.AddEvent(eventName);
621 Want testWant;
622 testWant.SetAction(eventAction);
623 testWant.AddEntity(entity);
624 CommonEventData commonEventData(testWant);
625 CommonEventPublishInfo publishInfo;
626 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
627 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
628 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
629 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
630 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
631 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
632 mtx_.lock();
633 struct tm startTime = {0};
634 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
635 struct tm doingTime = {0};
636 int64_t seconds = 0;
637 while (!mtx_.try_lock()) {
638 // get current time and compare it with the start time
639 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
640 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
641 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
642 resulttime = false;
643 break;
644 }
645 }
646 // The publisher sets the Entity, the receiver must set it, otherwise the receiver will not receive the information
647 EXPECT_FALSE(resulttime);
648 EVENT_LOGE("CES_TC_ModuleTest_2100 end");
649 mtx_.unlock();
650 }
651
652 /*
653 * @tc.number: CES_TC_ModuleTest_2200
654 * @tc.name: SubscribeCommonEvent
655 * @tc.desc: Verify Subscribe system event "COMMON_EVENT_ABILITY_ADDED"
656 */
657 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2200, Function | MediumTest | Level2)
658 {
659 EVENT_LOGE("CES_TC_ModuleTest_2200 start");
660 std::string eventName = CommonEventSupport::COMMON_EVENT_ABILITY_ADDED;
661 std::string eventAction = CommonEventSupport::COMMON_EVENT_ABILITY_ADDED;
662 bool result = false;
663 bool stickty = true;
664 MatchingSkills matchingSkills;
665 matchingSkills.AddEvent(eventName);
666 Want testWant;
667 testWant.SetAction(eventAction);
668 CommonEventData commonEventData(testWant);
669 CommonEventPublishInfo publishInfo;
670 publishInfo.SetSticky(stickty);
671 publishInfo.SetOrdered(true);
672 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
673 bundleObject->MockSetIsSystemApp(false);
674 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
675 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
676 if (commonEventManagerService_->SubscribeCommonEvent(
677 subscribeInfo, commonEventListener) &&
678 (commonEventManagerService_->PublishCommonEvent(
679 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER))) {
680 mtx_.lock();
681 }
682 struct tm startTime = {0};
683 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
684 struct tm doingTime = {0};
685 int64_t seconds = 0;
686 while (!mtx_.try_lock()) {
687 // get current time and compare it with the start time
688 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
689 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
690 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
691 result = false;
692 break;
693 }
694 }
695 // Unable to receive published system events, failed to send system events
696 EXPECT_FALSE(result);
697 commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener);
698 EVENT_LOGE("CES_TC_ModuleTest_2200 end");
699 mtx_.unlock();
700 }
701
702 /*
703 * @tc.number: CES_TC_ModuleTest_2300
704 * @tc.name: SubscribeCommonEvent
705 * @tc.desc: Subscribe system event "COMMON_EVENT_ABILITY_REMOVED"
706 */
707 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2300, Function | MediumTest | Level2)
708 {
709 EVENT_LOGE("CES_TC_ModuleTest_2300 start");
710 std::string eventName = CommonEventSupport::COMMON_EVENT_ABILITY_REMOVED;
711 std::string eventAction = CommonEventSupport::COMMON_EVENT_ABILITY_REMOVED;
712 bool result = false;
713 bool stickty = true;
714 MatchingSkills matchingSkills;
715 matchingSkills.AddEvent(eventName);
716 Want testWant;
717 testWant.SetAction(eventAction);
718 CommonEventData commonEventData(testWant);
719 CommonEventPublishInfo publishInfo;
720 publishInfo.SetSticky(stickty);
721 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
722 bundleObject->MockSetIsSystemApp(false);
723 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
724 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
725 if (commonEventManagerService_->SubscribeCommonEvent(
726 subscribeInfo, commonEventListener) &&
727 (commonEventManagerService_->PublishCommonEvent(
728 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER))) {
729 mtx_.lock();
730 }
731 struct tm startTime = {0};
732 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
733 struct tm doingTime = {0};
734 int64_t seconds = 0;
735 while (!mtx_.try_lock()) {
736 // get current time and compare it with the start time
737 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
738 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
739 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
740 result = false;
741 break;
742 }
743 }
744 // Unable to receive published system events, failed to send system events
745 EXPECT_FALSE(result);
746 EVENT_LOGE("CES_TC_ModuleTest_2300 end");
747 commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener);
748 mtx_.unlock();
749 }
750
751 /*
752 * @tc.number: CES_TC_ModuleTest_2400
753 * @tc.name: SubscribeCommonEvent
754 * @tc.desc: Subscribe system event "COMMON_EVENT_ABILITY_UPDATED"
755 */
756 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2400, Function | MediumTest | Level2)
757 {
758 EVENT_LOGE("CES_TC_ModuleTest_2400 start");
759 std::string eventName = CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED;
760 std::string eventAction = CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED;
761 bool result = false;
762 bool stickty = true;
763 MatchingSkills matchingSkills;
764 matchingSkills.AddEvent(eventName);
765 Want testWant;
766 testWant.SetAction(eventAction);
767 CommonEventData commonEventData(testWant);
768 CommonEventPublishInfo publishInfo;
769 publishInfo.SetSticky(stickty);
770 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
771 bundleObject->MockSetIsSystemApp(false);
772 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
773 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
774 if (commonEventManagerService_->SubscribeCommonEvent(
775 subscribeInfo, commonEventListener) &&
776 (commonEventManagerService_->PublishCommonEvent(
777 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER))) {
778 mtx_.lock();
779 }
780 struct tm startTime = {0};
781 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
782 struct tm doingTime = {0};
783 int64_t seconds = 0;
784 while (!mtx_.try_lock()) {
785 // get current time and compare it with the start time
786 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
787 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
788 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
789 result = false;
790 break;
791 }
792 }
793 // Unable to receive published system events, failed to send system events
794 EXPECT_FALSE(result);
795 EVENT_LOGE("CES_TC_ModuleTest_2400 end");
796 commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener);
797 mtx_.unlock();
798 }
799
800 /*
801 * @tc.number: CES_TC_ModuleTest_2500
802 * @tc.name: SubscribeCommonEvent
803 * @tc.desc: Subscribe custom event "TESTEVENT_MATCHEVENTTEST"
804 */
805 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2500, Function | MediumTest | Level1)
806 {
807 EVENT_LOGE("CES_TC_ModuleTest_2500 start");
808 std::string eventName = "TESTEVENT_MATCHEVENTTEST";
809
810 Want wantTest;
811 wantTest.SetAction(eventName);
812 MatchingSkills matchingSkills;
813 matchingSkills.AddEvent(eventName);
814 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
815 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
816 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
817 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
818 EXPECT_TRUE(matchingSkills.Match(wantTest));
819 EVENT_LOGE("CES_TC_ModuleTest_2500 end");
820 }
821
822 /*
823 * @tc.number: CES_TC_ModuleTest_2600
824 * @tc.name: PublishCommonEvent
825 * @tc.desc: Publish system event "COMMON_EVENT_ABILITY_ADDED"
826 */
827 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2600, Function | MediumTest | Level1)
828 {
829 EVENT_LOGE("CES_TC_ModuleTest_2600 start");
830 std::string eventName = CommonEventSupport::COMMON_EVENT_ABILITY_ADDED;
831 std::string eventAction = CommonEventSupport::COMMON_EVENT_ABILITY_ADDED;
832 bool stickty = true;
833 MatchingSkills matchingSkills;
834 matchingSkills.AddEvent(eventName);
835 Want testWant;
836 testWant.SetAction(eventAction);
837 CommonEventData commonEventData(testWant);
838 CommonEventPublishInfo publishInfo;
839 publishInfo.SetSticky(stickty);
840 publishInfo.SetOrdered(true);
841 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
842 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
843 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
844
845 bundleObject->MockSetIsSystemApp(true);
846 if (commonEventManagerService_->SubscribeCommonEvent(
847 subscribeInfo, commonEventListener) &&
848 (commonEventManagerService_->PublishCommonEvent(
849 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER))) {
850 mtx_.lock();
851 }
852 struct tm startTime = {0};
853 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
854 struct tm doingTime = {0};
855 int64_t seconds = 0;
856 while (!mtx_.try_lock()) {
857 // get current time and compare it with the start time
858 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
859 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
860 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
861 break;
862 }
863 }
864
865 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
866 EVENT_LOGE("CES_TC_ModuleTest_2600 end");
867 bundleObject->MockSetIsSystemApp(false);
868 commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener);
869 mtx_.unlock();
870 }
871
872 /*
873 * @tc.number: CES_TC_ModuleTest_2700
874 * @tc.name: PublishCommonEvent
875 * @tc.desc: Publish system event "COMMON_EVENT_ABILITY_UPDATED"
876 */
877 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2700, Function | MediumTest | Level1)
878 {
879 EVENT_LOGE("CES_TC_ModuleTest_2700 start");
880 std::string eventName = CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED;
881 std::string eventAction = CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED;
882 bool stickty = true;
883 MatchingSkills matchingSkills;
884 matchingSkills.AddEvent(eventName);
885 Want testWant;
886 testWant.SetAction(eventAction);
887 CommonEventData commonEventData(testWant);
888 CommonEventPublishInfo publishInfo;
889 publishInfo.SetSticky(stickty);
890 publishInfo.SetOrdered(true);
891 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
892 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
893 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
894
895 bundleObject->MockSetIsSystemApp(true);
896 if (commonEventManagerService_->SubscribeCommonEvent(
897 subscribeInfo, commonEventListener) &&
898 (commonEventManagerService_->PublishCommonEvent(
899 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER))) {
900 mtx_.lock();
901 }
902 struct tm startTime = {0};
903 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
904 struct tm doingTime = {0};
905 int64_t seconds = 0;
906 while (!mtx_.try_lock()) {
907 // get current time and compare it with the start time
908 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
909 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
910 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
911 break;
912 }
913 }
914
915 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
916 EVENT_LOGE("CES_TC_ModuleTest_2700 end");
917 bundleObject->MockSetIsSystemApp(false);
918 commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener);
919 mtx_.unlock();
920 }
921
922 /*
923 * @tc.number: CES_TC_ModuleTest_2800
924 * @tc.name: GetStickyCommonEvent
925 * @tc.desc: set sticky and get action, publish common event set sticky to true and verify the action of stickyData
926 */
927 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2800, Function | MediumTest | Level1)
928 {
929 EVENT_LOGE("CES_TC_ModuleTest_2800 start");
930 std::string eventName = "MODULETEST_ACTION_STICKY";
931 std::string eventAction = "MODULETEST_ACTION_STICKY";
932 std::string eventActionStr = "MODULETEST_ACTION_STICKYSTR";
933 bool stickty = true;
934 MatchingSkills matchingSkills;
935 matchingSkills.AddEvent(eventName);
936 Want testWant;
937 testWant.SetAction(eventAction);
938 CommonEventData commonEventData(testWant);
939 CommonEventPublishInfo publishInfo;
940 publishInfo.SetSticky(stickty);
941 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
942 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
943 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
944 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
945 EXPECT_TRUE(CommonEventManager::PublishCommonEvent(commonEventData, publishInfo));
946
947 CommonEventData stickyData;
948 commonEventManagerService_->GetStickyCommonEvent(eventAction, stickyData);
949 EXPECT_FALSE(eventActionStr == stickyData.GetWant().GetAction());
950 EVENT_LOGE("CES_TC_ModuleTest_2800 end");
951 }
952
953 /*
954 * @tc.number: CES_TC_ModuleTest_2900
955 * @tc.name: GetStickyCommonEvent
956 * @tc.desc: set sticky and get another action, publish common event set sticky to true and verify the action of
957 * stickyData
958 */
959 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_2900, Function | MediumTest | Level2)
960 {
961 EVENT_LOGE("CES_TC_ModuleTest_2900 start");
962 std::string eventName = "MODULETEST_ACTION_STICKY_FALSE";
963 std::string eventAction = "MODULETEST_ACTION_STICKY_FALSE";
964 std::string actionTest = "CHECKTESTACTION";
965 bool stickty = true;
966 MatchingSkills matchingSkills;
967 matchingSkills.AddEvent(eventName);
968 Want testWant;
969 testWant.SetAction(eventAction);
970 CommonEventData commonEventData(testWant);
971 CommonEventPublishInfo publishInfo;
972 publishInfo.SetSticky(stickty);
973 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
974 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
975 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
976 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
977 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
978 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
979
980 CommonEventData stickyData;
981 bool stickyResult =
982 commonEventManagerService_->GetStickyCommonEvent(eventAction, stickyData);
983 EXPECT_FALSE(stickyResult);
984 EVENT_LOGE("CES_TC_ModuleTest_2900 end");
985 }
986
987 /*
988 * @tc.number: CES_TC_ModuleTest_3000
989 * @tc.name: Set Permission
990 * @tc.desc: Set permission for common event subscribers and verify successfully subscribe to common events
991 */
992 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3000, Function | MediumTest | Level1)
993 {
994 EVENT_LOGE("CES_TC_ModuleTest_3000 start");
995 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_PERMISSION";
996 std::string permissin = "PERMISSION";
997 MatchingSkills matchingSkills;
998 matchingSkills.AddEvent(eventName);
999 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1000 subscribeInfo.SetPermission(permissin);
1001 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1002 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1003
1004 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
1005 subscribeInfo, commonEventListener), ERR_OK);
1006 EVENT_LOGE("CES_TC_ModuleTest_3000 end");
1007 }
1008
1009 /*
1010 * @tc.number: CES_TC_ModuleTest_3100
1011 * @tc.name: Set Permission and Priority
1012 * @tc.desc: Set permission and priority for common event subscribers and verify successfully subscribe to
1013 * common events
1014 */
1015 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3100, Function | MediumTest | Level1)
1016 {
1017 EVENT_LOGE("CES_TC_ModuleTest_3100 start");
1018 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_PERMISSION_PRIORITY";
1019 std::string permissin = "PERMISSION";
1020 MatchingSkills matchingSkills;
1021 matchingSkills.AddEvent(eventName);
1022 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1023 subscribeInfo.SetPermission(permissin);
1024 subscribeInfo.SetPriority(1);
1025 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1026 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1027
1028 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
1029 subscribeInfo, commonEventListener), ERR_OK);
1030 EVENT_LOGE("CES_TC_ModuleTest_3100 end");
1031 }
1032
1033 /*
1034 * @tc.number: CES_TC_ModuleTest_3200
1035 * @tc.name: Set Permission Priority and DeviceId
1036 * @tc.desc: Set permission and priority and DeviceId for common event subscribers and verify successfully
1037 * subscribe to common events
1038 */
1039 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3200, Function | MediumTest | Level1)
1040 {
1041 EVENT_LOGE("CES_TC_ModuleTest_3200 start");
1042 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_PERMISSION_PRIORITY_DEVICEDID";
1043 std::string permissin = "PERMISSION";
1044 std::string deviceId = "DEVICEDID";
1045 MatchingSkills matchingSkills;
1046 matchingSkills.AddEvent(eventName);
1047 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1048 subscribeInfo.SetPermission(permissin);
1049 subscribeInfo.SetPriority(1);
1050 subscribeInfo.SetDeviceId(deviceId);
1051 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1052 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1053
1054 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
1055 subscribeInfo, commonEventListener), ERR_OK);
1056 EVENT_LOGE("CES_TC_ModuleTest_3200 end");
1057 }
1058
1059 /*
1060 * @tc.number: CES_TC_ModuleTest_3300
1061 * @tc.name: Set Permission
1062 * @tc.desc: Set permission for common event subscribers and verify successfully Unsubscribe to common events
1063 */
1064 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3300, Function | MediumTest | Level1)
1065 {
1066 EVENT_LOGE("CES_TC_ModuleTest_3300 start");
1067 std::string eventName = "UNSUBSCRIBEEVENT_MODULETEST_PERMISSION";
1068 std::string permissin = "PERMISSION";
1069 MatchingSkills matchingSkills;
1070 matchingSkills.AddEvent(eventName);
1071 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1072 subscribeInfo.SetPermission(permissin);
1073 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1074 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1075 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1076 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
1077 EVENT_LOGE("CES_TC_ModuleTest_3300 end");
1078 }
1079
1080 /*
1081 * @tc.number: CES_TC_ModuleTest_3400
1082 * @tc.name: Set Permission and Priority
1083 * @tc.desc: Set permission and priority for common event subscribers and verify successfully Unsubscribe to
1084 * common events
1085 */
1086 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3400, Function | MediumTest | Level1)
1087 {
1088 EVENT_LOGE("CES_TC_ModuleTest_3400 start");
1089 std::string eventName = "UNSUBSCRIBEEVENT_MODULETEST_PERMISSION_PRIORITY";
1090 std::string permissin = "PERMISSION";
1091 MatchingSkills matchingSkills;
1092 matchingSkills.AddEvent(eventName);
1093 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1094 subscribeInfo.SetPermission(permissin);
1095 subscribeInfo.SetPriority(1);
1096 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1097 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1098 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1099 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
1100 EVENT_LOGE("CES_TC_ModuleTest_3400 end");
1101 }
1102
1103 /*
1104 * @tc.number: CES_TC_ModuleTest_3500
1105 * @tc.name: Set Permission Priority and DeviceId
1106 * @tc.desc: Set permission and priority and DeviceId for common event subscribers and verify successfully
1107 * Unsubscribe to common events
1108 */
1109 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3500, Function | MediumTest | Level1)
1110 {
1111 EVENT_LOGE("CES_TC_ModuleTest_3500 start");
1112 std::string eventName = "UNSUBSCRIBEEVENT_MODULETEST_PERMISSION_PRIORITY_DEVICEDID";
1113 std::string permissin = "PERMISSION";
1114 std::string deviceId = "DEVICEDID";
1115 MatchingSkills matchingSkills;
1116 matchingSkills.AddEvent(eventName);
1117 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1118 subscribeInfo.SetPermission(permissin);
1119 subscribeInfo.SetPriority(1);
1120 subscribeInfo.SetDeviceId(deviceId);
1121 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1122 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1123 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1124 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
1125 EVENT_LOGE("CES_TC_ModuleTest_3500 end");
1126 }
1127
1128 /*
1129 * @tc.number: CES_TC_ModuleTest_3600
1130 * @tc.name: Set Subscriber Permissions
1131 * @tc.desc: Set permission for common event subscribers and verify successfully publish common events
1132 */
1133 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3600, Function | MediumTest | Level1)
1134 {
1135 EVENT_LOGE("CES_TC_ModuleTest_3600 start");
1136 std::string eventName = "PUBLISHEVENT_MODULETEST_PERMISSION";
1137 std::string eventAction = "PUBLISHEVENT_MODULETEST_PERMISSION";
1138 std::string permissin = "PERMISSION";
1139 std::vector<std::string> permissions;
1140 permissions.emplace_back(permissin);
1141 MatchingSkills matchingSkills;
1142 matchingSkills.AddEvent(eventName);
1143 Want testWant;
1144 testWant.SetAction(eventAction);
1145 CommonEventData commonEventData(testWant);
1146 CommonEventPublishInfo publishInfo;
1147 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1148 publishInfo.SetSubscriberPermissions(permissions);
1149 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1150 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1151 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1152 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
1153 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
1154 EVENT_LOGE("CES_TC_ModuleTest_3600 end");
1155 }
1156
1157 /*
1158 * @tc.number: CES_TC_ModuleTest_3700
1159 * @tc.name: Set Subscriber Permissions
1160 * @tc.desc: Set permission for common event subscribers and verify successfully publish common events
1161 */
1162 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3700, Function | MediumTest | Level1)
1163 {
1164 EVENT_LOGE("CES_TC_ModuleTest_3700 start");
1165 std::string eventName = "PUBLISHEVENT_MODULETEST_PERMISSION";
1166 std::string eventAction = "PUBLISHEVENT_MODULETEST_PERMISSION";
1167 std::string permissin1 = "PERMISSION1";
1168 std::string permissin2 = "PERMISSION2";
1169 std::vector<std::string> permissions;
1170 permissions.emplace_back(permissin1);
1171 permissions.emplace_back(permissin2);
1172 MatchingSkills matchingSkills;
1173 matchingSkills.AddEvent(eventName);
1174 Want testWant;
1175 testWant.SetAction(eventAction);
1176 CommonEventData commonEventData(testWant);
1177 CommonEventPublishInfo publishInfo;
1178 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1179 publishInfo.SetSubscriberPermissions(permissions);
1180 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1181 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1182 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1183 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
1184 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
1185 EVENT_LOGE("CES_TC_ModuleTest_3700 end");
1186 }
1187
1188 /*
1189 * @tc.number: CES_TC_ModuleTest_3800
1190 * @tc.name: Set Subscriber Permissions
1191 * @tc.desc: Set permission for common event subscribers and verify successfully publish common events
1192 */
1193 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3800, Function | MediumTest | Level1)
1194 {
1195 EVENT_LOGE("CES_TC_ModuleTest_3800 start");
1196 std::string eventName = "PUBLISHEVENT_MODULETEST_PERMISSION";
1197 std::string eventAction = "PUBLISHEVENT_MODULETEST_PERMISSION";
1198 std::string permissin1 = "PERMISSION1";
1199 std::string permissin2 = "PERMISSION2";
1200 std::string permissin3 = "PERMISSION3";
1201 std::vector<std::string> permissions;
1202 permissions.emplace_back(permissin1);
1203 permissions.emplace_back(permissin2);
1204 permissions.emplace_back(permissin3);
1205 MatchingSkills matchingSkills;
1206 matchingSkills.AddEvent(eventName);
1207 Want testWant;
1208 testWant.SetAction(eventAction);
1209 CommonEventData commonEventData(testWant);
1210 CommonEventPublishInfo publishInfo;
1211 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1212 publishInfo.SetSubscriberPermissions(permissions);
1213 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1214 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1215 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1216 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
1217 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
1218 EVENT_LOGE("CES_TC_ModuleTest_3800 end");
1219 }
1220
1221 /*
1222 * @tc.number: CES_TC_ModuleTest_3900
1223 * @tc.name: Set ThreadMode
1224 * @tc.desc: Set ThreadMode for common event subscribers and verify successfully subscribe to common events
1225 */
1226 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_3900, Function | MediumTest | Level1)
1227 {
1228 EVENT_LOGE("CES_TC_ModuleTest_3900 start");
1229 std::string eventName = "SUBSCRIBEEVENT_MODULETEST_SETTHRERADMODE";
1230 MatchingSkills matchingSkills;
1231 matchingSkills.AddEvent(eventName);
1232 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1233 // set thread mode
1234 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER);
1235 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1236 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1237
1238 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
1239 subscribeInfo, commonEventListener), ERR_OK);
1240 EVENT_LOGE("CES_TC_ModuleTest_3900 end");
1241 }
1242
1243 /*
1244 * @tc.number: CES_TC_ModuleTest_4000
1245 * @tc.name: Set ThreadMode
1246 * @tc.desc: Set ThreadMode for common event subscribers and verify successfully Unsubscribe to common events
1247 */
1248 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_4000, Function | MediumTest | Level1)
1249 {
1250 EVENT_LOGE("CES_TC_ModuleTest_4000 start");
1251 std::string eventName = "UNSUBSCRIBEEVENT_MODULETEST_SETTHRERADMODE";
1252 MatchingSkills matchingSkills;
1253 matchingSkills.AddEvent(eventName);
1254 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1255 // set thread mode
1256 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER);
1257 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1258 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1259 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1260 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(commonEventListener), ERR_OK);
1261 EVENT_LOGE("CES_TC_ModuleTest_4000 end");
1262 }
1263
1264 /*
1265 * @tc.number: CES_TC_ModuleTest_4100
1266 * @tc.name: Set ThreadMode
1267 * @tc.desc: Set ThreadMode for common event subscribers and verify successfully publish to common events
1268 */
1269 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_4100, Function | MediumTest | Level1)
1270 {
1271 EVENT_LOGE("CES_TC_ModuleTest_4100 start");
1272 std::string eventName = "PUBLISHEVENT_MODULETEST_SETTHRERADMODE";
1273 std::string eventAction = "PUBLISHEVENT_MODULETEST_SETTHRERADMODE";
1274 MatchingSkills matchingSkills;
1275 matchingSkills.AddEvent(eventName);
1276 Want testWant;
1277 testWant.SetAction(eventAction);
1278 CommonEventData commonEventData(testWant);
1279 CommonEventPublishInfo publishInfo;
1280 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1281 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER);
1282 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1283 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1284 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1285 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
1286 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
1287 EVENT_LOGE("CES_TC_ModuleTest_4100 end");
1288 }
1289
1290 /*
1291 * @tc.number: CES_TC_ModuleTest_4200
1292 * @tc.name: Set ThreadMode
1293 * @tc.desc: Set ThreadMode for common event subscribers and verify successfully receive to common events
1294 */
1295 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_4200, Function | MediumTest | Level1)
1296 {
1297 EVENT_LOGE("CES_TC_ModuleTest_4200 start");
1298 std::string eventName = "PUBLISHEVENT_MODULETEST_SETTHRERADMODE_R";
1299 std::string eventAction = "PUBLISHEVENT_MODULETEST_SETTHRERADMODE_R";
1300 MatchingSkills matchingSkills;
1301 matchingSkills.AddEvent(eventName);
1302 Want testWant;
1303 testWant.SetAction(eventAction);
1304 CommonEventData commonEventData(testWant);
1305 CommonEventPublishInfo publishInfo;
1306 publishInfo.SetOrdered(true);
1307 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1308 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1309 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1310 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1311 mtx_.lock();
1312 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
1313 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
1314 struct tm startTime = {0};
1315 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1316 struct tm doingTime = {0};
1317 int64_t seconds = 0;
1318 while (!mtx_.try_lock()) {
1319 // get current time and compare it with the start time
1320 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1321 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1322 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
1323 break;
1324 }
1325 }
1326 // expect the subscriber could receive the event within 5 seconds.
1327 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
1328 mtx_.unlock();
1329 EVENT_LOGE("CES_TC_ModuleTest_4200 end");
1330 }
1331
1332 /**
1333 * @tc.name: CES_TC_ModuleTest_4300
1334 * @tc.desc: subscriber exceed maximum.
1335 * @tc.type: FUNC
1336 * @tc.require: I582Y4
1337 */
1338 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_4300, Function | MediumTest | Level1)
1339 {
1340 EVENT_LOGE("CES_TC_ModuleTest_4300 start");
1341
1342 // add maximum subscriber, the last subscriber trigger "SUBSCRIBER_EXCEED_MAXIMUM" hisysevent
1343 std::set<sptr<CommonEventListener>> cesListenerSet;
1344
1345 for (uint32_t i = 0; i <= MAX_SUBSCRIBER_NUM_PER_EVENT + 1; i++) {
1346 std::string eventName = "SUBSCRIBEEVENT_SUBSCRIBENUMTEST";
1347 MatchingSkills matchingSkills;
1348 matchingSkills.AddEvent(eventName);
1349 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1350 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1351 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1352 cesListenerSet.insert(commonEventListener);
1353 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(
1354 subscribeInfo, commonEventListener), ERR_OK);
1355 }
1356
1357 for (auto it = cesListenerSet.begin(); it != cesListenerSet.end(); ++it) {
1358 EXPECT_EQ(commonEventManagerService_->UnsubscribeCommonEvent(*it), ERR_OK);
1359 }
1360
1361 EVENT_LOGE("CES_TC_ModuleTest_4300 end");
1362 }
1363
1364 /*
1365 * @tc.number: CES_TC_ModuleTest_4400
1366 * @tc.name: DLP App publish common event
1367 * @tc.desc: DLP App publish common event failed.
1368 * @tc.require: I582VA
1369 */
1370 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_4400, Function | MediumTest | Level1)
1371 {
1372 EVENT_LOGE("CES_TC_ModuleTest_4400 start");
1373 std::string eventName = "PUBLISHEVENT_MODULETEST_ACTION";
1374 std::string eventAction = "PUBLISHEVENT_MODULETEST_ACTION";
1375 bool sticky = false;
1376 MatchingSkills matchingSkills;
1377 matchingSkills.AddEvent(eventName);
1378 Want testWant;
1379 testWant.SetAction(eventAction);
1380 CommonEventData commonEventData(testWant);
1381 CommonEventPublishInfo publishInfo;
1382 publishInfo.SetSticky(sticky);
1383 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1384 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1385 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1386 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1387 IPCSkeleton::SetCallingTokenID(1);
1388 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
1389 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_NOTIFICATION_CES_NOT_SA_SYSTEM_APP);
1390 IPCSkeleton::SetCallingTokenID(0);
1391 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
1392 commonEventData, publishInfo, commonEventListener, UNDEFINED_USER), ERR_OK);
1393 EVENT_LOGE("CES_TC_ModuleTest_4400 end");
1394 }
1395
1396 /*
1397 * @tc.number: CES_TC_ModuleTest_4500
1398 * @tc.name: OnReceiveEvent
1399 * @tc.desc: Verify receive common event
1400 */
1401 HWTEST_F(cesModuleTest, CES_TC_ModuleTest_4500, Function | MediumTest | Level1)
1402 {
1403 EVENT_LOGE("CES_TC_ModuleTest_4500 start");
1404 std::string eventName = "PUBLISHEVENT_MODULETEST_ACTION4500";
1405 MatchingSkills matchingSkills;
1406 matchingSkills.AddEvent(eventName);
1407 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
1408 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::COMMON);
1409 auto subscriberPtr = std::make_shared<CommonEventServicesModuleTest>(subscribeInfo);
1410 sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriberPtr);
1411 EXPECT_EQ(commonEventManagerService_->SubscribeCommonEvent(subscribeInfo, commonEventListener), ERR_OK);
1412 mtx_.lock();
1413
1414 Want testWant;
1415 testWant.SetAction(eventName);
1416 CommonEventData commonEventData(testWant);
1417 CommonEventPublishInfo publishInfo;
1418 EXPECT_EQ(commonEventManagerService_->PublishCommonEvent(
1419 commonEventData, publishInfo, nullptr, UNDEFINED_USER), ERR_OK);
1420 struct tm startTime = {0};
1421 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
1422 struct tm doingTime = {0};
1423 int64_t seconds = 0;
1424 while (!mtx_.try_lock()) {
1425 // get current time and compare it with the start time
1426 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
1427 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
1428 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
1429 break;
1430 }
1431 }
1432 // expect the subscriber could receive the event within 5 seconds.
1433 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
1434 mtx_.unlock();
1435 EVENT_LOGE("CES_TC_ModuleTest_4500 end");
1436 }
1437 } // namespace EventFwk
1438 } // namespace OHOS
1439