1
2
3
4
5
6
7
8
9
10
11
12
13
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
28
29
30
31
32
33 @RunWith(BrowserRunner.class)
34 public class GlobalFunctionsTest extends WebDriverTestCase {
35
36
37
38
39
40
41
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
61
62
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
103
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
118
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
130
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
158
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
183
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 }