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