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 #define UNIT_TEST
17 
18 #include <gtest/gtest.h>
19 
20 #define private public
21 #define protected public
22 #include "bundle_manager_helper.h"
23 #include "common_event_sticky_manager.h"
24 #undef private
25 #undef protected
26 
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 const std::string EVENT = "com.ces.test.event";
37 const std::string EVENT2 = "com.ces.test.event2";
38 const std::string EVENT3 = "com.ces.test.event3";
39 const std::string EVENT4 = "com.ces.test.event4";
40 const std::string EVENT5 = "com.ces.test.event5";
41 const std::string EVENT6 = "com.ces.test.event6";
42 const std::string TYPE = "com.ces.test.type";
43 const std::string TYPE2 = "com.ces.test.type2";
44 const std::string PERMISSION = "com.ces.test.permission";
45 const std::string STRING_EVENT = "com.ces.event";
46 const std::string STRING_DATA = "data";
47 constexpr uint8_t PID = 0;
48 constexpr uint16_t SYSTEM_UID = 1000;
49 }  // namespace
50 
51 static OHOS::sptr<OHOS::IRemoteObject> bundleObject = nullptr;
52 
53 class CommonEventStickyTest : public testing::Test {
54 public:
CommonEventStickyTest()55     CommonEventStickyTest()
56     {}
~CommonEventStickyTest()57     ~CommonEventStickyTest()
58     {}
59     static void SetUpTestCase(void);
60     static void TearDownTestCase(void);
61     void SetUp();
62     void TearDown();
63 };
64 
65 class SubscriberTest : public CommonEventSubscriber {
66 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)67     explicit SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
68     {}
69 
~SubscriberTest()70     ~SubscriberTest()
71     {}
72 
OnReceiveEvent(const CommonEventData & data)73     virtual void OnReceiveEvent(const CommonEventData &data)
74     {}
75 };
76 
SetUpTestCase(void)77 void CommonEventStickyTest::SetUpTestCase(void)
78 {
79     bundleObject = new MockBundleMgrService();
80     OHOS::DelayedSingleton<BundleManagerHelper>::GetInstance()->sptrBundleMgr_ =
81         OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(bundleObject);
82 }
83 
TearDownTestCase(void)84 void CommonEventStickyTest::TearDownTestCase(void)
85 {}
86 
SetUp(void)87 void CommonEventStickyTest::SetUp(void)
88 {}
89 
TearDown(void)90 void CommonEventStickyTest::TearDown(void)
91 {}
92 
93 /*
94  * @tc.number: CommonEventStickyTest_0100
95  * @tc.name: test StickyCommonEvent
96  * @tc.desc: Gets the published sticky event
97  */
98 HWTEST_F(CommonEventStickyTest, CommonEventStickyTest_0100, Function | MediumTest | Level1)
99 {
100     /* Publish */
101     // make a want
102     Want want;
103     want.SetAction(EVENT);
104     want.SetType(TYPE);
105     // make common event data
106     CommonEventData data;
107     data.SetWant(want);
108 
109     // make publish info
110     CommonEventPublishInfo publishInfo;
111     publishInfo.SetSticky(true);
112 
113     struct tm recordTime = {0};
114     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
115 
116     InnerCommonEventManager innerCommonEventManager;
117     EXPECT_TRUE(innerCommonEventManager.PublishCommonEvent(
118         data, publishInfo, nullptr, recordTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "hello"));
119 
120     sleep(1);
121 
122     CommonEventData Stickydata;
123     // EXPECT_TRUE(
124     //     OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance()->GetStickyCommonEvent(EVENT, Stickydata));
125     // EXPECT_EQ(EVENT, Stickydata.GetWant().GetAction());
126     // EXPECT_EQ(TYPE, Stickydata.GetWant().GetType());
127 }
128 
129 /*
130  * @tc.number: CommonEventStickyTest_0200
131  * @tc.name: test StickyCommonEvent
132  * @tc.desc: Confirm whether the acquired sticky event data is the latest released
133  */
134 HWTEST_F(CommonEventStickyTest, CommonEventStickyTest_0200, Function | MediumTest | Level1)
135 {
136     /* Publish */
137 
138     // make a want
139     Want want;
140     want.SetAction(EVENT2);
141     want.SetType(TYPE);
142     // make common event data
143     CommonEventData data;
144     data.SetWant(want);
145 
146     // make publish info
147     CommonEventPublishInfo publishInfo;
148     publishInfo.SetSticky(true);
149 
150     struct tm recordTime = {0};
151     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
152 
153     InnerCommonEventManager innerCommonEventManager;
154     EXPECT_TRUE(innerCommonEventManager.PublishCommonEvent(
155         data, publishInfo, nullptr, recordTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "hello"));
156 
157     // make a want
158     Want want2;
159     want2.SetAction(EVENT2);
160     want2.SetType(TYPE2);
161     // make common event data
162     CommonEventData data2;
163     data2.SetWant(want2);
164 
165     EXPECT_TRUE(innerCommonEventManager.PublishCommonEvent(
166         data2, publishInfo, nullptr, recordTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "hello"));
167 
168     sleep(1);
169 
170     CommonEventData Stickydata;
171     // EXPECT_TRUE(
172     //     OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance()->GetStickyCommonEvent(EVENT2, Stickydata));
173     // EXPECT_EQ(EVENT2, Stickydata.GetWant().GetAction());
174     // EXPECT_EQ(TYPE2, Stickydata.GetWant().GetType());
175 }
176 
177 /*
178  * @tc.number: CommonEventStickyTest_0300
179  * @tc.name: test StickyCommonEvent
180  * @tc.desc: The event parameter of the obtained sticky event is ""
181  */
182 HWTEST_F(CommonEventStickyTest, CommonEventStickyTest_0300, Function | MediumTest | Level1)
183 {
184     /* Publish */
185 
186     // make a want
187     Want want;
188     want.SetAction(EVENT3);
189     want.SetType(TYPE);
190     // make common event data
191     CommonEventData data;
192     data.SetWant(want);
193 
194     // make publish info
195     CommonEventPublishInfo publishInfo;
196     publishInfo.SetSticky(true);
197 
198     struct tm recordTime = {0};
199     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
200 
201     InnerCommonEventManager innerCommonEventManager;
202     EXPECT_TRUE(innerCommonEventManager.PublishCommonEvent(
203         data, publishInfo, nullptr, recordTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "hello"));
204 
205     CommonEventData Stickydata;
206     EXPECT_FALSE(
207         OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance()->GetStickyCommonEvent("", Stickydata));
208 }
209 
210 /*
211  * @tc.number: CommonEventStickyTest_0400
212  * @tc.name: test StickyCommonEvent
213  * @tc.desc: Get an unpublished sticky event
214  */
215 HWTEST_F(CommonEventStickyTest, CommonEventStickyTest_0400, Function | MediumTest | Level1)
216 {
217     /* Publish */
218     // make a want
219     Want want;
220     want.SetAction(EVENT4);
221     want.SetType(TYPE);
222     // make common event data
223     CommonEventData data;
224     data.SetWant(want);
225 
226     // make publish info
227     CommonEventPublishInfo publishInfo;
228     publishInfo.SetSticky(true);
229 
230     struct tm recordTime = {0};
231     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
232 
233     InnerCommonEventManager innerCommonEventManager;
234     EXPECT_TRUE(innerCommonEventManager.PublishCommonEvent(
235         data, publishInfo, nullptr, recordTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "hello"));
236 
237     CommonEventData Stickydata;
238     EXPECT_FALSE(
239         OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance()->GetStickyCommonEvent(EVENT6, Stickydata));
240 }
241 
242 /*
243  * @tc.number: CommonEventStickyTest_0500
244  * @tc.name: test StickyCommonEvent
245  * @tc.desc: Gets a non sticky event
246  */
247 HWTEST_F(CommonEventStickyTest, CommonEventStickyTest_0500, Function | MediumTest | Level1)
248 {
249     /* Publish */
250     // make a want
251     Want want;
252     want.SetAction(EVENT5);
253     want.SetType(TYPE);
254     // make common event data
255     CommonEventData data;
256     data.SetWant(want);
257 
258     // make publish info
259     CommonEventPublishInfo publishInfo;
260     publishInfo.SetSticky(false);
261 
262     struct tm recordTime = {0};
263     OHOS::Security::AccessToken::AccessTokenID tokenID = 1;
264 
265     InnerCommonEventManager innerCommonEventManager;
266     EXPECT_TRUE(innerCommonEventManager.PublishCommonEvent(
267         data, publishInfo, nullptr, recordTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "hello"));
268 
269     CommonEventData Stickydata;
270     EXPECT_FALSE(
271         OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance()->GetStickyCommonEvent(EVENT5, Stickydata));
272 }
273 
274 /*
275  * @tc.number: FindStickyEvents_0600
276  * @tc.name: Find events of no subscriber info
277  * @tc.desc: Get invalid return value.
278  */
279 HWTEST_F(CommonEventStickyTest, FindStickyEvents_0600, TestSize.Level1)
280 {
281     // make a vector of records
282     std::vector<std::shared_ptr<CommonEventRecord>> records;
283 
284     // get common event sticky manager
285     auto stickyManagerPtr = OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance();
286     // find sticky events
287     int result = stickyManagerPtr->FindStickyEvents(nullptr, records);
288 
289     EXPECT_EQ(result, OHOS::ERR_INVALID_VALUE);
290 }
291 
292 /*
293  * @tc.number: FindStickyEvents_0700
294  * @tc.name: Fail to find events of subscriber info
295  * @tc.desc: No event would be pushed into common event records.
296  */
297 HWTEST_F(CommonEventStickyTest, FindStickyEvents_0700, TestSize.Level1)
298 {
299     // make matching skills
300     MatchingSkills matchingSkills;
301     matchingSkills.AddEvent(STRING_EVENT);
302 
303     // make subscriber info
304     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
305     auto subscribeInfoPtr = std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
306 
307     // make a vector of records
308     std::vector<std::shared_ptr<CommonEventRecord>> records;
309 
310     // get common event sticky manager
311     auto stickyManagerPtr = OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance();
312     // find sticky events
313     int result = stickyManagerPtr->FindStickyEvents(subscribeInfoPtr, records);
314     // check result of finding sticky events
315     EXPECT_EQ(result, OHOS::ERR_OK);
316 }
317 
318 /*
319  * @tc.number: FindStickyEvents_0800
320  * @tc.name: Succeed to find events of subscriber info
321  * @tc.desc: Found events would be pushed into common event records.
322  */
323 HWTEST_F(CommonEventStickyTest, FindStickyEvents_0800, TestSize.Level1)
324 {
325     // make matching skills
326     MatchingSkills matchingSkills;
327     matchingSkills.AddEvent(STRING_EVENT);
328 
329     // make subscriber info
330     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
331     auto subscribeInfoPtr = std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
332 
333     // make a vector of records
334     std::vector<std::shared_ptr<CommonEventRecord>> records;
335     // check size of the records
336     EXPECT_EQ((int)records.size(), 0);
337 
338     // make a want
339     Want want;
340     want.SetAction(STRING_EVENT);
341 
342     // make common event data
343     CommonEventData data;
344     data.SetWant(want);
345 
346     // make a publish info and set it unordered
347     CommonEventPublishInfo publishInfo;
348     publishInfo.SetOrdered(false);
349 
350     // make a record
351     auto recordPtr = std::make_shared<CommonEventRecord>();
352     recordPtr->commonEventData = std::make_shared<CommonEventData>(data);
353     recordPtr->publishInfo = std::make_shared<CommonEventPublishInfo>(publishInfo);
354 
355     // get common event sticky manager
356     auto stickyManagerPtr = OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance();
357     // add a record in common event sticky manager
358     stickyManagerPtr->commonEventRecords_[STRING_EVENT] = recordPtr;
359 
360     // find sticky events
361     int result = stickyManagerPtr->FindStickyEvents(subscribeInfoPtr, records);
362     // check result of finding sticky events
363     EXPECT_EQ(result, OHOS::ERR_OK);
364     // check size of the records
365     EXPECT_EQ((int)records.size(), 1);
366 
367     // check the action
368     auto action = records.front()->commonEventData->GetWant().GetAction();
369     EXPECT_EQ(action, STRING_EVENT);
370 }
371 
372 /*
373  * @tc.number: UpdateStickyEvent_0900
374  * @tc.name: Update an unexisting record
375  * @tc.desc: A new record would be added.
376  */
377 HWTEST_F(CommonEventStickyTest, UpdateStickyEvent_0900, TestSize.Level1)
378 {
379     // make a want
380     Want want;
381     want.SetAction(STRING_EVENT);
382 
383     // make common event data
384     CommonEventData data;
385     data.SetWant(want);
386     auto dataPtr = std::make_shared<CommonEventData>(data);
387 
388     // make a publish info and set it unordered
389     CommonEventPublishInfo publishInfo;
390     publishInfo.SetOrdered(false);
391     auto publishInfoPtr = std::make_shared<CommonEventPublishInfo>(publishInfo);
392 
393     // get common event sticky manager
394     auto stickyManagerPtr = OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance();
395 
396     // update sticky event
397     CommonEventRecord eventRecord;
398     eventRecord.publishInfo = publishInfoPtr;
399     eventRecord.commonEventData = dataPtr;
400     int result = stickyManagerPtr->UpdateStickyEvent(eventRecord);
401     // check result of updating sticky event
402     EXPECT_EQ(result, OHOS::ERR_OK);
403 
404     // get record the event
405     auto recordPtr = stickyManagerPtr->commonEventRecords_[STRING_EVENT];
406     // check record of the event
407     EXPECT_NE(recordPtr, nullptr);
408 
409     // check the action
410     auto action = recordPtr->commonEventData->GetWant().GetAction();
411     EXPECT_EQ(action, STRING_EVENT);
412 }
413 
414 /*
415  * @tc.number: UpdateStickyEvent_1000
416  * @tc.name: Update an existing record
417  * @tc.desc: The existing record would be updated.
418  */
419 HWTEST_F(CommonEventStickyTest, UpdateStickyEvent_1000, TestSize.Level1)
420 {
421     // get common event sticky manager
422     auto stickyManagerPtr = OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance();
423 
424     // get record of the event
425     auto recordPtr = stickyManagerPtr->commonEventRecords_[STRING_EVENT];
426 
427     // make a want
428     Want want;
429     want.SetAction(STRING_EVENT);
430 
431     // make common event data
432     CommonEventData data;
433     data.SetWant(want);
434     recordPtr->commonEventData = std::make_shared<CommonEventData>(data);
435     // check the data
436     EXPECT_EQ(recordPtr->commonEventData->GetData(), "");
437 
438     // make a publish info and set it unordered
439     CommonEventPublishInfo publishInfo;
440     publishInfo.SetOrdered(false);
441     recordPtr->publishInfo = std::make_shared<CommonEventPublishInfo>(publishInfo);
442 
443     // make a new want
444     Want newWant;
445     newWant.SetAction(STRING_EVENT);
446 
447     // make common event data
448     CommonEventData newData;
449     newData.SetWant(newWant);
450     newData.SetData(STRING_DATA);
451     auto newDataPtr = std::make_shared<CommonEventData>(newData);
452 
453     // make a publish info and set it unordered
454     CommonEventPublishInfo newPublishInfo;
455     newPublishInfo.SetOrdered(false);
456     auto newPublishInfoPtr = std::make_shared<CommonEventPublishInfo>(newPublishInfo);
457 
458     // update sticky event
459     CommonEventRecord eventRecord;
460     eventRecord.publishInfo = newPublishInfoPtr;
461     eventRecord.commonEventData = newDataPtr;
462     int result = stickyManagerPtr->UpdateStickyEvent(eventRecord);
463     // check result of updating sticky event
464     EXPECT_EQ(result, OHOS::ERR_OK);
465 
466     // get record the event
467     recordPtr = stickyManagerPtr->commonEventRecords_[STRING_EVENT];
468     // check record of the event
469     EXPECT_NE(recordPtr, nullptr);
470 
471     // check the action
472     auto action = recordPtr->commonEventData->GetWant().GetAction();
473     EXPECT_EQ(action, STRING_EVENT);
474 
475     // check the data
476     auto updatedData = recordPtr->commonEventData->GetData();
477     EXPECT_EQ(updatedData, STRING_DATA);
478 }
479 
480 /*
481  * @tc.number: RemoveStickyCommonEvent_1100
482  * @tc.name: test RemoveStickyCommonEvent
483  * @tc.desc: Get sticky common event failed when removed.
484  */
485 HWTEST_F(CommonEventStickyTest, RemoveStickyCommonEvent_1100, Function | MediumTest | Level1)
486 {
487     Want want;
488     want.SetAction(EVENT);
489     want.SetType(TYPE);
490     // make common event data
491     CommonEventData data;
492     data.SetWant(want);
493 
494     // make publish info
495     CommonEventPublishInfo publishInfo;
496     publishInfo.SetSticky(true);
497 
498     struct tm recordTime = {0};
499     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
500 
501     InnerCommonEventManager innerCommonEventManager;
502     EXPECT_TRUE(innerCommonEventManager.PublishCommonEvent(
503         data, publishInfo, nullptr, recordTime, PID, SYSTEM_UID, tokenID, UNDEFINED_USER, "hello"));
504 
505     sleep(1);
506 
507     CommonEventData Stickydata;
508     // EXPECT_TRUE(
509     //     OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance()->GetStickyCommonEvent(EVENT, Stickydata));
510 
511     EXPECT_EQ(innerCommonEventManager.RemoveStickyCommonEvent(EVENT, SYSTEM_UID), OHOS::ERR_OK);
512     // EXPECT_FALSE(
513     //     OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance()->GetStickyCommonEvent(EVENT, Stickydata));
514 }
515 
516 /*
517  * @tc.number: DumpState_0100
518  * @tc.name: DumpState
519  * @tc.desc: Found events would be pushed into common event records.
520  */
521 HWTEST_F(CommonEventStickyTest, DumpState_0100, TestSize.Level1)
522 {
523     // make matching skills
524     MatchingSkills matchingSkills;
525     matchingSkills.AddEvent(STRING_EVENT);
526 
527     // make subscriber info
528     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
529     auto subscribeInfoPtr = std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
530 
531     // make a vector of records
532     std::vector<std::shared_ptr<CommonEventRecord>> records;
533     // check size of the records
534     EXPECT_EQ((int)records.size(), 0);
535 
536     // make a want
537     Want want;
538     want.SetAction(STRING_EVENT);
539 
540     // make common event data
541     CommonEventData data;
542     data.SetWant(want);
543 
544     // make a publish info and set it unordered
545     CommonEventPublishInfo publishInfo;
546     publishInfo.SetOrdered(false);
547 
548     // make a record
549     auto recordPtr = std::make_shared<CommonEventRecord>();
550     recordPtr->commonEventData = std::make_shared<CommonEventData>(data);
551     recordPtr->publishInfo = std::make_shared<CommonEventPublishInfo>(publishInfo);
552 
553     // get common event sticky manager
554     auto stickyManagerPtr = OHOS::DelayedSingleton<CommonEventStickyManager>::GetInstance();
555     // add a record in common event sticky manager
556     stickyManagerPtr->commonEventRecords_[STRING_EVENT] = recordPtr;
557 
558     // find sticky events
559     int result = stickyManagerPtr->FindStickyEvents(subscribeInfoPtr, records);
560     // check result of finding sticky events
561     EXPECT_EQ(result, OHOS::ERR_OK);
562     // check size of the records
563     EXPECT_EQ((int)records.size(), 1);
564 
565     std::string event = "this is event";
566     int32_t userId = 1;
567     std::vector<std::string> state;
568     stickyManagerPtr->DumpState(event, userId, state);
569 }
570