1 /*
2  * Copyright (c) 2022-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 
18 #define private public
19 #define protected public
20 #include "notification_user_input.h"
21 #undef private
22 #undef protected
23 
24 using namespace testing::ext;
25 namespace OHOS {
26 namespace Notification {
27 class NotificationUserInputTest : 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: NotificationUserInput_00001
37  * @tc.desc: Test NotificationUserInput parameters.
38  * @tc.type: FUNC
39  * @tc.require: issue
40  */
41 HWTEST_F(NotificationUserInputTest, Dump_00001, Function | SmallTest | Level1)
42 {
43     std::string inputKey = "InputKey";
44     std::string tag = "Tag";
45     std::vector<std::string> options;
46     bool permitFreeFormInput = true;
47     std::set<std::string> permitMimeTypes;
48     std::shared_ptr<AAFwk::WantParams> additional = nullptr;
49     Notification::NotificationConstant::InputEditType editType =
50             Notification::NotificationConstant::InputEditType(1);
51     auto rrc = std::make_shared<NotificationUserInput>(inputKey, tag, options, permitFreeFormInput, permitMimeTypes,
52     additional, editType);
53     std::string ret = rrc->GetInputKey();
54     EXPECT_EQ(ret, inputKey);
55 }
56 
57 /**
58  * @tc.name: ToJson_00001
59  * @tc.desc: Test ToJson parameters.
60  * @tc.type: FUNC
61  * @tc.require: issue
62  */
63 HWTEST_F(NotificationUserInputTest, ToJson_00001, Function | SmallTest | Level1)
64 {
65     nlohmann::json jsonObject;
66     auto rrc = std::make_shared<NotificationUserInput>();
67     rrc->FromJson(jsonObject);
68     EXPECT_EQ(rrc->ToJson(jsonObject), true);
69 }
70 
71 /**
72  * @tc.name: ToJson_00002
73  * @tc.desc: Test ToJson parameters.
74  * @tc.type: FUNC
75  * @tc.require: issue
76  */
77 HWTEST_F(NotificationUserInputTest, ToJson_00002, Function | SmallTest | Level1)
78 {
79     std::string inputKey = "InputKey";
80     std::string tag = "Tag";
81     std::vector<std::string> options;
82     bool permitFreeFormInput = true;
83     std::set<std::string> permitMimeTypes;
84     std::shared_ptr<AAFwk::WantParams> additional = std::make_shared<AAFwk::WantParams>();;
85     Notification::NotificationConstant::InputEditType editType =
86         Notification::NotificationConstant::InputEditType(1);
87     auto rrc = std::make_shared<NotificationUserInput>(inputKey, tag, options, permitFreeFormInput, permitMimeTypes,
88     additional, editType);
89     std::shared_ptr<AAFwk::WantParams> ret = rrc->GetAdditionalData();
90 
91     nlohmann::json jsonObject;
92     rrc->FromJson(jsonObject);
93     EXPECT_EQ(rrc->ToJson(jsonObject), true);
94 }
95 
96 /**
97  * @tc.name: FromJson_00001
98  * @tc.desc: Test FromJson parameters.
99  * @tc.type: FUNC
100  * @tc.require: issue
101  */
102 HWTEST_F(NotificationUserInputTest, FromJson_00001, Function | SmallTest | Level1)
103 {
104     nlohmann::json jsonObject;
105     auto rrc = std::make_shared<NotificationUserInput>();
106     std::shared_ptr<NotificationUserInput> userInput = nullptr;
107     rrc->FromJson(jsonObject);
108     EXPECT_EQ(rrc->FromJson(jsonObject), nullptr);
109 }
110 
111 /**
112  * @tc.name: FromJson_00002
113  * @tc.desc: Test FromJson parameters.
114  * @tc.type: FUNC
115  * @tc.require: issue
116  */
117 HWTEST_F(NotificationUserInputTest, FromJson_00002, Function | SmallTest | Level1)
118 {
119     auto rrc = std::make_shared<NotificationUserInput>();
120     nlohmann::json jsonObject = nlohmann::json{"processName", "soundEnabled", "name", "arrivedTime1"};
121     rrc->FromJson(jsonObject);
122     EXPECT_EQ(jsonObject.is_object(), false);
123     EXPECT_EQ(rrc->FromJson(jsonObject), nullptr);
124 }
125 
126 /**
127  * @tc.name: FromJson_00003
128  * @tc.desc: Test FromJson parameters.
129  * @tc.type: FUNC
130  * @tc.require: issue
131  */
132 HWTEST_F(NotificationUserInputTest, FromJson_00003, Function | SmallTest | Level1)
133 {
134     auto rrc = std::make_shared<NotificationUserInput>();
135     nlohmann::json jsonObject = nlohmann::json{
136         {"processName", "process6"}, {"APL", 1},
137         {"version", 2}, {"tokenId", 685266937},
138         {"tokenAttr", 0},
139         {"dcaps", {"AT_CAP", "ST_CAP"}}};
140     rrc->FromJson(jsonObject);
141     EXPECT_EQ(jsonObject.is_object(), true);
142 }
143 
144 /**
145  * @tc.name: FromJson_00004
146  * @tc.desc: Test FromJson parameters.
147  * @tc.type: FUNC
148  * @tc.require: issue
149  */
150 HWTEST_F(NotificationUserInputTest, FromJson_00004, Function | SmallTest | Level1)
151 {
152     auto userInput = std::make_shared<NotificationUserInput>();
153     nlohmann::json jsonObject = nlohmann::json{
154         {"inputKey", "testKey"}, {"tag", "testTag"},
155         {"options", {"testOption"}}, {"permitFreeFormInput", 1},
156         {"permitMimeTypes", {"testType"}}, {"additionalData", "testData"},
157         {"editType", 1}};
158     EXPECT_EQ(jsonObject.is_object(), true);
159     auto res = userInput->FromJson(jsonObject);
160     EXPECT_EQ(res->GetPermitMimeTypes().size(), 1);
161 }
162 
163 /**
164  * @tc.name: Marshalling_00001
165  * @tc.desc: Test Marshalling parameters.
166  * @tc.type: FUNC
167  * @tc.require: issue
168  */
169 HWTEST_F(NotificationUserInputTest, Marshalling_00001, Function | SmallTest | Level1)
170 {
171     Parcel parcel;
172     auto rrc = std::make_shared<NotificationUserInput>();
173     EXPECT_EQ(rrc->Marshalling(parcel), true);
174 }
175 
176 /**
177  * @tc.name: Unmarshalling_00001
178  * @tc.desc: Test Unmarshalling parameters.
179  * @tc.type: FUNC
180  * @tc.require: issue
181  */
182 HWTEST_F(NotificationUserInputTest, Unmarshalling_001, Function | SmallTest | Level1)
183 {
184     bool unmarshalling = true;
185     Parcel parcel;
186     std::shared_ptr<NotificationUserInput> result =
187     std::make_shared<NotificationUserInput>();
188 
189     if (nullptr != result) {
190         if (nullptr == result->Unmarshalling(parcel)) {
191             unmarshalling = false;
192         }
193     }
194     EXPECT_EQ(unmarshalling, false);
195 }
196 
197 /**
198  * @tc.name: Unmarshalling_00002
199  * @tc.desc: Test Unmarshalling parameters.
200  * @tc.type: FUNC
201  * @tc.require: issue
202  */
203 HWTEST_F(NotificationUserInputTest, Unmarshalling_002, Function | SmallTest | Level1)
204 {
205     Parcel parcel;
206     auto userInput = std::make_shared<NotificationUserInput>("testKey");
207     std::vector<std::string> options = {"test1", "test2"};
208     userInput->SetOptions(options);
209     userInput->SetPermitMimeTypes("test", true);
210     EXPECT_EQ(userInput->Marshalling(parcel), true);
211     EXPECT_NE(userInput->Unmarshalling(parcel), nullptr);
212 }
213 
214 /**
215  * @tc.name: ReadFromParcel_00001
216  * @tc.desc: Test ReadFromParcel parameters.
217  * @tc.type: FUNC
218  * @tc.require: issue
219  */
220 HWTEST_F(NotificationUserInputTest, ReadFromParcel_00001, Function | SmallTest | Level1)
221 {
222     Parcel parcel;
223     auto rrc = std::make_shared<NotificationUserInput>();
224     EXPECT_EQ(rrc->ReadFromParcel(parcel), false);
225 }
226 
227 /**
228  * @tc.name: Create_00001
229  * @tc.desc: Test Create parameters.
230  * @tc.type: FUNC
231  * @tc.require: issue
232  */
233 HWTEST_F(NotificationUserInputTest, Create_00001, Function | SmallTest | Level1)
234 {
235     std::string inputKey = "";
236     auto rrc = std::make_shared<NotificationUserInput>();
237     std::shared_ptr<NotificationUserInput> result = rrc->Create(inputKey);
238     EXPECT_EQ(result, nullptr);
239 }
240 
241 /**
242  * @tc.name: Create_00002
243  * @tc.desc: Test NotificationUserInput parameters.
244  * @tc.type: FUNC
245  * @tc.require: issue
246  */
247 HWTEST_F(NotificationUserInputTest, Create_00002, Function | SmallTest | Level1)
248 {
249     std::string inputKey = "";
250     std::string tag = "Tag";
251     std::vector<std::string> options;
252     bool permitFreeFormInput = true;
253     std::set<std::string> permitMimeTypes;
254     std::shared_ptr<AAFwk::WantParams> additional = nullptr;
255     Notification::NotificationConstant::InputEditType editType =
256         Notification::NotificationConstant::InputEditType(1);
257     auto rrc = std::make_shared<NotificationUserInput>();
258     std::shared_ptr<NotificationUserInput> result = rrc->Create(inputKey, tag, options,
259         permitFreeFormInput, permitMimeTypes, additional, editType);
260     EXPECT_EQ(result, nullptr);
261 }
262 
263 /**
264  * @tc.name: Create_00003
265  * @tc.desc: Test NotificationUserInput parameters.
266  * @tc.type: FUNC
267  * @tc.require: issue
268  */
269 HWTEST_F(NotificationUserInputTest, Create_00003, Function | SmallTest | Level1)
270 {
271     std::string inputKey = "this is inputKey";
272     std::string tag = "Tag";
273     std::vector<std::string> options;
274     bool permitFreeFormInput = false;
275     std::set<std::string> permitMimeTypes;
276     std::shared_ptr<AAFwk::WantParams> additional = nullptr;
277     Notification::NotificationConstant::InputEditType editType =
278         Notification::NotificationConstant::InputEditType::EDIT_ENABLED;
279     auto rrc = std::make_shared<NotificationUserInput>();
280     std::shared_ptr<NotificationUserInput> result = rrc->Create(inputKey, tag, options,
281         permitFreeFormInput, permitMimeTypes, additional, editType);
282     EXPECT_EQ(result, nullptr);
283 }
284 
285 /**
286  * @tc.name: Create_00004
287  * @tc.desc: Test NotificationUserInput parameters.
288  * @tc.type: FUNC
289  * @tc.require: issue
290  */
291 HWTEST_F(NotificationUserInputTest, Create_00004, Function | SmallTest | Level1)
292 {
293     std::string inputKey = "this is inputKey";
294     std::string tag = "Tag";
295     std::vector<std::string> options;
296     bool permitFreeFormInput = true;
297     std::set<std::string> permitMimeTypes;
298     std::shared_ptr<AAFwk::WantParams> additional = nullptr;
299     Notification::NotificationConstant::InputEditType editType =
300         Notification::NotificationConstant::InputEditType::EDIT_ENABLED;
301     std::shared_ptr<NotificationUserInput> notificationUserInput = std::make_shared<NotificationUserInput>();
302     ASSERT_NE(nullptr, notificationUserInput);
303     notificationUserInput->Create(inputKey, tag, options, permitFreeFormInput, permitMimeTypes,
304         additional, editType);
305 }
306 
307 /**
308  * @tc.name: Create_00005
309  * @tc.desc: Test NotificationUserInput parameters.
310  * @tc.type: FUNC
311  * @tc.require: issue
312  */
313 HWTEST_F(NotificationUserInputTest, Create_00005, Function | SmallTest | Level1)
314 {
315     std::string inputKey = "this is inputKey";
316     std::string tag = "Tag";
317     std::vector<std::string> options;
318     std::string option = "this is option";
319     options.emplace_back(option);
320     bool permitFreeFormInput = false;
321     std::set<std::string> permitMimeTypes;
322     std::string permitMimeType = "this is permitMimeType";
323     permitMimeTypes.insert(permitMimeType);
324     std::shared_ptr<AAFwk::WantParams> additional = nullptr;
325     Notification::NotificationConstant::InputEditType editType =
326         Notification::NotificationConstant::InputEditType::EDIT_DISABLED;
327     std::shared_ptr<NotificationUserInput> notificationUserInput = std::make_shared<NotificationUserInput>();
328     ASSERT_NE(nullptr, notificationUserInput);
329     notificationUserInput->Create(inputKey, tag, options, permitFreeFormInput, permitMimeTypes,
330         additional, editType);
331 }
332 
333 /**
334  * @tc.name: Create_00006
335  * @tc.desc: Test NotificationUserInput parameters.
336  * @tc.type: FUNC
337  * @tc.require: issue
338  */
339 HWTEST_F(NotificationUserInputTest, Create_00006, Function | SmallTest | Level1)
340 {
341     std::string inputKey = "inputKey";
342     std::string tag = "Tag";
343     std::vector<std::string> vecs;
344     std::string option = "option";
345     vecs.emplace_back(option);
346     bool permitFreeFormInput = false;
347     std::set<std::string> permitMimeTypes;
348     std::string permitMimeType = "permitMimeType";
349     permitMimeTypes.insert(permitMimeType);
350     std::shared_ptr<AAFwk::WantParams> additional = std::make_shared<AAFwk::WantParams>();
351     Notification::NotificationConstant::InputEditType editType =
352         Notification::NotificationConstant::InputEditType::EDIT_DISABLED;
353     std::shared_ptr<NotificationUserInput> notificationUserInput = std::make_shared<NotificationUserInput>();
354     ASSERT_NE(nullptr, notificationUserInput);
355     notificationUserInput->Create(inputKey, tag, vecs, permitFreeFormInput, permitMimeTypes,
356         additional, editType);
357 }
358 
359 /**
360  * @tc.name: SetPermitMimeTypes_00001
361  * @tc.desc: Test SetPermitMimeTypes parameters.
362  * @tc.type: FUNC
363  * @tc.require: issue
364  */
365 HWTEST_F(NotificationUserInputTest, SetPermitMimeTypes_00001, Function | SmallTest | Level1)
366 {
367     std::string mimeType = "";
368     bool doPermit = true;
369     auto rrc = std::make_shared<NotificationUserInput>();
370     rrc->SetPermitMimeTypes(mimeType, doPermit);
371     auto result = rrc->GetPermitMimeTypes();
372     EXPECT_EQ(result.size(), 0);
373 }
374 
375 /**
376  * @tc.name: AddAdditionalData_00001
377  * @tc.desc: Test AddAdditionalData parameters.
378  * @tc.type: FUNC
379  * @tc.require: issue
380  */
381 HWTEST_F(NotificationUserInputTest, AddAdditionalData_00001, Function | SmallTest | Level1)
382 {
383     std::string inputKey = "InputKey";
384     std::string tag = "Tag";
385     std::vector<std::string> vecs;
386     bool permitFreeFormInput = true;
387     std::set<std::string> permitMimeTypes;
388     std::shared_ptr<AAFwk::WantParams> additional = nullptr;
389     Notification::NotificationConstant::InputEditType inputEditType =
390         Notification::NotificationConstant::InputEditType(1);
391     auto rrc = std::make_shared<NotificationUserInput>(inputKey, tag, vecs, permitFreeFormInput, permitMimeTypes,
392     additional, inputEditType);
393     std::shared_ptr<AAFwk::WantParams> ret = rrc->GetAdditionalData();
394     EXPECT_EQ(ret, nullptr);
395     AAFwk::WantParams aitional;
396     rrc->AddAdditionalData(aitional);
397 }
398 
399 /**
400  * @tc.name: Dump_00002
401  * @tc.desc: Test Dump parameters.
402  * @tc.type: FUNC
403  * @tc.require: issue
404  */
405 HWTEST_F(NotificationUserInputTest, Dump_00002, Function | SmallTest | Level1)
406 {
407     std::string inputKey = "InputKey";
408     std::string tag = "Tag";
409     std::vector<std::string> options;
410     std::string option1 = "this is option1";
411     std::string option2 = "this is option2";
412     options.emplace_back(option1);
413     options.emplace_back(option2);
414     bool permitFreeFormInput = true;
415     std::set<std::string> permitMimeTypes;
416     std::string permitMimeType1 = "this is permitMimeType1";
417     std::string permitMimeType2 = "this is permitMimeType2";
418     permitMimeTypes.insert(permitMimeType1);
419     permitMimeTypes.insert(permitMimeType2);
420     std::shared_ptr<AAFwk::WantParams> additional = nullptr;
421     Notification::NotificationConstant::InputEditType editType =
422         Notification::NotificationConstant::InputEditType(1);
423     auto rrc = std::make_shared<NotificationUserInput>(inputKey, tag, options, permitFreeFormInput, permitMimeTypes,
424     additional, editType);
425     std::string ret = rrc->Dump();
426     std::string dump = "NotificationUserInput{ inputKey = InputKey, tag = Tag, "
427     "options = [this is option1, this is option2], permitFreeFormInput = true, "
428     "permitMimeTypes = [this is permitMimeType1, this is permitMimeType2], editType = 1 }";
429     EXPECT_EQ(ret, dump);
430 }
431 }
432 }
433