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.h"
17
18 #include "napi/native_api.h"
19 #include "napi/native_node_api.h"
20
21 #include "js_xml.h"
22 #include "utils/log.h"
23
24 #define ASSERT_CHECK_CALL(call) \
25 { \
26 ASSERT_EQ(call, napi_ok); \
27 }
28
29 #define ASSERT_CHECK_VALUE_TYPE(env, value, type) \
30 { \
31 napi_valuetype valueType = napi_undefined; \
32 ASSERT_TRUE(value != nullptr); \
33 ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
34 ASSERT_EQ(valueType, type); \
35 }
36
37 std::string testStr = "";
Method(napi_env env,napi_callback_info info)38 napi_value Method(napi_env env, napi_callback_info info)
39 {
40 napi_value thisVar = nullptr;
41 size_t argc = 0;
42 napi_value args[6] = { 0 }; // 6:six args
43 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
44 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
45
46 napi_value name = args[0];
47 napi_value value = args[1];
48
49 std::string buffer1 = "";
50 size_t bufferSize1 = 0;
51 napi_get_value_string_utf8(env, name, nullptr, 0, &bufferSize1);
52 buffer1.reserve(bufferSize1 + 1);
53 buffer1.resize(bufferSize1);
54 napi_get_value_string_utf8(env, name, buffer1.data(), bufferSize1 + 1, &bufferSize1);
55
56 std::string buffer2 = "";
57 size_t bufferSize2 = 0;
58 napi_get_value_string_utf8(env, value, nullptr, 0, &bufferSize2);
59 buffer2.reserve(bufferSize2 + 1);
60 buffer2.resize(bufferSize2);
61 napi_get_value_string_utf8(env, value, buffer2.data(), bufferSize2 + 1, &bufferSize2);
62 testStr += buffer1 + buffer2;
63 napi_value result = nullptr;
64 napi_get_boolean(env, true, &result);
65 return result;
66 }
67
TokenValueCallbackFunction(napi_env env,napi_callback_info info)68 napi_value TokenValueCallbackFunction(napi_env env, napi_callback_info info)
69 {
70 napi_value thisVar = nullptr;
71 size_t argc = 0;
72 napi_value args[6] = { 0 }; // 6:six args
73 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
74 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
75 napi_value value = args[1];
76 napi_value value3 = nullptr;
77 napi_get_named_property(env, value, "getDepth", &value3);
78 napi_get_named_property(env, value, "getColumnNumber", &value3);
79 napi_get_named_property(env, value, "getLineNumber", &value3);
80 napi_get_named_property(env, value, "getAttributeCount", &value3);
81 napi_get_named_property(env, value, "getName", &value3);
82 napi_get_named_property(env, value, "getNamespace", &value3);
83 napi_get_named_property(env, value, "getPrefix", &value3);
84 napi_get_named_property(env, value, "getText", &value3);
85 napi_get_named_property(env, value, "isEmptyElementTag", &value3);
86 napi_get_named_property(env, value, "isWhitespace", &value3);
87 napi_valuetype result2 = napi_null;
88 napi_typeof(env, value3, &result2);
89 napi_value returnVal = nullptr;
90 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
91
92 napi_value result = nullptr;
93 napi_get_boolean(env, true, &result);
94 return result;
95 }
96
TokenValueCallbackFunc(napi_env env,napi_callback_info info)97 napi_value TokenValueCallbackFunc(napi_env env, napi_callback_info info)
98 {
99 napi_value thisVar = nullptr;
100 size_t argc = 0;
101 napi_value args[6] = { 0 }; // 6:six args
102 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
103 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
104 napi_value value = args[1];
105 napi_value value3 = nullptr;
106 napi_get_named_property(env, value, "getDepth", &value3);
107 napi_value returnVal = nullptr;
108 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
109 napi_get_named_property(env, value, "getColumnNumber", &value3);
110 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
111 napi_get_named_property(env, value, "getLineNumber", &value3);
112 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
113 napi_get_named_property(env, value, "getAttributeCount", &value3);
114 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
115 napi_get_named_property(env, value, "getName", &value3);
116 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
117 napi_get_named_property(env, value, "getNamespace", &value3);
118 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
119 napi_get_named_property(env, value, "getPrefix", &value3);
120 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
121 napi_get_named_property(env, value, "getText", &value3);
122 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
123 napi_get_named_property(env, value, "isEmptyElementTag", &value3);
124 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
125 napi_get_named_property(env, value, "isWhitespace", &value3);
126 napi_call_function(env, thisVar, value3, 0, nullptr, &returnVal);
127
128 napi_value result = nullptr;
129 napi_get_boolean(env, true, &result);
130 return result;
131 }
132 /* @tc.name: StartElementTest001
133 * @tc.desc: Test whether write a elemnet start tag with the given name successfully.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(NativeEngineTest, StartElementTest001, testing::ext::TestSize.Level0)
137 {
138 napi_env env = (napi_env)engine_;
139 napi_value arrayBuffer = nullptr;
140 void* pBuffer = nullptr;
141 size_t size = 1024;
142 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
143 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
144 xmlSerializer.StartElement("note1");
145 xmlSerializer.EndElement();
146 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note1/>");
147 }
148
149 /* @tc.name: StartElementTest002
150 * @tc.desc: Test whether write a elemnet start tag with the given name successfully.
151 * @tc.type: FUNC
152 */
153 HWTEST_F(NativeEngineTest, StartElementTest002, testing::ext::TestSize.Level0)
154 {
155 napi_env env = (napi_env)engine_;
156 napi_value arrayBuffer = nullptr;
157 void* pBuffer = nullptr;
158 size_t size = 1024;
159 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
160 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
161 xmlSerializer.StartElement("note1");
162 xmlSerializer.EndElement();
163 xmlSerializer.StartElement("note2");
164 xmlSerializer.EndElement();
165 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note1/>\r\n<note2/>");
166 }
167
168 /* @tc.name: StartElementTest003
169 * @tc.desc: Test whether write a elemnet start tag with the given name successfully.
170 * @tc.type: FUNC
171 */
172 HWTEST_F(NativeEngineTest, StartElementTest003, testing::ext::TestSize.Level0)
173 {
174 napi_env env = (napi_env)engine_;
175 napi_value arrayBuffer = nullptr;
176 void* pBuffer = nullptr;
177 size_t size = 1024;
178 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
179 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
180 xmlSerializer.StartElement("note1");
181 xmlSerializer.StartElement("note2");
182 xmlSerializer.EndElement();
183 xmlSerializer.EndElement();
184 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note1>\r\n <note2/>\r\n</note1>");
185 }
186
187 /* @tc.name: StartElementTest004
188 * @tc.desc: Test whether write a elemnet start tag with the given name successfully.
189 * @tc.type: FUNC
190 */
191 HWTEST_F(NativeEngineTest, StartElementTest004, testing::ext::TestSize.Level0)
192 {
193 napi_env env = (napi_env)engine_;
194 napi_value arrayBuffer = nullptr;
195 void* pBuffer = nullptr;
196 size_t size = 1024;
197 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
198 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
199 xmlSerializer.StartElement("note1");
200 xmlSerializer.StartElement("note2");
201 xmlSerializer.StartElement("note3");
202 xmlSerializer.EndElement();
203 xmlSerializer.EndElement();
204 xmlSerializer.EndElement();
205 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note1>\r\n <note2>\r\n <note3/>\r\n </note2>\r\n</note1>");
206 }
207
208 /* @tc.name: StartElementTest005
209 * @tc.desc: Test whether write a elemnet start tag with the given name successfully.
210 * @tc.type: FUNC
211 */
212 HWTEST_F(NativeEngineTest, StartElementTest005, testing::ext::TestSize.Level0)
213 {
214 napi_env env = (napi_env)engine_;
215 napi_value arrayBuffer = nullptr;
216 void* pBuffer = nullptr;
217 size_t size = 1024;
218 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
219 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
220 xmlSerializer.StartElement("note1");
221 xmlSerializer.StartElement("note2");
222 xmlSerializer.EndElement();
223 xmlSerializer.StartElement("note3");
224 xmlSerializer.EndElement();
225 xmlSerializer.EndElement();
226 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note1>\r\n <note2/>\r\n <note3/>\r\n</note1>");
227 }
228
229 /* @tc.name: SetAttributesTest001
230 * @tc.desc: Test whether write an attribute successfully.
231 * @tc.type: FUNC
232 */
233 HWTEST_F(NativeEngineTest, SetAttributesTest001, testing::ext::TestSize.Level0)
234 {
235 napi_env env = (napi_env)engine_;
236 napi_value arrayBuffer = nullptr;
237 void* pBuffer = nullptr;
238 size_t size = 1024;
239 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
240 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
241 xmlSerializer.StartElement("note");
242 xmlSerializer.SetAttributes("importance1", "high1");
243 xmlSerializer.EndElement();
244 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note importance1=\"high1\"/>");
245 }
246
247 /* @tc.name: SetAttributesTest002
248 * @tc.desc: Test whether write an attribute successfully.
249 * @tc.type: FUNC
250 */
251 HWTEST_F(NativeEngineTest, SetAttributesTest002, testing::ext::TestSize.Level0)
252 {
253 napi_env env = (napi_env)engine_;
254 napi_value arrayBuffer = nullptr;
255 void* pBuffer = nullptr;
256 size_t size = 1024;
257 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
258 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
259 xmlSerializer.StartElement("note");
260 xmlSerializer.SetAttributes("importance1", "high1");
261 xmlSerializer.SetAttributes("importance2", "high2");
262 xmlSerializer.EndElement();
263 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note importance1=\"high1\" importance2=\"high2\"/>");
264 }
265
266 /* @tc.name: SetAttributesTest003
267 * @tc.desc: Test whether write an attribute successfully.
268 * @tc.type: FUNC
269 */
270 HWTEST_F(NativeEngineTest, SetAttributesTest003, testing::ext::TestSize.Level0)
271 {
272 napi_env env = (napi_env)engine_;
273 napi_value arrayBuffer = nullptr;
274 void* pBuffer = nullptr;
275 size_t size = 1024;
276 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
277 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
278 xmlSerializer.StartElement("note");
279 xmlSerializer.SetAttributes("importance1", "high1");
280 xmlSerializer.SetAttributes("importance2", "high2");
281 xmlSerializer.SetAttributes("importance3", "high3");
282 xmlSerializer.EndElement();
283 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
284 "<note importance1=\"high1\" importance2=\"high2\" importance3=\"high3\"/>");
285 }
286
287 /* @tc.name: SetAttributesTest004
288 * @tc.desc: Test whether write an attribute successfully.
289 * @tc.type: FUNC
290 */
291 HWTEST_F(NativeEngineTest, SetAttributesTest004, testing::ext::TestSize.Level0)
292 {
293 napi_env env = (napi_env)engine_;
294 napi_value arrayBuffer = nullptr;
295 void* pBuffer = nullptr;
296 size_t size = 1024;
297 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
298 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
299 xmlSerializer.StartElement("note");
300 xmlSerializer.SetAttributes("importance1", "high1");
301 xmlSerializer.SetAttributes("importance2", "high2");
302 xmlSerializer.SetAttributes("importance3", "high3");
303 xmlSerializer.SetAttributes("importance4", "high4");
304 xmlSerializer.EndElement();
305 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
306 "<note importance1=\"high1\" importance2=\"high2\" importance3=\"high3\" importance4=\"high4\"/>");
307 }
308
309 /* @tc.name: SetAttributesTest005
310 * @tc.desc: Test whether write an attribute successfully.
311 * @tc.type: FUNC
312 */
313 HWTEST_F(NativeEngineTest, SetAttributesTest005, testing::ext::TestSize.Level0)
314 {
315 napi_env env = (napi_env)engine_;
316 napi_value arrayBuffer = nullptr;
317 void* pBuffer = nullptr;
318 size_t size = 1024;
319 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
320 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
321 xmlSerializer.StartElement("note");
322 xmlSerializer.SetAttributes("importance1", "high1");
323 xmlSerializer.SetAttributes("importance2", "high2");
324 xmlSerializer.SetAttributes("importance3", "high3");
325 xmlSerializer.SetAttributes("importance4", "high4");
326 xmlSerializer.SetAttributes("importance5", "high5");
327 xmlSerializer.EndElement();
328 std::string strPrior = "<note importance1=\"high1\" importance2=\"high2\" ";
329 std::string strBack = "importance3=\"high3\" importance4=\"high4\" importance5=\"high5\"/>";
330 std::string strEnd = strPrior + strBack;
331 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), strEnd.c_str());
332 }
333
334 /* @tc.name: AddEmptyElementTest001
335 * @tc.desc: Test whether add an empty element successfully.
336 * @tc.type: FUNC
337 */
338 HWTEST_F(NativeEngineTest, AddEmptyElementTest001, testing::ext::TestSize.Level0)
339 {
340 napi_env env = (napi_env)engine_;
341 napi_value arrayBuffer = nullptr;
342 void* pBuffer = nullptr;
343 size_t size = 1024;
344 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
345 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
346 xmlSerializer.StartElement("note");
347 xmlSerializer.AddEmptyElement("a");
348 xmlSerializer.EndElement();
349 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note>\r\n <a/>\r\n</note>");
350 }
351
352 /* @tc.name: AddEmptyElementTest002
353 * @tc.desc: Test whether add an empty element successfully.
354 * @tc.type: FUNC
355 */
356 HWTEST_F(NativeEngineTest, AddEmptyElementTest002, testing::ext::TestSize.Level0)
357 {
358 napi_env env = (napi_env)engine_;
359 napi_value arrayBuffer = nullptr;
360 void* pBuffer = nullptr;
361 size_t size = 1024;
362 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
363 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
364 xmlSerializer.StartElement("note");
365 xmlSerializer.EndElement();
366 xmlSerializer.AddEmptyElement("b");
367 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note/>\r\n<b/>");
368 }
369
370 /* @tc.name: AddEmptyElementTest003
371 * @tc.desc: Test whether add an empty element successfully.
372 * @tc.type: FUNC
373 */
374 HWTEST_F(NativeEngineTest, AddEmptyElementTest003, testing::ext::TestSize.Level0)
375 {
376 napi_env env = (napi_env)engine_;
377 napi_value arrayBuffer = nullptr;
378 void* pBuffer = nullptr;
379 size_t size = 1024;
380 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
381 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
382 xmlSerializer.AddEmptyElement("c");
383 xmlSerializer.StartElement("note");
384 xmlSerializer.EndElement();
385 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<c/>\r\n<note/>");
386 }
387
388 /* @tc.name: AddEmptyElementTest004
389 * @tc.desc: Test whether add an empty element successfully.
390 * @tc.type: FUNC
391 */
392 HWTEST_F(NativeEngineTest, AddEmptyElementTest004, testing::ext::TestSize.Level0)
393 {
394 napi_env env = (napi_env)engine_;
395 napi_value arrayBuffer = nullptr;
396 void* pBuffer = nullptr;
397 size_t size = 1024;
398 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
399 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
400 xmlSerializer.AddEmptyElement("d");
401 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<d/>");
402 }
403
404 /* @tc.name: AddEmptyElementTest005
405 * @tc.desc: Test whether add an empty element successfully.
406 * @tc.type: FUNC
407 */
408 HWTEST_F(NativeEngineTest, AddEmptyElementTest005, testing::ext::TestSize.Level0)
409 {
410 napi_env env = (napi_env)engine_;
411 napi_value arrayBuffer = nullptr;
412 void* pBuffer = nullptr;
413 size_t size = 1024;
414 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
415 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
416 xmlSerializer.StartElement("note");
417 xmlSerializer.AddEmptyElement("c");
418 xmlSerializer.AddEmptyElement("d");
419 xmlSerializer.EndElement();
420 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note>\r\n <c/>\r\n <d/>\r\n</note>");
421 }
422
423 /* @tc.name: SetDeclarationTest001
424 * @tc.desc: Test whether write xml declaration with encoding successfully.
425 * @tc.type: FUNC
426 */
427 HWTEST_F(NativeEngineTest, SetDeclarationTest001, testing::ext::TestSize.Level0)
428 {
429 napi_env env = (napi_env)engine_;
430 napi_value arrayBuffer = nullptr;
431 void* pBuffer = nullptr;
432 size_t size = 1024;
433 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
434 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
435 xmlSerializer.SetDeclaration();
436 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
437 }
438
439 /* @tc.name: SetDeclarationTest002
440 * @tc.desc: Test whether write xml declaration with encoding successfully.
441 * @tc.type: FUNC
442 */
443 HWTEST_F(NativeEngineTest, SetDeclarationTest002, testing::ext::TestSize.Level0)
444 {
445 napi_env env = (napi_env)engine_;
446 napi_value arrayBuffer = nullptr;
447 void* pBuffer = nullptr;
448 size_t size = 1024;
449 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
450 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
451 xmlSerializer.SetDeclaration();
452 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
453 }
454
455 /* @tc.name: SetDeclarationTest003
456 * @tc.desc: Test whether write xml declaration with encoding successfully.
457 * @tc.type: FUNC
458 */
459 HWTEST_F(NativeEngineTest, SetDeclarationTest003, testing::ext::TestSize.Level0)
460 {
461 napi_env env = (napi_env)engine_;
462 napi_value arrayBuffer = nullptr;
463 void* pBuffer = nullptr;
464 size_t size = 1024;
465 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
466 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
467 xmlSerializer.SetDeclaration();
468 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
469 }
470
471 /* @tc.name: SetDeclarationTest004
472 * @tc.desc: Test whether write xml declaration with encoding successfully.
473 * @tc.type: FUNC
474 */
475 HWTEST_F(NativeEngineTest, SetDeclarationTest004, testing::ext::TestSize.Level0)
476 {
477 napi_env env = (napi_env)engine_;
478 napi_value arrayBuffer = nullptr;
479 void* pBuffer = nullptr;
480 size_t size = 1024;
481 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
482 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
483 xmlSerializer.SetDeclaration();
484 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
485 }
486
487 /* @tc.name: SetDeclarationTest005
488 * @tc.desc: Test whether write xml declaration with encoding successfully.
489 * @tc.type: FUNC
490 */
491 HWTEST_F(NativeEngineTest, SetDeclarationTest005, testing::ext::TestSize.Level0)
492 {
493 napi_env env = (napi_env)engine_;
494 napi_value arrayBuffer = nullptr;
495 void* pBuffer = nullptr;
496 size_t size = 1024;
497 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
498 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
499 xmlSerializer.SetDeclaration();
500 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
501 }
502
503 /* @tc.name: EndElementTest001
504 * @tc.desc: Test whether write end tag of the element successfully.
505 * @tc.type: FUNC
506 */
507 HWTEST_F(NativeEngineTest, EndElementTest001, testing::ext::TestSize.Level0)
508 {
509 napi_env env = (napi_env)engine_;
510 napi_value arrayBuffer = nullptr;
511 void* pBuffer = nullptr;
512 size_t size = 1024;
513 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
514 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
515 xmlSerializer.StartElement("note");
516 xmlSerializer.EndElement();
517 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note/>");
518 }
519
520 /* @tc.name: EndElementTest002
521 * @tc.desc: Test whether write end tag of the element successfully.
522 * @tc.type: FUNC
523 */
524 HWTEST_F(NativeEngineTest, EndElementTest002, testing::ext::TestSize.Level0)
525 {
526 napi_env env = (napi_env)engine_;
527 napi_value arrayBuffer = nullptr;
528 void* pBuffer = nullptr;
529 size_t size = 1024;
530 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
531 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
532 xmlSerializer.StartElement("note");
533 xmlSerializer.SetAttributes("importance", "high");
534 xmlSerializer.EndElement();
535 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note importance=\"high\"/>");
536 }
537
538 /* @tc.name: EndElementTest003
539 * @tc.desc: Test whether write end tag of the element successfully.
540 * @tc.type: FUNC
541 */
542 HWTEST_F(NativeEngineTest, EndElementTest003, testing::ext::TestSize.Level0)
543 {
544 napi_env env = (napi_env)engine_;
545 napi_value arrayBuffer = nullptr;
546 void* pBuffer = nullptr;
547 size_t size = 1024;
548 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
549 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
550 xmlSerializer.StartElement("note");
551 xmlSerializer.SetAttributes("impo", "hi");
552 xmlSerializer.EndElement();
553 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note impo=\"hi\"/>");
554 }
555
556 /* @tc.name: EndElementTest004
557 * @tc.desc: Test whether write end tag of the element successfully.
558 * @tc.type: FUNC
559 */
560 HWTEST_F(NativeEngineTest, EndElementTest004, testing::ext::TestSize.Level0)
561 {
562 napi_env env = (napi_env)engine_;
563 napi_value arrayBuffer = nullptr;
564 void* pBuffer = nullptr;
565 size_t size = 1024;
566 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
567 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
568 xmlSerializer.StartElement("note1");
569 xmlSerializer.StartElement("note2");
570 xmlSerializer.EndElement();
571 xmlSerializer.EndElement();
572 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note1>\r\n <note2/>\r\n</note1>");
573 }
574
575 /* @tc.name: EndElementTest005
576 * @tc.desc: Test whether write end tag of the element successfully.
577 * @tc.type: FUNC
578 */
579 HWTEST_F(NativeEngineTest, EndElementTest005, testing::ext::TestSize.Level0)
580 {
581 napi_env env = (napi_env)engine_;
582 napi_value arrayBuffer = nullptr;
583 void* pBuffer = nullptr;
584 size_t size = 1024;
585 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
586 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
587 xmlSerializer.StartElement("note2");
588 xmlSerializer.SetAttributes("importance", "high");
589 xmlSerializer.EndElement();
590 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note2 importance=\"high\"/>");
591 }
592
593 /* @tc.name: SetNamespaceTest001
594 * @tc.desc: Test whether write the namespace of the current element tag successfully.
595 * @tc.type: FUNC
596 */
597 HWTEST_F(NativeEngineTest, SetNamespaceTest001, testing::ext::TestSize.Level0)
598 {
599 napi_env env = (napi_env)engine_;
600 napi_value arrayBuffer = nullptr;
601 void* pBuffer = nullptr;
602 size_t size = 1024;
603 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
604 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
605 xmlSerializer.SetDeclaration();
606 xmlSerializer.SetNamespace("h", "http://www.w3.org/TR/html4/");
607 xmlSerializer.StartElement("note");
608 xmlSerializer.EndElement();
609 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
610 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<h:note xmlns:h=\"http://www.w3.org/TR/html4/\"/>");
611 }
612
613 /* @tc.name: SetNamespaceTest002
614 * @tc.desc: Test whether write the namespace of the current element tag successfully.
615 * @tc.type: FUNC
616 */
617 HWTEST_F(NativeEngineTest, SetNamespaceTest002, testing::ext::TestSize.Level0)
618 {
619 napi_env env = (napi_env)engine_;
620 napi_value arrayBuffer = nullptr;
621 void* pBuffer = nullptr;
622 size_t size = 1024;
623 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
624 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
625 xmlSerializer.SetDeclaration();
626 xmlSerializer.SetNamespace("b", "http://www.w3.org/TR/html4/");
627 xmlSerializer.StartElement("note");
628 xmlSerializer.EndElement();
629 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
630 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<b:note xmlns:b=\"http://www.w3.org/TR/html4/\"/>");
631 }
632
633 /* @tc.name: SetNamespaceTest003
634 * @tc.desc: Test whether write the namespace of the current element tag successfully.
635 * @tc.type: FUNC
636 */
637 HWTEST_F(NativeEngineTest, SetNamespaceTest003, testing::ext::TestSize.Level0)
638 {
639 napi_env env = (napi_env)engine_;
640 napi_value arrayBuffer = nullptr;
641 void* pBuffer = nullptr;
642 size_t size = 1024;
643 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
644 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
645 xmlSerializer.SetDeclaration();
646 xmlSerializer.SetNamespace("h", "http://www.111/");
647 xmlSerializer.StartElement("note");
648 xmlSerializer.EndElement();
649 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
650 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<h:note xmlns:h=\"http://www.111/\"/>");
651 }
652
653 /* @tc.name: SetNamespaceTest004
654 * @tc.desc: Test whether write the namespace of the current element tag successfully.
655 * @tc.type: FUNC
656 */
657 HWTEST_F(NativeEngineTest, SetNamespaceTest004, testing::ext::TestSize.Level0)
658 {
659 napi_env env = (napi_env)engine_;
660 napi_value arrayBuffer = nullptr;
661 void* pBuffer = nullptr;
662 size_t size = 1024;
663 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
664 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
665 xmlSerializer.SetDeclaration();
666 xmlSerializer.SetNamespace("h", "http://www.w3.org/TR/html4/");
667 xmlSerializer.StartElement("note1");
668 xmlSerializer.StartElement("note2");
669 xmlSerializer.EndElement();
670 xmlSerializer.EndElement();
671 std::string strPrior = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
672 std::string strBack = "<h:note1 xmlns:h=\"http://www.w3.org/TR/html4/\">\r\n <h:note2/>\r\n</h:note1>";
673 std::string strEnd = strPrior + strBack;
674 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), strEnd.c_str());
675 }
676
677 /* @tc.name: SetNamespaceTest005
678 * @tc.desc: Test whether write the namespace of the current element tag successfully.
679 * @tc.type: FUNC
680 */
681 HWTEST_F(NativeEngineTest, SetNamespaceTest005, testing::ext::TestSize.Level0)
682 {
683 napi_env env = (napi_env)engine_;
684 napi_value arrayBuffer = nullptr;
685 void* pBuffer = nullptr;
686 size_t size = 1024;
687 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
688 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
689 xmlSerializer.SetDeclaration();
690 xmlSerializer.SetNamespace("h", "http://www.w3.org/TR/html4/");
691 xmlSerializer.StartElement("note1");
692 xmlSerializer.StartElement("note2");
693 xmlSerializer.EndElement();
694 xmlSerializer.EndElement();
695 std::string strPrior = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
696 std::string strBack = "<h:note1 xmlns:h=\"http://www.w3.org/TR/html4/\">\r\n <h:note2/>\r\n</h:note1>";
697 std::string strEnd = strPrior + strBack;
698 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), strEnd.c_str());
699 }
700
701 /* @tc.name: SetCommentTest001
702 * @tc.desc: Test write the comment successfully.
703 * @tc.type: FUNC
704 */
705 HWTEST_F(NativeEngineTest, SetCommentTest001, testing::ext::TestSize.Level0)
706 {
707 napi_env env = (napi_env)engine_;
708 napi_value arrayBuffer = nullptr;
709 void* pBuffer = nullptr;
710 size_t size = 1024;
711 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
712 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
713 xmlSerializer.StartElement("note");
714 xmlSerializer.SetComment("Hi!");
715 xmlSerializer.EndElement();
716 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note>\r\n <!--Hi!-->\r\n</note>");
717 }
718
719 /* @tc.name: SetCommentTest002
720 * @tc.desc: Test write the comment successfully.
721 * @tc.type: FUNC
722 */
723 HWTEST_F(NativeEngineTest, SetCommentTest002, testing::ext::TestSize.Level0)
724 {
725 napi_env env = (napi_env)engine_;
726 napi_value arrayBuffer = nullptr;
727 void* pBuffer = nullptr;
728 size_t size = 1024;
729 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
730 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
731 xmlSerializer.StartElement("note");
732 xmlSerializer.SetComment("Hello, World!");
733 xmlSerializer.EndElement();
734 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note>\r\n <!--Hello, World!-->\r\n</note>");
735 }
736
737 /* @tc.name: SetCommentTest003
738 * @tc.desc: Test write the comment successfully.
739 * @tc.type: FUNC
740 */
741 HWTEST_F(NativeEngineTest, SetCommentTest003, testing::ext::TestSize.Level0)
742 {
743 napi_env env = (napi_env)engine_;
744 napi_value arrayBuffer = nullptr;
745 void* pBuffer = nullptr;
746 size_t size = 1024;
747 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
748 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
749 xmlSerializer.SetComment("Hello, World!");
750 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<!--Hello, World!-->");
751 }
752
753 /* @tc.name: SetCommentTest004
754 * @tc.desc: Test write the comment successfully.
755 * @tc.type: FUNC
756 */
757 HWTEST_F(NativeEngineTest, SetCommentTest004, testing::ext::TestSize.Level0)
758 {
759 napi_env env = (napi_env)engine_;
760 napi_value arrayBuffer = nullptr;
761 void* pBuffer = nullptr;
762 size_t size = 1024;
763 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
764 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
765 xmlSerializer.SetComment("Hello, World!");
766 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<!--Hello, World!-->");
767 }
768
769 /* @tc.name: SetCommentTest005
770 * @tc.desc: Test write the comment successfully.
771 * @tc.type: FUNC
772 */
773 HWTEST_F(NativeEngineTest, SetCommentTest005, testing::ext::TestSize.Level0)
774 {
775 napi_env env = (napi_env)engine_;
776 napi_value arrayBuffer = nullptr;
777 void* pBuffer = nullptr;
778 size_t size = 1024;
779 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
780 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
781 xmlSerializer.SetComment("Hello, World!");
782 xmlSerializer.StartElement("note");
783 xmlSerializer.EndElement();
784 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<!--Hello, World!-->\r\n<note/>");
785 }
786
787 /* @tc.name: Test001
788 * @tc.desc: Test .
789 * @tc.type: FUNC
790 */
791 HWTEST_F(NativeEngineTest, SetCDATATest001, testing::ext::TestSize.Level0)
792 {
793 napi_env env = (napi_env)engine_;
794 napi_value arrayBuffer = nullptr;
795 void* pBuffer = nullptr;
796 size_t size = 1024;
797 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
798 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
799 xmlSerializer.SetCData("root SYSTEM");
800 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<![CDATA[root SYSTEM]]>");
801 }
802
803 /* @tc.name: SetCDATATest002
804 * @tc.desc: Test whether Writes the CDATA successfully.
805 * @tc.type: FUNC
806 */
807 HWTEST_F(NativeEngineTest, SetCDATATest002, testing::ext::TestSize.Level0)
808 {
809 napi_env env = (napi_env)engine_;
810 napi_value arrayBuffer = nullptr;
811 void* pBuffer = nullptr;
812 size_t size = 1024;
813 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
814 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
815 xmlSerializer.SetCData("root SYSTEM \"http://www.test.org/test.dtd\"");
816 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<![CDATA[root SYSTEM \"http://www.test.org/test.dtd\"]]>");
817 }
818
819 /* @tc.name: SetCDATATest003
820 * @tc.desc: Test whether Writes the CDATA successfully.
821 * @tc.type: FUNC
822 */
823 HWTEST_F(NativeEngineTest, SetCDATATest003, testing::ext::TestSize.Level0)
824 {
825 napi_env env = (napi_env)engine_;
826 napi_value arrayBuffer = nullptr;
827 void* pBuffer = nullptr;
828 size_t size = 1024;
829 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
830 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
831 xmlSerializer.StartElement("note");
832 xmlSerializer.EndElement();
833 xmlSerializer.SetCData("root SYSTEM \"http://www.test.org/test.dtd\"");
834 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
835 "<note/>\r\n<![CDATA[root SYSTEM \"http://www.test.org/test.dtd\"]]>");
836 }
837
838 /* @tc.name: SetCDATATest004
839 * @tc.desc: Test whether Writes the CDATA successfully.
840 * @tc.type: FUNC
841 */
842 HWTEST_F(NativeEngineTest, SetCDATATest004, testing::ext::TestSize.Level0)
843 {
844 napi_env env = (napi_env)engine_;
845 napi_value arrayBuffer = nullptr;
846 void* pBuffer = nullptr;
847 size_t size = 1024;
848 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
849 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
850 xmlSerializer.SetCData("root SYSTEM \"http://www.test.org/test.dtd\"");
851 xmlSerializer.StartElement("note");
852 xmlSerializer.EndElement();
853 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
854 "<![CDATA[root SYSTEM \"http://www.test.org/test.dtd\"]]>\r\n<note/>");
855 }
856
857 /* @tc.name: SetCDATATest005
858 * @tc.desc: Test whether Writes the CDATA successfully.
859 * @tc.type: FUNC
860 */
861 HWTEST_F(NativeEngineTest, SetCDATATest005, testing::ext::TestSize.Level0)
862 {
863 napi_env env = (napi_env)engine_;
864 napi_value arrayBuffer = nullptr;
865 void* pBuffer = nullptr;
866 size_t size = 1024;
867 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
868 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
869 xmlSerializer.StartElement("note");
870 xmlSerializer.SetCData("root SYSTEM \"http://www.test.org/test.dtd\"");
871 xmlSerializer.EndElement();
872 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
873 "<note>\r\n <![CDATA[root SYSTEM \"http://www.test.org/test.dtd\"]]>\r\n</note>");
874 }
875
876 /* @tc.name: SetTextTest001
877 * @tc.desc: Test whether Writes the text successfully.
878 * @tc.type: FUNC
879 */
880 HWTEST_F(NativeEngineTest, SetTextTest001, testing::ext::TestSize.Level0)
881 {
882 napi_env env = (napi_env)engine_;
883 napi_value arrayBuffer = nullptr;
884 void* pBuffer = nullptr;
885 size_t size = 1024;
886 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
887 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
888 xmlSerializer.StartElement("note");
889 xmlSerializer.SetAttributes("importance", "high");
890 xmlSerializer.SetText("Happy1");
891 xmlSerializer.EndElement();
892 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note importance=\"high\">Happy1</note>");
893 }
894
895 /* @tc.name: SetTextTest002
896 * @tc.desc: Test whether Writes the text successfully.
897 * @tc.type: FUNC
898 */
899 HWTEST_F(NativeEngineTest, SetTextTest002, testing::ext::TestSize.Level0)
900 {
901 napi_env env = (napi_env)engine_;
902 napi_value arrayBuffer = nullptr;
903 void* pBuffer = nullptr;
904 size_t size = 1024;
905 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
906 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
907 xmlSerializer.StartElement("note");
908 xmlSerializer.SetAttributes("importance", "high");
909 xmlSerializer.SetText("Happy2");
910 xmlSerializer.EndElement();
911 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note importance=\"high\">Happy2</note>");
912 }
913
914 /* @tc.name: SetTextTest003
915 * @tc.desc: Test whether Writes the text successfully.
916 * @tc.type: FUNC
917 */
918 HWTEST_F(NativeEngineTest, SetTextTest003, testing::ext::TestSize.Level0)
919 {
920 napi_env env = (napi_env)engine_;
921 napi_value arrayBuffer = nullptr;
922 void* pBuffer = nullptr;
923 size_t size = 1024;
924 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
925 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
926 xmlSerializer.StartElement("note");
927 xmlSerializer.SetAttributes("importance", "high");
928 xmlSerializer.SetText("Happy3");
929 xmlSerializer.EndElement();
930 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note importance=\"high\">Happy3</note>");
931 }
932
933 /* @tc.name: SetTextTest004
934 * @tc.desc: Test whether Writes the text successfully.
935 * @tc.type: FUNC
936 */
937 HWTEST_F(NativeEngineTest, SetTextTest004, testing::ext::TestSize.Level0)
938 {
939 napi_env env = (napi_env)engine_;
940 napi_value arrayBuffer = nullptr;
941 void* pBuffer = nullptr;
942 size_t size = 1024;
943 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
944 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
945 xmlSerializer.StartElement("note");
946 xmlSerializer.SetAttributes("importance", "high");
947 xmlSerializer.SetText("Happy4");
948 xmlSerializer.EndElement();
949 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note importance=\"high\">Happy4</note>");
950 }
951
952 /* @tc.name: SetTextTest005
953 * @tc.desc: Test whether Writes the text successfully.
954 * @tc.type: FUNC
955 */
956 HWTEST_F(NativeEngineTest, SetTextTest005, testing::ext::TestSize.Level0)
957 {
958 napi_env env = (napi_env)engine_;
959 napi_value arrayBuffer = nullptr;
960 void* pBuffer = nullptr;
961 size_t size = 1024;
962 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
963 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
964 xmlSerializer.StartElement("note");
965 xmlSerializer.SetAttributes("importance", "high");
966 xmlSerializer.SetText("Happy5");
967 xmlSerializer.EndElement();
968 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<note importance=\"high\">Happy5</note>");
969 }
970 /* @tc.name: SetDocTypeTest001
971 * @tc.desc: Test whether rites the DOCTYPE successfully.
972 * @tc.type: FUNC
973 */
974 HWTEST_F(NativeEngineTest, SetDocTypeTest001, testing::ext::TestSize.Level0)
975 {
976 napi_env env = (napi_env)engine_;
977 napi_value arrayBuffer = nullptr;
978 void* pBuffer = nullptr;
979 size_t size = 1024;
980 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
981 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
982 xmlSerializer.SetDocType("root SYSTEM");
983 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<!DOCTYPE root SYSTEM>");
984 }
985 /* @tc.name: SetDocTypeTest002
986 * @tc.desc: Test whether rites the DOCTYPE successfully.
987 * @tc.type: FUNC
988 */
989 HWTEST_F(NativeEngineTest, SetDocTypeTest002, testing::ext::TestSize.Level0)
990 {
991 napi_env env = (napi_env)engine_;
992 napi_value arrayBuffer = nullptr;
993 void* pBuffer = nullptr;
994 size_t size = 1024;
995 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
996 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
997 xmlSerializer.SetDocType("root SYSTEM \"http://www.test.org/test.dtd\"");
998 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer), "<!DOCTYPE root SYSTEM \"http://www.test.org/test.dtd\">");
999 }
1000
1001 /* @tc.name: SetDocTypeTest003
1002 * @tc.desc: Test whether rites the DOCTYPE successfully.
1003 * @tc.type: FUNC
1004 */
1005 HWTEST_F(NativeEngineTest, SetDocTypeTest003, testing::ext::TestSize.Level0)
1006 {
1007 napi_env env = (napi_env)engine_;
1008 napi_value arrayBuffer = nullptr;
1009 void* pBuffer = nullptr;
1010 size_t size = 1024;
1011 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
1012 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
1013 xmlSerializer.StartElement("note");
1014 xmlSerializer.EndElement();
1015 xmlSerializer.SetDocType("root SYSTEM \"http://www.test.org/test.dtd\"");
1016 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
1017 "<note/>\r\n<!DOCTYPE root SYSTEM \"http://www.test.org/test.dtd\">");
1018 }
1019
1020 /* @tc.name: SetDocTypeTest004
1021 * @tc.desc: Test whether rites the DOCTYPE successfully.
1022 * @tc.type: FUNC
1023 */
1024 HWTEST_F(NativeEngineTest, SetDocTypeTest004, testing::ext::TestSize.Level0)
1025 {
1026 napi_env env = (napi_env)engine_;
1027 napi_value arrayBuffer = nullptr;
1028 void* pBuffer = nullptr;
1029 size_t size = 1024;
1030 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
1031 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
1032 xmlSerializer.SetDocType("root SYSTEM \"http://www.test.org/test.dtd\"");
1033 xmlSerializer.StartElement("note");
1034 xmlSerializer.EndElement();
1035 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
1036 "<!DOCTYPE root SYSTEM \"http://www.test.org/test.dtd\">\r\n<note/>");
1037 }
1038
1039 /* @tc.name: SetDocTypeTest005
1040 * @tc.desc: Test whether rites the DOCTYPE successfully.
1041 * @tc.type: FUNC
1042 */
1043 HWTEST_F(NativeEngineTest, SetDocTypeTest005, testing::ext::TestSize.Level0)
1044 {
1045 napi_env env = (napi_env)engine_;
1046 napi_value arrayBuffer = nullptr;
1047 void* pBuffer = nullptr;
1048 size_t size = 1024;
1049 napi_create_arraybuffer(env, size, &pBuffer, &arrayBuffer);
1050 OHOS::xml::XmlSerializer xmlSerializer(reinterpret_cast<char*>(pBuffer), size, "utf-8");
1051 xmlSerializer.StartElement("note");
1052 xmlSerializer.SetDocType("root SYSTEM \"http://www.test.org/test.dtd\"");
1053 xmlSerializer.EndElement();
1054 ASSERT_STREQ(reinterpret_cast<char*>(pBuffer),
1055 "<note>\r\n <!DOCTYPE root SYSTEM \"http://www.test.org/test.dtd\">\r\n</note>");
1056 }
1057
1058 /* @tc.name: XmlParseTest001
1059 * @tc.desc: To XML text to JavaScript object.
1060 * @tc.type: FUNC
1061 */
1062 HWTEST_F(NativeEngineTest, XmlParseTest001, testing::ext::TestSize.Level0)
1063 {
1064 napi_env env = (napi_env)engine_;
1065 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE note [\n<!ENTITY foo \"baa\">]>";
1066 std::string str2 = "<note importance=\"high\" logged=\"true\">";
1067 std::string str3 = "<![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>";
1068 std::string str4 = "<!--Hello, World!--> <company>John & Hans</company> <title>Happy</title>";
1069 std::string str5 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo> <?go there?>";
1070 std::string str6 = "<a><b/></a> <h:table xmlns:h=\"http://www.w3.org/TR/html4/\"> <h:tr>";
1071 std::string str7 = "<h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr>";
1072 std::string str8 = "</h:table></note>";
1073 std::string strXml = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8;
1074 testStr = "";
1075 OHOS::xml::XmlPullParser xmlPullParser(strXml, "utf-8");
1076 napi_value options = nullptr;
1077 napi_create_object(env, &options);
1078 const char* key1 = "supportDoctype";
1079 const char* key2 = "ignoreNameSpace";
1080 const char* key3 = "tagValueCallbackFunction";
1081 napi_value object = nullptr;
1082 napi_create_object(env, &object);
1083 napi_value value1 = nullptr;
1084 napi_value value2 = nullptr;
1085 napi_get_boolean(env, true, &value1);
1086 napi_get_boolean(env, false, &value2);
1087 napi_value value3 = nullptr;
1088 std::string cbName = "Method";
1089 napi_create_function(env, cbName.c_str(), cbName.size(), Method, nullptr, &value3);
1090 napi_set_named_property(env, object, key1, value1);
1091 napi_set_named_property(env, object, key2, value2);
1092 napi_set_named_property(env, object, key3, value3);
1093 xmlPullParser.DealOptionInfo(env, object);
1094 xmlPullParser.Parse(env, options);
1095 std::string res1 = " note [\n<!ENTITY foo \"baa\">]note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n";
1096 std::string res2 = "Hello, World! companyJohn amp;amp; Hanscompany titleHappytitletitleHappytitle";
1097 std::string res3 = " todoWorktodo todoPlaytodo go thereabba table trtdApplestd tdBananastd trtablenote";
1098 std::string result = res1 + res2 + res3;
1099 ASSERT_STREQ(testStr.c_str(), result.c_str());
1100 }
1101
1102 /* @tc.name: XmlParseTest002
1103 * @tc.desc: To XML text to JavaScript object.
1104 * @tc.type: FUNC
1105 */
1106 HWTEST_F(NativeEngineTest, XmlParseTest002, testing::ext::TestSize.Level0)
1107 {
1108 napi_env env = (napi_env)engine_;
1109 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE note [\n<!ENTITY foo \"baa\">]>";
1110 std::string str2 = "<note importance=\"high\" logged=\"true\">";
1111 std::string str3 = " <![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>";
1112 std::string str4 = " <!--Hello, World!--> <company>John & Hans</company> <title>Happy</title>";
1113 std::string str5 = " <title>Happy</title> <todo>Work</todo> <todo>Play</todo> <?go there?>";
1114 std::string str6 = " <a><b/></a> <h:table xmlns:h=\"http://www.w3.org/TR/html4/\"> <h:tr>";
1115 std::string str7 = " <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr>";
1116 std::string str8 = " </h:table></note>";
1117 std::string strXml = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8;
1118 testStr = "";
1119 OHOS::xml::XmlPullParser xmlPullParser(strXml, "utf-8");
1120 napi_value options = nullptr;
1121 napi_create_object(env, &options);
1122 const char* key1 = "supportDoctype";
1123 const char* key2 = "ignoreNameSpace";
1124 const char* key3 = "attributeValueCallbackFunction";
1125 napi_value object = nullptr;
1126 napi_create_object(env, &object);
1127 napi_value value1 = nullptr;
1128 napi_value value2 = nullptr;
1129 napi_get_boolean(env, false, &value1);
1130 napi_get_boolean(env, true, &value2);
1131 napi_value value3 = nullptr;
1132 std::string cbName = "Method";
1133 napi_create_function(env, cbName.c_str(), cbName.size(), Method, nullptr, &value3);
1134 napi_set_named_property(env, object, key1, value1);
1135 napi_set_named_property(env, object, key2, value2);
1136 napi_set_named_property(env, object, key3, value3);
1137 xmlPullParser.DealOptionInfo(env, object);
1138 xmlPullParser.Parse(env, options);
1139 ASSERT_STREQ(testStr.c_str(), "importancehighloggedtruexmlns:hhttp://www.w3.org/TR/html4/");
1140 }
1141
1142 /* @tc.name: XmlParseTest003
1143 * @tc.desc: To XML text to JavaScript object.
1144 * @tc.type: FUNC
1145 */
1146 HWTEST_F(NativeEngineTest, XmlParseTest003, testing::ext::TestSize.Level0)
1147 {
1148 napi_env env = (napi_env)engine_;
1149 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE note [\n<!ENTITY foo \"baa\">]>";
1150 std::string str2 = "<note importance=\"high\" logged=\"true\">";
1151 std::string str3 = "<![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>";
1152 std::string str4 = "<!--Hello, World!--> <company>John & Hans</company> <title>Happy</title>";
1153 std::string str5 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo> <?go there?>";
1154 std::string str6 = "<a><b/></a> <h:table xmlns:h=\"http://www.w3.org/TR/html4/\"> <h:tr>";
1155 std::string str7 = "<h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr>";
1156 std::string str8 = "</h:table></note>";
1157 std::string strXml = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8;
1158 testStr = "";
1159 OHOS::xml::XmlPullParser xmlPullParser(strXml, "utf-8");
1160 napi_value options = nullptr;
1161 napi_create_object(env, &options);
1162 const char* key1 = "supportDoctype";
1163 const char* key2 = "ignoreNameSpace";
1164 const char* key3 = "tagValueCallbackFunction";
1165 napi_value object = nullptr;
1166 napi_create_object(env, &object);
1167 napi_value value1 = nullptr;
1168 napi_value value2 = nullptr;
1169 napi_get_boolean(env, false, &value1);
1170 napi_get_boolean(env, true, &value2);
1171 napi_value value3 = nullptr;
1172 std::string cbName = "Method";
1173 napi_create_function(env, cbName.c_str(), cbName.size(), Method, nullptr, &value3);
1174 napi_set_named_property(env, object, key1, value1);
1175 napi_set_named_property(env, object, key2, value2);
1176 napi_set_named_property(env, object, key3, value3);
1177 xmlPullParser.DealOptionInfo(env, object);
1178 xmlPullParser.Parse(env, options);
1179 std::string res1 = "note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\nHello, World! companyJohn amp;amp;";
1180 std::string res2 = " Hanscompany titleHappytitletitleHappytitle todoWorktodo todoPlaytodo go thereabba h:table";
1181 std::string res3 = " h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
1182 std::string result = res1 + res2 + res3;
1183 ASSERT_STREQ(testStr.c_str(), result.c_str());
1184 }
1185
1186 /* @tc.name: XmlParseTest004
1187 * @tc.desc: To XML text to JavaScript object.
1188 * @tc.type: FUNC
1189 */
1190 HWTEST_F(NativeEngineTest, XmlParseTest004, testing::ext::TestSize.Level0)
1191 {
1192 napi_env env = (napi_env)engine_;
1193 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE note [\n<!ENTITY foo \"baa\">]>";
1194 std::string str2 = "<note importance=\"high\" logged=\"true\">";
1195 std::string str3 = " <![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>";
1196 std::string str4 = " <!--Hello, World!--> <company>John & Hans</company> <title>Happy</title>";
1197 std::string str5 = " <title>Happy</title> <todo>Work</todo> <todo>Play</todo> <?go there?>";
1198 std::string str6 = " <a><b/></a> <h:table xmlns:h=\"http://www.w3.org/TR/html4/\"> <h:tr>";
1199 std::string str7 = " <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr>";
1200 std::string str8 = " </h:table></note>";
1201 std::string strXml = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8;
1202 testStr = "";
1203 OHOS::xml::XmlPullParser xmlPullParser(strXml, "utf-8");
1204 napi_value options = nullptr;
1205 napi_create_object(env, &options);
1206 const char* key1 = "supportDoctype";
1207 const char* key2 = "ignoreNameSpace";
1208 const char* key3 = "attributeValueCallbackFunction";
1209 napi_value object = nullptr;
1210 napi_create_object(env, &object);
1211 napi_value value1 = nullptr;
1212 napi_value value2 = nullptr;
1213 napi_get_boolean(env, true, &value1);
1214 napi_get_boolean(env, true, &value2);
1215 napi_value value3 = nullptr;
1216 std::string cbName = "Method";
1217 napi_create_function(env, cbName.c_str(), cbName.size(), Method, nullptr, &value3);
1218 napi_set_named_property(env, object, key1, value1);
1219 napi_set_named_property(env, object, key2, value2);
1220 napi_set_named_property(env, object, key3, value3);
1221 xmlPullParser.DealOptionInfo(env, object);
1222 xmlPullParser.Parse(env, options);
1223 ASSERT_STREQ(testStr.c_str(), "importancehighloggedtruexmlns:hhttp://www.w3.org/TR/html4/");
1224 }
1225
1226 /* @tc.name: XmlParseTest005
1227 * @tc.desc: To XML text to JavaScript object.
1228 * @tc.type: FUNC
1229 */
1230 HWTEST_F(NativeEngineTest, XmlParseTest005, testing::ext::TestSize.Level0)
1231 {
1232 napi_env env = (napi_env)engine_;
1233 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE note [\n<!ENTITY foo \"baa\">]>";
1234 std::string str2 = "<note importance=\"high\" logged=\"true\">";
1235 std::string str3 = "<![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>";
1236 std::string str4 = "<!--Hello, World!--> <company>John & Hans</company> <title>Happy</title>";
1237 std::string str5 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo> <?go there?>";
1238 std::string str6 = "<a><b/></a> <h:table xmlns:h=\"http://www.w3.org/TR/html4/\"> <h:tr>";
1239 std::string str7 = "<h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr>";
1240 std::string str8 = "</h:table></note>";
1241 std::string strXml = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8;
1242 testStr = "";
1243 OHOS::xml::XmlPullParser xmlPullParser(strXml, "utf-8");
1244 napi_value options = nullptr;
1245 napi_create_object(env, &options);
1246 const char* key1 = "supportDoctype";
1247 const char* key2 = "ignoreNameSpace";
1248 const char* key3 = "tagValueCallbackFunction";
1249 napi_value object = nullptr;
1250 napi_create_object(env, &object);
1251 napi_value value1 = nullptr;
1252 napi_value value2 = nullptr;
1253 napi_get_boolean(env, true, &value1);
1254 napi_get_boolean(env, true, &value2);
1255 napi_value value3 = nullptr;
1256 std::string cbName = "Method";
1257 napi_create_function(env, cbName.c_str(), cbName.size(), Method, nullptr, &value3);
1258 napi_set_named_property(env, object, key1, value1);
1259 napi_set_named_property(env, object, key2, value2);
1260 napi_set_named_property(env, object, key3, value3);
1261 xmlPullParser.DealOptionInfo(env, object);
1262 xmlPullParser.Parse(env, options);
1263 std::string res1 = " note [\n<!ENTITY foo \"baa\">]note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n";
1264 std::string res2 = "Hello, World! companyJohn amp;amp; Hanscompany titleHappytitletitleHappytitle todoWorktodo";
1265 std::string res3 = " todoPlaytodo go thereabba h:table h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
1266 std::string result = res1 + res2 + res3;
1267 ASSERT_STREQ(testStr.c_str(), result.c_str());
1268 }
1269
1270 /* @tc.name: XmlParseTest006
1271 * @tc.desc: To XML text to JavaScript object.
1272 * @tc.type: FUNC
1273 */
1274 HWTEST_F(NativeEngineTest, XmlParseTest006, testing::ext::TestSize.Level0)
1275 {
1276 napi_env env = (napi_env)engine_;
1277 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE note [\n<!ENTITY foo \"baa\">]>";
1278 std::string str2 = "<note importance=\"high\" logged=\"true\">";
1279 std::string str3 = "<![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>";
1280 std::string str4 = "<!--Hello, World!--> <company>John & Hans</company> <title>Happy</title>";
1281 std::string str5 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo> <?go there?>";
1282 std::string str6 = "<a><b/></a> <h:table xmlns:h=\"http://www.w3.org/TR/html4/\"> <h:tr>";
1283 std::string str7 = "<h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr>";
1284 std::string str8 = "</h:table></note>";
1285 std::string strXml = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8;
1286 OHOS::xml::XmlPullParser xmlPullParser(strXml, "utf-8");
1287 napi_value options = nullptr;
1288 napi_create_object(env, &options);
1289 const char* key1 = "supportDoctype";
1290 const char* key2 = "ignoreNameSpace";
1291 const char* key3 = "tokenValueCallbackFunction";
1292 napi_value object = nullptr;
1293 napi_create_object(env, &object);
1294 napi_value value1 = nullptr;
1295 napi_value value2 = nullptr;
1296 napi_get_boolean(env, true, &value1);
1297 napi_get_boolean(env, true, &value2);
1298 napi_value value3 = nullptr;
1299 std::string cbName = "TokenValueCallbackFunction";
1300 napi_create_function(env, cbName.c_str(), cbName.size(), TokenValueCallbackFunction, nullptr, &value3);
1301 napi_set_named_property(env, object, key1, value1);
1302 napi_set_named_property(env, object, key2, value2);
1303 napi_set_named_property(env, object, key3, value3);
1304 xmlPullParser.DealOptionInfo(env, object);
1305 xmlPullParser.Parse(env, options);
1306 std::string res1 = " note [\n<!ENTITY foo \"baa\">]note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n";
1307 std::string res2 = "Hello, World! companyJohn amp;amp; Hanscompany titleHappytitletitleHappytitle todoWorktodo";
1308 std::string res3 = " todoPlaytodo go thereabba h:table h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
1309 std::string result = res1 + res2 + res3;
1310 ASSERT_STREQ(testStr.c_str(), result.c_str());
1311 }
1312
1313 /* @tc.name: XmlParseTest007
1314 * @tc.desc: To XML text to JavaScript object.
1315 * @tc.type: FUNC
1316 */
1317 HWTEST_F(NativeEngineTest, XmlParseTest007, testing::ext::TestSize.Level0)
1318 {
1319 napi_env env = (napi_env)engine_;
1320 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
1321 std::string str2 = "<note importance=\"high\" logged=\"true\">";
1322 std::string str3 = " <![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>";
1323 std::string str4 = " <!--Hello, World!--> <company>John & Hans</company> <title>Happy</title>";
1324 std::string str5 = " <title>Happy</title> <todo>Work</todo> <todo>Play</todo> <?go there?>";
1325 std::string str6 = " <a><b/></a> <h:table xmlns:h=\"http://www.w3.org/TR/html4/\"> <h:tr>";
1326 std::string str7 = " <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr>";
1327 std::string str8 = " </h:table></note>";
1328 std::string strXml = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8;
1329 testStr = "";
1330 OHOS::xml::XmlPullParser xmlPullParser(strXml, "utf-8");
1331 napi_value options = nullptr;
1332 napi_create_object(env, &options);
1333 const char* key1 = "supportDoctype";
1334 const char* key2 = "ignoreNameSpace";
1335 const char* key3 = "attributeValueCallbackFunction";
1336 napi_value object = nullptr;
1337 napi_create_object(env, &object);
1338 napi_value value1 = nullptr;
1339 napi_value value2 = nullptr;
1340 napi_get_boolean(env, true, &value1);
1341 napi_get_boolean(env, true, &value2);
1342 napi_value value3 = nullptr;
1343 std::string cbName = "Method";
1344 napi_create_function(env, cbName.c_str(), cbName.size(), Method, nullptr, &value3);
1345 napi_set_named_property(env, object, key1, value1);
1346 napi_set_named_property(env, object, key2, value2);
1347 napi_set_named_property(env, object, key3, value3);
1348 xmlPullParser.DealOptionInfo(env, object);
1349 xmlPullParser.Parse(env, options);
1350 ASSERT_STREQ(testStr.c_str(), "importancehighloggedtruexmlns:hhttp://www.w3.org/TR/html4/");
1351 }
1352
1353 /* @tc.name: XmlParseTest008
1354 * @tc.desc: To XML text to JavaScript object.
1355 * @tc.type: FUNC
1356 */
1357 HWTEST_F(NativeEngineTest, XmlParseTest008, testing::ext::TestSize.Level0)
1358 {
1359 napi_env env = (napi_env)engine_;
1360 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
1361 std::string str2 = "<note importance=\"high\" logged=\"true\">";
1362 std::string str3 = "<![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>";
1363 std::string str4 = "<!--Hello, World!--> <company>John & Hans</company> <title>Happy</title>";
1364 std::string str5 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo> <?go there?>";
1365 std::string str6 = "<a><b/></a> <h:table xmlns:h=\"http://www.w3.org/TR/html4/\"> <h:tr>";
1366 std::string str7 = "<h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr>";
1367 std::string str8 = "</h:table></note>";
1368 std::string strXml = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8;
1369 testStr = "";
1370 OHOS::xml::XmlPullParser xmlPullParser(strXml, "utf-8");
1371 napi_value options = nullptr;
1372 napi_create_object(env, &options);
1373 const char* key1 = "supportDoctype";
1374 const char* key2 = "ignoreNameSpace";
1375 const char* key3 = "tagValueCallbackFunction";
1376 napi_value object = nullptr;
1377 napi_create_object(env, &object);
1378 napi_value value1 = nullptr;
1379 napi_value value2 = nullptr;
1380 napi_get_boolean(env, true, &value1);
1381 napi_get_boolean(env, true, &value2);
1382 napi_value value3 = nullptr;
1383 std::string cbName = "Method";
1384 napi_create_function(env, cbName.c_str(), cbName.size(), Method, nullptr, &value3);
1385 napi_set_named_property(env, object, key1, value1);
1386 napi_set_named_property(env, object, key2, value2);
1387 napi_set_named_property(env, object, key3, value3);
1388 xmlPullParser.DealOptionInfo(env, object);
1389 xmlPullParser.Parse(env, options);
1390 std::string res1 = "note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\nHello, World! companyJohn amp;amp;";
1391 std::string res2 = " Hanscompany titleHappytitletitleHappytitle todoWorktodo todoPlaytodo go thereabba h:table ";
1392 std::string res3 = "h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
1393 std::string result = res1 + res2 + res3;
1394 ASSERT_STREQ(testStr.c_str(), result.c_str());
1395 }
1396
1397 /* @tc.name: XmlParseTest009
1398 * @tc.desc: To XML text to JavaScript object.
1399 * @tc.type: FUNC
1400 */
1401 HWTEST_F(NativeEngineTest, XmlParseTest009, testing::ext::TestSize.Level0)
1402 {
1403 napi_env env = (napi_env)engine_;
1404 std::string str1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
1405 std::string str2 = "<note importance=\"high\" logged=\"true\">";
1406 std::string str3 = "<![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>";
1407 std::string str4 = "<!--Hello, World!--> <company>John & Hans</company> <title>Happy</title>";
1408 std::string str5 = "<title>Happy</title> <todo>Work</todo> <todo>Play</todo> <?go there?>";
1409 std::string str6 = "<a><b/></a> <h:table xmlns:h=\"http://www.w3.org/TR/html4/\"> <h:tr>";
1410 std::string str7 = "<h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr>";
1411 std::string str8 = "</h:table></note>";
1412 std::string strXml = str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8;
1413 OHOS::xml::XmlPullParser xmlPullParser(strXml, "utf-8");
1414 napi_value options = nullptr;
1415 napi_create_object(env, &options);
1416 const char* key1 = "supportDoctype";
1417 const char* key2 = "ignoreNameSpace";
1418 const char* key3 = "tokenValueCallbackFunction";
1419 napi_value object = nullptr;
1420 napi_create_object(env, &object);
1421 napi_value value1 = nullptr;
1422 napi_value value2 = nullptr;
1423 napi_get_boolean(env, true, &value1);
1424 napi_get_boolean(env, true, &value2);
1425 napi_value value3 = nullptr;
1426 std::string cbName = "TokenValueCallbackFunc";
1427 napi_create_function(env, cbName.c_str(), cbName.size(), TokenValueCallbackFunc, nullptr, &value3);
1428 napi_set_named_property(env, object, key1, value1);
1429 napi_set_named_property(env, object, key2, value2);
1430 napi_set_named_property(env, object, key3, value3);
1431 xmlPullParser.DealOptionInfo(env, object);
1432 xmlPullParser.Parse(env, options);
1433 std::string res1 = "note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\nHello, World! companyJohn amp;amp;";
1434 std::string res2 = " Hanscompany titleHappytitletitleHappytitle todoWorktodo todoPlaytodo go thereabba h:table";
1435 std::string res3 = " h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
1436 std::string result = res1 + res2 + res3;
1437 ASSERT_STREQ(testStr.c_str(), result.c_str());
1438
1439 }
1440