• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)103     virtual int32_t SubscribeCommonEvent(
104         const CommonEventSubscribeInfo &subscribeInfo, const OHOS::sptr<IRemoteObject> &commonEventListener)
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 fail because runner is null
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(false, 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
439  */
440 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribe_015, TestSize.Level1)
441 {
442     MatchingSkills matchingSkills;
443     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
444     std::shared_ptr<CommonEventSubscribeInfo> commonEventSubscribeInfo =
445         std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
446     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
447     CommonEventListener commonEventListener(subscriber);
448     commonEventListener.handler_ = nullptr;
449 
450     bool result = commonEventListener.IsReady();
451 
452     EXPECT_EQ(false, result);
453 }
454 
455 /*
456  * tc.number: CommonEventManager_001
457  * tc.name: test Freeze
458  * tc.type: FUNC
459  * tc.require: issueI5NGO7
460  * tc.desc: Invoke Freeze interface verify whether it is normal
461  */
462 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_001, TestSize.Level1)
463 {
464     CommonEventManager commonEventManager;
465     bool freeze = commonEventManager.Freeze(SYSTEM_UID);
466     EXPECT_EQ(true, freeze);
467 }
468 
469 /*
470  * tc.number: CommonEventManager_002
471  * tc.name: test Unfreeze
472  * tc.type: FUNC
473  * tc.require: issueI5NGO7
474  * tc.desc: Invoke Unfreeze interface verify whether it is normal
475  */
476 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_002, TestSize.Level1)
477 {
478     CommonEventManager commonEventManager;
479     bool unfreeze = commonEventManager.Unfreeze(SYSTEM_UID);
480     EXPECT_EQ(true, unfreeze);
481 }
482 
483 /*
484  * tc.number: CommonEventManager_003
485  * tc.name: test UnfreezeAll
486  * tc.type: FUNC
487  * tc.require: issueI5NGO7
488  * tc.desc: Invoke UnfreezeAll interface verify whether it is normal
489  */
490 HWTEST_F(CommonEventSubscribeTest, CommonEventManager_003, TestSize.Level1)
491 {
492     CommonEventManager commonEventManager;
493     bool unfreezeAll = commonEventManager.UnfreezeAll();
494     EXPECT_EQ(true, unfreezeAll);
495 }
496 
497 /*
498  * tc.number: CommonEventPublishInfo_001
499  * tc.name: test ReadFromParcel
500  * tc.type: FUNC
501  * tc.require: issueI5RULW
502  * tc.desc: Invoke ReadFromParcel interface verify whether it is normal
503  */
504 HWTEST_F(CommonEventSubscribeTest, CommonEventPublishInfo_001, TestSize.Level1)
505 {
506     Parcel parcel;
507     CommonEventPublishInfo commonEventPublishInfo;
508     bool result = commonEventPublishInfo.ReadFromParcel(parcel);
509     EXPECT_EQ(result, true);
510 }
511 
512 /*
513  * tc.number: CommonEventSubscribeInfo_001
514  * tc.name: test ReadFromParcel
515  * tc.type: FUNC
516  * tc.require: issueI5RULW
517  * tc.desc: Invoke ReadFromParcel interface verify whether it is normal
518  */
519 HWTEST_F(CommonEventSubscribeTest, CommonEventSubscribeInfo_001, TestSize.Level1)
520 {
521     Parcel parcel;
522     CommonEventSubscribeInfo commonEventSubscribeInfo;
523     bool result = commonEventSubscribeInfo.ReadFromParcel(parcel);
524     EXPECT_EQ(result, false);
525 }
526 
527 /*
528  * tc.number: MatchingSkills_001
529  * tc.name: test ReadFromParcel
530  * tc.type: FUNC
531  * tc.require: issueI5RULW
532  * tc.desc: Invoke ReadFromParcel interface verify whether it is normal
533  */
534 HWTEST_F(CommonEventSubscribeTest, MatchingSkills_001, TestSize.Level1)
535 {
536     Parcel parcel;
537     MatchingSkills matchingSkills;
538 
539     // write entity
540     int32_t value = 1;
541     parcel.WriteInt32(value);
542     std::vector<std::u16string> actionU16Entity;
543     for (std::vector<std::string>::size_type i = 0; i < 3; i++) {
544         actionU16Entity.emplace_back(OHOS::Str8ToStr16("test"));
545     }
546     matchingSkills.WriteVectorInfo(parcel, actionU16Entity);
547 
548     //write event
549     parcel.WriteInt32(value);
550     std::vector<std::u16string> actionU16Event;
551     for (std::vector<std::string>::size_type i = 0; i < 3; i++) {
552         actionU16Event.emplace_back(OHOS::Str8ToStr16("test"));
553     }
554     matchingSkills.WriteVectorInfo(parcel, actionU16Event);
555 
556     // write scheme
557     parcel.WriteInt32(value);
558     std::vector<std::u16string> actionU16Scheme;
559     for (std::vector<std::string>::size_type i = 0; i < 3; i++) {
560         actionU16Scheme.emplace_back(OHOS::Str8ToStr16("test"));
561     }
562     matchingSkills.WriteVectorInfo(parcel, actionU16Scheme);
563 
564     bool result = matchingSkills.ReadFromParcel(parcel);
565     EXPECT_EQ(result, true);
566 }
567