View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://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  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   * Tests for {@link TextEncoder}.
23   *
24   * @author Ronald Brill
25   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder">TextEncoder() - Web APIs | MDN</a>
26   */
27  public class TextEncoderTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception on test failure
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       * @throws Exception on test failure
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       * @throws Exception on test failure
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 }