1 /*
2 * Copyright (c) 2025-2026 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gtest/gtest.h"
17 #include "common_event_publish_info.h"
18
19 using namespace testing;
20 using namespace testing::ext;
21 using namespace OHOS;
22 using namespace OHOS::EventFwk;
23 namespace {
24 const int32_t SUBSCRIBER_UIDS_MAX_NUM = 3;
25 }
26
27 class CommonEventPublishInfoTest : public ::testing::Test {
28 protected:
SetUp()29 void SetUp() override {}
TearDown()30 void TearDown() override {}
31 };
32
33
34 /**
35 * @tc.name : SetSubscriberUid_ShouldSetCorrectly_WhenUidsLessThanMax
36 * @tc.number: CommonEventPublishInfoTest_001
37 * @tc.desc : Test SetSubscriberUid method when the size of subscriberUids is less than the maximum limit
38 */
39 HWTEST_F(CommonEventPublishInfoTest, SetSubscriberUid_ShouldSetCorrectly_WhenUidsLessThanMax, TestSize.Level0)
40 {
41 CommonEventPublishInfo commonEventPublishInfo;
42 std::vector<int32_t> subscriberUids = {1, 2, 3};
43 commonEventPublishInfo.SetSubscriberUid(subscriberUids);
44 EXPECT_EQ(commonEventPublishInfo.GetSubscriberUid(), subscriberUids);
45 }
46
47 /**
48 * @tc.name : SetSubscriberUid_ShouldSetCorrectly_WhenUidsMoreThanMax
49 * @tc.number: CommonEventPublishInfoTest_002
50 * @tc.desc : Test SetSubscriberUid method when the size of subscriberUids is more than the maximum limit
51 */
52 HWTEST_F(CommonEventPublishInfoTest, SetSubscriberUid_ShouldSetCorrectly_WhenUidsMoreThanMax, TestSize.Level0)
53 {
54 CommonEventPublishInfo commonEventPublishInfo;
55 std::vector<int32_t> subscriberUids(SUBSCRIBER_UIDS_MAX_NUM + 1, 1);
56 commonEventPublishInfo.SetSubscriberUid(subscriberUids);
57 std::vector<int32_t> expectedUids(subscriberUids.begin(), subscriberUids.begin() + SUBSCRIBER_UIDS_MAX_NUM);
58 EXPECT_EQ(commonEventPublishInfo.GetSubscriberUid(), expectedUids);
59 }
60
61 /**
62 * @tc.name : SetSubscriberUid_ShouldSetEmpty_WhenUidsIsEmpty
63 * @tc.number: CommonEventPublishInfoTest_003
64 * @tc.desc : Test SetSubscriberUid method when the subscriberUids is empty
65 */
66 HWTEST_F(CommonEventPublishInfoTest, SetSubscriberUid_ShouldSetEmpty_WhenUidsIsEmpty, TestSize.Level0)
67 {
68 CommonEventPublishInfo commonEventPublishInfo;
69 std::vector<int32_t> subscriberUids;
70 commonEventPublishInfo.SetSubscriberUid(subscriberUids);
71 EXPECT_TRUE(commonEventPublishInfo.GetSubscriberUid().empty());
72 }
73
74 /*
75 * tc.number: CommonEventPublishInfo_001
76 * tc.name: test ReadFromParcel
77 * tc.type: FUNC
78 * tc.require: issueI5RULW
79 * tc.desc: Invoke ReadFromParcel interface verify whether it is normal
80 */
81 HWTEST_F(CommonEventPublishInfoTest, CommonEventPublishInfo_001, TestSize.Level0)
82 {
83 Parcel parcel;
84 CommonEventPublishInfo commonEventPublishInfo;
85 commonEventPublishInfo.SetBundleName("testBundle");
86 commonEventPublishInfo.SetOrdered(true);
87 commonEventPublishInfo.SetSticky(true);
88 std::vector<std::string> permissions;
89 permissions.push_back("testPermission");
90 commonEventPublishInfo.SetSubscriberPermissions(permissions);
91 commonEventPublishInfo.SetSubscriberType(OHOS::EventFwk::SYSTEM_SUBSCRIBER_TYPE);
92 commonEventPublishInfo.SetValidationRule(OHOS::EventFwk::ValidationRule::OR);
93 std::vector<int32_t> uids;
94 commonEventPublishInfo.SetSubscriberUid(uids);
95
96 EXPECT_EQ(commonEventPublishInfo.Marshalling(parcel), true);
97
98 sptr<CommonEventPublishInfo> publishInfo = CommonEventPublishInfo::Unmarshalling(parcel);
99 EXPECT_EQ(publishInfo->GetBundleName(), "testBundle");
100 EXPECT_TRUE(publishInfo->IsOrdered());
101 EXPECT_TRUE(publishInfo->IsSticky());
102 EXPECT_EQ(publishInfo->GetSubscriberType(), OHOS::EventFwk::SYSTEM_SUBSCRIBER_TYPE);
103 EXPECT_EQ(publishInfo->GetValidationRule(), OHOS::EventFwk::ValidationRule::OR);
104 EXPECT_EQ(publishInfo->GetSubscriberPermissions().size(), 1);
105 EXPECT_EQ(publishInfo->GetSubscriberUid().size(), 0);
106 }
107
108 HWTEST_F(CommonEventPublishInfoTest, GetFilterSettings_002, TestSize.Level0)
109 {
110 CommonEventPublishInfo commonEventPublishInfo;
111 EXPECT_EQ(commonEventPublishInfo.GetFilterSettings(), 0);
112 }