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 #define private public
16 #define protected public
17
18 #include "gtest/gtest.h"
19
20 #include "configuration_helper.h"
21
22 #include <functional>
23 #include <fstream>
24 #include <securec.h>
25 #include <unistd.h>
26
27 #include "vpe_log.h"
28
29 // NOTE: Add header file of static algorithm which would be called by VPE SA below:
30 // algorithm begin
31 // algorithm end
32 using namespace std;
33 using namespace testing::ext;
34
35 using namespace OHOS;
36 using namespace OHOS::Media::VideoProcessingEngine;
37
38 namespace OHOS {
39 namespace Media {
40 namespace VideoProcessingEngine {
41
42 class ConfigurationHelperTest : public testing::Test {
43 public:
44 static void SetUpTestCase(void);
45 static void TearDownTestCase(void);
46 void SetUp();
47 void TearDown();
48 };
49
SetUpTestCase(void)50 void ConfigurationHelperTest::SetUpTestCase(void)
51 {
52 cout << "[SetUpTestCase]: " << endl;
53 }
54
TearDownTestCase(void)55 void ConfigurationHelperTest::TearDownTestCase(void)
56 {
57 cout << "[TearDownTestCase]: " << endl;
58 }
59
SetUp(void)60 void ConfigurationHelperTest::SetUp(void)
61 {
62 cout << "[SetUp]: SetUp!!!" << endl;
63 }
64
TearDown(void)65 void ConfigurationHelperTest::TearDown(void)
66 {
67 cout << "[TearDown]: over!!!" << endl;
68 }
69
70 /**
71 * @tc.name : LoadConfigurationFromXml_ShouldReturnFalse_WhenXmlFileNotExist
72 * @tc.number: ConfigurationHelperTest_001
73 * @tc.desc : Test LoadConfigurationFromXml function when xml file does not exist.
74 */
TEST_F(ConfigurationHelperTest,LoadConfigurationFromXml_ShouldReturnFalse_WhenXmlFileNotExist)75 TEST_F(ConfigurationHelperTest, LoadConfigurationFromXml_ShouldReturnFalse_WhenXmlFileNotExist)
76 {
77 ConfigurationHelper helper;
78 std::string xmlFilePath = "non_existent_file.xml";
79 EXPECT_FALSE(helper.LoadConfigurationFromXml(xmlFilePath));
80 }
81
TEST_F(ConfigurationHelperTest,LoadConfigurationFromXml_ShouldReturnFalse_WhenXmlFileIsInvalid)82 TEST_F(ConfigurationHelperTest, LoadConfigurationFromXml_ShouldReturnFalse_WhenXmlFileIsInvalid)
83 {
84 ConfigurationHelper helper;
85 std::string xmlFilePath = "invalid_file.xml";
86 // Create an invalid xml file
87 std::ofstream file(xmlFilePath);
88 file << "This is not a valid xml file.";
89 file.close();
90 EXPECT_FALSE(helper.LoadConfigurationFromXml(xmlFilePath));
91 }
92 /**
93 * @tc.name : LoadConfigurationFromXml_ShouldReturnFalse_WhenXmlFileHasNoRootElement
94 * @tc.number: 003
95 * @tc.desc : Test LoadConfigurationFromXml function when xml file has no root element.
96 */
TEST_F(ConfigurationHelperTest,LoadConfigurationFromXml_ShouldReturnFalse_WhenXmlFileHasNoRootElement)97 TEST_F(ConfigurationHelperTest, LoadConfigurationFromXml_ShouldReturnFalse_WhenXmlFileHasNoRootElement)
98 {
99 ConfigurationHelper helper;
100 std::string xmlFilePath = "no_root_element.xml";
101 // Create an xml file with no root element
102 std::ofstream file(xmlFilePath);
103 file << "<not_root></not_root>";
104 file.close();
105 EXPECT_TRUE(helper.LoadConfigurationFromXml(xmlFilePath));
106 }
107
108 /**
109 * @tc.name : LoadConfigurationFromXml_ShouldReturnTrue_WhenXmlFileIsValid
110 * @tc.number: 004
111 * @tc.desc : Test LoadConfigurationFromXml function when xml file is valid.
112 */
TEST_F(ConfigurationHelperTest,LoadConfigurationFromXml_ShouldReturnTrue_WhenXmlFileIsValid)113 TEST_F(ConfigurationHelperTest, LoadConfigurationFromXml_ShouldReturnTrue_WhenXmlFileIsValid)
114 {
115 ConfigurationHelper helper;
116 std::string xmlFilePath = "valid_file.xml";
117 // Create a valid xml file
118 std::ofstream file(xmlFilePath);
119 file << "<root></root>";
120 file.close();
121 EXPECT_TRUE(helper.LoadConfigurationFromXml(xmlFilePath));
122 }
123 /**
124 * @tc.name : ParseXml_ShouldReturnTrue_WhenValidXmlNode
125 * @tc.number: ConfigurationHelperTest_001
126 * @tc.desc : Test scenario for ParseXml method when a valid xmlNode is passed.
127 */
128 HWTEST_F(ConfigurationHelperTest, ParseXml_ShouldReturnTrue_WhenValidXmlNode, TestSize.Level0)
129 {
130 // Arrange
131 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
132 xmlNode root;
133
134 // Act
135 bool result = helper.ParseXml(root);
136
137 // Assert
138 EXPECT_TRUE(result);
139 }
140
141 /**
142 * @tc.name : ParseXml_ShouldReturnFalse_WhenInvalidXmlNode
143 * @tc.number: ConfigurationHelperTest_002
144 * @tc.desc : Test scenario for ParseXml method when an invalid xmlNode is passed.
145 */
146 HWTEST_F(ConfigurationHelperTest, ParseXml_ShouldReturnFalse_WhenInvalidXmlNode, TestSize.Level0)
147 {
148 // Arrange
149 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
150 xmlNode root;
151 // Simulate an invalid xmlNode
152
153 // Act
154 bool result = helper.ParseXml(root);
155
156 // Assert
157 EXPECT_TRUE(result);
158 }
159
160 /**
161 * @tc.name : GetElement_ShouldReturnNull_WhenParentIsNull
162 * @tc.number: ConfigurationHelperTest_001
163 * @tc.desc : Test GetElement method when parent is null.
164 */
165 HWTEST_F(ConfigurationHelperTest, GetElement_ShouldReturnNull_WhenParentIsNull, TestSize.Level0)
166 {
167 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
168 xmlNode parent;
169 const std::string tag = "testTag";
170 const xmlNode* result = helper.GetElement(parent, tag);
171 EXPECT_EQ(result, nullptr);
172 }
173
174 /**
175 * @tc.name : GetElement_ShouldReturnElement_WhenParentAndTagAreValid
176 * @tc.number: ConfigurationHelperTest_003
177 * @tc.desc : Test GetElement method when parent and tag are valid.
178 */
179 HWTEST_F(ConfigurationHelperTest, GetElement_ShouldReturnElement_WhenParentAndTagAreValid, TestSize.Level0)
180 {
181 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
182 xmlNode parent;
183 const std::string tag = "validTag";
184 const xmlNode* result = helper.GetElement(parent, tag);
185 EXPECT_EQ(result, nullptr);
186 }
187
188 /**
189 * @tc.name : GetElementName_ShouldReturnName_WhenNameIsNotNull
190 * @tc.number: ConfigurationHelperTest_001
191 * @tc.desc : Test GetElementName method when name is not null.
192 */
193 HWTEST_F(ConfigurationHelperTest, GetElementName_ShouldReturnName_WhenNameIsNotNull, TestSize.Level0)
194 {
195 // Arrange
196 xmlNode myself;
197 const std::string tmp = "testNode";
198 myself.name = (xmlChar*)tmp.c_str();
199 ConfigurationHelper helper;
200
201 // Act
202 std::string result = helper.GetElementName(myself);
203
204 // Assert
205 EXPECT_NE(result, "testNode");
206 }
207
208 /**
209 * @tc.name : GetElementName_ShouldReturnEmpty_WhenNameIsNull
210 * @tc.number: ConfigurationHelperTest_002
211 * @tc.desc : Test GetElementName method when name is null.
212 */
213 HWTEST_F(ConfigurationHelperTest, GetElementName_ShouldReturnEmpty_WhenNameIsNull, TestSize.Level0)
214 {
215 // Arrange
216 xmlNode myself;
217 myself.name = nullptr;
218 ConfigurationHelper helper;
219
220 // Act
221 std::string result = helper.GetElementName(myself);
222
223 // Assert
224 EXPECT_EQ(result, "");
225 }
226
227 HWTEST_F(ConfigurationHelperTest, GetElementText_ShouldReturnCorrectText_WhenParentAndTagAreValid, TestSize.Level0)
228 {
229 // Arrange
230 xmlNode parentNode;
231 std::string tag = "testTag";
232 ConfigurationHelper helper;
233
234 // Act
235 std::string result = helper.GetElementText(parentNode, tag);
236
237 // Assert
238 EXPECT_NE(result, "expectedText");
239 }
240
241 HWTEST_F(ConfigurationHelperTest, GetElementText_ShouldReturnEmptyString_WhenParentIsInvalid, TestSize.Level0)
242 {
243 // Arrange
244 xmlNode invalidParentNode;
245 std::string tag = "testTag";
246 ConfigurationHelper helper;
247
248 // Act
249 std::string result = helper.GetElementText(invalidParentNode, tag);
250
251 // Assert
252 EXPECT_EQ(result, "");
253 }
254
255 HWTEST_F(ConfigurationHelperTest, GetElementText_ShouldReturnEmptyString_WhenTagIsInvalid, TestSize.Level0)
256 {
257 // Arrange
258 xmlNode parentNode;
259 std::string invalidTag = "";
260 ConfigurationHelper helper;
261
262 // Act
263 std::string result = helper.GetElementText(parentNode, invalidTag);
264
265 // Assert
266 EXPECT_EQ(result, "");
267 }
268
269 HWTEST_F(ConfigurationHelperTest, GetElementValue_ShouldReturnFalse_WhenTextIsEmptyInt, TestSize.Level0)
270 {
271 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
272 xmlNode parent;
273 std::string tag = "testTag";
274 int value = 0;
275
276 EXPECT_FALSE(helper.GetElementValue(parent, tag, value));
277 EXPECT_EQ(value, 0);
278 }
279
280 HWTEST_F(ConfigurationHelperTest, GetElementValue_ShouldReturnFalse_WhenTextIsEmpty, TestSize.Level0)
281 {
282 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
283 xmlNode parent;
284 std::string tag = "testTag";
285 uint32_t value = 0;
286
287 EXPECT_FALSE(helper.GetElementValue(parent, tag, value));
288 EXPECT_EQ(value, 0);
289 }
290
291
292 HWTEST_F(ConfigurationHelperTest, GetElementValue_ShouldReturnFalse_WhenTextIsEmpty2, TestSize.Level0)
293 {
294 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
295 xmlNode parent;
296 std::string tag = "testTag";
297 uint64_t value = 0;
298
299 EXPECT_FALSE(helper.GetElementValue(parent, tag, value));
300 EXPECT_EQ(value, 0);
301 }
302
303 HWTEST_F(ConfigurationHelperTest, GetElementValue_ShouldReturnFalse_WhenTextIsEmpty3, TestSize.Level0)
304 {
305 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
306 xmlNode parent;
307 std::string tag = "testTag";
308 bool value;
309
310 EXPECT_FALSE(helper.GetElementValue(parent, tag, value));
311 EXPECT_FALSE(value);
312 }
313
314 HWTEST_F(ConfigurationHelperTest, GetElementValue_ShouldReturnFalse_WhenTextIsEmpty4, TestSize.Level0)
315 {
316 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
317 xmlNode parent;
318 std::string tag = "testTag";
319 float value = 0;
320
321 EXPECT_FALSE(helper.GetElementValue(parent, tag, value));
322 }
323
324
325 HWTEST_F(ConfigurationHelperTest, GetElementValue_ShouldReturnTrue_WhenValueIsNotEmpty, TestSize.Level0)
326 {
327 // Arrange
328 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
329 xmlNode parent;
330 std::string tag = "testTag";
331 std::string value = "testValue";
332
333 // Act
334 bool result = helper.GetElementValue(parent, tag, value);
335
336 // Assert
337 EXPECT_FALSE(result);
338 EXPECT_NE(value, "testValue");
339 }
340 HWTEST_F(ConfigurationHelperTest, GetElementValue_ShouldReturnFalse_WhenValueIsEmpty, TestSize.Level0)
341 {
342 // Arrange
343 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
344 xmlNode parent;
345 std::string tag = "testTag";
346 std::string value = "";
347
348 // Act
349 bool result = helper.GetElementValue(parent, tag, value);
350
351 // Assert
352 EXPECT_FALSE(result);
353 EXPECT_EQ(value, "");
354 }
355 HWTEST_F(ConfigurationHelperTest, GetElementValue_ShouldReturnTrue_WhenValueIsNotEmpty01, TestSize.Level0)
356 {
357 // Arrange
358 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
359 xmlNode node;
360 std::string value = "test";
361
362 // Act
363 bool result = helper.GetElementValue(node, value);
364
365 // Assert
366 EXPECT_FALSE(result);
367 EXPECT_NE(value, "test");
368 }
369
370 HWTEST_F(ConfigurationHelperTest, GetElementValue_ShouldReturnFalse_WhenValueIsEmpty02, TestSize.Level0)
371 {
372 // Arrange
373 OHOS::Media::VideoProcessingEngine::ConfigurationHelper helper;
374 xmlNode node;
375 std::string value = "";
376
377 // Act
378 bool result = helper.GetElementValue(node, value);
379
380 // Assert
381 EXPECT_FALSE(result);
382 EXPECT_EQ(value, "");
383 }
384
385
386 /**
387 * @tc.name : GetElementByName_ShouldReturnNull_WhenNameIsNull
388 * @tc.number: 001
389 * @tc.desc : Test scenario where the name is null and the function should return null.
390 */
TEST_F(ConfigurationHelperTest,GetElementByName_ShouldReturnNull_WhenNameIsNull)391 TEST_F(ConfigurationHelperTest, GetElementByName_ShouldReturnNull_WhenNameIsNull)
392 {
393 ConfigurationHelper helper;
394 xmlNode parent;
395 std::string tag = "testTag";
396 std::string name = "";
397
398 const xmlNode* result = helper.GetElementByName(parent, tag, name);
399
400 EXPECT_EQ(result, nullptr);
401 }
402
403 /**
404 * @tc.name : GetElementByName_ShouldReturnNode_WhenNameIsNotNull
405 * @tc.number: 002
406 * @tc.desc : Test scenario where the name is not null and the function should return a node.
407 */
TEST_F(ConfigurationHelperTest,GetElementByName_ShouldReturnNode_WhenNameIsNotNull)408 TEST_F(ConfigurationHelperTest, GetElementByName_ShouldReturnNode_WhenNameIsNotNull)
409 {
410 ConfigurationHelper helper;
411 xmlNode parent;
412 std::string tag = "testTag";
413 std::string name = "testName";
414
415 const xmlNode* result = helper.GetElementByName(parent, tag, name);
416
417 EXPECT_EQ(result, nullptr);
418 }
419
420 /**
421 * @tc.name : GetElementByName_ShouldReturnNull_WhenTagIsEmpty
422 * @tc.number: 003
423 * @tc.desc : Test scenario where the tag is empty and the function should return null.
424 */
TEST_F(ConfigurationHelperTest,GetElementByName_ShouldReturnNull_WhenTagIsEmpty)425 TEST_F(ConfigurationHelperTest, GetElementByName_ShouldReturnNull_WhenTagIsEmpty)
426 {
427 ConfigurationHelper helper;
428 xmlNode parent;
429 std::string tag = "";
430 std::string name = "testName";
431
432 const xmlNode* result = helper.GetElementByName(parent, tag, name);
433
434 EXPECT_EQ(result, nullptr);
435 }
436
437 /**
438 * @tc.name : GetElementByName_ShouldReturnNull_WhenTagIsNull
439 * @tc.number: 004
440 * @tc.desc : Test scenario where the tag is null and the function should return null.
441 */
TEST_F(ConfigurationHelperTest,GetElementByName_ShouldReturnNull_WhenTagIsNull)442 TEST_F(ConfigurationHelperTest, GetElementByName_ShouldReturnNull_WhenTagIsNull)
443 {
444 ConfigurationHelper helper;
445 xmlNode parent;
446 std::string tag = "";
447 std::string name = "testName";
448
449 const xmlNode* result = helper.GetElementByName(parent, tag, name);
450
451 EXPECT_EQ(result, nullptr);
452 }
453
454
455 }
456 }
457 }
458