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 }");
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 
133     if (nullptr != result) {
134         if (nullptr == result->Unmarshalling(parcel)) {
135             unmarshalling = false;
136         }
137     }
138     EXPECT_EQ(unmarshalling, true);
139 }
140 
141 /**
142  * @tc.name: SetIcon_00001
143  * @tc.desc: Test SetIcon.
144  * @tc.type: FUNC
145  * @tc.require: issueI5WBBH
146  */
147 HWTEST_F(NotificationCapsuleTest, SetIcon_00001, Function | SmallTest | Level1)
148 {
149     auto pixmap = std::make_shared<Media::PixelMap>();
150     auto capsule = std::make_shared<NotificationCapsule>();
151     capsule->SetIcon(pixmap);
152     EXPECT_NE(capsule->GetIcon(), nullptr);
153 }
154 }
155 }
156