1 /*
2 * Copyright (c) 2021-2025 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 <thread>
17 #include <chrono>
18
19 #include <sys/time.h>
20
21 // redefine private and protected since testcase need to invoke and test private function
22 #define private public
23 #define protected public
24 #include "common_event.h"
25 #include "common_event_manager.h"
26 #include "common_event_stub.h"
27 #include "common_event_subscriber_manager.h"
28 #include "inner_common_event_manager.h"
29 #include "common_event_publish_info.h"
30 #include "common_event_subscribe_info.h"
31 #include "matching_skills.h"
32 #undef private
33 #undef protected
34
35 #include "datetime_ex.h"
36
37 #include <gtest/gtest.h>
38
39 using namespace testing::ext;
40 using namespace OHOS::EventFwk;
41 using OHOS::Parcel;
42 using OHOS::ErrCode;
43
44 namespace {
45 const std::string EVENT = "com.ces.test.event";
46 std::mutex mtx;
47 const time_t TIME_OUT_SECONDS_LIMIT = 5;
48 constexpr uint16_t SYSTEM_UID = 1000;
49 constexpr int32_t ERR_COMMON = -1;
50 } // namespace
51
52 class SubscriberTest;
53
54 class CommonEventSubscribeTest : public testing::Test {
55 public:
56 static void SetUpTestCase(void);
57 static void TearDownTestCase(void);
58 void SetUp();
59 void TearDown();
60
61 public:
62 static constexpr int TEST_WAIT_TIME = 100000;
63 CommonEventManager commonEventManager;
64 MatchingSkills matchingSkills;
65 };
66
67 class SubscriberTest : public CommonEventSubscriber {
68 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)69 explicit SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
70 {}
71
~SubscriberTest()72 ~SubscriberTest()
73 {}
74
OnReceiveEvent(const CommonEventData & data)75 virtual void OnReceiveEvent(const CommonEventData &data)
76 {
77 mtx.unlock();
78 }
79 };
80
81 class EventReceiveStubTest : public EventReceiveStub {
82 public:
EventReceiveStubTest()83 EventReceiveStubTest()
84 {}
85
~EventReceiveStubTest()86 ~EventReceiveStubTest()
87 {}
88
NotifyEvent(const CommonEventData & data,bool ordered,bool sticky)89 OHOS::ErrCode NotifyEvent(const CommonEventData& data, bool ordered, bool sticky) override
90 {
91 return OHOS::ERR_OK;
92 }
CallbackEnter(uint32_t code)93 int32_t CallbackEnter([[maybe_unused]] uint32_t code) override
94 {
95 return 0;
96 }
CallbackExit(uint32_t code,int32_t result)97 int32_t CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override
98 {
99 return 0;
100 }
101 };
102
103 class CommonEventStubTest : public CommonEventStub {
104 public:
CommonEventStubTest()105 CommonEventStubTest()
106 {}
107
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,const OHOS::sptr<IRemoteObject> & commonEventListener,int32_t userId,int32_t & funcResult)108 virtual ErrCode PublishCommonEvent(const CommonEventData& event, const CommonEventPublishInfo& publishInfo,
109 const OHOS::sptr<IRemoteObject>& commonEventListener, int32_t userId, int32_t& funcResult)
110 {
111 return ERR_COMMON;
112 }
113
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const OHOS::sptr<IRemoteObject> & commonEventListener,int32_t instanceKey,int32_t & funcResult)114 virtual ErrCode SubscribeCommonEvent(const CommonEventSubscribeInfo& subscribeInfo,
115 const OHOS::sptr<IRemoteObject>& commonEventListener, int32_t instanceKey, int32_t& funcResult)
116 {
117 return ERR_COMMON;
118 }
119
UnsubscribeCommonEvent(const OHOS::sptr<IRemoteObject> & commonEventListener,int32_t & funcResult)120 virtual ErrCode UnsubscribeCommonEvent(const OHOS::sptr<IRemoteObject>& commonEventListener, int32_t& funcResult)
121 {
122 return ERR_COMMON;
123 }
124
DumpState(uint8_t dumpType,const std::string & event,int32_t userId,std::vector<std::string> & state,bool & funcResult)125 virtual ErrCode DumpState(uint8_t dumpType, const std::string& event, int32_t userId,
126 std::vector<std::string>& state, bool& funcResult)
127 {
128 funcResult = false;
129 return OHOS::ERR_OK;
130 }
131
~CommonEventStubTest()132 virtual ~CommonEventStubTest()
133 {}
134
FinishReceiver(const OHOS::sptr<IRemoteObject> & proxy,int32_t code,const std::string & receiverData,bool abortEvent,bool & funcResult)135 virtual ErrCode FinishReceiver(const OHOS::sptr<IRemoteObject>& proxy, int32_t code,
136 const std::string& receiverData, bool abortEvent, bool& funcResult)
137 {
138 funcResult = false;
139 return OHOS::ERR_OK;
140 }
141 };
142
SetUpTestCase(void)143 void CommonEventSubscribeTest::SetUpTestCase(void)
144 {}
145
TearDownTestCase(void)146 void CommonEventSubscribeTest::TearDownTestCase(void)
147 {}
148
SetUp(void)149 void CommonEventSubscribeTest::SetUp(void)
150 {}
151
TearDown(void)152 void CommonEventSubscribeTest::TearDown(void)
153 {}
154
155 /*
156 * Feature: CommonEventSubscribeTest
157 * Function:CommonEvent SubscribeCommonEvent
158 * SubFunction: Subscribe common event
159 * FunctionPoints: test subscribe event
160 * EnvConditions: system run normally
161 * CaseDescription: 1. subscribe common event
162 * 2. success subscribe common event with right parameters
163 */
164
165 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_001, TestSize.Level0)
166 {
167 /* Subscribe */
168 MatchingSkills matchingSkills;
169 matchingSkills.AddEvent(EVENT);
170 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
171 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
172 bool subscribeResult = CommonEventManager::SubscribeCommonEvent(subscriber);
173
174 EXPECT_EQ(true, subscribeResult);
175
176 /* Publish */
177
178 // make a want
179 Want want;
180 want.SetAction(EVENT);
181
182 // make common event data
183 CommonEventData data;
184 data.SetWant(want);
185
186 // lock the mutex
187 mtx.lock();
188
189 // publish a common event
190 bool publishResult = CommonEventManager::PublishCommonEvent(data);
191
192 EXPECT_EQ(true, publishResult);
193
194 // record start time of publishing
195 struct tm startTime = {0};
196 EXPECT_EQ(OHOS::GetSystemCurrentTime(&startTime), true);
197
198 // record current time
199 struct tm doingTime = {0};
200 int64_t seconds = 0;
201
202 while (!mtx.try_lock()) {
203 // get current time and compare it with the start time
204 EXPECT_EQ(OHOS::GetSystemCurrentTime(&doingTime), true);
205 seconds = OHOS::GetSecondsBetween(startTime, doingTime);
206 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
207 break;
208 }
209 }
210
211 // expect the subscriber could receive the event within 5 seconds.
212 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
213 mtx.unlock();
214 }
215
216 /*
217 * Feature: CommonEventSubscribeTest
218 * Function:CommonEventSubscriberManager InsertSubscriber
219 * SubFunction: Subscribe common event
220 * FunctionPoints: test subscribe event
221 * EnvConditions: system run normally
222 * CaseDescription: 1. subscribe common event
223 * 2. different subscriber subscribe event
224 * 3. success subscribe common event with right parameters
225 */
226 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_002, TestSize.Level0)
227 {
228 MatchingSkills matchingSkills;
229 matchingSkills.AddEvent(EVENT);
230 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
231 std::shared_ptr<SubscriberTest> subscribera = std::make_shared<SubscriberTest>(subscribeInfo);
232 std::shared_ptr<SubscriberTest> subscriberb = std::make_shared<SubscriberTest>(subscribeInfo);
233
234 bool subscribeResulta = CommonEventManager::SubscribeCommonEvent(subscribera);
235
236 EXPECT_EQ(true, subscribeResulta);
237
238 bool subscribeResultb = CommonEventManager::SubscribeCommonEvent(subscriberb);
239
240 EXPECT_EQ(true, subscribeResultb);
241 }
242
243 /*
244 * Feature: CommonEventSubscribeTest
245 * Function:CommonEvent SubscribeCommonEvent
246 * SubFunction: Subscribe common event
247 * FunctionPoints: test subscribe event
248 * EnvConditions: system run normally
249 * CaseDescription: 1. subscribe common event
250 * 2. fail subscribe common event kit with null subscriber
251 */
252
253 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_003, TestSize.Level0)
254 {
255 bool subscribeResult = CommonEventManager::SubscribeCommonEvent(nullptr);
256
257 EXPECT_EQ(false, subscribeResult);
258 }
259
260 /*
261 * Feature: CommonEventSubscribeTest
262 * Function:CommonEvent SubscribeCommonEvent
263 * SubFunction: Subscribe common event
264 * FunctionPoints: test subscribe event
265 * EnvConditions: system run normally
266 * CaseDescription: 1. subscribe common event
267 * 2. fail subscribe common event with no event
268 */
269 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_004, TestSize.Level0)
270 {
271 MatchingSkills matchingSkills;
272 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
273 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
274
275 bool subscribeResult = CommonEventManager::SubscribeCommonEvent(subscriber);
276
277 EXPECT_EQ(false, subscribeResult);
278 }
279
280 /*
281 * Feature: CommonEventSubscribeTest
282 * Function:CommonEvent SubscribeCommonEvent
283 * SubFunction: Subscribe common event
284 * FunctionPoints: test subscribe event
285 * EnvConditions: system run normally
286 * CaseDescription: 1. subscribe common event
287 * 2. fail subscribe common event because common event listener has subsrciber
288 */
289 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_005, TestSize.Level0)
290 {
291 MatchingSkills matchingSkills;
292 matchingSkills.AddEvent(EVENT);
293 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
294 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
295
296 CommonEventManager::SubscribeCommonEvent(subscriber);
297
298 bool subscribeResult = CommonEventManager::SubscribeCommonEvent(subscriber);
299
300 EXPECT_EQ(true, subscribeResult);
301 }
302
303 /*
304 * Feature: CommonEventSubscribeTest
305 * Function: InnerCommonEventManager SubscribeCommonEvent
306 * SubFunction: Subscribe common event
307 * FunctionPoints: test subscribe event
308 * EnvConditions: system run normally
309 * CaseDescription: 1. subscribe common event
310 * 2. fail subscribe common event, inner common event manager
311 * common event listener is null
312 */
313 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_009, TestSize.Level0)
314 {
315 MatchingSkills matchingSkills;
316 matchingSkills.AddEvent(EVENT);
317 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
318 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
319 OHOS::sptr<OHOS::IRemoteObject> sp(nullptr);
320
321 struct tm curTime {0};
322 OHOS::Security::AccessToken::AccessTokenID tokenID = 1;
323
324 EXPECT_EQ(false, innerCommonEventManager->SubscribeCommonEvent(subscribeInfo, sp, curTime, 0, 0, tokenID, ""));
325 }
326
327 /*
328 * Feature: CommonEventSubscribeTest
329 * Function: CommonEventSubscriberManager InsertSubscriber
330 * SubFunction: Subscribe common event
331 * FunctionPoints: test subscribe event
332 * EnvConditions: system run normally
333 * CaseDescription: 1. subscribe common event
334 * 2. fail subscribe common event , common event subscriber manager
335 * event subscriber info is null
336 */
337 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_010, TestSize.Level0)
338 {
339 MatchingSkills matchingSkills;
340 matchingSkills.AddEvent(EVENT);
341 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
342 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
343 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
344 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
345 OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
346
347 struct tm curTime {0};
348 EventRecordInfo eventRecordInfo;
349 eventRecordInfo.pid = 0;
350 eventRecordInfo.uid = 0;
351 eventRecordInfo.bundleName = "bundleName";
352
353 auto result = OHOS::DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->InsertSubscriber(
354 nullptr, commonEventListenerPtr, curTime, eventRecordInfo);
355
356 EXPECT_EQ(nullptr, result);
357 }
358
359 /*
360 * Feature: CommonEventSubscribeTest
361 * Function: CommonEventSubscriberManager InsertSubscriber
362 * SubFunction: Subscribe common event
363 * FunctionPoints: test subscribe event
364 * EnvConditions: system run normally
365 * CaseDescription: 1. subscribe common event
366 * 2. fail subscribe common event , common event subscriber manager
367 * event common event listener is null
368 */
369 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_011, TestSize.Level0)
370 {
371 MatchingSkills matchingSkills;
372 matchingSkills.AddEvent(EVENT);
373 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
374 std::shared_ptr<CommonEventSubscribeInfo> commonEventSubscribeInfo =
375 std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
376
377 struct tm curTime {0};
378 EventRecordInfo eventRecordInfo;
379 eventRecordInfo.pid = 0;
380 eventRecordInfo.uid = 0;
381 eventRecordInfo.bundleName = "bundleName";
382
383 auto result = OHOS::DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->InsertSubscriber(
384 commonEventSubscribeInfo, nullptr, curTime, eventRecordInfo);
385
386 EXPECT_EQ(nullptr, result);
387 }
388
389 /*
390 * Feature: CommonEventSubscribeTest
391 * Function: CommonEventSubscriberManager InsertSubscriber
392 * SubFunction: Subscribe common event
393 * FunctionPoints: test subscribe event
394 * EnvConditions: system run normally
395 * CaseDescription: 1. subscribe common event
396 * 2. fail subscribe common event , common event subscriber manager
397 * event size is null
398 */
399 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_012, TestSize.Level0)
400 {
401 MatchingSkills matchingSkills;
402 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
403 std::shared_ptr<CommonEventSubscribeInfo> commonEventSubscribeInfo =
404 std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
405 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
406 OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
407 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
408 OHOS::sptr<OHOS::IRemoteObject> commonEventListenerSp(commonEventListener);
409
410 struct tm curTime {0};
411 EventRecordInfo eventRecordInfo;
412 eventRecordInfo.pid = 0;
413 eventRecordInfo.uid = 0;
414 eventRecordInfo.bundleName = "bundleName";
415
416 auto result = OHOS::DelayedSingleton<CommonEventSubscriberManager>::GetInstance()->InsertSubscriber(
417 commonEventSubscribeInfo, nullptr, curTime, eventRecordInfo);
418
419 EXPECT_EQ(nullptr, result);
420 }
421
422 /*
423 * Feature: CommonEventSubscribeTest
424 * Function: CommonEventListener IsReady
425 * SubFunction: Subscribe common event
426 * FunctionPoints: test is ready
427 * EnvConditions: system run normally
428 * CaseDescription: 1. ready true because handler is null but ThreadMode is not handler
429 */
430 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_014, TestSize.Level0)
431 {
432 MatchingSkills matchingSkills;
433 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
434 std::shared_ptr<CommonEventSubscribeInfo> commonEventSubscribeInfo =
435 std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
436 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
437 CommonEventListener commonEventListener(subscriber);
438 commonEventListener.runner_ = nullptr;
439
440 bool result = commonEventListener.IsReady();
441
442 EXPECT_EQ(true, result);
443 }
444
445 /*
446 * Feature: CommonEventSubscribeTest
447 * Function: CommonEventListener IsReady
448 * SubFunction: Subscribe common event
449 * FunctionPoints: test is ready
450 * EnvConditions: system run normally
451 * CaseDescription: 1. ready fail because handler is null and threadMode is handler
452 */
453 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_015, TestSize.Level0)
454 {
455 MatchingSkills matchingSkills;
456 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
457 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::HANDLER);
458 std::shared_ptr<CommonEventSubscribeInfo> commonEventSubscribeInfo =
459 std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
460 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
461 CommonEventListener commonEventListener(subscriber);
462 commonEventListener.handler_ = nullptr;
463
464 bool result = commonEventListener.IsReady();
465
466 EXPECT_EQ(false, result);
467 }
468
469 /*
470 * tc.number: CommonEventManager_001
471 * tc.name: test Freeze
472 * tc.type: FUNC
473 * tc.require: issueI5NGO7
474 * tc.desc: Invoke Freeze interface verify whether it is normal
475 */
476 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_001, TestSize.Level0)
477 {
478 CommonEventManager commonEventManager;
479 bool freeze = commonEventManager.Freeze(SYSTEM_UID);
480 EXPECT_EQ(false, freeze);
481 }
482
483 /*
484 * tc.number: CommonEventManager_002
485 * tc.name: test Unfreeze
486 * tc.type: FUNC
487 * tc.require: issueI5NGO7
488 * tc.desc: Invoke Unfreeze interface verify whether it is normal
489 */
490 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_002, TestSize.Level0)
491 {
492 CommonEventManager commonEventManager;
493 bool unfreeze = commonEventManager.Unfreeze(SYSTEM_UID);
494 EXPECT_EQ(false, unfreeze);
495 }
496
497 /*
498 * tc.number: CommonEventManager_003
499 * tc.name: test UnfreezeAll
500 * tc.type: FUNC
501 * tc.require: issueI5NGO7
502 * tc.desc: Invoke UnfreezeAll interface verify whether it is normal
503 */
504 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_003, TestSize.Level0)
505 {
506 CommonEventManager commonEventManager;
507 bool unfreezeAll = commonEventManager.UnfreezeAll();
508 EXPECT_EQ(false, unfreezeAll);
509 }
510
511 /*
512 * tc.number: CommonEventManager_004
513 * tc.name: test Freeze
514 * tc.type: FUNC
515 * tc.require: issue
516 * tc.desc: Invoke PublishCommonEventAsUser interface verify whether it is normal
517 */
518 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_004, TestSize.Level0)
519 {
520 CommonEventManager commonEventManager;
521 CommonEventData data;
522 int32_t userId = 10;
523 bool result = commonEventManager.PublishCommonEventAsUser(data, userId);
524 EXPECT_EQ(result, false);
525 }
526
527 /*
528 * tc.number: CommonEventManager_005
529 * tc.name: test Freeze
530 * tc.type: FUNC
531 * tc.require: issue
532 * tc.desc: Invoke PublishCommonEventAsUser interface verify whether it is normal
533 */
534 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_005, TestSize.Level0)
535 {
536 CommonEventManager commonEventManager;
537 CommonEventData data;
538 int32_t userId = 10;
539 CommonEventPublishInfo publishInfo;
540 bool result = commonEventManager.PublishCommonEventAsUser(data, publishInfo, userId);
541 EXPECT_EQ(result, false);
542 }
543
544 /*
545 * tc.number: CommonEventManager_006
546 * tc.name: test SetStaticSubscriberState
547 * tc.type: FUNC
548 * tc.require: issueI5NGO7
549 * tc.desc: Invoke Unfreeze interface verify whether it is normal
550 */
551 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_006, TestSize.Level0)
552 {
553 CommonEventManager commonEventManager;
554 int32_t ret = commonEventManager.SetStaticSubscriberState(true);
555 EXPECT_NE(ret, OHOS::ERR_OK);
556 }
557
558 /*
559 * tc.number: CommonEventManager_007
560 * tc.name: test SetFreezeStatus
561 * tc.type: FUNC
562 * tc.desc: Invoke SetFreezeStatus interface verify whether it is normal
563 */
564 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_007, TestSize.Level0)
565 {
566 CommonEventManager commonEventManager;
567 std::set<int> pidList = {1000};
568 bool result = commonEventManager.SetFreezeStatus(pidList, true);
569 EXPECT_EQ(false, result);
570 }
571
572 /*
573 * tc.number: CommonEventSubscribeInfo_001
574 * tc.name: test ReadFromParcel
575 * tc.type: FUNC
576 * tc.require: issueI5RULW
577 * tc.desc: Invoke ReadFromParcel interface verify whether it is normal
578 */
579 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribeInfo_001, TestSize.Level0)
580 {
581 Parcel parcel;
582 CommonEventSubscribeInfo commonEventSubscribeInfo;
583 bool result = commonEventSubscribeInfo.ReadFromParcel(parcel);
584 EXPECT_EQ(result, false);
585 }
586
587 /*
588 * tc.number: MatchingSkills_001
589 * tc.name: test ReadFromParcel
590 * tc.type: FUNC
591 * tc.require: issueI5RULW
592 * tc.desc: Invoke ReadFromParcel interface verify whether it is normal
593 */
594 HWTEST_F(CommonEventSubscribeTest, MatchingSkills_001, TestSize.Level0)
595 {
596 Parcel parcel;
597 MatchingSkills matchingSkills;
598
599 // write entity
600 int32_t value = 1;
601 parcel.WriteInt32(value);
602 std::vector<std::u16string> actionU16Entity;
603 for (std::vector<std::string>::size_type i = 0; i < 3; i++) {
604 actionU16Entity.emplace_back(OHOS::Str8ToStr16("test"));
605 }
606 matchingSkills.WriteVectorInfo(parcel, actionU16Entity);
607
608 //write event
609 parcel.WriteInt32(value);
610 std::vector<std::u16string> actionU16Event;
611 for (std::vector<std::string>::size_type i = 0; i < 3; i++) {
612 actionU16Event.emplace_back(OHOS::Str8ToStr16("test"));
613 }
614 matchingSkills.WriteVectorInfo(parcel, actionU16Event);
615
616 // write scheme
617 parcel.WriteInt32(value);
618 std::vector<std::u16string> actionU16Scheme;
619 for (std::vector<std::string>::size_type i = 0; i < 3; i++) {
620 actionU16Scheme.emplace_back(OHOS::Str8ToStr16("test"));
621 }
622 matchingSkills.WriteVectorInfo(parcel, actionU16Scheme);
623
624 bool result = matchingSkills.ReadFromParcel(parcel);
625 EXPECT_EQ(result, true);
626 }
627
628 /*
629 * tc.number: CommonEventSubscriber_001
630 * tc.name: test SetCode
631 * tc.type: FUNC
632 * tc.require: issue
633 * tc.desc: when SetCode return false
634 */
635 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscriber_001, TestSize.Level0)
636 {
637 MatchingSkills matchingSkills;
638 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
639 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
640
641 subscriber->SetAsyncCommonEventResult(nullptr);
642 EXPECT_EQ(subscriber->CheckSynchronous(), false);
643 const int code = 1;
644 bool result = subscriber->SetCode(code);
645 EXPECT_EQ(result, false);
646 }
647
648 /*
649 * tc.number: CommonEventSubscriber_002
650 * tc.name: test GetCode
651 * tc.type: FUNC
652 * tc.require: issue
653 * tc.desc: when GetCode return false
654 */
655 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscriber_002, TestSize.Level0)
656 {
657 MatchingSkills matchingSkills;
658 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
659 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
660
661 subscriber->SetAsyncCommonEventResult(nullptr);
662 EXPECT_EQ(subscriber->CheckSynchronous(), false);
663 bool result = subscriber->GetCode();
664 EXPECT_EQ(result, false);
665 }
666
667 /*
668 * tc.number: CommonEventSubscriber_003
669 * tc.name: test SetData
670 * tc.type: FUNC
671 * tc.require: issue
672 * tc.desc: when SetData return false
673 */
674 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscriber_003, TestSize.Level0)
675 {
676 MatchingSkills matchingSkills;
677 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
678 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
679
680 subscriber->SetAsyncCommonEventResult(nullptr);
681 EXPECT_EQ(subscriber->CheckSynchronous(), false);
682 const std::string data = "this is data";
683 bool result = subscriber->SetData(data);
684 EXPECT_EQ(result, false);
685 }
686
687 /*
688 * tc.number: CommonEventSubscriber_004
689 * tc.name: test GetData
690 * tc.type: FUNC
691 * tc.require: issue
692 * tc.desc: when GetData return null
693 */
694 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscriber_004, TestSize.Level0)
695 {
696 MatchingSkills matchingSkills;
697 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
698 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
699
700 subscriber->SetAsyncCommonEventResult(nullptr);
701 EXPECT_EQ(subscriber->CheckSynchronous(), false);
702 std::string result = subscriber->GetData();
703 std::string ret = "";
704 EXPECT_EQ(result, ret);
705 }
706
707 /*
708 * tc.number: CommonEventSubscriber_005
709 * tc.name: test SetCodeAndData
710 * tc.type: FUNC
711 * tc.require: issue
712 * tc.desc: when SetCodeAndData return false
713 */
714 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscriber_005, TestSize.Level0)
715 {
716 MatchingSkills matchingSkills;
717 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
718 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
719
720 subscriber->SetAsyncCommonEventResult(nullptr);
721 EXPECT_EQ(subscriber->CheckSynchronous(), false);
722 const int32_t code = 1;
723 const std::string data = "this is data";
724 bool result = subscriber->SetCodeAndData(code, data);
725 EXPECT_EQ(result, false);
726 }
727
728 /*
729 * tc.number: CommonEventSubscriber_006
730 * tc.name: test ClearAbortCommonEvent and AbortCommonEvent
731 * tc.type: FUNC
732 * tc.require: issue
733 * tc.desc: when ClearAbortCommonEvent
734 */
735 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscriber_006, TestSize.Level0)
736 {
737 MatchingSkills matchingSkills;
738 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
739
740 std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
741 std::shared_ptr<AsyncCommonEventResult> result =
742 std::make_shared<AsyncCommonEventResult>(12, "data", true, false, nullptr);
743 subscriber->SetAsyncCommonEventResult(result);
744 EXPECT_EQ(subscriber->CheckSynchronous(), true);
745
746 EXPECT_EQ(subscriber->AbortCommonEvent(), true);
747 EXPECT_EQ(subscriber->GetAbortCommonEvent(), true);
748
749 EXPECT_EQ(subscriber->ClearAbortCommonEvent(), true);
750 EXPECT_EQ(subscriber->GetAbortCommonEvent(), false);
751 }