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;
16  
17  import static java.nio.charset.StandardCharsets.UTF_8;
18  
19  import org.htmlunit.WebDriverTestCase;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.Test;
22  import org.openqa.selenium.WebDriver;
23  
24  /**
25   * Test for functions/properties of the global object.
26   *
27   * @author Marc Guillemot
28   * @author Ronald Brill
29   * @author Frank Danek
30   */
31  public class GlobalFunctionsTest extends WebDriverTestCase {
32  
33      /**
34       * Test for bug <a href="http://sourceforge.net/support/tracker.php?aid=2815674">2815674</a>
35       * due to Rhino bug <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=501972">501972</a>
36       * and for bug <a href="http://sourceforge.net/support/tracker.php?aid=2903514">2903514</a>
37       * due to Rhino bug <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=531436">531436</a>.
38       * @throws Exception if the test fails
39       */
40      @Test
41      @Alerts({"7.89", "7.89"})
42      public void parseFloat() throws Exception {
43          final String html = DOCTYPE_HTML
44              +  "<html><head><script>\n"
45              + LOG_TITLE_FUNCTION
46              + "function doTest() {\n"
47              + "  log(parseFloat('\\n 7.89 '));\n"
48              + "  log(parseFloat('7.89em'));\n"
49              + "}\n"
50              + "</script></head><body onload='doTest()'>\n"
51              + "</body></html>";
52  
53          loadPageVerifyTitle2(html);
54      }
55  
56      /**
57       * Test for bug <a href="http://sourceforge.net/p/htmlunit/bugs/1563/">1563</a>.
58       *
59       * @throws Exception if the test fails
60       */
61      @Test
62      @Alerts({"0", "1", "-2345", "1", "12", "NaN", "0", "1", "8", "9", "100", "0", "1", "8", "9", "100",
63                  "100", "NaN", "NaN"})
64      public void parseInt() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html><head><script>\n"
67              + LOG_TITLE_FUNCTION
68              + "function doTest() {\n"
69              + "  log(parseInt('0'));\n"
70              + "  log(parseInt(' 1 '));\n"
71              + "  log(parseInt('-2345'));\n"
72              + "  log(parseInt('1.23'));\n"
73              + "  log(parseInt('12,3'));\n"
74              + "  log(parseInt('abc'));\n"
75  
76              + "  log(parseInt('0'));\n"
77              + "  log(parseInt(' 01 '));\n"
78              + "  log(parseInt('08'));\n"
79              + "  log(parseInt('09'));\n"
80              + "  log(parseInt('0100'));\n"
81  
82              + "  log(parseInt('0', 10));\n"
83              + "  log(parseInt(' 01 ', 10));\n"
84              + "  log(parseInt('08', 10));\n"
85              + "  log(parseInt('09', 10));\n"
86              + "  log(parseInt('0100', 10));\n"
87  
88              + "  log(parseInt('100', 0));\n"
89              + "  log(parseInt('100', -1));\n"
90              + "  log(parseInt('100', -7));\n"
91              + "}\n"
92              + "</script></head><body onload='doTest()'>\n"
93              + "</body></html>";
94  
95          loadPageVerifyTitle2(html);
96      }
97  
98      /**
99       * Test for the methods with the same expectations for all browsers.
100      * @throws Exception if the test fails
101      */
102     @Test
103     @Alerts({"decodeURI: function", "decodeURIComponent: function", "encodeURI: function",
104         "encodeURIComponent: function", "escape: function", "eval: function", "isFinite: function", "isNaN: function",
105         "parseFloat: function", "parseInt: function", "unescape: function"})
106     public void methods_common() throws Exception {
107         final String[] methods = {"decodeURI", "decodeURIComponent", "encodeURI", "encodeURIComponent", "escape",
108             "eval", "isFinite", "isNaN", "parseFloat", "parseInt", "unescape"};
109         final String html = NativeDateTest.createHTMLTestMethods("this", methods);
110         loadPageVerifyTitle2(html);
111     }
112 
113     /**
114      * Test for the methods with the different expectations depending on the browsers.
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts({"isXMLName: undefined", "uneval: undefined"})
119     public void methods_different() throws Exception {
120         final String[] methods = {"isXMLName", "uneval"};
121         final String html = NativeDateTest.createHTMLTestMethods("this", methods);
122         loadPageVerifyTitle2(html);
123     }
124 
125     /**
126      * Test case for #1439.
127      * @throws Exception if the test fails
128      */
129     @Test
130     @Alerts({"http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab",
131                 "%E6%B5%8B%E8%A9%A6"})
132     public void encodeURIComponent() throws Exception {
133         final String html = DOCTYPE_HTML
134             + "<html>\n"
135             + "<head>\n"
136             + "<script>\n"
137             + LOG_TITLE_FUNCTION
138             + "  function test() {\n"
139             + "    var uri='http://w3schools.com/my test.asp?name=st\u00E5le&car=saab';\n"
140             + "    log(encodeURIComponent(uri));\n"
141 
142             + "    uri='\\u6D4B\\u8A66';\n"
143             + "    log(encodeURIComponent(uri));\n"
144             + "  }\n"
145             + "</script>\n"
146             + "</head>\n"
147             + "<body onload='test()'>\n"
148             + "</body></html>";
149 
150         loadPageVerifyTitle2(html);
151     }
152 
153     /**
154      * Test case for #1439.
155      * @throws Exception if the test fails
156      */
157     @Test
158     @Alerts("%E6%B5%8B%E8%A9%A6")
159     public void encodeURIComponentUtf8() throws Exception {
160         final String html = DOCTYPE_HTML
161             + "<html>\n"
162             + "<head>\n"
163             + "<script>\n"
164             + LOG_TITLE_FUNCTION
165             + "  function test() {\n"
166             + "    uri='\u6D4B\u8A66';\n"
167             + "    log(encodeURIComponent(uri));\n"
168             + "  }\n"
169             + "</script>\n"
170             + "</head>\n"
171             + "<body onload='test()'>\n"
172             + "</body></html>";
173 
174         final WebDriver driver = loadPage2(html, URL_FIRST, "text/html;charset=UTF-8", UTF_8);
175         verifyTitle2(driver, getExpectedAlerts());
176     }
177 
178     /**
179      * Test case for https://github.com/HtmlUnit/htmlunit/issues/94.
180      * @throws Exception if the test fails
181      */
182     @Test
183     @Alerts({"\\u00ee\\u0010\\u0043\\u0072\\u00f4\\u00ef\\u00b6\\u0062\\u0034",
184              "\\u00ee\\u0010\\u0043\\u0072\\u00f4\\u00ef\\u00b6\\u0062\\u0034"})
185     public void decodeURIComponent() throws Exception {
186         final String html = DOCTYPE_HTML
187             + "<html>\n"
188             + "<head>\n"
189             + "<script>\n"
190             + "  function log(msg) {\n"
191             + "    var escaped = '';\n"
192             + "    for (var i = 0; i < msg.length; ++i) {\n"
193             + "      var hex = msg.charCodeAt(i).toString(16).toLowerCase();\n"
194             + "      escaped += '\\\\u' + '0000'.substr(hex.length) + hex;\n"
195             + "    }\n"
196             + "    window.document.title += escaped + '\\u00a7';\n"
197             + "  }\n"
198 
199             + "  function test() {\n"
200             + "    var uri='%c3%ae%10%43%72%c3%b4%c3%af%c2%b6%62%34';\n"
201             + "    log(decodeURIComponent(uri));\n"
202             + "    log(decodeURIComponent(uri, false));\n"
203             + "  }\n"
204             + "</script>\n"
205             + "</head>\n"
206             + "<body onload='test()'>\n"
207             + "</body></html>";
208 
209         loadPageVerifyTitle2(html);
210     }
211 }