1 /*
2  * Copyright (c) 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_conversational_content.h"
21 #undef private
22 #undef protected
23 
24 using namespace testing::ext;
25 namespace OHOS {
26 namespace Notification {
27 class NotificationConversationalContentTest : public testing::Test {
28 public:
SetUpTestCase()29     static void SetUpTestCase() {}
TearDownTestCase()30     static void TearDownTestCase() {}
SetUp()31     void SetUp() {}
TearDown()32     void TearDown() {}
33 };
34 
35 /**
36  * @tc.name: ToJson_00001
37  * @tc.desc: Test ToJson parameters.
38  * @tc.type: FUNC
39  * @tc.require: issue
40  */
41 HWTEST_F(NotificationConversationalContentTest, ToJson_00001, Function | SmallTest | Level1)
42 {
43     MessageUser messageUser;
44     nlohmann::json jsonObject;
45     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
46     rrc->FromJson(jsonObject);
47     EXPECT_EQ(rrc->ToJson(jsonObject), true);
48 }
49 
50 /**
51  * @tc.name: FromJson_00001
52  * @tc.desc: Test FromJson parameters.
53  * @tc.type: FUNC
54  * @tc.require: issue
55  */
56 HWTEST_F(NotificationConversationalContentTest, FromJson_00001, Function | SmallTest | Level1)
57 {
58     MessageUser messageUser;
59     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
60 
61     nlohmann::json jsonObject = nlohmann::json{"processName", "process6", "messageUser", "arrivedTime1"};
62     rrc->FromJson(jsonObject);
63     EXPECT_EQ(jsonObject.is_object(), false);
64     EXPECT_EQ(rrc->FromJson(jsonObject), nullptr);
65 }
66 
67 /**
68  * @tc.name: FromJson_00002
69  * @tc.desc: Test FromJson parameters.
70  * @tc.type: FUNC
71  * @tc.require: issue
72  */
73 HWTEST_F(NotificationConversationalContentTest, FromJson_00002, Function | SmallTest | Level1)
74 {
75     MessageUser messageUser;
76     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
77 
78     nlohmann::json jsonObject = nlohmann::json{
79         {"processName", "process6"}, {"APL", 1},
80         {"version", 2}, {"tokenId", 685266937},
81         {"tokenAttr", 0},
82         {"dcaps", {"AT_CAP", "ST_CAP"}}};
83     messageUser.FromJson(jsonObject);
84     EXPECT_EQ(jsonObject.is_object(), true);
85 }
86 
87 /**
88  * @tc.name: FromJson_00003
89  * @tc.desc: Test FromJson parameters.
90  * @tc.type: FUNC
91  * @tc.require: issue
92  */
93 HWTEST_F(NotificationConversationalContentTest, FromJson_00003, Function | SmallTest | Level1)
94 {
95     MessageUser messageUser;
96     auto content = std::make_shared<NotificationConversationalContent>(messageUser);
97     nlohmann::json jsonObject = nlohmann::json{
98         {"messageUser", {{"key", "testKey"}, {"name", "test"}}},
99         {"message", {{"arrivedTime", 1}}}};
100     auto newContent = content->FromJson(jsonObject);
101 
102     EXPECT_NE(newContent, nullptr);
103 }
104 
105 /**
106  * @tc.name: Marshalling_00001
107  * @tc.desc: Test Marshalling parameters.
108  * @tc.type: FUNC
109  * @tc.require: issue
110  */
111 HWTEST_F(NotificationConversationalContentTest, Marshalling_00001, Function | SmallTest | Level1)
112 {
113     MessageUser messageUser;
114     Parcel parcel;
115     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
116     EXPECT_EQ(rrc->Marshalling(parcel), true);
117 }
118 
119 /**
120  * @tc.name: Unmarshalling_00001
121  * @tc.desc: Test Unmarshalling parameters.
122  * @tc.type: FUNC
123  * @tc.require: issue
124  */
125 HWTEST_F(NotificationConversationalContentTest, Unmarshalling_001, Function | SmallTest | Level1)
126 {
127     MessageUser messageUser;
128     bool unmarshalling = true;
129     Parcel parcel;
130     std::shared_ptr<NotificationConversationalContent> result =
131     std::make_shared<NotificationConversationalContent>(messageUser);
132 
133     if (nullptr != result) {
134         if (nullptr == result->Unmarshalling(parcel)) {
135             unmarshalling = false;
136         }
137     }
138     EXPECT_EQ(unmarshalling, false);
139 }
140 
141 /**
142  * @tc.name: Unmarshalling_00001
143  * @tc.desc: Test Unmarshalling parameters.
144  * @tc.type: FUNC
145  * @tc.require: issue
146  */
147 HWTEST_F(NotificationConversationalContentTest, Unmarshalling_002, Function | SmallTest | Level1)
148 {
149     MessageUser messageUser;
150     Parcel parcel;
151     auto content = std::make_shared<NotificationConversationalContent>(messageUser);
152     content->SetConversationTitle("testTitle");
153     content->AddConversationalMessage("text", 1, messageUser);
154     content->Marshalling(parcel);
155 
156     EXPECT_NE(content->Unmarshalling(parcel), nullptr);
157 }
158 
159 /**
160  * @tc.name: ReadFromParcel_00001
161  * @tc.desc: Test ReadFromParcel parameters.
162  * @tc.type: FUNC
163  * @tc.require: issue
164  */
165 HWTEST_F(NotificationConversationalContentTest, ReadFromParcel_00001, Function | SmallTest | Level1)
166 {
167     MessageUser messageUser;
168     Parcel parcel;
169     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
170     EXPECT_EQ(rrc->ReadFromParcel(parcel), false);
171 }
172 
173 /**
174  * @tc.name: AddConversationalMessage_00001
175  * @tc.desc: Test AddConversationalMessage parameters.
176  * @tc.type: FUNC
177  * @tc.require: issue
178  */
179 HWTEST_F(NotificationConversationalContentTest, AddConversationalMessage_00001, Function | SmallTest | Level1)
180 {
181     MessageUser messageUser;
182     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
183 
184     MessageUser  sender;
185     std::shared_ptr<NotificationConversationalMessage> messagePtr =
186         std::make_shared<NotificationConversationalMessage>("messageptr", 0, sender);
187     EXPECT_NE(messagePtr, nullptr);
188     rrc->AddConversationalMessage(messagePtr);
189     EXPECT_EQ(rrc->GetAllConversationalMessages().size(), 1);
190 }
191 
192 /**
193  * @tc.name: AddConversationalMessage_00002
194  * @tc.desc: Test AddConversationalMessage parameters.
195  * @tc.type: FUNC
196  * @tc.require: issue
197  */
198 HWTEST_F(NotificationConversationalContentTest, AddConversationalMessage_00002, Function | SmallTest | Level1)
199 {
200     MessageUser messageUser;
201     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
202 
203     MessageUser  sender;
204     std::shared_ptr<NotificationConversationalMessage> messagePtr = nullptr;
205     rrc->AddConversationalMessage(messagePtr);
206     EXPECT_EQ(rrc->GetAllConversationalMessages().size(), 0);
207     std::string result = rrc->Dump();
208     std::string ret = "NotificationConversationalContent{ title = , text = , additionalText = ,"
209         " lockScreenPicture = null, conversationTitle = , isGroup = false, messageUser = MessageUser{ key = , name = , "
210         "pixelMap = null, uri = , isMachine = false, isUserImportant = false }, messages =  }";
211     EXPECT_EQ(result, ret);
212 }
213 
214 /**
215  * @tc.name: ToJson_00002
216  * @tc.desc: Test ToJson parameters.
217  * @tc.type: FUNC
218  * @tc.require: issue
219  */
220 HWTEST_F(NotificationConversationalContentTest, ToJson_00002, Function | SmallTest | Level1)
221 {
222     MessageUser messageUser;
223     nlohmann::json jsonObject = nlohmann::json{
224         {"processName", "process6"}, {"APL", 1},
225         {"version", 2}, {"tokenId", 685266937},
226         {"tokenAttr", 0},
227         {"dcaps", {"AT_CAP", "ST_CAP"}}};
228     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
229     rrc->FromJson(jsonObject);
230     EXPECT_EQ(rrc->ToJson(jsonObject), true);
231 }
232 
233 /**
234  * @tc.name: ToJson_00003
235  * @tc.desc: Test ToJson parameters.
236  * @tc.type: FUNC
237  * @tc.require: issue
238  */
239 HWTEST_F(NotificationConversationalContentTest, ToJson_00003, Function | SmallTest | Level1)
240 {
241     MessageUser messageUser;
242     nlohmann::json jsonObject = nlohmann::json{
243         {"processName", "process6"}, {"APL", 1},
244         {"version", 2}, {"tokenId", 685266937},
245         {"tokenAttr", 0},
246         {"dcaps", {"AT_CAP", "ST_CAP"}}};
247     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
248     MessageUser sender;
249     std::shared_ptr<NotificationConversationalMessage> messagePtr =
250         std::make_shared<NotificationConversationalMessage>("testMessage", 0, sender);
251     EXPECT_NE(messagePtr, nullptr);
252     rrc->AddConversationalMessage(messagePtr);
253     rrc->FromJson(jsonObject);
254     EXPECT_EQ(rrc->ToJson(jsonObject), true);
255 }
256 
257 /**
258  * @tc.name: FromJson_00004
259  * @tc.desc: Test ToJson parameters.
260  * @tc.type: FUNC
261  * @tc.require: issue
262  */
263 HWTEST_F(NotificationConversationalContentTest, FromJson_00004, Function | SmallTest | Level1)
264 {
265     MessageUser messageUser;
266     nlohmann::json jsonObject = nlohmann::json{
267         {"processName", "process6"}, {"APL", 1},
268         {"version", 2}, {"tokenId", 685266937},
269         {"tokenAttr", 0},
270         {"dcaps", {"AT_CAP", "ST_CAP"}}};
271     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
272     std::shared_ptr<NotificationConversationalMessage> messagePtr = nullptr;
273     rrc->AddConversationalMessage(messagePtr);
274     EXPECT_NE(rrc->FromJson(jsonObject), nullptr);
275 }
276 
277 /**
278  * @tc.name: Dump_00001
279  * @tc.desc: Test Dump.
280  * @tc.type: FUNC
281  * @tc.require: issue
282  */
283 HWTEST_F(NotificationConversationalContentTest, Dump_00001, Function | SmallTest | Level1)
284 {
285     MessageUser messageUser;
286     auto rrc = std::make_shared<NotificationConversationalContent>(messageUser);
287     MessageUser sender;
288     int64_t timestamp = 0;
289     auto messagePtr = std::make_shared<NotificationConversationalMessage>("test", timestamp, sender);
290     rrc->AddConversationalMessage(messagePtr);
291     messagePtr = nullptr;
292     EXPECT_EQ(rrc->GetAllConversationalMessages().size(), 1);
293     std::string result = rrc->Dump();
294 }
295 
296 }
297 }
298