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.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link TextEncoder}.
25   *
26   * @author Ronald Brill
27   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder">TextEncoder() - Web APIs | MDN</a>
28   */
29  @RunWith(BrowserRunner.class)
30  public class TextEncoderTest extends WebDriverTestCase {
31  
32      /**
33       * @throws Exception on test failure
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       * @throws Exception on test failure
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       * @throws Exception on test failure
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 }