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
18 // redefine private and protected since testcase need to invoke and test private function
19 #define private public
20 #define protected public
21 #include "bundle_manager_helper.h"
22 #include "common_event_control_manager.h"
23 #include "inner_common_event_manager.h"
24 #undef private
25 #undef protected
26
27 #include "common_event_listener.h"
28 #include "common_event_subscriber.h"
29 #include "mock_bundle_manager.h"
30
31 using namespace testing::ext;
32 using namespace OHOS::EventFwk;
33 using namespace OHOS::AppExecFwk;
34
35 namespace {
36 constexpr uint8_t PID = 0;
37 constexpr uint16_t SYSTEM_UID = 1000;
38 const std::string EVENT = "com.ces.test.event";
39 const std::string ENTITY = "com.ces.test.entity";
40 const std::string ACTION = "acion";
41 std::mutex mtx;
42 static OHOS::sptr<OHOS::IRemoteObject> bundleObject = nullptr;
43 InnerCommonEventManager innerCommonEventManager;
44 std::shared_ptr<EventHandler> handler_;
45 } // namespace
46
47 class CommonEventPublishOrderedEventUnitTest : public testing::Test {
48 public:
CommonEventPublishOrderedEventUnitTest()49 CommonEventPublishOrderedEventUnitTest()
50 {}
~CommonEventPublishOrderedEventUnitTest()51 ~CommonEventPublishOrderedEventUnitTest()
52 {}
53 static void SetUpTestCase(void);
54 static void TearDownTestCase(void);
55 void SetUp();
56 void TearDown();
57 static std::shared_ptr<CommonEventControlManager> commonEventControlManager;
58 };
59
60 std::shared_ptr<CommonEventControlManager>
61 CommonEventPublishOrderedEventUnitTest::commonEventControlManager = nullptr;
62
63 class SubscriberTest : public CommonEventSubscriber {
64 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)65 explicit SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
66 {}
67
~SubscriberTest()68 ~SubscriberTest()
69 {}
70
OnReceiveEvent(const CommonEventData & data)71 virtual void OnReceiveEvent(const CommonEventData &data)
72 {
73 GTEST_LOG_(INFO) << "OnReceiveEvent receive";
74 mtx.unlock();
75 }
76 };
77
SetUpTestCase(void)78 void CommonEventPublishOrderedEventUnitTest::SetUpTestCase(void)
79 {
80 handler_ = std::make_shared<EventHandler>(EventRunner::Create(true));
81 auto task = []() {
82 EventRunner::GetMainEventRunner()->Run();
83 };
84 handler_->PostTask(task);
85
86 bundleObject = new MockBundleMgrService();
87 OHOS::DelayedSingleton<BundleManagerHelper>::GetInstance()->sptrBundleMgr_ =
88 OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(bundleObject);
89 commonEventControlManager = std::make_shared<CommonEventControlManager>();
90 }
91
TearDownTestCase(void)92 void CommonEventPublishOrderedEventUnitTest::TearDownTestCase(void)
93 {
94 EventRunner::GetMainEventRunner()->Stop();
95 if (commonEventControlManager != nullptr) {
96 if (commonEventControlManager->orderedQueue_ != nullptr) {
97 commonEventControlManager->orderedQueue_.reset();
98 }
99 if (commonEventControlManager->unorderedQueue_ != nullptr) {
100 commonEventControlManager->unorderedQueue_.reset();
101 }
102 if (commonEventControlManager->unorderedImmediateQueue_ != nullptr) {
103 commonEventControlManager->unorderedImmediateQueue_.reset();
104 }
105 }
106
107 if (innerCommonEventManager.controlPtr_ != nullptr) {
108 innerCommonEventManager.controlPtr_.reset();
109 }
110 if (innerCommonEventManager.staticSubscriberManager_ != nullptr) {
111 innerCommonEventManager.staticSubscriberManager_.reset();
112 }
113 }
114
SetUp(void)115 void CommonEventPublishOrderedEventUnitTest::SetUp(void)
116 {}
117
TearDown(void)118 void CommonEventPublishOrderedEventUnitTest::TearDown(void)
119 {}
120
121 /*
122 * @tc.number: CommonEventPublishOrderedUnitTest_0700
123 * @tc.name: test PublishCommonEvent
124 * @tc.desc: Verify InnerCommonEventManager PublishCommonEvent success
125 */
126 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_0700, Function | MediumTest | Level1)
127 {
128 // make a want
129 Want want;
130 want.SetAction(ACTION);
131
132 // make common event data
133 CommonEventData data;
134 data.SetWant(want);
135
136 // make publish info
137 CommonEventPublishInfo publishInfo;
138 publishInfo.SetOrdered(true);
139
140 MatchingSkills matchingSkills;
141
142 // make subscriber info
143 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
144
145 // make a subscriber object
146 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
147
148 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
149
150 OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
151
152 mtx.lock();
153
154 struct tm curTime {
155 0
156 };
157 // publish ordered event
158 bool result = innerCommonEventManager.PublishCommonEvent(
159 data, publishInfo, commonEventListener, curTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "bundlename");
160
161 EXPECT_TRUE(result);
162
163 int count = 0;
164 while (!mtx.try_lock()) {
165 if (count == 0) {
166 GTEST_LOG_(INFO) << "Wait OnReceive callback function process";
167 count = 1;
168 } else {
169 usleep(100 * 1000);
170 }
171 }
172
173 mtx.unlock();
174 GTEST_LOG_(INFO) << "Testcase finished";
175 }
176
177 /*
178 * @tc.number: CommonEventPublishOrderedUnitTest_0800
179 * @tc.name: test PublishCommonEvent
180 * @tc.desc: Verify InnerCommonEventManager PublishCommonEvent fail because data has no aciton
181 */
182 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_0800, Function | MediumTest | Level1)
183 {
184 // make common event data
185 CommonEventData data;
186
187 // make publish info
188 CommonEventPublishInfo publishInfo;
189 publishInfo.SetOrdered(true);
190
191 MatchingSkills matchingSkills;
192
193 // make subscriber info
194 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
195
196 // make a subscriber object
197 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
198
199 // make commonEventListener
200 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
201
202 struct tm curTime {
203 0
204 };
205 OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
206 // publish ordered event
207 bool result = innerCommonEventManager.PublishCommonEvent(
208 data, publishInfo, commonEventListener, curTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "bundlename");
209 EXPECT_FALSE(result);
210 }
211
212 /*
213 * @tc.number: CommonEventPublishOrderedUnitTest_0900
214 * @tc.name: test PublishCommonEvent
215 * @tc.desc: Verify CommonEventControlManager PublishCommonEvent success
216 */
217 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_0900, Function | MediumTest | Level1)
218 {
219 // make a commonEventRecord
220 Want want;
221 want.SetAction(ACTION);
222
223 CommonEventData eventRef;
224 eventRef.SetWant(want);
225
226 CommonEventPublishInfo publishinfoRef;
227
228 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>(eventRef);
229 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>(publishinfoRef);
230 publishInfo->SetOrdered(true);
231
232 CommonEventRecord commonEventRecord;
233 commonEventRecord.commonEventData = commonEventData;
234 commonEventRecord.publishInfo = publishInfo;
235 commonEventRecord.eventRecordInfo.pid = 0;
236 commonEventRecord.eventRecordInfo.uid = 0;
237 commonEventRecord.eventRecordInfo.bundleName = "bundleName";
238 commonEventRecord.recordTime = {0};
239
240 // make subscriber info
241 MatchingSkills matchingSkills;
242 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
243
244 // make subscriber
245 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
246
247 // make common event listener
248 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
249 OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
250
251 mtx.lock();
252
253 bool result = false;
254 result = commonEventControlManager->PublishCommonEvent(commonEventRecord, commonEventListenerPtr);
255 EXPECT_TRUE(result);
256
257 int count = 0;
258 while (!mtx.try_lock()) {
259 if (count == 0) {
260 GTEST_LOG_(INFO) << "Wait OnReceive callback function process";
261 count = 1;
262 } else {
263 usleep(100 * 1000);
264 }
265 }
266 mtx.unlock();
267 GTEST_LOG_(INFO) << "Testcase finished";
268 }
269
270 /*
271 * @tc.number: CommonEventPublishOrderedUnitTest_1000
272 * @tc.name: test ProcessOrderedEvent
273 * @tc.desc: Verify CommonEventControlManager ProcessOrderedEvent success
274 */
275 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1000, Function | MediumTest | Level1)
276 {
277 // make common event record
278 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
279 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
280 publishInfo->SetOrdered(true);
281
282 CommonEventRecord commonEventRecord;
283 commonEventRecord.commonEventData = commonEventData;
284 commonEventRecord.publishInfo = publishInfo;
285 commonEventRecord.eventRecordInfo.pid = 0;
286 commonEventRecord.eventRecordInfo.uid = 0;
287 commonEventRecord.eventRecordInfo.bundleName = "bundleName";
288
289 // make subscriber info
290 MatchingSkills matchingSkills;
291 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
292
293 // make subscriber
294 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
295
296 // make common event listener
297 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
298 OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
299 mtx.lock();
300 bool result = commonEventControlManager->ProcessOrderedEvent(commonEventRecord, commonEventListenerPtr);
301 EXPECT_TRUE(result);
302
303 int count = 0;
304 while (!mtx.try_lock()) {
305 if (count == 0) {
306 GTEST_LOG_(INFO) << "Wait OnReceive callback function process";
307 count = 1;
308 } else {
309 usleep(100 * 1000);
310 }
311 }
312 mtx.unlock();
313 GTEST_LOG_(INFO) << "Testcase finished";
314 }
315
316 /*
317 * @tc.number: CommonEventPublishOrderedUnitTest_1100
318 * @tc.name: test EnqueueOrderedRecord eventRecordPtr is null
319 * @tc.desc: Verify EnqueueOrderedRecord eventRecordPtr is null orderedEventQueue_ size is 0
320 */
321 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1100, Function | MediumTest | Level1)
322 {
323 bool result = commonEventControlManager->EnqueueOrderedRecord(nullptr);
324 EXPECT_FALSE(result);
325 }
326
327 /*
328 * @tc.number: CommonEventPublishOrderedUnitTest_1200
329 * @tc.name: test EnqueueOrderedRecord
330 * @tc.desc: Verify EnqueueOrderedRecord success
331 */
332 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1200, Function | MediumTest | Level1)
333 {
334 // make common event record
335 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
336 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
337
338 // make ordered event record
339 std::shared_ptr<OrderedEventRecord> eventRecord = std::make_shared<OrderedEventRecord>();
340 eventRecord->commonEventData = commonEventData;
341 eventRecord->publishInfo = publishInfo;
342 eventRecord->resultTo = nullptr;
343 eventRecord->state = OrderedEventRecord::IDLE;
344 eventRecord->nextReceiver = 0;
345
346 bool result = commonEventControlManager->EnqueueOrderedRecord(eventRecord);
347 EXPECT_TRUE(result);
348 }
349
350 /*
351 * @tc.number: CommonEventPublishOrderedUnitTest_1300
352 * @tc.name: test ScheduleOrderedCommonEvent
353 * @tc.desc: Verify ScheduleOrderedCommonEvent success when scheduled is true
354 */
355 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1300, Function | MediumTest | Level1)
356 {
357 commonEventControlManager->scheduled_ = true;
358 bool result = commonEventControlManager->ScheduleOrderedCommonEvent();
359 EXPECT_TRUE(result);
360 }
361
362 /*
363 * @tc.number: CommonEventPublishOrderedUnitTest_1400
364 * @tc.name: test ScheduleOrderedCommonEvent
365 * @tc.desc: Verify ScheduleOrderedCommonEvent success when scheduled is false
366 */
367 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1400, Function | MediumTest | Level1)
368 {
369 commonEventControlManager->scheduled_ = false;
370
371 bool result = commonEventControlManager->ScheduleOrderedCommonEvent();
372 EXPECT_TRUE(result);
373 GTEST_LOG_(INFO) << "Testcase finished";
374 }
375
376 /*
377 * @tc.number: CommonEventPublishOrderedUnitTest_1500
378 * @tc.name: test FinishReceiver
379 * @tc.desc: Verify FinishReceiver return true because eventRecord state is received
380 */
381 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1500, Function | MediumTest | Level1)
382 {
383 // make common event record
384 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
385 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
386
387 // make ordered event record
388 std::shared_ptr<OrderedEventRecord> eventRecord = std::make_shared<OrderedEventRecord>();
389 eventRecord->commonEventData = commonEventData;
390 eventRecord->publishInfo = publishInfo;
391 eventRecord->resultTo = nullptr;
392 eventRecord->state = OrderedEventRecord::RECEIVED;
393 eventRecord->nextReceiver = 0;
394
395 std::string receiverData = "receiverData";
396 bool result = commonEventControlManager->FinishReceiver(eventRecord, 0, receiverData, false);
397 EXPECT_TRUE(result);
398 }
399
400 /*
401 * @tc.number: CommonEventPublishOrderedUnitTest_1600
402 * @tc.name: test FinishReceiver
403 * @tc.desc: Verify FinishReceiver return false eventRecord state is idle
404 */
405 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1600, Function | MediumTest | Level1)
406 {
407 // make common event record
408 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
409 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
410
411 // make ordered event record
412 std::shared_ptr<OrderedEventRecord> eventRecord = std::make_shared<OrderedEventRecord>();
413 eventRecord->commonEventData = commonEventData;
414 eventRecord->publishInfo = publishInfo;
415 eventRecord->resultTo = nullptr;
416 eventRecord->state = OrderedEventRecord::IDLE;
417 eventRecord->nextReceiver = 0;
418
419 std::string receiverData = "receiverData";
420 bool result = commonEventControlManager->FinishReceiver(eventRecord, 0, receiverData, false);
421 EXPECT_FALSE(result);
422 }
423
424 /*
425 * @tc.number: CommonEventPublishOrderedUnitTest_1700
426 * @tc.name: test FinishReceiver recordPtr is null
427 * @tc.desc: Verify FinishReceiver recordPtr is null return false
428 */
429 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1700, Function | MediumTest | Level1)
430 {
431 std::string receiverData = "receiverData";
432 bool result = commonEventControlManager->FinishReceiver(nullptr, 0, receiverData, false);
433 EXPECT_FALSE(result);
434 }
435
436 /*
437 * @tc.number: CommonEventPublishOrderedUnitTest_1800
438 * @tc.name: test GetOrderedEventHandler handlerOrdered_ is not null
439 * @tc.desc: Verify GetOrderedEventHandler handlerOrdered_ is not return true
440 */
441 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1800, Function | MediumTest | Level1)
442 {
443 commonEventControlManager->handlerOrdered_ =
444 std::make_shared<OrderedEventHandler>(EventRunner::Create(), commonEventControlManager);
445 bool result = commonEventControlManager->GetOrderedEventHandler();
446 EXPECT_TRUE(result);
447 }
448
449 /*
450 * @tc.number: CommonEventPublishOrderedUnitTest_1900
451 * @tc.name: test CurrentOrderedEventTimeout orderedEventQueue_ is null
452 * @tc.desc: Verify CurrentOrderedEventTimeout fail because orderedEventQueue_ is null
453 */
454 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_1900, Function | MediumTest | Level1)
455 {
456 commonEventControlManager->orderedEventQueue_.clear();
457 commonEventControlManager->CurrentOrderedEventTimeout(true);
458
459 bool result = false;
460 if (commonEventControlManager->orderedEventQueue_.size() == 0) {
461 result = true;
462 }
463 EXPECT_TRUE(result);
464 commonEventControlManager->orderedEventQueue_.clear();
465 }
466
467 /*
468 * @tc.number: CommonEventPublishOrderedUnitTest_2000
469 * @tc.name: test CurrentOrderedEventTimeout
470 * @tc.desc: Verify CurrentOrderedEventTimeout success with eventRecord->nextReceiver = 0;
471 */
472 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_2000, Function | MediumTest | Level1)
473 {
474 // make common event record
475 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
476 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
477
478 // make ordered event record
479 std::shared_ptr<OrderedEventRecord> eventRecord = std::make_shared<OrderedEventRecord>();
480 eventRecord->commonEventData = commonEventData;
481 eventRecord->publishInfo = publishInfo;
482 eventRecord->resultTo = nullptr;
483 eventRecord->state = OrderedEventRecord::IDLE;
484 eventRecord->nextReceiver = 0;
485
486 // enqueue ordered record
487 commonEventControlManager->scheduled_ = true;
488 commonEventControlManager->EnqueueOrderedRecord(eventRecord);
489 commonEventControlManager->CurrentOrderedEventTimeout(true);
490
491 bool result = false;
492 if (commonEventControlManager->orderedEventQueue_.size() > 0) {
493 result = true;
494 }
495 EXPECT_TRUE(result);
496 commonEventControlManager->orderedEventQueue_.clear();
497 }
498
499 /*
500 * @tc.number: CommonEventPublishOrderedUnitTest_2100
501 * @tc.name: test CurrentOrderedEventTimeout
502 * @tc.desc: Verify CurrentOrderedEventTimeout success with eventRecord->nextReceiver = 1;
503 */
504 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_2100, Function | MediumTest | Level1)
505 {
506 // make event record
507 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
508 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
509
510 std::shared_ptr<EventSubscriberRecord> subscriberRecord = std::make_shared<EventSubscriberRecord>();
511
512 std::shared_ptr<OrderedEventRecord> eventRecord = std::make_shared<OrderedEventRecord>();
513 eventRecord->commonEventData = commonEventData;
514 eventRecord->publishInfo = publishInfo;
515 eventRecord->resultTo = nullptr;
516 eventRecord->state = OrderedEventRecord::IDLE;
517 eventRecord->nextReceiver = 1;
518 eventRecord->deliveryState.emplace_back(OrderedEventRecord::PENDING);
519 eventRecord->receivers.emplace_back(subscriberRecord);
520
521 commonEventControlManager->scheduled_ = true;
522 bool ret = commonEventControlManager->EnqueueOrderedRecord(eventRecord);
523 EXPECT_TRUE(ret);
524 commonEventControlManager->CurrentOrderedEventTimeout(true);
525
526 bool result = false;
527 if (commonEventControlManager->orderedEventQueue_.front()->nextReceiver > 0) {
528 GTEST_LOG_(INFO) << std::to_string(commonEventControlManager->orderedEventQueue_.front()->nextReceiver);
529 result = true;
530 }
531 EXPECT_TRUE(result);
532 commonEventControlManager->orderedEventQueue_.clear();
533 }
534
535 /*
536 * @tc.number: CommonEventPublishOrderedUnitTest_2200
537 * @tc.name: test CancelTimeout
538 * @tc.desc: Verify CancelTimeout return true when pendingTimeoutMessage is true
539 */
540 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_2200, Function | MediumTest | Level1)
541 {
542 commonEventControlManager->pendingTimeoutMessage_ = true;
543 bool result = commonEventControlManager->CancelTimeout();
544 EXPECT_TRUE(result);
545 }
546
547 /*
548 * @tc.number: CommonEventPublishOrderedUnitTest_2300
549 * @tc.name: test CancelTimeout
550 * @tc.desc: Verify CancelTimeout return true when pendingTimeoutMessage is false
551 */
552 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_2300, Function | MediumTest | Level1)
553 {
554 bool result = false;
555 commonEventControlManager->pendingTimeoutMessage_ = false;
556 result = commonEventControlManager->CancelTimeout();
557 EXPECT_TRUE(result);
558 }
559
560 /*
561 * @tc.number: CommonEventPublishOrderedUnitTest_2400
562 * @tc.name: test PublishCommonEvent
563 * @tc.desc: 1.Set thread mode handler
564 * 2.Verify InnerCommonEventManager PublishCommonEvent success
565 */
566 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_2400, Function | MediumTest | Level1)
567 {
568 // make a want
569 Want want;
570 want.SetAction(ACTION);
571
572 // make common event data
573 CommonEventData data;
574 data.SetWant(want);
575
576 // make publish info
577 CommonEventPublishInfo publishInfo;
578 publishInfo.SetOrdered(true);
579
580 MatchingSkills matchingSkills;
581
582 // make subscriber info
583 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
584 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER);
585
586 // make a subscriber object
587 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
588
589 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
590
591 mtx.lock();
592
593 struct tm curTime {
594 0
595 };
596 OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
597 // publish ordered event
598 bool result = innerCommonEventManager.PublishCommonEvent(
599 data, publishInfo, commonEventListener, curTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "bundlename");
600
601 EXPECT_TRUE(result);
602
603 int count = 0;
604 while (!mtx.try_lock()) {
605 if (count == 0) {
606 GTEST_LOG_(INFO) << "Wait OnReceive callback function process";
607 count = 1;
608 } else {
609 usleep(100 * 1000);
610 }
611 }
612
613 mtx.unlock();
614 GTEST_LOG_(INFO) << "Testcase finished";
615 }
616
617 /*
618 * @tc.number: CommonEventPublishOrderedEventUnitTest_2500
619 * @tc.name: test PublishCommonEvent
620 * @tc.desc: 1.Set thread mode POST
621 * 2.Verify InnerCommonEventManager PublishCommonEvent success
622 */
623 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_2500, Function | MediumTest | Level1)
624 {
625 // make a want
626 Want want;
627 want.SetAction(ACTION);
628
629 // make common event data
630 CommonEventData data;
631 data.SetWant(want);
632
633 // make publish info
634 CommonEventPublishInfo publishInfo;
635 publishInfo.SetOrdered(true);
636
637 MatchingSkills matchingSkills;
638
639 // make subscriber info
640 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
641 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::POST);
642
643 // make a subscriber object
644 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
645
646 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
647
648 mtx.lock();
649
650 struct tm curTime {
651 0
652 };
653 OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
654 // publish ordered event
655 bool result = innerCommonEventManager.PublishCommonEvent(
656 data, publishInfo, commonEventListener, curTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "bundlename");
657
658 EXPECT_TRUE(result);
659
660 int count = 0;
661 while (!mtx.try_lock()) {
662 if (count == 0) {
663 GTEST_LOG_(INFO) << "Wait OnReceive callback function process";
664 count = 1;
665 } else {
666 usleep(100 * 1000);
667 }
668 }
669
670 mtx.unlock();
671 GTEST_LOG_(INFO) << "Testcase finished";
672 }
673
674 /*
675 * @tc.number: CommonEventPublishOrderedEventUnitTest_2600
676 * @tc.name: test PublishCommonEvent
677 * @tc.desc: 1.Set thread mode ASYNC
678 * 2.Verify InnerCommonEventManager PublishCommonEvent success
679 */
680 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_2600, Function | MediumTest | Level1)
681 {
682 // make a want
683 Want want;
684 want.SetAction(ACTION);
685
686 // make common event data
687 CommonEventData data;
688 data.SetWant(want);
689
690 // make publish info
691 CommonEventPublishInfo publishInfo;
692 publishInfo.SetOrdered(true);
693
694 MatchingSkills matchingSkills;
695
696 // make subscriber info
697 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
698 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::ASYNC);
699
700 // make a subscriber object
701 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
702
703 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
704
705 mtx.lock();
706
707 struct tm curTime {
708 0
709 };
710 OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
711 // publish ordered event
712 bool result = innerCommonEventManager.PublishCommonEvent(
713 data, publishInfo, commonEventListener, curTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "bundlename");
714
715 EXPECT_TRUE(result);
716
717 int count = 0;
718 while (!mtx.try_lock()) {
719 if (count == 0) {
720 GTEST_LOG_(INFO) << "Wait OnReceive callback function process";
721 count = 1;
722 } else {
723 usleep(100 * 1000);
724 }
725 }
726
727 mtx.unlock();
728 GTEST_LOG_(INFO) << "Testcase finished";
729 }
730
731 /*
732 * @tc.number: CommonEventPublishOrderedEventUnitTest_2700
733 * @tc.name: test PublishCommonEvent
734 * @tc.desc: 1.Set thread mode BACKGROUND
735 * 2.Verify InnerCommonEventManager PublishCommonEvent success
736 */
737 HWTEST_F(CommonEventPublishOrderedEventUnitTest, CommonEventPublishOrderedUnitTest_2700, Function | MediumTest | Level1)
738 {
739 // make a want
740 Want want;
741 want.SetAction(ACTION);
742
743 // make common event data
744 CommonEventData data;
745 data.SetWant(want);
746
747 // make publish info
748 CommonEventPublishInfo publishInfo;
749 publishInfo.SetOrdered(true);
750
751 MatchingSkills matchingSkills;
752
753 // make subscriber info
754 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
755 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::BACKGROUND);
756
757 // make a subscriber object
758 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
759
760 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
761
762 mtx.lock();
763
764 struct tm curTime {
765 0
766 };
767 OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
768 // publish ordered event
769 bool result = innerCommonEventManager.PublishCommonEvent(
770 data, publishInfo, commonEventListener, curTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "bundlename");
771
772 EXPECT_TRUE(result);
773
774 int count = 0;
775 while (!mtx.try_lock()) {
776 if (count == 0) {
777 GTEST_LOG_(INFO) << "Wait OnReceive callback function process";
778 count = 1;
779 } else {
780 usleep(100 * 1000);
781 }
782 }
783
784 mtx.unlock();
785 GTEST_LOG_(INFO) << "Testcase finished";
786 }
787