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 "tools/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
setPropertyForTrim(napi_env env,napi_value obj,std::vector<std::string> proVec)47 napi_value setPropertyForTrim(napi_env env, napi_value obj, std::vector<std::string> proVec)
48 {
49 const size_t boolNum = 8; // 8 : the counts of the bool value
50 const size_t firstNum = 0;
51 const size_t secondNum = 1;
52 const size_t thirdNum = 2;
53 napi_value val = nullptr;
54 for (size_t i = 0; i < proVec.size();) {
55 if (i == firstNum) {
56 napi_get_boolean(env, true, &val);
57 napi_set_named_property(env, obj, proVec[i].c_str(), val);
58 i++;
59 continue;
60 }
61 if (i == secondNum) {
62 const char* str = "abc";
63 napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &val);
64 napi_set_named_property(env, obj, proVec[i].c_str(), val);
65 i++;
66 continue;
67 }
68 if (i == thirdNum) {
69 int32_t number = 12345;
70 napi_create_int32(env, number, &val);
71 napi_set_named_property(env, obj, proVec[i].c_str(), val);
72 i++;
73 continue;
74 }
75 if (i < boolNum) {
76 napi_get_boolean(env, false, &val);
77 napi_set_named_property(env, obj, proVec[i].c_str(), val);
78 i++;
79 } else {
80 napi_create_string_utf8(env, proVec[i + 1].c_str(), proVec[i + 1].size(), &val);
81 napi_set_named_property(env, obj, proVec[i].c_str(), val);
82 i += 2; // 2 : the length of the value and property
83 }
84 }
85 return obj;
86 }
87
88 /* @tc.name: ConvertXmlTest001
89 * @tc.desc: Convert the xml object containing only declaration items to a js object.
90 * @tc.type: FUNC
91 */
92 HWTEST_F(NativeEngineTest, ConvertXmlTest001, testing::ext::TestSize.Level0)
93 {
94 size_t size = 1024; // 1024 : the size is 1024 byte
95 void* pBuffer = nullptr;
96 napi_value arrayBuffer = nullptr;
97 napi_env env = (napi_env)engine_;
98 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
99 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
100 xmlSerializer.SetDeclaration();
101
102 Options op;
103 ConvertXml *convertXml = new ConvertXml(env);
104 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
105 napi_value jsObj = convertXml->Convert(env, xmlStr, true);
106
107 // Do not set start tag '<'
108 napi_value declarationObj = nullptr;
109 napi_value declarationProp = nullptr;
110 napi_value versionVal = nullptr;
111 napi_value encodingVal = nullptr;
112 napi_get_named_property(env, jsObj, op.declaration.c_str(), &declarationObj);
113 napi_get_named_property(env, declarationObj, op.attributes.c_str(), &declarationProp);
114 napi_get_named_property(env, declarationProp, "version", &versionVal);
115 napi_get_named_property(env, declarationProp, "encoding", &encodingVal);
116
117 std::string verisonStr;
118 std::string encodingStr;
119 convertXml->DealNapiStrValue(env, versionVal, verisonStr);
120 convertXml->DealNapiStrValue(env, encodingVal, encodingStr);
121 EXPECT_STREQ(verisonStr.c_str(), "1.0");
122 EXPECT_STREQ(encodingStr.c_str(), "utf-8");
123 delete convertXml;
124 }
125
126 /* @tc.name: ConvertXmlTest002
127 * @tc.desc: Convert the xml object containing an empty element to a js object.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(NativeEngineTest, ConvertXmlTest002, testing::ext::TestSize.Level0)
131 {
132 size_t size = 1024; // 1024 : the size is 1024 byte
133 void* pBuffer = nullptr;
134 napi_value arrayBuffer = nullptr;
135 napi_env env = (napi_env)engine_;
136 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
137 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
138 xmlSerializer.StartElement("note1");
139 xmlSerializer.EndElement();
140
141 Options op;
142 ConvertXml *convertXml = new ConvertXml(env);
143 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
144 napi_value jsObj = convertXml->Convert(env, xmlStr, true);
145
146 napi_value element = nullptr;
147 napi_value elements = nullptr;
148 napi_value nameVal = nullptr;
149 napi_value typeVal = nullptr;
150 napi_get_named_property(env, jsObj, op.elements.c_str(), &elements);
151 napi_get_element(env, elements, 0, &element);
152 napi_get_named_property(env, element, op.name.c_str(), &nameVal);
153 napi_get_named_property(env, element, op.type.c_str(), &typeVal);
154
155 std::string nameStr;
156 std::string typeStr;
157 convertXml->DealNapiStrValue(env, nameVal, nameStr);
158 convertXml->DealNapiStrValue(env, typeVal, typeStr);
159 EXPECT_STREQ(nameStr.c_str(), "note1");
160 EXPECT_STREQ(typeStr.c_str(), "element");
161 }
162
163 /* @tc.name: ConvertXmlTest003
164 * @tc.desc: Convert the xml object containing attributes to a js object.
165 * @tc.type: FUNC
166 */
167 HWTEST_F(NativeEngineTest, ConvertXmlTest003, testing::ext::TestSize.Level0)
168 {
169 size_t size = 1024; // 1024 : the size is 1024 byte
170 void* pBuffer = nullptr;
171 napi_value arrayBuffer = nullptr;
172 napi_env env = (napi_env)engine_;
173 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
174 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
175 xmlSerializer.StartElement("note1");
176 xmlSerializer.SetAttributes("colour", "red");
177 xmlSerializer.SetAttributes("shape", "circle");
178 xmlSerializer.EndElement();
179
180 Options op;
181 ConvertXml *convertXml = new ConvertXml(env);
182 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
183 napi_value jsObj = convertXml->Convert(env, xmlStr, true);
184
185 napi_value elements = nullptr;
186 napi_value element = nullptr;
187 napi_value attributes = nullptr;
188 napi_value colour = nullptr;
189 napi_value shape = nullptr;
190 napi_get_named_property(env, jsObj, op.elements.c_str(), &elements);
191 napi_get_element(env, elements, 0, &element);
192 napi_get_named_property(env, element, op.attributes.c_str(), &attributes);
193 napi_get_named_property(env, attributes, "colour", &colour);
194 napi_get_named_property(env, attributes, "shape", &shape);
195
196 std::string colourStr;
197 std::string shapeStr;
198 convertXml->DealNapiStrValue(env, colour, colourStr);
199 convertXml->DealNapiStrValue(env, shape, shapeStr);
200 EXPECT_STREQ(colourStr.c_str(), "red");
201 EXPECT_STREQ(shapeStr.c_str(), "circle");
202 }
203
204 /* @tc.name: ConvertXmlTest004
205 * @tc.desc: Convert the xml object containing comment to a js object.
206 * @tc.type: FUNC
207 */
208 HWTEST_F(NativeEngineTest, ConvertXmlTest004, testing::ext::TestSize.Level0)
209 {
210 size_t size = 1024; // 1024 : the size is 1024 byte
211 void* pBuffer = nullptr;
212 napi_value arrayBuffer = nullptr;
213 napi_env env = (napi_env)engine_;
214 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
215 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
216 xmlSerializer.SetComment("This is a comment");
217 xmlSerializer.StartElement("note1");
218 xmlSerializer.EndElement();
219
220 Options op;
221 ConvertXml *convertXml = new ConvertXml(env);
222 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
223 napi_value jsObj = convertXml->Convert(env, xmlStr, true);
224
225 napi_value elements = nullptr;
226 napi_value element = nullptr;
227 napi_value commentType = nullptr;
228 napi_value commentText = nullptr;
229 EXPECT_EQ(napi_get_named_property(env, jsObj, op.elements.c_str(), &elements), napi_status::napi_ok);
230 EXPECT_EQ(napi_get_element(env, elements, 0, &element), napi_status::napi_ok);
231 EXPECT_EQ(napi_get_named_property(env, element, op.type.c_str(), &commentType), napi_status::napi_ok);
232 EXPECT_EQ(napi_get_named_property(env, element, op.comment.c_str(), &commentText), napi_status::napi_ok);
233
234 std::string commentTypeStr;
235 std::string commentTextStr;
236 convertXml->DealNapiStrValue(env, commentType, commentTypeStr);
237 convertXml->DealNapiStrValue(env, commentText, commentTextStr);
238 EXPECT_STREQ(commentTypeStr.c_str(), "comment");
239 EXPECT_STREQ(commentTextStr.c_str(), "This is a comment");
240 }
241
242 /* @tc.name: ConvertXmlTest005
243 * @tc.desc: Convert the xml object containing cdata to a js object.
244 * @tc.type: FUNC
245 */
246 HWTEST_F(NativeEngineTest, ConvertXmlTest005, testing::ext::TestSize.Level0)
247 {
248 size_t size = 1024; // 1024 : the size is 1024 byte
249 void* pBuffer = nullptr;
250 napi_value arrayBuffer = nullptr;
251 napi_env env = (napi_env)engine_;
252 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
253 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
254 xmlSerializer.SetCData("function foo() {}");
255 xmlSerializer.StartElement("note1");
256 xmlSerializer.EndElement();
257
258 Options op;
259 ConvertXml *convertXml = new ConvertXml(env);
260 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
261 napi_value jsObj = convertXml->Convert(env, xmlStr, true);
262
263 napi_value elements = nullptr;
264 napi_value element = nullptr;
265 napi_value cdataType = nullptr;
266 napi_value cdataText = nullptr;
267 EXPECT_EQ(napi_get_named_property(env, jsObj, op.elements.c_str(), &elements), napi_status::napi_ok);
268 EXPECT_EQ(napi_get_element(env, elements, 0, &element), napi_status::napi_ok);
269 EXPECT_EQ(napi_get_named_property(env, element, op.type.c_str(), &cdataType), napi_status::napi_ok);
270 EXPECT_EQ(napi_get_named_property(env, element, op.cdata.c_str(), &cdataText), napi_status::napi_ok);
271
272 std::string cdataTypeStr;
273 std::string cdataTextStr;
274 convertXml->DealNapiStrValue(env, cdataType, cdataTypeStr);
275 convertXml->DealNapiStrValue(env, cdataText, cdataTextStr);
276 EXPECT_STREQ(cdataTypeStr.c_str(), "cdata");
277 EXPECT_STREQ(cdataTextStr.c_str(), "function foo() {}");
278 }
279
280 /* @tc.name: ConvertXmlTest006
281 * @tc.desc: Convert the xml object containing doctype to a js object.
282 * @tc.type: FUNC
283 */
284 HWTEST_F(NativeEngineTest, ConvertXmlTest006, testing::ext::TestSize.Level0)
285 {
286 size_t size = 1024; // 1024 : the size is 1024 byte
287 void* pBuffer = nullptr;
288 napi_value arrayBuffer = nullptr;
289 napi_env env = (napi_env)engine_;
290 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
291 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
292 xmlSerializer.SetDocType("root SYSTEM \"http://www.test.org/test.dtd\"");
293 xmlSerializer.StartElement("note1");
294 xmlSerializer.EndElement();
295
296 Options op;
297 ConvertXml *convertXml = new ConvertXml(env);
298 std::string xmlStr(reinterpret_cast<char*>(pBuffer));
299 napi_value jsObj = convertXml->Convert(env, xmlStr, true);
300
301 napi_value elements = nullptr;
302 napi_value element = nullptr;
303 napi_value docType = nullptr;
304 napi_value docText = nullptr;
305 EXPECT_EQ(napi_get_named_property(env, jsObj, op.elements.c_str(), &elements), napi_status::napi_ok);
306 EXPECT_EQ(napi_get_element(env, elements, 0, &element), napi_status::napi_ok);
307 EXPECT_EQ(napi_get_named_property(env, element, op.type.c_str(), &docType), napi_status::napi_ok);
308 EXPECT_EQ(napi_get_named_property(env, element, op.doctype.c_str(), &docText), napi_status::napi_ok);
309
310 std::string docTypeStr;
311 std::string docStr;
312 convertXml->DealNapiStrValue(env, docType, docTypeStr);
313 convertXml->DealNapiStrValue(env, docText, docStr);
314 EXPECT_STREQ(docTypeStr.c_str(), "doctype");
315 EXPECT_STREQ(docStr.c_str(), "root");
316 }
317
318 /* @tc.name: ConstructorTest001
319 * @tc.desc: Convert the xml object containing doctype to a js object.
320 * @tc.type: FUNC
321 */
322 HWTEST_F(NativeEngineTest, ConstructorTest001, testing::ext::TestSize.Level0)
323 {
324 napi_env env = (napi_env)engine_;
325 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
326 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
327 std::string str2 = "<note importance=\"high\" logged=\"true\"><todo>Play</todo></note>";
328 std::string strXml = str1 + str2;
329 napi_valuetype valuetype = napi_undefined;
330
331 napi_typeof(env, convertXml.Convert(env, strXml, true), &valuetype);
332 bool isObj = valuetype == napi_valuetype::napi_object;
333 ASSERT_TRUE(isObj);
334 }
335
336 /* @tc.name: ConstructorTest002
337 * @tc.desc: Convert the xml object containing doctype to a js object.
338 * @tc.type: FUNC
339 */
340 HWTEST_F(NativeEngineTest, ConstructorTest002, testing::ext::TestSize.Level0)
341 {
342 napi_env env = (napi_env)engine_;
343 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
344 std::string str2 = "<note importance=\"high\" logged=\"true\"><todo>Play</todo></note>";
345 std::string strXml = str1 + str2;
346 napi_value object = nullptr;
347 const char* utf8Name = "_declaration";
348 napi_create_object(env, &object);
349 bool isHas = false;
350 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
351
352 object = convertXml.Convert(env, strXml, true);
353 napi_has_named_property(env, object, utf8Name, &isHas);
354 ASSERT_TRUE(isHas);
355 }
356
357 /* @tc.name: ConstructorTest003
358 * @tc.desc: Convert the xml object containing doctype to a js object.
359 * @tc.type: FUNC
360 */
361 HWTEST_F(NativeEngineTest, ConstructorTest003, testing::ext::TestSize.Level0)
362 {
363 napi_env env = (napi_env)engine_;
364 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
365 std::string str2 = "<note importance=\"high\" logged=\"true\"><todo>Play</todo></note>";
366 std::string strXml = str1 + str2;
367 napi_value object = nullptr;
368 const char* utf8Name = "_declaration";
369 napi_create_object(env, &object);
370 bool isHas = false;
371 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
372
373 object = convertXml.Convert(env, strXml, true);
374 napi_has_named_property(env, object, utf8Name, &isHas);
375 ASSERT_TRUE(isHas);
376 }
377
378 /* @tc.name: ConvertTest001
379 * @tc.desc: Convert the xml object containing doctype to a js object.
380 * @tc.type: FUNC
381 */
382 HWTEST_F(NativeEngineTest, ConvertTest001, testing::ext::TestSize.Level0)
383 {
384 napi_env env = (napi_env)engine_;
385 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
386 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><note importance=\"high\" logged=\"true\"> ";
387 std::string str2 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo></note>";
388 std::string strXml = str1 + str2;
389 napi_valuetype valuetype = napi_undefined;
390
391 napi_typeof(env, convertXml.Convert(env, strXml, true), &valuetype);
392 bool isObj = valuetype == napi_valuetype::napi_object;
393 ASSERT_TRUE(isObj);
394 }
395
396 /* @tc.name: ConvertTest002
397 * @tc.desc: Convert the xml object containing doctype to a js object.
398 * @tc.type: FUNC
399 */
400 HWTEST_F(NativeEngineTest, ConvertTest002, testing::ext::TestSize.Level0)
401 {
402 napi_env env = (napi_env)engine_;
403 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><note importance=\"high\" logged=\"true\"> ";
404 std::string str2 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo></note>";
405 std::string strXml = str1 + str2;
406 napi_value object = nullptr;
407 const char* utf8Name = "_declaration";
408 napi_create_object(env, &object);
409 bool isHas = false;
410 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
411
412 object = convertXml.Convert(env, strXml, true);
413 napi_has_named_property(env, object, utf8Name, &isHas);
414 ASSERT_TRUE(isHas);
415 }
416
417 /* @tc.name: ConvertTest003
418 * @tc.desc: Convert the xml object containing doctype to a js object.
419 * @tc.type: FUNC
420 */
421 HWTEST_F(NativeEngineTest, ConvertTest003, testing::ext::TestSize.Level0)
422 {
423 napi_env env = (napi_env)engine_;
424 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><note importance=\"high\" logged=\"true\"> ";
425 std::string str2 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo></note>";
426 std::string strXml = str1 + str2;
427 napi_value object = nullptr;
428 const char* utf8Name = "_elements";
429 napi_create_object(env, &object);
430 bool isHas = false;
431 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
432
433 object = convertXml.Convert(env, strXml, true);
434 napi_has_named_property(env, object, utf8Name, &isHas);
435 ASSERT_TRUE(isHas);
436 }
437
438 /* @tc.name: ConvertTest004
439 * @tc.desc: Convert the xml object containing doctype to a js object.
440 * @tc.type: FUNC
441 */
442 HWTEST_F(NativeEngineTest, ConvertTest004, testing::ext::TestSize.Level0)
443 {
444 napi_env env = (napi_env)engine_;
445 napi_value obj = nullptr;
446 napi_create_object(env, &obj);
447
448 std::vector<std::string> proVec = {"trim", "ignoreDeclaration", "ignoreInstruction", "ignoreAttributes",
449 "ignoreComment", "ignoreCDATA", "ignoreDoctype", "ignoreText", "declarationKey", "_declaration",
450 "instructionKey", "_instruction", "attributesKey", "_attributes", "textKey", "_text", "cdataKey", "_cdata",
451 "doctypeKey", "_doctype", "commentKey", "_comment", "parentKey", "_parent", "typeKey", "_type",
452 "nameKey", "_name", "elementsKey", "_elements"};
453 obj = setPropertyForTrim(env, obj, proVec);
454 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
455 convertXml.DealOptions(env, obj, false);
456 bool isHas = false;
457 napi_has_named_property(env, obj, "textKey", &isHas);
458 ASSERT_TRUE(isHas);
459
460 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
461 std::string str2 = "<note importance=\"high\" logged=\"true\"><todo>Play</todo></note>";
462 std::string strXml = str1 + str2;
463 napi_valuetype valuetype = napi_undefined;
464
465 napi_typeof(env, convertXml.Convert(env, strXml, true), &valuetype);
466 bool isObj = valuetype == napi_valuetype::napi_object;
467 ASSERT_TRUE(isObj);
468 }
469
470 /* @tc.name: ConvertTest005
471 * @tc.desc: Convert the xml object and set spaces info.
472 * @tc.type: FUNC
473 */
474 HWTEST_F(NativeEngineTest, ConvertTest005, testing::ext::TestSize.Level0)
475 {
476 napi_env env = (napi_env)engine_;
477 napi_value obj = nullptr;
478 napi_create_object(env, &obj);
479
480 std::vector<std::string> proVec = {"trim", "spaces", "ignoreInstruction", "ignoreAttributes",
481 "ignoreComment", "ignoreCDATA", "ignoreDoctype", "ignoreText", "declarationKey", "_declaration",
482 "instructionKey", "_instruction", "attributesKey", "_attributes", "textKey", "_text", "cdataKey", "_cdata",
483 "doctypeKey", "_doctype", "commentKey", "_comment", "parentKey", "_parent", "typeKey", "_type",
484 "nameKey", "_name", "elementsKey", "_elements"};
485 obj = setPropertyForTrim(env, obj, proVec);
486 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
487 convertXml.DealOptions(env, obj, false);
488 bool isHas = false;
489 napi_has_named_property(env, obj, "textKey", &isHas);
490 ASSERT_TRUE(isHas);
491
492 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
493 std::string str2 = "<note importance=\"high\" logged=\"true\"><todo>Play</todo></note>";
494 std::string strXml = str1 + str2;
495 napi_valuetype valuetype = napi_undefined;
496
497 napi_typeof(env, convertXml.Convert(env, strXml, true), &valuetype);
498 bool isObj = valuetype == napi_valuetype::napi_object;
499 ASSERT_TRUE(isObj);
500 }
501
502 /* @tc.name: ConvertTest006
503 * @tc.desc: Convert the xml object and set spaces info.
504 * @tc.type: FUNC
505 */
506 HWTEST_F(NativeEngineTest, ConvertTest006, testing::ext::TestSize.Level0)
507 {
508 napi_env env = (napi_env)engine_;
509 napi_value obj = nullptr;
510 napi_create_object(env, &obj);
511
512 std::vector<std::string> proVec = {"trim", "ignoreDeclaration", "spaces", "compact",
513 "ignoreComment", "ignoreCDATA", "ignoreDoctype", "ignoreText", "declarationKey", "_declaration",
514 "instructionKey", "_instruction", "attributesKey", "_attributes", "textKey", "_text", "cdataKey", "_cdata",
515 "doctypeKey", "_doctype", "commentKey", "_comment", "parentKey", "_parent", "typeKey", "_type",
516 "nameKey", "_name", "elementsKey", "_elements"};
517 obj = setPropertyForTrim(env, obj, proVec);
518 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
519 convertXml.DealOptions(env, obj, false);
520 bool isHas = false;
521 napi_has_named_property(env, obj, "textKey", &isHas);
522 ASSERT_TRUE(isHas);
523
524 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
525 std::string str2 = "<note importance=\"high\" logged=\"true\"><todo>Play</todo></note>";
526 std::string strXml = str1 + str2;
527 napi_valuetype valuetype = napi_undefined;
528
529 napi_typeof(env, convertXml.Convert(env, strXml, true), &valuetype);
530 bool isObj = valuetype == napi_valuetype::napi_object;
531 ASSERT_TRUE(isObj);
532 }
533
534 /* @tc.name: DealNapiStrValueTest001
535 * @tc.desc: Deal napi string value.
536 * @tc.type: FUNC
537 */
538 HWTEST_F(NativeEngineTest, DealNapiStrValueTest001, testing::ext::TestSize.Level0)
539 {
540 napi_env env = (napi_env)engine_;
541 napi_value obj = nullptr;
542 napi_create_object(env, &obj);
543 std::string str = "";
544
545 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
546 napi_status rel = convertXml.DealNapiStrValue(env, nullptr, str);
547
548 ASSERT_FALSE(rel == napi_ok);
549 }
550
551 /* @tc.name: DealIgnoreTest001
552 * @tc.desc: Deal napi string value.
553 * @tc.type: FUNC
554 */
555 HWTEST_F(NativeEngineTest, DealIgnoreTest001, testing::ext::TestSize.Level0)
556 {
557 napi_env env = (napi_env)engine_;
558 napi_value obj = nullptr;
559 napi_create_object(env, &obj);
560 std::string str = "";
561
562 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
563 napi_status rel = convertXml.DealNapiStrValue(env, nullptr, str);
564
565 ASSERT_FALSE(rel == napi_ok);
566 }
567
568 /* @tc.name: DealOptionsTest001
569 * @tc.desc: Convert the xml object containing doctype to a js object.
570 * @tc.type: FUNC
571 */
572 HWTEST_F(NativeEngineTest, DealOptionsTest001, testing::ext::TestSize.Level0)
573 {
574 napi_env env = (napi_env)engine_;
575 napi_value obj = nullptr;
576 napi_create_object(env, &obj);
577
578 std::vector<std::string> proVec = {"trim", "ignoreDeclaration", "ignoreInstruction", "ignoreAttributes",
579 "ignoreComment", "ignoreCDATA", "ignoreDoctype", "ignoreText", "declarationKey", "_declaration",
580 "instructionKey", "_instruction", "attributesKey", "_attributes", "textKey", "_text", "cdataKey", "_cdata",
581 "doctypeKey", "_doctype", "commentKey", "_comment", "parentKey", "_parent", "typeKey", "_type",
582 "nameKey", "_name", "elementsKey", "_elements"};
583
584 obj = setProperty(env, obj, proVec);
585 OHOS::Xml::ConvertXml convertXml = OHOS::Xml::ConvertXml(env);
586 convertXml.DealOptions(env, obj, false);
587 bool isHas = false;
588 napi_has_named_property(env, obj, "textKey", &isHas);
589 ASSERT_TRUE(isHas);
590 }
591
592 /* @tc.name: NativeModuleConvertXmlTest001
593 * @tc.desc: Convert the xml object containing doctype to a js object.
594 * @tc.type: FUNC
595 */
596 HWTEST_F(NativeEngineTest, NativeModuleConvertXmlTest001, testing::ext::TestSize.Level1)
597 {
598 napi_env env = (napi_env)engine_;
599 napi_value exports = nullptr;
600 napi_create_object(env, &exports);
601 OHOS::Xml::ConvertXmlInit(env, exports);
602 napi_value convertXmlClass = nullptr;
603 napi_get_named_property(env, exports, "ConvertXml", &convertXmlClass);
604
605 napi_value instance = nullptr;
606 napi_new_instance(env, convertXmlClass, 0, nullptr, &instance);
607
608 napi_value args[2]; // 2: number of arguments
609 std::string firXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><note importance=\"high\" logged=\"true\">";
610 std::string secXml = "<title>Happy</title></note>";
611 std::string strXml = firXml + secXml;
612 napi_create_string_utf8(env, strXml.c_str(), strXml.size(), &args[0]);
613
614 napi_value obj = nullptr;
615 napi_create_object(env, &obj);
616 std::vector<std::string> proVec = {"trim", "ignoreDeclaration", "ignoreInstruction", "ignoreAttributes",
617 "ignoreComment", "ignoreCDATA", "ignoreDoctype", "ignoreText", "declarationKey", "_declaration",
618 "instructionKey", "_instruction", "attributesKey", "_attributes", "textKey", "_text", "cdataKey", "_cdata",
619 "doctypeKey", "_doctype", "commentKey", "_comment", "parentKey", "_parent", "typeKey", "_type",
620 "nameKey", "_name", "elementsKey", "_elements"};
621 args[1] = setProperty(env, obj, proVec);
622
623 napi_value funcResultValue = nullptr;
624 napi_value testFunc = nullptr;
625 napi_get_named_property(env, instance, "convert", &testFunc);
626 napi_call_function(env, instance, testFunc, 2, args, &funcResultValue); // 2: number of arguments
627 ASSERT_NE(funcResultValue, nullptr);
628 }
629
630 HWTEST_F(NativeEngineTest, TrimTest001, testing::ext::TestSize.Level1)
631 {
632 napi_env env = (napi_env)engine_;
633 CxmlTest::Trim(env, "");
634 std::string res = CxmlTest::Trim(env, " #e ");
635 ASSERT_STREQ(res.c_str(), "#e");
636 }
637
638 HWTEST_F(NativeEngineTest, GetNodeTypeTest001, testing::ext::TestSize.Level1)
639 {
640 napi_env env = (napi_env)engine_;
641 xmlElementType enumType = XML_ATTRIBUTE_NODE;
642 std::string res = CxmlTest::GetNodeType(env, enumType);
643 ASSERT_STREQ(res.c_str(), "attribute");
644 enumType = XML_ENTITY_REF_NODE;
645 CxmlTest::GetNodeType(env, enumType);
646 enumType = XML_ENTITY_NODE;
647 CxmlTest::GetNodeType(env, enumType);
648 enumType = XML_PI_NODE;
649 CxmlTest::GetNodeType(env, enumType);
650 enumType = XML_DOCUMENT_NODE;
651 CxmlTest::GetNodeType(env, enumType);
652 enumType = XML_DOCUMENT_TYPE_NODE;
653 CxmlTest::GetNodeType(env, enumType);
654 enumType = XML_DOCUMENT_FRAG_NODE;
655 CxmlTest::GetNodeType(env, enumType);
656 enumType = XML_DOCB_DOCUMENT_NODE;
657 CxmlTest::GetNodeType(env, enumType);
658 enumType = XML_XINCLUDE_END;
659 res = CxmlTest::GetNodeType(env, enumType);
660 ASSERT_STREQ(res.c_str(), "");
661 }
662
663 HWTEST_F(NativeEngineTest, GetPrevNodeListTest001, testing::ext::TestSize.Level1)
664 {
665 napi_env env = (napi_env)engine_;
666 xmlNodePtr curNode = new xmlNode;
667 xmlNodePtr curNode1 = new xmlNode;
668 curNode->prev = curNode1;
669 curNode1->prev = nullptr;
670 curNode1->type = XML_PI_NODE;
671 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
672 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>("Hello world!"));
673 CxmlTest::GetPrevNodeList(env, curNode);
674 ASSERT_TRUE(curNode != nullptr);
675 delete curNode;
676 delete curNode1;
677 }
678
679 HWTEST_F(NativeEngineTest, SetPrevInfoTest001, testing::ext::TestSize.Level1)
680 {
681 napi_env env = (napi_env)engine_;
682 xmlNodePtr curNode = new xmlNode;
683 xmlNodePtr curNode1 = new xmlNode;
684 curNode->prev = curNode1;
685 curNode1->prev = nullptr;
686 curNode1->type = XML_PI_NODE;
687 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
688 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>("Hello world!"));
689 CxmlTest::GetAnDSetPrevNodeList(env, curNode);
690 ASSERT_TRUE(curNode != nullptr);
691 delete curNode;
692 delete curNode1;
693 }
694
695 HWTEST_F(NativeEngineTest, SetXmlElementTypeTest001, testing::ext::TestSize.Level1)
696 {
697 napi_env env = (napi_env)engine_;
698 napi_value elementsObject = nullptr;
699 napi_create_object(env, &elementsObject);
700 xmlNodePtr curNode1 = new xmlNode;
701 curNode1->type = XML_PI_NODE;
702 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
703 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>("Hello world!"));
704 bool flag = false;
705 CxmlTest::SetXmlElementType(env, curNode1, elementsObject, flag);
706 flag = false;
707 curNode1->type = XML_COMMENT_NODE;
708 CxmlTest::SetXmlElementType(env, curNode1, elementsObject, flag);
709 delete curNode1;
710 ASSERT_TRUE(flag);
711 }
712
713 HWTEST_F(NativeEngineTest, SetNodeInfoTest001, testing::ext::TestSize.Level1)
714 {
715 napi_env env = (napi_env)engine_;
716 napi_value elementsObject = nullptr;
717 napi_create_object(env, &elementsObject);
718 xmlNodePtr curNode1 = new xmlNode;
719 curNode1->type = XML_PI_NODE;
720 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
721 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>("Hello world!"));
722 bool flag = true;
723 CxmlTest::SetNodeInfo(env, curNode1, elementsObject);
724 delete curNode1;
725 ASSERT_TRUE(flag);
726 }
727
728 HWTEST_F(NativeEngineTest, DealSpacesTest001, testing::ext::TestSize.Level1)
729 {
730 napi_env env = (napi_env)engine_;
731 napi_value napiObj = nullptr;
732 napi_create_object(env, &napiObj);
733 napi_value spacesValue;
734 napi_create_string_utf8(env, "hello world", NAPI_AUTO_LENGTH, &spacesValue);
735 napi_set_named_property(env, napiObj, "spaces", spacesValue);
736 bool flag = true;
737 CxmlTest::DealSpaces(env, napiObj);
738 ASSERT_TRUE(flag);
739 }
740
741 HWTEST_F(NativeEngineTest, DealSpacesTest002, testing::ext::TestSize.Level1)
742 {
743 napi_env env = (napi_env)engine_;
744 napi_value napiObj = nullptr;
745 napi_create_object(env, &napiObj);
746 napi_value spacesValue;
747 napi_create_int32(env, 123, &spacesValue); // 123: number of test number
748 napi_set_named_property(env, napiObj, "spaces", spacesValue);
749 bool flag = true;
750 CxmlTest::DealSpaces(env, napiObj);
751 ASSERT_TRUE(flag);
752 }
753
754 HWTEST_F(NativeEngineTest, SetDefaultKeyTest001, testing::ext::TestSize.Level1)
755 {
756 napi_env env = (napi_env)engine_;
757 size_t i = 15; // 15: number of default number
758 std::string key = "hello";
759 CxmlTest::SetDefaultKey(env, i, key);
760 ASSERT_STREQ(key.c_str(), "hello");
761 }
762
763 HWTEST_F(NativeEngineTest, DealSingleLineTest001, testing::ext::TestSize.Level1)
764 {
765 napi_env env = (napi_env)engine_;
766 std::string key = "xmlsss<zyyzyy>ssa";
767 napi_value napiObj = nullptr;
768 napi_create_object(env, &napiObj);
769 CxmlTest::DealSingleLine(env, key, napiObj);
770 ASSERT_STREQ(key.c_str(), "<node>xmlsss<zyyzyy>ssassa</node>");
771 }
772
773 HWTEST_F(NativeEngineTest, DealSingleLineTest002, testing::ext::TestSize.Level1)
774 {
775 napi_env env = (napi_env)engine_;
776 std::string key = " xmlsss<zyyzyy>ssa";
777 napi_value napiObj = nullptr;
778 napi_create_object(env, &napiObj);
779 CxmlTest::DealSingleLine(env, key, napiObj);
780 ASSERT_STREQ(key.c_str(), "<node> xmlsss<zyyzyy>ssassa</node>");
781 }
782
783 HWTEST_F(NativeEngineTest, DealComplexTest001, testing::ext::TestSize.Level1)
784 {
785 napi_env env = (napi_env)engine_;
786 std::string key = "xmlsss<!DOCTYPE>ssa";
787 napi_value napiObj = nullptr;
788 napi_create_object(env, &napiObj);
789 CxmlTest::DealComplex(env, key, napiObj);
790 ASSERT_STREQ(key.c_str(), "xmlsss<!DOCTYPE>ssa<node></node>");
791 }
792
793 HWTEST_F(NativeEngineTest, ReplaceTest001, testing::ext::TestSize.Level1)
794 {
795 napi_env env = (napi_env)engine_;
796 std::string str = "xmlsss<!DOCTYPE>ssa";
797 std::string src = "sss";
798 std::string dst = "zyy";
799 CxmlTest::Replace(env, str, src, dst);
800 ASSERT_STREQ(str.c_str(), "xmlzyy<!DOCTYPE>ssa");
801 }
802
803 HWTEST_F(NativeEngineTest, DealCDataInfo001, testing::ext::TestSize.Level1)
804 {
805 napi_env env = (napi_env)engine_;
806 bool flag = true;
807 xmlNodePtr curNode = new xmlNode;
808 xmlNodePtr curNode1 = new xmlNode;
809 xmlNodePtr curNode2 = new xmlNode;
810 curNode->next = curNode1;
811 curNode->type = XML_CDATA_SECTION_NODE;
812 curNode1->type = XML_TEXT_NODE;
813 curNode1->next = curNode2;
814 curNode2->type = XML_CDATA_SECTION_NODE;
815 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
816 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>("Hello world!"));
817 CxmlTest::DealCDataInfo(env, flag, curNode);
818 delete curNode2;
819 delete curNode1;
820 delete curNode;
821 ASSERT_TRUE(flag);
822 }
823
824 HWTEST_F(NativeEngineTest, DealCDataInfo002, testing::ext::TestSize.Level1)
825 {
826 napi_env env = (napi_env)engine_;
827 bool flag = true;
828 xmlNodePtr curNode = new xmlNode;
829 xmlNodePtr curNode1 = new xmlNode;
830 xmlNodePtr curNode2 = new xmlNode;
831 curNode->next = curNode1;
832 curNode->type = XML_CDATA_SECTION_NODE;
833 curNode1->type = XML_TEXT_NODE;
834 curNode1->next = curNode2;
835 curNode2->type = XML_CDATA_SECTION_NODE;
836
837 curNode1->name = reinterpret_cast<const xmlChar *>("Hello world!");
838 curNode1->content = const_cast<xmlChar *>(reinterpret_cast<const xmlChar *>(" \t\t\t\t\t"));
839 CxmlTest::DealCDataInfo(env, flag, curNode);
840 ASSERT_TRUE(flag);
841 }