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