1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.BrowserRunner;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22
23
24
25
26
27
28
29 @RunWith(BrowserRunner.class)
30 public class TextEncoderTest extends WebDriverTestCase {
31
32
33
34
35 @Test
36 @Alerts("utf-8")
37 public void encoding() throws Exception {
38 final String html = DOCTYPE_HTML
39 + "<html>\n"
40 + "<head>\n"
41 + " <script>\n"
42 + LOG_TITLE_FUNCTION
43 + " function doTest() {\n"
44 + " if (typeof TextEncoder === 'undefined') {\n"
45 + " log('no TextEncoder');\n"
46 + " return;\n"
47 + " };\n"
48 + " var enc = new TextEncoder();\n"
49 + " log(enc.encoding);\n"
50 + " }\n"
51 + " </script>\n"
52 + "</head>\n"
53 + "<body onload='doTest()'>\n"
54 + "</body></html>";
55
56 loadPageVerifyTitle2(html);
57 }
58
59
60
61
62 @Test
63 @Alerts({"0", "8", "72", "116"})
64 public void encode() throws Exception {
65 final String html = DOCTYPE_HTML
66 + "<html>\n"
67 + "<head>\n"
68 + " <script>\n"
69 + LOG_TITLE_FUNCTION
70 + " function doTest() {\n"
71 + " if (typeof TextEncoder === 'undefined') {\n"
72 + " log('no TextEncoder');\n"
73 + " return;\n"
74 + " };\n"
75 + " var enc = new TextEncoder();\n"
76 + " var encoded = enc.encode('');\n"
77 + " log(encoded.length);\n"
78
79 + " encoded = enc.encode('HtmlUnit');\n"
80 + " log(encoded.length);\n"
81 + " log(encoded[0]);\n"
82 + " log(encoded[encoded.length - 1]);\n"
83 + " }\n"
84 + " </script>\n"
85 + "</head>\n"
86 + "<body onload='doTest()'>\n"
87 + "</body></html>";
88
89 loadPageVerifyTitle2(html);
90 }
91
92
93
94
95 @Test
96 @Alerts({"0", "0", "4"})
97 public void encode2() throws Exception {
98 final String html = DOCTYPE_HTML
99 + "<html>\n"
100 + "<head>\n"
101 + " <script>\n"
102 + LOG_TITLE_FUNCTION
103 + " function doTest() {\n"
104 + " if (typeof TextEncoder === 'undefined') {\n"
105 + " log('no TextEncoder');\n"
106 + " return;\n"
107 + " };\n"
108 + " var enc = new TextEncoder();\n"
109 + " var encoded = enc.encode();\n"
110 + " log(encoded.length);\n"
111
112 + " var encoded = enc.encode(undefined);\n"
113 + " log(encoded.length);\n"
114
115 + " var encoded = enc.encode(null);\n"
116 + " log(encoded.length);\n"
117 + " }\n"
118 + " </script>\n"
119 + "</head>\n"
120 + "<body onload='doTest()'>\n"
121 + "</body></html>";
122
123 loadPageVerifyTitle2(html);
124 }
125 }