• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <memory>
18 #include <string>
19 #include <unistd.h>
20 #include "notification_capsule.h"
21 
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace Notification {
25 class NotificationCapsuleTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase() {}
TearDownTestCase()28     static void TearDownTestCase() {}
SetUp()29     void SetUp() {}
TearDown()30     void TearDown() {}
31 };
32 
33 /**
34  * @tc.name: SetTitle_00001
35  * @tc.desc: Test title_ parameters.
36  * @tc.type: FUNC
37  * @tc.require: issue
38  */
39 HWTEST_F(NotificationCapsuleTest, SetTitle_00001, Function | SmallTest | Level1)
40 {
41     auto rrc = std::make_shared<NotificationCapsule>();
42     std::string title = "testTitle";
43     rrc->SetTitle(title);
44     EXPECT_EQ(rrc->GetTitle(), title);
45 }
46 
47 /**
48  * @tc.name: SetBackgroundColor_00001
49  * @tc.desc: Test buttonNames_ parameters.
50  * @tc.type: FUNC
51  * @tc.require: issue
52  */
53 HWTEST_F(NotificationCapsuleTest, SetBackgroundColor_00001, Function | SmallTest | Level1)
54 {
55     auto rrc = std::make_shared<NotificationCapsule>();
56     std::string backgroundColor = "testBackgroundColor";
57     rrc->SetBackgroundColor(backgroundColor);
58     EXPECT_EQ(rrc->GetBackgroundColor(), backgroundColor);
59 }
60 
61 /**
62  * @tc.name: Dump_00001
63  * @tc.desc: Test buttonNames_ dump.
64  * @tc.type: FUNC
65  * @tc.require: issue
66  */
67 HWTEST_F(NotificationCapsuleTest, Dump_00001, Function | SmallTest | Level1)
68 {
69     auto rrc = std::make_shared<NotificationCapsule>();
70     std::string title = "testTitle";
71     rrc->SetTitle(title);
72     std::string backgroundColor = "testBackgroundColor";
73     rrc->SetBackgroundColor(backgroundColor);
74     EXPECT_EQ(rrc->Dump(), "Capsule{ title = " + title + ", backgroundColor = " + backgroundColor +
75         ", content = , icon = null, time = 0 }");
76 }
77 
78 /**
79  * @tc.name: FromJson_00001
80  * @tc.desc: Test FromJson parameters.
81  * @tc.type: FUNC
82  * @tc.require: issue
83  */
84 HWTEST_F(NotificationCapsuleTest, FromJson_00001, Function | SmallTest | Level1)
85 {
86     auto rrc = std::make_shared<NotificationCapsule>();
87     nlohmann::json jsonObject = nlohmann::json{"title", "backgroundColor", "icon"};
88     EXPECT_EQ(jsonObject.is_object(), false);
89     EXPECT_EQ(rrc->FromJson(jsonObject), nullptr);
90 }
91 
92 /**
93  * @tc.name: FromJson_00002
94  * @tc.desc: Test FromJson parameters.
95  * @tc.type: FUNC
96  * @tc.require: issue
97  */
98 HWTEST_F(NotificationCapsuleTest, FromJson_00002, Function | SmallTest | Level1)
99 {
100     auto rrc = std::make_shared<NotificationCapsule>();
101     nlohmann::json jsonObject = nlohmann::json{{"title", "testTitle"}, {"backgroundColor", "testBkColor"},
102         {"icon", ""}};
103     EXPECT_EQ(jsonObject.is_object(), true);
104     EXPECT_NE(rrc->FromJson(jsonObject), nullptr);
105 }
106 
107 /**
108  * @tc.name: Marshalling_00001
109  * @tc.desc: Test Marshalling parameters.
110  * @tc.type: FUNC
111  * @tc.require: issueI5WBBH
112  */
113 HWTEST_F(NotificationCapsuleTest, Marshalling_00001, Function | SmallTest | Level1)
114 {
115     Parcel parcel;
116     auto rrc = std::make_shared<NotificationCapsule>();
117     EXPECT_EQ(rrc->Marshalling(parcel), true);
118 }
119 
120 /**
121  * @tc.name: Unmarshalling_00001
122  * @tc.desc: Test Unmarshalling parameters.
123  * @tc.type: FUNC
124  * @tc.require: issueI5WBBH
125  */
126 HWTEST_F(NotificationCapsuleTest, Unmarshalling_00001, Function | SmallTest | Level1)
127 {
128     bool unmarshalling = true;
129     Parcel parcel;
130     std::shared_ptr<NotificationCapsule> result =
131         std::make_shared<NotificationCapsule>();
132     result->Marshalling(parcel);
133 
134     if (nullptr != result) {
135         if (nullptr == result->Unmarshalling(parcel)) {
136             unmarshalling = false;
137         }
138     }
139     EXPECT_EQ(unmarshalling, true);
140 }
141 
142 /**
143  * @tc.name: SetIcon_00001
144  * @tc.desc: Test SetIcon.
145  * @tc.type: FUNC
146  * @tc.require: issueI5WBBH
147  */
148 HWTEST_F(NotificationCapsuleTest, SetIcon_00001, Function | SmallTest | Level1)
149 {
150     auto pixmap = std::make_shared<Media::PixelMap>();
151     auto capsule = std::make_shared<NotificationCapsule>();
152     capsule->SetIcon(pixmap);
153     EXPECT_NE(capsule->GetIcon(), nullptr);
154 }
155 
156 /**
157  * @tc.name: SetContent_00001
158  * @tc.desc: Test SetContent.
159  * @tc.type: FUNC
160  * @tc.require: issueI5WBBH
161  */
162 HWTEST_F(NotificationCapsuleTest, SetContent_00001, Function | SmallTest | Level1)
163 {
164     NotificationCapsule capsule;
165     capsule.SetContent("test");
166     ASSERT_EQ(capsule.GetContent(), "test");
167 }
168 
169 /**
170  * @tc.name: SetTime_00001
171  * @tc.desc: Test SetTime.
172  * @tc.type: FUNC
173  * @tc.require: issueI5WBBH
174  */
175 HWTEST_F(NotificationCapsuleTest, SetTime_00001, Function | SmallTest | Level1)
176 {
177     NotificationCapsule capsule;
178     capsule.SetTime(1);
179     ASSERT_EQ(capsule.GetTime(), 1);
180 }
181 
182 /**
183  * @tc.name: GetCapsuleButton_00001
184  * @tc.desc: Test GetCapsuleButton.
185  * @tc.type: FUNC
186  * @tc.require: issueI5WBBH
187  */
188 HWTEST_F(NotificationCapsuleTest, GetCapsuleButton_00001, Function | SmallTest | Level1)
189 {
190     NotificationCapsule capsule;
191 
192     std::vector<NotificationIconButton> buttons;
193     NotificationIconButton button;
194     buttons.push_back(button);
195 
196     capsule.SetCapsuleButton(buttons);
197     ASSERT_EQ(capsule.GetCapsuleButton().size(), 1);
198 }
199 
200 /**
201  * @tc.name: GetCapsuleButton_00002
202  * @tc.desc: Test GetCapsuleButton.
203  * @tc.type: FUNC
204  * @tc.require: issueI5WBBH
205  */
206 HWTEST_F(NotificationCapsuleTest, GetCapsuleButton_00002, Function | SmallTest | Level1)
207 {
208     NotificationCapsule capsule;
209     capsule.SetContent("test");
210 
211     nlohmann::json jsonObject;
212     auto res = capsule.ToJson(jsonObject);
213     ASSERT_TRUE(res);
214 
215     auto jsonString = jsonObject.dump();
216 
217     auto it = jsonString.find("content");
218     ASSERT_NE(it, std::string::npos);
219 
220     sptr<NotificationCapsule> capsuleSptr = capsule.FromJson(jsonObject);
221     ASSERT_NE(capsuleSptr, nullptr);
222     ASSERT_EQ(capsuleSptr->GetContent(), "test");
223 }
224 
225 /**
226  * @tc.name: FromJson_00003
227  * @tc.desc: Test FromJson_00003.
228  * @tc.type: FUNC
229  * @tc.require: issueI5WBBH
230  */
231 HWTEST_F(NotificationCapsuleTest, FromJson_00003, Function | SmallTest | Level1)
232 {
233     NotificationCapsule capsule;
234     capsule.SetContent("test");
235 
236     nlohmann::json jsonObject;
237     auto res = capsule.ToJson(jsonObject);
238     ASSERT_TRUE(res);
239 
240     auto jsonString = jsonObject.dump();
241 
242     auto it = jsonString.find("content");
243     ASSERT_NE(it, std::string::npos);
244 
245     sptr<NotificationCapsule> capsuleSptr = capsule.FromJson(jsonObject);
246     ASSERT_NE(capsuleSptr, nullptr);
247     ASSERT_EQ(capsuleSptr->GetContent(), "test");
248 }
249 
250 /**
251  * @tc.name: Unmarshalling_00002
252  * @tc.desc: Test Unmarshalling_00002.
253  * @tc.type: FUNC
254  * @tc.require: issueI5WBBH
255  */
256 HWTEST_F(NotificationCapsuleTest, Unmarshalling_00002, Function | SmallTest | Level1)
257 {
258     NotificationCapsule capsule;
259     capsule.SetContent("test");
260 
261     Parcel parcel;
262     auto res = capsule.Marshalling(parcel);
263     ASSERT_TRUE(res);
264 
265     sptr<NotificationCapsule> capsuleSptr = capsule.Unmarshalling(parcel);
266     ASSERT_NE(capsuleSptr, nullptr);
267     ASSERT_EQ(capsuleSptr->GetContent(), "test");
268 }
269 
270 /**
271  * @tc.name: ResetIcon_00001
272  * @tc.desc: Test ResetIcon.
273  * @tc.type: FUNC
274  * @tc.require: issueI5WBBH
275  */
276 HWTEST_F(NotificationCapsuleTest, ResetIcon_00001, Function | SmallTest | Level1)
277 {
278     NotificationCapsule capsule;
279     auto pixmap = std::make_shared<Media::PixelMap>();
280     capsule.SetIcon(pixmap);
281     ASSERT_NE(capsule.GetIcon(), nullptr);
282     capsule.ResetIcon();
283     ASSERT_EQ(capsule.GetIcon(), nullptr);
284 }
285 }
286 }
287