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