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 "test_convertxml.h"
17 #include "test.h"
18
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21
22 #include "js_convertxml.h"
23 #include "js_xml.h"
24 #include "native_module_convertxml.h"
25 #include "utils/log.h"
26
27 using namespace OHOS::Xml;
28
setProperty(napi_env env,napi_value obj,std::vector<std::string> proVec)29 napi_value setProperty(napi_env env, napi_value obj, std::vector<std::string> proVec)
30 {
31 const size_t boolNum = 8; // 8 : the counts of the bool value
32 napi_value val = nullptr;
33 for (size_t i = 0; i < proVec.size();) {
34 if (i < boolNum) {
35 napi_get_boolean(env, false, &val);
36 napi_set_named_property(env, obj, proVec[i].c_str(), val);
37 i++;
38 } else {
39 napi_create_string_utf8(env, proVec[i + 1].c_str(), proVec[i + 1].size(), &val);
40 napi_set_named_property(env, obj, proVec[i].c_str(), val);
41 i += 2; // 2 : the length of the value and property
42 }
43 }
44 return obj;
45 }
46
47 /* @tc.name: ConvertXmlTest001
48 * @tc.desc: Convert the xml object containing only declaration items to a js object.
49 * @tc.type: FUNC
50 */
51 HWTEST_F(NativeEngineTest, ConvertXmlTest001, testing::ext::TestSize.Level0)
52 {
53 size_t size = 1024; // 1024 : the size is 1024 byte
54 void* pBuffer = nullptr;
55 napi_value arrayBuffer = nullptr;
56 napi_env env = (napi_env)engine_;
57 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
58 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
59 xmlSerializer.SetDeclaration();
60
61 Options op;
62 ConvertXml *convertXml = new ConvertXml();
63 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
64 napi_value jsObj = convertXml->Convert(env, xmlStr);
65
66 // Do not set start tag '<'
67 napi_value declarationObj = nullptr;
68 napi_value declarationProp = nullptr;
69 napi_value versionVal = nullptr;
70 napi_value encodingVal = nullptr;
71 napi_get_named_property(env, jsObj, op.declaration.c_str(), &declarationObj);
72 napi_get_named_property(env, declarationObj, op.attributes.c_str(), &declarationProp);
73 napi_get_named_property(env, declarationProp, "version", &versionVal);
74 napi_get_named_property(env, declarationProp, "encoding", &encodingVal);
75
76 std::string verisonStr;
77 std::string encodingStr;
78 convertXml->DealNapiStrValue(env, versionVal, verisonStr);
79 convertXml->DealNapiStrValue(env, encodingVal, encodingStr);
80 EXPECT_STREQ(verisonStr.c_str(), "1.0");
81 EXPECT_STREQ(encodingStr.c_str(), "utf-8");
82 }
83
84 /* @tc.name: ConvertXmlTest002
85 * @tc.desc: Convert the xml object containing an empty element to a js object.
86 * @tc.type: FUNC
87 */
88 HWTEST_F(NativeEngineTest, ConvertXmlTest002, testing::ext::TestSize.Level0)
89 {
90 size_t size = 1024; // 1024 : the size is 1024 byte
91 void* pBuffer = nullptr;
92 napi_value arrayBuffer = nullptr;
93 napi_env env = (napi_env)engine_;
94 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
95 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
96 xmlSerializer.StartElement("note1");
97 xmlSerializer.EndElement();
98
99 Options op;
100 ConvertXml *convertXml = new ConvertXml();
101 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
102 napi_value jsObj = convertXml->Convert(env, xmlStr);
103
104 napi_value element = nullptr;
105 napi_value elements = nullptr;
106 napi_value nameVal = nullptr;
107 napi_value typeVal = nullptr;
108 napi_get_named_property(env, jsObj, op.elements.c_str(), &elements);
109 napi_get_element(env, elements, 0, &element);
110 napi_get_named_property(env, element, op.name.c_str(), &nameVal);
111 napi_get_named_property(env, element, op.type.c_str(), &typeVal);
112
113 std::string nameStr;
114 std::string typeStr;
115 convertXml->DealNapiStrValue(env, nameVal, nameStr);
116 convertXml->DealNapiStrValue(env, typeVal, typeStr);
117 EXPECT_STREQ(nameStr.c_str(), "note1");
118 EXPECT_STREQ(typeStr.c_str(), "element");
119 }
120
121 /* @tc.name: ConvertXmlTest003
122 * @tc.desc: Convert the xml object containing attributes to a js object.
123 * @tc.type: FUNC
124 */
125 HWTEST_F(NativeEngineTest, ConvertXmlTest003, testing::ext::TestSize.Level0)
126 {
127 size_t size = 1024; // 1024 : the size is 1024 byte
128 void* pBuffer = nullptr;
129 napi_value arrayBuffer = nullptr;
130 napi_env env = (napi_env)engine_;
131 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
132 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
133 xmlSerializer.StartElement("note1");
134 xmlSerializer.SetAttributes("colour", "red");
135 xmlSerializer.SetAttributes("shape", "circle");
136 xmlSerializer.EndElement();
137
138 Options op;
139 ConvertXml *convertXml = new ConvertXml();
140 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
141 napi_value jsObj = convertXml->Convert(env, xmlStr);
142
143 napi_value elements = nullptr;
144 napi_value element = nullptr;
145 napi_value attributes = nullptr;
146 napi_value colour = nullptr;
147 napi_value shape = nullptr;
148 napi_get_named_property(env, jsObj, op.elements.c_str(), &elements);
149 napi_get_element(env, elements, 0, &element);
150 napi_get_named_property(env, element, op.attributes.c_str(), &attributes);
151 napi_get_named_property(env, attributes, "colour", &colour);
152 napi_get_named_property(env, attributes, "shape", &shape);
153
154 std::string colourStr;
155 std::string shapeStr;
156 convertXml->DealNapiStrValue(env, colour, colourStr);
157 convertXml->DealNapiStrValue(env, shape, shapeStr);
158 EXPECT_STREQ(colourStr.c_str(), "red");
159 EXPECT_STREQ(shapeStr.c_str(), "circle");
160 }
161
162 /* @tc.name: ConvertXmlTest004
163 * @tc.desc: Convert the xml object containing comment to a js object.
164 * @tc.type: FUNC
165 */
166 HWTEST_F(NativeEngineTest, ConvertXmlTest004, testing::ext::TestSize.Level0)
167 {
168 size_t size = 1024; // 1024 : the size is 1024 byte
169 void* pBuffer = nullptr;
170 napi_value arrayBuffer = nullptr;
171 napi_env env = (napi_env)engine_;
172 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
173 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
174 xmlSerializer.SetComment("This is a comment");
175 xmlSerializer.StartElement("note1");
176 xmlSerializer.EndElement();
177
178 Options op;
179 ConvertXml *convertXml = new ConvertXml();
180 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
181 napi_value jsObj = convertXml->Convert(env, xmlStr);
182
183 napi_value elements = nullptr;
184 napi_value element = nullptr;
185 napi_value commentType = nullptr;
186 napi_value commentText = nullptr;
187 EXPECT_EQ(napi_get_named_property(env, jsObj, op.elements.c_str(), &elements), napi_status::napi_ok);
188 EXPECT_EQ(napi_get_element(env, elements, 0, &element), napi_status::napi_ok);
189 EXPECT_EQ(napi_get_named_property(env, element, op.type.c_str(), &commentType), napi_status::napi_ok);
190 EXPECT_EQ(napi_get_named_property(env, element, op.comment.c_str(), &commentText), napi_status::napi_ok);
191
192 std::string commentTypeStr;
193 std::string commentTextStr;
194 convertXml->DealNapiStrValue(env, commentType, commentTypeStr);
195 convertXml->DealNapiStrValue(env, commentText, commentTextStr);
196 EXPECT_STREQ(commentTypeStr.c_str(), "comment");
197 EXPECT_STREQ(commentTextStr.c_str(), "This is a comment");
198 }
199
200 /* @tc.name: ConvertXmlTest005
201 * @tc.desc: Convert the xml object containing cdata to a js object.
202 * @tc.type: FUNC
203 */
204 HWTEST_F(NativeEngineTest, ConvertXmlTest005, testing::ext::TestSize.Level0)
205 {
206 size_t size = 1024; // 1024 : the size is 1024 byte
207 void* pBuffer = nullptr;
208 napi_value arrayBuffer = nullptr;
209 napi_env env = (napi_env)engine_;
210 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
211 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
212 xmlSerializer.SetCData("function foo() {}");
213 xmlSerializer.StartElement("note1");
214 xmlSerializer.EndElement();
215
216 Options op;
217 ConvertXml *convertXml = new ConvertXml();
218 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
219 napi_value jsObj = convertXml->Convert(env, xmlStr);
220
221 napi_value elements = nullptr;
222 napi_value element = nullptr;
223 napi_value cdataType = nullptr;
224 napi_value cdataText = nullptr;
225 EXPECT_EQ(napi_get_named_property(env, jsObj, op.elements.c_str(), &elements), napi_status::napi_ok);
226 EXPECT_EQ(napi_get_element(env, elements, 0, &element), napi_status::napi_ok);
227 EXPECT_EQ(napi_get_named_property(env, element, op.type.c_str(), &cdataType), napi_status::napi_ok);
228 EXPECT_EQ(napi_get_named_property(env, element, op.cdata.c_str(), &cdataText), napi_status::napi_ok);
229
230 std::string cdataTypeStr;
231 std::string cdataTextStr;
232 convertXml->DealNapiStrValue(env, cdataType, cdataTypeStr);
233 convertXml->DealNapiStrValue(env, cdataText, cdataTextStr);
234 EXPECT_STREQ(cdataTypeStr.c_str(), "cdata");
235 EXPECT_STREQ(cdataTextStr.c_str(), "function foo() {}");
236 }
237
238 /* @tc.name: ConvertXmlTest006
239 * @tc.desc: Convert the xml object containing doctype to a js object.
240 * @tc.type: FUNC
241 */
242 HWTEST_F(NativeEngineTest, ConvertXmlTest006, testing::ext::TestSize.Level0)
243 {
244 size_t size = 1024; // 1024 : the size is 1024 byte
245 void* pBuffer = nullptr;
246 napi_value arrayBuffer = nullptr;
247 napi_env env = (napi_env)engine_;
248 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
249 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
250 xmlSerializer.SetDocType("root SYSTEM \"http://www.test.org/test.dtd\"");
251 xmlSerializer.StartElement("note1");
252 xmlSerializer.EndElement();
253
254 Options op;
255 ConvertXml *convertXml = new ConvertXml();
256 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
257 napi_value jsObj = convertXml->Convert(env, xmlStr);
258
259 napi_value elements = nullptr;
260 napi_value element = nullptr;
261 napi_value docType = nullptr;
262 napi_value docText = nullptr;
263 EXPECT_EQ(napi_get_named_property(env, jsObj, op.elements.c_str(), &elements), napi_status::napi_ok);
264 EXPECT_EQ(napi_get_element(env, elements, 0, &element), napi_status::napi_ok);
265 EXPECT_EQ(napi_get_named_property(env, element, op.type.c_str(), &docType), napi_status::napi_ok);
266 EXPECT_EQ(napi_get_named_property(env, element, op.doctype.c_str(), &docText), napi_status::napi_ok);
267
268 std::string docTypeStr;
269 std::string docStr;
270 convertXml->DealNapiStrValue(env, docType, docTypeStr);
271 convertXml->DealNapiStrValue(env, docText, docStr);
272 EXPECT_STREQ(docTypeStr.c_str(), "doctype");
273 EXPECT_STREQ(docStr.c_str(), "root");
274 }
275
276 /* @tc.name: ConstructorTest001
277 * @tc.desc: Convert the xml object containing doctype to a js object.
278 * @tc.type: FUNC
279 */
280 HWTEST_F(NativeEngineTest, ConstructorTest001, testing::ext::TestSize.Level0)
281 {
282 napi_env env = (napi_env)engine_;
283 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml();
284 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
285 std::string str2 = "<note importance=\"high\" logged=\"true\"><todo>Play</todo></note>";
286 std::string strXml = str1 + str2;
287 napi_valuetype valuetype = napi_undefined;
288
289 napi_typeof(env, convertXml.Convert(env, strXml), &valuetype);
290 bool isObj = valuetype == napi_valuetype::napi_object;
291 ASSERT_TRUE(isObj);
292 }
293
294 /* @tc.name: ConstructorTest002
295 * @tc.desc: Convert the xml object containing doctype to a js object.
296 * @tc.type: FUNC
297 */
298 HWTEST_F(NativeEngineTest, ConstructorTest002, testing::ext::TestSize.Level0)
299 {
300 napi_env env = (napi_env)engine_;
301 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
302 std::string str2 = "<note importance=\"high\" logged=\"true\"><todo>Play</todo></note>";
303 std::string strXml = str1 + str2;
304 napi_value object = nullptr;
305 const char* utf8Name = "_declaration";
306 napi_create_object(env, &object);
307 bool isHas = false;
308 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml();
309
310 object = convertXml.Convert(env, strXml);
311 napi_has_named_property(env, object, utf8Name, &isHas);
312 ASSERT_TRUE(isHas);
313 }
314
315 /* @tc.name: ConstructorTest003
316 * @tc.desc: Convert the xml object containing doctype to a js object.
317 * @tc.type: FUNC
318 */
319 HWTEST_F(NativeEngineTest, ConstructorTest003, testing::ext::TestSize.Level0)
320 {
321 napi_env env = (napi_env)engine_;
322 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
323 std::string str2 = "<note importance=\"high\" logged=\"true\"><todo>Play</todo></note>";
324 std::string strXml = str1 + str2;
325 napi_value object = nullptr;
326 const char* utf8Name = "_declaration";
327 napi_create_object(env, &object);
328 bool isHas = false;
329 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml();
330
331 object = convertXml.Convert(env, strXml);
332 napi_has_named_property(env, object, utf8Name, &isHas);
333 ASSERT_TRUE(isHas);
334 }
335
336 /* @tc.name: ConvertTest001
337 * @tc.desc: Convert the xml object containing doctype to a js object.
338 * @tc.type: FUNC
339 */
340 HWTEST_F(NativeEngineTest, ConvertTest001, testing::ext::TestSize.Level0)
341 {
342 napi_env env = (napi_env)engine_;
343 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml();
344 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><note importance=\"high\" logged=\"true\"> ";
345 std::string str2 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo></note>";
346 std::string strXml = str1 + str2;
347 napi_valuetype valuetype = napi_undefined;
348
349 napi_typeof(env, convertXml.Convert(env, strXml), &valuetype);
350 bool isObj = valuetype == napi_valuetype::napi_object;
351 ASSERT_TRUE(isObj);
352 }
353
354 /* @tc.name: ConvertTest002
355 * @tc.desc: Convert the xml object containing doctype to a js object.
356 * @tc.type: FUNC
357 */
358 HWTEST_F(NativeEngineTest, ConvertTest002, testing::ext::TestSize.Level0)
359 {
360 napi_env env = (napi_env)engine_;
361 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><note importance=\"high\" logged=\"true\"> ";
362 std::string str2 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo></note>";
363 std::string strXml = str1 + str2;
364 napi_value object = nullptr;
365 const char* utf8Name = "_declaration";
366 napi_create_object(env, &object);
367 bool isHas = false;
368 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml();
369
370 object = convertXml.Convert(env, strXml);
371 napi_has_named_property(env, object, utf8Name, &isHas);
372 ASSERT_TRUE(isHas);
373 }
374
375 /* @tc.name: ConvertTest003
376 * @tc.desc: Convert the xml object containing doctype to a js object.
377 * @tc.type: FUNC
378 */
379 HWTEST_F(NativeEngineTest, ConvertTest003, testing::ext::TestSize.Level0)
380 {
381 napi_env env = (napi_env)engine_;
382 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><note importance=\"high\" logged=\"true\"> ";
383 std::string str2 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo></note>";
384 std::string strXml = str1 + str2;
385 napi_value object = nullptr;
386 const char* utf8Name = "_elements";
387 napi_create_object(env, &object);
388 bool isHas = false;
389 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml();
390
391 object = convertXml.Convert(env, strXml);
392 napi_has_named_property(env, object, utf8Name, &isHas);
393 ASSERT_TRUE(isHas);
394 }
395
396 /* @tc.name: DealOptionsTest001
397 * @tc.desc: Convert the xml object containing doctype to a js object.
398 * @tc.type: FUNC
399 */
400 HWTEST_F(NativeEngineTest, DealOptionsTest001, testing::ext::TestSize.Level0)
401 {
402 napi_env env = (napi_env)engine_;
403 napi_value obj = nullptr;
404 napi_create_object(env, &obj);
405
406 std::vector<std::string> proVec = {"trim", "ignoreDeclaration", "ignoreInstruction", "ignoreAttributes",
407 "ignoreComment", "ignoreCDATA", "ignoreDoctype", "ignoreText", "declarationKey", "_declaration",
408 "instructionKey", "_instruction", "attributesKey", "_attributes", "textKey", "_text", "cdataKey", "_cdata",
409 "doctypeKey", "_doctype", "commentKey", "_comment", "parentKey", "_parent","typeKey", "_type",
410 "nameKey", "_name", "elementsKey", "_elements"};
411
412 obj = setProperty(env, obj, proVec);
413 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml();
414 convertXml.DealOptions(env, obj);
415 bool isHas = false;
416 napi_has_named_property(env, obj, "textKey", &isHas);
417 ASSERT_TRUE(isHas);
418 }
419
420 /* @tc.name: NativeModuleConvertXmlTest001
421 * @tc.desc: Convert the xml object containing doctype to a js object.
422 * @tc.type: FUNC
423 */
424 HWTEST_F(NativeEngineTest, NativeModuleConvertXmlTest001, testing::ext::TestSize.Level1)
425 {
426 napi_env env = (napi_env)engine_;
427 napi_value exports = nullptr;
428 napi_create_object(env, &exports);
429 OHOS::Xml::ConvertXmlInit(env, exports);
430 napi_value convertXmlClass = nullptr;
431 napi_get_named_property(env, exports, "ConvertXml", &convertXmlClass);
432
433 napi_value instance = nullptr;
434 napi_new_instance(env, convertXmlClass, 0, nullptr, &instance);
435
436 napi_value args[2]; // 2: number of arguments
437 std::string firXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><note importance=\"high\" logged=\"true\">";
438 std::string secXml = "<title>Happy</title></note>";
439 std::string strXml = firXml + secXml;
440 napi_create_string_utf8(env, strXml.c_str(), strXml.size(), &args[0]);
441
442 napi_value obj = nullptr;
443 napi_create_object(env, &obj);
444 std::vector<std::string> proVec = {"trim", "ignoreDeclaration", "ignoreInstruction", "ignoreAttributes",
445 "ignoreComment", "ignoreCDATA", "ignoreDoctype", "ignoreText", "declarationKey", "_declaration",
446 "instructionKey", "_instruction", "attributesKey", "_attributes", "textKey", "_text", "cdataKey", "_cdata",
447 "doctypeKey", "_doctype", "commentKey", "_comment", "parentKey", "_parent", "typeKey", "_type",
448 "nameKey", "_name", "elementsKey", "_elements"};
449 args[1] = setProperty(env, obj, proVec);
450
451 napi_value funcResultValue = nullptr;
452 napi_value testFunc = nullptr;
453 napi_get_named_property(env, instance, "convert", &testFunc);
454 napi_call_function(env, instance, testFunc, 2, args, &funcResultValue); // 2: number of arguments
455 ASSERT_NE(funcResultValue, nullptr);
456 }
457
458 HWTEST_F(NativeEngineTest, TrimTest001, testing::ext::TestSize.Level1)
459 {
460 CxmlTest::Trim("");
461 std::string res = CxmlTest::Trim(" #e ");
462 ASSERT_STREQ(res.c_str(), "#e");
463 }
464
465 HWTEST_F(NativeEngineTest, GetNodeTypeTest001, testing::ext::TestSize.Level1)
466 {
467 xmlElementType enumType = XML_ATTRIBUTE_NODE;
468 std::string res = CxmlTest::GetNodeType(enumType);
469 ASSERT_STREQ(res.c_str(), "attribute");
470 enumType = XML_ENTITY_REF_NODE;
471 CxmlTest::GetNodeType(enumType);
472 enumType = XML_ENTITY_NODE;
473 CxmlTest::GetNodeType(enumType);
474 enumType = XML_PI_NODE;
475 CxmlTest::GetNodeType(enumType);
476 enumType = XML_DOCUMENT_NODE;
477 CxmlTest::GetNodeType(enumType);
478 enumType = XML_DOCUMENT_TYPE_NODE;
479 CxmlTest::GetNodeType(enumType);
480 enumType = XML_DOCUMENT_FRAG_NODE;
481 CxmlTest::GetNodeType(enumType);
482 enumType = XML_DOCB_DOCUMENT_NODE;
483 CxmlTest::GetNodeType(enumType);
484 enumType = XML_XINCLUDE_END;
485 res = CxmlTest::GetNodeType(enumType);
486 ASSERT_STREQ(res.c_str(), "");
487 }
488
489 HWTEST_F(NativeEngineTest, GetPrevNodeListTest001, testing::ext::TestSize.Level1)
490 {
491 napi_env env = (napi_env)engine_;
492 xmlNodePtr curNode = new xmlNode;
493 xmlNodePtr curNode1 = new xmlNode;
494 curNode->prev = curNode1;
495 curNode1->prev = nullptr;
496 curNode1->type = XML_PI_NODE;
497 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
498 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>("Hello world!"));
499 CxmlTest::GetPrevNodeList(env, curNode);
500 delete curNode;
501 delete curNode1;
502 }
503
504 HWTEST_F(NativeEngineTest, SetXmlElementTypeTest001, testing::ext::TestSize.Level1)
505 {
506 napi_env env = (napi_env)engine_;
507 napi_value elementsObject = nullptr;
508 napi_create_object(env, &elementsObject);
509 xmlNodePtr curNode1 = new xmlNode;
510 curNode1->type = XML_PI_NODE;
511 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
512 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>("Hello world!"));
513 bool flag = false;
514 CxmlTest::SetXmlElementType(env, curNode1, elementsObject, flag);
515 flag = false;
516 curNode1->type = XML_COMMENT_NODE;
517 CxmlTest::SetXmlElementType(env, curNode1, elementsObject, flag);
518 delete curNode1;
519 ASSERT_TRUE(flag);
520 }
521
522 HWTEST_F(NativeEngineTest, SetNodeInfoTest001, testing::ext::TestSize.Level1)
523 {
524 napi_env env = (napi_env)engine_;
525 napi_value elementsObject = nullptr;
526 napi_create_object(env, &elementsObject);
527 xmlNodePtr curNode1 = new xmlNode;
528 curNode1->type = XML_PI_NODE;
529 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
530 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>("Hello world!"));
531 bool flag = true;
532 CxmlTest::SetNodeInfo(env, curNode1, elementsObject);
533 delete curNode1;
534 ASSERT_TRUE(flag);
535 }
536
537 HWTEST_F(NativeEngineTest, DealSpacesTest001, testing::ext::TestSize.Level1)
538 {
539 napi_env env = (napi_env)engine_;
540 napi_value napiObj = nullptr;
541 napi_create_object(env, &napiObj);
542 napi_value spacesValue;
543 napi_create_string_utf8(env, "hello world", NAPI_AUTO_LENGTH, &spacesValue);
544 napi_set_named_property(env, napiObj, "spaces", spacesValue);
545 bool flag = true;
546 CxmlTest::DealSpaces(env, napiObj);
547 ASSERT_TRUE(flag);
548 }
549
550 HWTEST_F(NativeEngineTest, DealSpacesTest002, testing::ext::TestSize.Level1)
551 {
552 napi_env env = (napi_env)engine_;
553 napi_value napiObj = nullptr;
554 napi_create_object(env, &napiObj);
555 napi_value spacesValue;
556 napi_create_int32(env, 123, &spacesValue); // 123: number of test number
557 napi_set_named_property(env, napiObj, "spaces", spacesValue);
558 bool flag = true;
559 CxmlTest::DealSpaces(env, napiObj);
560 ASSERT_TRUE(flag);
561 }
562
563 HWTEST_F(NativeEngineTest, SetDefaultKeyTest001, testing::ext::TestSize.Level1)
564 {
565 size_t i = 15; // 15: number of default number
566 std::string key = "hello";
567 CxmlTest::SetDefaultKey(i, key);
568 ASSERT_STREQ(key.c_str(), "hello");
569 }
570
571 HWTEST_F(NativeEngineTest, DealSingleLineTest001, testing::ext::TestSize.Level1)
572 {
573 napi_env env = (napi_env)engine_;
574 std::string key = "xmlsss<zyyzyy>ssa";
575 napi_value napiObj = nullptr;
576 napi_create_object(env, &napiObj);
577 CxmlTest::DealSingleLine(env, key, napiObj);
578 ASSERT_STREQ(key.c_str(), "<node>xmlsss<zyyzyy>ssassa</node>");
579 }
580
581 HWTEST_F(NativeEngineTest, DealComplexTest001, testing::ext::TestSize.Level1)
582 {
583 napi_env env = (napi_env)engine_;
584 std::string key = "xmlsss<!DOCTYPE>ssa";
585 napi_value napiObj = nullptr;
586 napi_create_object(env, &napiObj);
587 CxmlTest::DealComplex(env, key, napiObj);
588 ASSERT_STREQ(key.c_str(), "xmlsss<!DOCTYPE>ssa<node></node>");
589 }
590
591 HWTEST_F(NativeEngineTest, ReplaceTest001, testing::ext::TestSize.Level1)
592 {
593 std::string str = "xmlsss<!DOCTYPE>ssa";
594 std::string src = "sss";
595 std::string dst = "zyy";
596 CxmlTest::Replace(str, src, dst);
597 ASSERT_STREQ(str.c_str(), "xmlzyy<!DOCTYPE>ssa");
598 }
599
600 HWTEST_F(NativeEngineTest, DealCDataInfo001, testing::ext::TestSize.Level1)
601 {
602 bool flag = true;
603 xmlNodePtr curNode = new xmlNode;
604 xmlNodePtr curNode1 = new xmlNode;
605 xmlNodePtr curNode2 = new xmlNode;
606 curNode->next = curNode1;
607 curNode->type = XML_CDATA_SECTION_NODE;
608 curNode1->type = XML_TEXT_NODE;
609 curNode1->next = curNode2;
610 curNode2->type = XML_CDATA_SECTION_NODE;
611 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
612 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>("Hello world!"));
613 CxmlTest::DealCDataInfo(flag, curNode);
614 delete curNode2;
615 delete curNode1;
616 delete curNode;
617 ASSERT_TRUE(flag);
618 }