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