1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.xml;
16
17 import static java.nio.charset.StandardCharsets.ISO_8859_1;
18 import static java.nio.charset.StandardCharsets.UTF_8;
19 import static org.htmlunit.javascript.host.xml.XMLDocumentTest.LOAD_XML_DOCUMENT_FROM_FILE_FUNCTION;
20 import static org.htmlunit.javascript.host.xml.XMLDocumentTest.LOAD_XML_DOCUMENT_FROM_STRING_FUNCTION;
21 import static org.htmlunit.javascript.host.xml.XMLDocumentTest.callLoadXMLDocumentFromFile;
22 import static org.htmlunit.javascript.host.xml.XMLDocumentTest.callLoadXMLDocumentFromString;
23
24 import java.util.Collections;
25
26 import org.htmlunit.WebDriverTestCase;
27 import org.htmlunit.junit.BrowserRunner;
28 import org.htmlunit.junit.annotation.Alerts;
29 import org.htmlunit.util.MimeType;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.openqa.selenium.WebDriver;
33
34
35
36
37
38
39
40
41
42 @RunWith(BrowserRunner.class)
43 public class XMLDocument3Test extends WebDriverTestCase {
44
45
46
47
48 @Test
49 @Alerts({"1610", "1575", "32", "1604", "1610", "1610", "1610", "1610", "1610", "1610", "1604"})
50 public void load_Encoding() throws Exception {
51 final String html = DOCTYPE_HTML
52 + "<html><head>\n"
53 + "<script>\n"
54 + LOG_TITLE_FUNCTION
55 + " function test() {\n"
56 + " var doc = " + callLoadXMLDocumentFromFile("'" + URL_SECOND + "'") + ";\n"
57 + " var value = doc.documentElement.firstChild.nodeValue;\n"
58 + " for (var i = 0; i < value.length; i++) {\n"
59 + " log(value.charCodeAt(i));\n"
60 + " }\n"
61 + " }\n"
62 + LOAD_XML_DOCUMENT_FROM_FILE_FUNCTION
63 + "</script></head>\n"
64 + "<body onload='test()'>\n"
65 + "</body></html>";
66
67 final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
68 + "<something>\u064A\u0627 \u0644\u064A\u064A\u064A\u064A\u064A\u064A\u0644</something>";
69
70 getMockWebConnection().setResponse(URL_SECOND, xml.getBytes("UTF-8"), 200, "OK",
71 MimeType.TEXT_XML, Collections.emptyList());
72
73 loadPageVerifyTitle2(html);
74 }
75
76
77
78
79 @Test
80 @Alerts({"230", "230"})
81 public void parseIso88591Encoding() throws Exception {
82 final String html = DOCTYPE_HTML
83 + "<html>\n"
84 + " <head>\n"
85 + "<script>\n"
86 + LOG_TITLE_FUNCTION
87 + " function test(encoding) {\n"
88 + " var text=\"<?xml version='1.0' encoding='\" + encoding + \"'?><body>\u00e6</body>\";\n"
89 + " var doc=" + callLoadXMLDocumentFromString("text") + ";\n"
90 + " var value = doc.documentElement.firstChild.nodeValue;\n"
91 + " for (var i = 0; i < value.length; i++) {\n"
92 + " log(value.charCodeAt(i));\n"
93 + " }\n"
94 + " }\n"
95 + LOAD_XML_DOCUMENT_FROM_STRING_FUNCTION
96 + "</script></head>\n"
97 + "<body onload='test(\"ISO-8859-1\");test(\"UTF8\");'>\n"
98 + "</body></html>";
99
100
101 final WebDriver driver = loadPage2(html, URL_FIRST, "text/html; charset=ISO-8859-1", ISO_8859_1);
102 verifyTitle2(driver, getExpectedAlerts());
103 }
104
105
106
107
108 @Test
109 @Alerts({"1044", "1044"})
110 public void parseUtf8Encoding() throws Exception {
111 final String html = DOCTYPE_HTML
112 + "<html>\n"
113 + " <head>\n"
114 + "<script>\n"
115 + LOG_TITLE_FUNCTION
116 + " function test(encoding) {\n"
117 + " var text=\"<?xml version='1.0' encoding='\" + encoding + \"'?><body>\u0414</body>\";\n"
118 + " var doc=" + callLoadXMLDocumentFromString("text") + ";\n"
119 + " var value = doc.documentElement.firstChild.nodeValue;\n"
120 + " for (var i = 0; i < value.length; i++) {\n"
121 + " log(value.charCodeAt(i));\n"
122 + " }\n"
123 + " }\n"
124 + LOAD_XML_DOCUMENT_FROM_STRING_FUNCTION
125 + "</script></head>\n"
126 + "<body onload='test(\"UTF-8\");test(\"ISO-8859-1\");'>\n"
127 + "</body></html>";
128
129
130 final WebDriver driver = loadPage2(html, URL_FIRST, "text/html; charset=UTF-8", UTF_8);
131 verifyTitle2(driver, getExpectedAlerts());
132 }
133 }