• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
18 #include "ans_log_wrapper.h"
19 
20 #define private public
21 #define protected public
22 #include "notification_do_not_disturb_profile.h"
23 #undef private
24 #undef protected
25 
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace Notification {
29 class NotificationDoNotDisturbProfileTest : public testing::Test {
30 public:
SetUpTestCase()31     static void SetUpTestCase() {}
TearDownTestCase()32     static void TearDownTestCase() {}
SetUp()33     void SetUp() {}
TearDown()34     void TearDown() {}
35 };
36 
37 /**
38  * @tc.name: SetProfileId_0100
39  * @tc.desc: test SetProfileId parameters.
40  * @tc.type: FUNC
41  */
42 HWTEST_F(NotificationDoNotDisturbProfileTest, SetProfileId_0100, TestSize.Level1)
43 {
44     int32_t id = 1;
45     std::string name = "name";
46     std::vector<NotificationBundleOption> trustlist;
47     NotificationBundleOption bundleOption;
48     trustlist.emplace_back(bundleOption);
49     auto rrc = std::make_shared<NotificationDoNotDisturbProfile>(id, name, trustlist);
50     rrc->SetProfileId(2);
51     EXPECT_EQ(rrc->GetProfileId(), 2);
52 }
53 
54 /**
55  * @tc.name: SetProfileName_0100
56  * @tc.desc: test SetProfileName parameters.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(NotificationDoNotDisturbProfileTest, SetProfileName_0100, TestSize.Level1)
60 {
61     int32_t id = 1;
62     std::string name = "name";
63     std::vector<NotificationBundleOption> trustlist;
64     NotificationBundleOption bundleOption;
65     trustlist.emplace_back(bundleOption);
66     auto rrc = std::make_shared<NotificationDoNotDisturbProfile>(id, name, trustlist);
67     rrc->SetProfileName("newName");
68     EXPECT_EQ(rrc->GetProfileName(), "newName");
69 }
70 
71 /**
72  * @tc.name: SetProfileTrustList_0100
73  * @tc.desc: test SetProfileTrustList parameters.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(NotificationDoNotDisturbProfileTest, SetProfileTrustList_0100, TestSize.Level1)
77 {
78     int32_t id = 1;
79     std::string name = "name";
80     std::vector<NotificationBundleOption> trustlist;
81     NotificationBundleOption bundleOption;
82     trustlist.emplace_back(bundleOption);
83     auto rrc = std::make_shared<NotificationDoNotDisturbProfile>(id, name, trustlist);
84     std::vector<NotificationBundleOption> myTrustlist;
85     bundleOption.SetBundleName("bundleName");
86     bundleOption.SetUid(1);
87     myTrustlist.emplace_back(bundleOption);
88     rrc->SetProfileTrustList(myTrustlist);
89     auto getTrust = rrc->GetProfileTrustList();
90     EXPECT_EQ(getTrust[0].GetUid(), myTrustlist[0].GetUid());
91     EXPECT_EQ(getTrust[0].GetBundleName(), myTrustlist[0].GetBundleName());
92 }
93 
94 /**
95  * @tc.name: Marshalling_0100
96  * @tc.desc: test Marshalling when it can run to end.
97  * @tc.type: FUNC
98  */
99 HWTEST_F(NotificationDoNotDisturbProfileTest, Marshalling_0100, TestSize.Level1)
100 {
101     int32_t id = 1;
102     std::string name = "name";
103     std::vector<NotificationBundleOption> trustlist;
104     NotificationBundleOption bundleOption;
105     trustlist.emplace_back(bundleOption);
106     auto rrc = std::make_shared<NotificationDoNotDisturbProfile>(id, name, trustlist);
107     Parcel parcel;
108     auto res = rrc->Marshalling(parcel);
109     EXPECT_EQ(res, true);
110 }
111 
112 /**
113  * @tc.name: ReadFromParcel_0200
114  * @tc.desc: test it when trustlist_ emplace success.
115  * @tc.type: FUNC
116  */
117 HWTEST_F(NotificationDoNotDisturbProfileTest, ReadFromParcel_0200, TestSize.Level1)
118 {
119     int32_t id = 1;
120     std::string name = "name";
121     std::vector<NotificationBundleOption> trustlist;
122     NotificationBundleOption bundleOption;
123     trustlist.emplace_back(bundleOption);
124     auto rrc = std::make_shared<NotificationDoNotDisturbProfile>(id, name, trustlist);
125 
126     Parcel parcel;
127     parcel.WriteUint32(10);
128     sptr<NotificationBundleOption> notification = new (std::nothrow) NotificationBundleOption();
129     parcel.WriteParcelable(notification);
130     auto res = rrc->ReadFromParcel(parcel);
131     EXPECT_EQ(res, true);
132 }
133 
134 /**
135  * @tc.name: Unmarshalling_0100
136  * @tc.desc: test it when Unmarshalling success.
137  * @tc.type: FUNC
138  */
139 HWTEST_F(NotificationDoNotDisturbProfileTest, Unmarshalling_0100, TestSize.Level1)
140 {
141     bool unmarshalling = true;
142     Parcel parcel;
143     int32_t id = 1;
144     std::string name = "name";
145     std::vector<NotificationBundleOption> trustlist;
146     NotificationBundleOption bundleOption;
147     trustlist.emplace_back(bundleOption);
148     std::shared_ptr<NotificationDoNotDisturbProfile> result =
149         std::make_shared<NotificationDoNotDisturbProfile>(id, name, trustlist);
150     if (nullptr != result) {
151         if (nullptr == result->Unmarshalling(parcel)) {
152             unmarshalling = false;
153         }
154     }
155     EXPECT_EQ(unmarshalling, true);
156 }
157 
158 /**
159  * @tc.name: ReadFromParcel_0300
160  * @tc.desc: test it when Unmarshalling success.
161  * @tc.type: FUNC
162  */
163 HWTEST_F(NotificationDoNotDisturbProfileTest, ReadFromParcel_0300, TestSize.Level1)
164 {
165     NotificationDoNotDisturbProfile notificationDoNotDisturbProfile;
166     Parcel parcel;
167     parcel.WriteInt64(1);
168     parcel.WriteString("1");
169     parcel.WriteUint32(1);
170 
171     sptr<NotificationBundleOption> bundleOption(new NotificationBundleOption());
172     parcel.WriteParcelable(bundleOption);
173     auto res = notificationDoNotDisturbProfile.ReadFromParcel(parcel);
174     ASSERT_TRUE(res);
175     ASSERT_EQ(notificationDoNotDisturbProfile.GetProfileTrustList().size(), 1);
176 }
177 
178 /**
179  * @tc.name: FromJson_0100
180  * @tc.desc: test it when Unmarshalling success.
181  * @tc.type: FUNC
182  */
183 HWTEST_F(NotificationDoNotDisturbProfileTest, FromJson_0100, TestSize.Level1)
184 {
185     NotificationDoNotDisturbProfile notificationDoNotDisturbProfile;
186     std::vector<NotificationBundleOption> trustList;
187     NotificationBundleOption notificationBundleOption;
188     trustList.push_back(notificationBundleOption);
189     notificationDoNotDisturbProfile.SetProfileTrustList(trustList);
190 
191     auto jsonString = notificationDoNotDisturbProfile.ToJson();
192 
193     NotificationDoNotDisturbProfile temp;
194     temp.FromJson(jsonString);
195 
196     ASSERT_EQ(temp.GetProfileTrustList().size(), 1);
197 }
198 }  // namespace Notification
199 }  // namespace OHOS
200