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 <gtest/gtest.h>
17 
18 #define private public
19 #define protected public
20 #include "notification_basic_content.h"
21 #include "notification_content.h"
22 #undef private
23 #undef protected
24 
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace Notification {
28 class NotificationContentTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase() {};
TearDownTestCase()31     static void TearDownTestCase() {};
SetUp()32     void SetUp() {};
TearDown()33     void TearDown() {};
34 };
35 
36 /**
37  * @tc.name: NotificationContentMarshalling_0100
38  * @tc.desc: Marshalling
39  * @tc.type: FUNC
40  * @tc.require: issueI5S0ZS
41  */
HWTEST_F(NotificationContentTest,NotificationContentMarshalling_0100,Level1)42 HWTEST_F(NotificationContentTest, NotificationContentMarshalling_0100, Level1)
43 {
44     Parcel parcel;
45     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
46     EXPECT_NE(normalContent, nullptr);
47     NotificationContent notificationContent(normalContent);
48     auto result = notificationContent.Marshalling(parcel);
49     EXPECT_EQ(result, true);
50 }
51 
52 /**
53  * @tc.name: NotificationContentReadFromParcel_0100
54  * @tc.desc: ReadFromParcel
55  * @tc.type: FUNC
56  * @tc.require: issueI5S0ZS
57  */
HWTEST_F(NotificationContentTest,NotificationContentReadFromParcel_0100,Level1)58 HWTEST_F(NotificationContentTest, NotificationContentReadFromParcel_0100, Level1)
59 {
60     Parcel parcel;
61     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
62     EXPECT_NE(normalContent, nullptr);
63     NotificationContent notificationContent(normalContent);
64     auto result = notificationContent.ReadFromParcel(parcel);
65     EXPECT_EQ(result, true);
66 }
67 
68 /**
69  * @tc.name: NotificationBasicContentGetAdditionalText_0100
70  * @tc.desc: GetAdditionalText
71  * @tc.type: FUNC
72  * @tc.require: issueI5S0ZS
73  */
HWTEST_F(NotificationContentTest,NotificationBasicContentGetAdditionalText_0100,Level1)74 HWTEST_F(NotificationContentTest, NotificationBasicContentGetAdditionalText_0100, Level1)
75 {
76     std::string additionalText = "test";
77     NotificationBasicContent notificationBasicContent;
78     notificationBasicContent.SetAdditionalText(additionalText);
79     auto result = notificationBasicContent.GetAdditionalText();
80     EXPECT_EQ(result, additionalText);
81 }
82 
83 /**
84  * @tc.name: NotificationBasicContentGetText_0100
85  * @tc.desc: GetText
86  * @tc.type: FUNC
87  * @tc.require: issueI5S0ZS
88  */
HWTEST_F(NotificationContentTest,NotificationBasicContentGetText_0100,Level1)89 HWTEST_F(NotificationContentTest, NotificationBasicContentGetText_0100, Level1)
90 {
91     std::string Text = "test";
92     NotificationBasicContent notificationBasicContent;
93     notificationBasicContent.SetText(Text);
94     auto result = notificationBasicContent.GetText();
95     EXPECT_EQ(result, Text);
96 }
97 
98 /**
99  * @tc.name: NotificationBasicContentGetTitle_0100
100  * @tc.desc: GetTitle
101  * @tc.type: FUNC
102  * @tc.require: issueI5S0ZS
103  */
HWTEST_F(NotificationContentTest,NotificationBasicContentGetTitle_0100,Level1)104 HWTEST_F(NotificationContentTest, NotificationBasicContentGetTitle_0100, Level1)
105 {
106     std::string title = "titleTest";
107     NotificationBasicContent notificationBasicContent;
108     notificationBasicContent.SetTitle(title);
109     auto result = notificationBasicContent.GetTitle();
110     EXPECT_EQ(result, title);
111 }
112 
113 /**
114  * @tc.name: NotificationBasicContentMarshalling_0100
115  * @tc.desc: Marshalling
116  * @tc.type: FUNC
117  * @tc.require: issueI5S0ZS
118  */
HWTEST_F(NotificationContentTest,NotificationBasicContentMarshalling_0100,Level1)119 HWTEST_F(NotificationContentTest, NotificationBasicContentMarshalling_0100, Level1)
120 {
121     Parcel parcel;
122     NotificationBasicContent notificationBasicContent;
123     auto result = notificationBasicContent.Marshalling(parcel);
124     EXPECT_EQ(result, true);
125 }
126 
127 /**
128  * @tc.name: NotificationContentReadFromParcel_0200
129  * @tc.desc: ReadFromParcel
130  * @tc.type: FUNC
131  * @tc.require: issueI665WK
132  */
HWTEST_F(NotificationContentTest,NotificationContentReadFromParcel_0200,Level1)133 HWTEST_F(NotificationContentTest, NotificationContentReadFromParcel_0200, Level1)
134 {
135     std::shared_ptr<NotificationNormalContent> normalContent0 = nullptr;
136     NotificationContent notificationContent0(normalContent0);
137 
138     std::shared_ptr<NotificationLongTextContent> longTextContent = nullptr;
139     NotificationContent notificationContent1(longTextContent);
140 
141     std::shared_ptr<NotificationPictureContent> pictureContent = nullptr;
142     NotificationContent notificationContent2(pictureContent);
143 
144     std::shared_ptr<NotificationConversationalContent> conversationContent = nullptr;
145     NotificationContent notificationContent3(conversationContent);
146 
147     std::shared_ptr<NotificationMultiLineContent> multiLineContent = nullptr;
148     NotificationContent notificationContent4(multiLineContent);
149 
150     std::shared_ptr<NotificationMediaContent> mediaContent = nullptr;
151     NotificationContent notificationContent5(mediaContent);
152 
153     std::shared_ptr<NotificationLiveViewContent> liveViewContent = nullptr;
154     NotificationContent notificationContent6(liveViewContent);
155 
156     std::shared_ptr<NotificationLocalLiveViewContent> localLiveViewContent = nullptr;
157     NotificationContent notificationContent7(localLiveViewContent);
158 
159     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
160     NotificationContent notificationContent(normalContent);
161     auto result = notificationContent.GetContentType();
162     EXPECT_EQ(result, NotificationContent::Type::BASIC_TEXT);
163 
164     Parcel parcel;
165     auto result1 = notificationContent.ReadFromParcel(parcel);
166     EXPECT_EQ(result1, true);
167 }
168 
169 /**
170  * @tc.name: NotificationContentReadFromParcel_0300
171  * @tc.desc: ReadFromParcel
172  * @tc.type: FUNC
173  * @tc.require: issueI665WK
174  */
HWTEST_F(NotificationContentTest,NotificationContentReadFromParcel_0300,Level1)175 HWTEST_F(NotificationContentTest, NotificationContentReadFromParcel_0300, Level1)
176 {
177     std::shared_ptr<NotificationConversationalContent> conversationContent =
178         std::make_shared<NotificationConversationalContent>();
179     NotificationContent notificationContent(conversationContent);
180     auto result = notificationContent.GetContentType();
181     EXPECT_EQ(result, NotificationContent::Type::CONVERSATION);
182 
183     Parcel parcel;
184     auto result1 = notificationContent.ReadFromParcel(parcel);
185     EXPECT_EQ(result1, true);
186 }
187 
188 /**
189  * @tc.name: NotificationContentReadFromParcel_0400
190  * @tc.desc: ReadFromParcel
191  * @tc.type: FUNC
192  * @tc.require: issueI665WK
193  */
HWTEST_F(NotificationContentTest,NotificationContentReadFromParcel_0400,Level1)194 HWTEST_F(NotificationContentTest, NotificationContentReadFromParcel_0400, Level1)
195 {
196     std::shared_ptr<NotificationPictureContent> pictureContent = std::make_shared<NotificationPictureContent>();
197     NotificationContent notificationContent(pictureContent);
198     auto result = notificationContent.GetContentType();
199     EXPECT_EQ(result, NotificationContent::Type::PICTURE);
200 
201     Parcel parcel;
202     auto result1 = notificationContent.ReadFromParcel(parcel);
203     EXPECT_EQ(result1, true);
204 }
205 
206 /**
207  * @tc.name: NotificationContentReadFromParcel_0500
208  * @tc.desc: ReadFromParcel
209  * @tc.type: FUNC
210  * @tc.require: issueI665WK
211  */
HWTEST_F(NotificationContentTest,NotificationContentReadFromParcel_0500,Level1)212 HWTEST_F(NotificationContentTest, NotificationContentReadFromParcel_0500, Level1)
213 {
214     std::shared_ptr<NotificationMultiLineContent> multiLineContent = std::make_shared<NotificationMultiLineContent>();
215     NotificationContent notificationContent(multiLineContent);
216     auto result = notificationContent.GetContentType();
217     EXPECT_EQ(result, NotificationContent::Type::MULTILINE);
218 
219     Parcel parcel;
220     auto result1 = notificationContent.ReadFromParcel(parcel);
221     EXPECT_EQ(result1, true);
222 }
223 
224 /**
225  * @tc.name: NotificationContentReadFromParcel_0600
226  * @tc.desc: ReadFromParcel
227  * @tc.type: FUNC
228  * @tc.require: issueI665WK
229  */
HWTEST_F(NotificationContentTest,NotificationContentReadFromParcel_0600,Level1)230 HWTEST_F(NotificationContentTest, NotificationContentReadFromParcel_0600, Level1)
231 {
232     std::shared_ptr<NotificationMediaContent> mediaContent = std::make_shared<NotificationMediaContent>();
233     NotificationContent notificationContent(mediaContent);
234     auto result = notificationContent.GetContentType();
235     EXPECT_EQ(result, NotificationContent::Type::MEDIA);
236 
237     Parcel parcel;
238     auto result1 = notificationContent.ReadFromParcel(parcel);
239     EXPECT_EQ(result1, true);
240     notificationContent.Unmarshalling(parcel);
241 }
242 
243 /**
244  * @tc.name: ConvertJsonToContent_0100
245  * @tc.desc: ConvertJsonToContent
246  * @tc.type: FUNC
247  * @tc.require: issueI665WK
248  */
HWTEST_F(NotificationContentTest,ConvertJsonToContent_0100,Level1)249 HWTEST_F(NotificationContentTest, ConvertJsonToContent_0100, Level1)
250 {
251     std::shared_ptr<NotificationMediaContent> mediaContent = std::make_shared<NotificationMediaContent>();
252     NotificationContent notificationContent(mediaContent);
253 
254     nlohmann::json jsonObject;
255     auto result1 = notificationContent.ConvertJsonToContent(nullptr, jsonObject);
256     EXPECT_EQ(result1, false);
257 }
258 
259 /**
260  * @tc.name: FromJson_00002
261  * @tc.desc: Test FromJson parameters.
262  * @tc.type: FUNC
263  * @tc.require: issue
264  */
265 HWTEST_F(NotificationContentTest, FromJson_00002, Function | SmallTest | Level1)
266 {
267     std::shared_ptr<NotificationMediaContent> mediaContent = std::make_shared<NotificationMediaContent>();
268     NotificationContent notificationContent(mediaContent);
269     nlohmann::json jsonObject = nlohmann::json{"processName", "soundEnabled", "name", "arrivedTime1"};
270     notificationContent.FromJson(jsonObject);
271     EXPECT_EQ(jsonObject.is_object(), false);
272     EXPECT_EQ(notificationContent.FromJson(jsonObject), nullptr);
273 }
274 
275 /**
276  * @tc.name: FromJson_00003
277  * @tc.desc: Test FromJson parameters.
278  * @tc.type: FUNC
279  * @tc.require: issue
280  */
281 HWTEST_F(NotificationContentTest, FromJson_00003, Function | SmallTest | Level1)
282 {
283     std::shared_ptr<NotificationMediaContent> mediaContent = std::make_shared<NotificationMediaContent>();
284     NotificationContent notificationContent(mediaContent);
285     nlohmann::json jsonObject = nlohmann::json{
286         {"processName", "process6"}, {"APL", 1},
287         {"version", 2}, {"tokenId", 685266937},
288         {"tokenAttr", 0},
289         {"dcaps", {"AT_CAP", "ST_CAP"}}};
290     notificationContent.FromJson(jsonObject);
291     EXPECT_EQ(jsonObject.is_object(), true);
292 }
293 
294 /**
295  * @tc.name: FromJson_00004
296  * @tc.desc: Test FromJson parameters.
297  * @tc.type: FUNC
298  * @tc.require: issue
299  */
300 HWTEST_F(NotificationContentTest, FromJson_00004, Function | SmallTest | Level1)
301 {
302     NotificationContent content;
303     nlohmann::json jsonObject = nlohmann::json{{"contentType", 1}, {"content", {}}};
304     EXPECT_EQ(content.FromJson(jsonObject), nullptr);
305 
306     jsonObject["content"] = {{"text", "test"}, {"title", "testTitle"}};
307     jsonObject["contentType"] = NotificationContent::Type::BASIC_TEXT;
308     EXPECT_NE(content.FromJson(jsonObject), nullptr);
309 
310     jsonObject["contentType"] = NotificationContent::Type::CONVERSATION;
311     EXPECT_NE(content.FromJson(jsonObject), nullptr);
312 
313     jsonObject["contentType"] = NotificationContent::Type::LONG_TEXT;
314     EXPECT_NE(content.FromJson(jsonObject), nullptr);
315 
316     jsonObject["contentType"] = NotificationContent::Type::MULTILINE;
317     EXPECT_NE(content.FromJson(jsonObject), nullptr);
318 
319     jsonObject["contentType"] = NotificationContent::Type::PICTURE;
320     EXPECT_NE(content.FromJson(jsonObject), nullptr);
321 
322     jsonObject["contentType"] = NotificationContent::Type::LOCAL_LIVE_VIEW;
323     EXPECT_NE(content.FromJson(jsonObject), nullptr);
324 }
325 
326 /**
327  * @tc.name: ToJson_00001
328  * @tc.desc: Test ToJson parameters.
329  * @tc.type: FUNC
330  * @tc.require: issue
331  */
332 HWTEST_F(NotificationContentTest, ToJson_00001, Function | SmallTest | Level1)
333 {
334     std::shared_ptr<NotificationMediaContent> mediaContent = nullptr;
335     NotificationContent notificationContent(mediaContent);
336     std::shared_ptr<NotificationBasicContent> result = notificationContent.GetNotificationContent();
337     EXPECT_EQ(result, nullptr);
338     nlohmann::json jsonObject = nlohmann::json{
339         {"processName", "process6"}, {"APL", 1},
340         {"version", 2}, {"tokenId", 685266937},
341         {"tokenAttr", 0},
342         {"dcaps", {"AT_CAP", "ST_CAP"}}};
343     bool result2 = notificationContent.ToJson(jsonObject);
344     EXPECT_EQ(result2, false);
345 }
346 
347 /**
348  * @tc.name: NotificationContentMarshalling_0200
349  * @tc.desc: Marshalling
350  * @tc.type: FUNC
351  * @tc.require: issueI5S0ZS
352  */
HWTEST_F(NotificationContentTest,NotificationContentMarshalling_0200,Level1)353 HWTEST_F(NotificationContentTest, NotificationContentMarshalling_0200, Level1)
354 {
355     std::shared_ptr<NotificationNormalContent> normalContent = nullptr;
356     auto sptr1 = std::make_shared<NotificationContent>(normalContent);
357     EXPECT_NE(sptr1, nullptr);
358     std::shared_ptr<NotificationLongTextContent> longTextContent = nullptr;
359     auto sptr2 = std::make_shared<NotificationContent>(longTextContent);
360     EXPECT_NE(sptr2, nullptr);
361     std::shared_ptr<NotificationPictureContent> pictureContent = nullptr;
362     auto sptr3 = std::make_shared<NotificationContent>(pictureContent);
363     EXPECT_NE(sptr3, nullptr);
364     std::shared_ptr<NotificationConversationalContent> conversationContent = nullptr;
365     auto sptr4 = std::make_shared<NotificationContent>(conversationContent);
366     EXPECT_NE(sptr4, nullptr);
367     std::shared_ptr<NotificationMultiLineContent> multiLineContent = nullptr;
368     auto sptr5 = std::make_shared<NotificationContent>(multiLineContent);
369     EXPECT_NE(sptr5, nullptr);
370     std::shared_ptr<NotificationMediaContent> mediaContent = nullptr;
371     auto sptr6 = std::make_shared<NotificationContent>(mediaContent);
372     EXPECT_NE(sptr6, nullptr);
373 }
374 
375 /**
376  * @tc.name: ToJson_00002
377  * @tc.desc: Test ToJson parameters.
378  * @tc.type: FUNC
379  * @tc.require: issue
380  */
381 HWTEST_F(NotificationContentTest, ToJson_00002, Function | SmallTest | Level1)
382 {
383     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
384     EXPECT_NE(normalContent, nullptr);
385     NotificationContent notificationContent(normalContent);
386 
387     nlohmann::json jsonObject = nlohmann::json{
388         {"processName", "process6"}, {"APL", 1},
389         {"version", 2}, {"tokenId", 685266937},
390         {"tokenAttr", 0},
391         {"dcaps", {"AT_CAP", "ST_CAP"}}};
392     bool result2 = notificationContent.ToJson(jsonObject);
393     EXPECT_EQ(result2, true);
394 }
395 
396 /**
397  * @tc.name: NotificationBasicContentReadFromJson_00001
398  * @tc.desc: GetAdditionalText
399  * @tc.type: FUNC
400  * @tc.require: issueI5S0ZS
401  */
HWTEST_F(NotificationContentTest,NotificationBasicContentReadFromJson_00001,Level1)402 HWTEST_F(NotificationContentTest, NotificationBasicContentReadFromJson_00001, Level1)
403 {
404     auto notificationBasicContent = std::make_shared<NotificationBasicContent>();
405     nlohmann::json jsonObject = nlohmann::json{
406         {"text", "test"},
407         {"title", "test"},
408         {"additionalText", "test"}};
409     notificationBasicContent->ReadFromJson(jsonObject);
410     EXPECT_NE(notificationBasicContent, nullptr);
411 }
412 
413 /**
414  * @tc.name: Unmarshalling_00001
415  * @tc.desc: Test Unmarshalling
416  * @tc.type: FUNC
417  * @tc.require: issueI5S0ZS
418  */
HWTEST_F(NotificationContentTest,Unmarshalling_00001,Level1)419 HWTEST_F(NotificationContentTest, Unmarshalling_00001, Level1)
420 {
421     auto normalContent = std::make_shared<NotificationNormalContent>();
422     NotificationContent content(normalContent);
423 
424     Parcel parcel;
425     EXPECT_EQ(content.Marshalling(parcel), true);
426     EXPECT_NE(content.Unmarshalling(parcel), nullptr);
427 
428     auto conversationalContent = std::make_shared<NotificationConversationalContent>();
429     NotificationContent content1(conversationalContent);
430     EXPECT_EQ(content1.Marshalling(parcel), true);
431     EXPECT_NE(content1.Unmarshalling(parcel), nullptr);
432 
433     auto longContent = std::make_shared<NotificationLongTextContent>();
434     NotificationContent content2(longContent);
435     EXPECT_EQ(content2.Marshalling(parcel), true);
436     EXPECT_NE(content2.Unmarshalling(parcel), nullptr);
437 
438     auto pictureContent = std::make_shared<NotificationPictureContent>();
439     NotificationContent content3(pictureContent);
440     EXPECT_EQ(content3.Marshalling(parcel), true);
441     EXPECT_NE(content3.Unmarshalling(parcel), nullptr);
442 
443     auto mediaContent = std::make_shared<NotificationMediaContent>();
444     NotificationContent content4(mediaContent);
445     EXPECT_EQ(content4.Marshalling(parcel), true);
446     EXPECT_NE(content4.Unmarshalling(parcel), nullptr);
447 
448     auto multiLineContent = std::make_shared<NotificationMultiLineContent>();
449     NotificationContent content5(multiLineContent);
450     EXPECT_EQ(content5.Marshalling(parcel), true);
451     EXPECT_NE(content5.Unmarshalling(parcel), nullptr);
452 }
453 
454 /**
455  * @tc.name: Unmarshalling_00002
456  * @tc.desc: Test Unmarshalling
457  * @tc.type: FUNC
458  * @tc.require: issueI5S0ZS
459  */
HWTEST_F(NotificationContentTest,Unmarshalling_00002,Level1)460 HWTEST_F(NotificationContentTest, Unmarshalling_00002, Level1)
461 {
462     auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
463     NotificationContent content(localLiveViewContent);
464 
465     Parcel parcel;
466     EXPECT_EQ(content.Marshalling(parcel), true);
467     EXPECT_NE(content.Unmarshalling(parcel), nullptr);
468 
469     auto liveViewContent = std::make_shared<NotificationLiveViewContent>();
470     NotificationContent content1(liveViewContent);
471     EXPECT_EQ(content1.Marshalling(parcel), true);
472     EXPECT_NE(content1.Unmarshalling(parcel), nullptr);
473 }
474 }
475 }
476