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.xml;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.junit.annotation.HtmlUnitNYI;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Tests for {@link XMLHttpRequestEventTarget}.
26   *
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class XMLHttpRequestEventTargetTest extends WebDriverTestCase {
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts("true")
37      public void inWindow() throws Exception {
38          final String html = DOCTYPE_HTML
39              + "<html>\n"
40              + "<head>\n"
41              + "  <script>\n"
42              + LOG_TITLE_FUNCTION
43              + "    function test() {\n"
44              + "      log('XMLHttpRequestEventTarget' in window);\n"
45              + "    }\n"
46              + "  </script>\n"
47              + "</head>\n"
48              + "<body onload='test()'>\n"
49              + "</body>\n"
50              + "</html>";
51  
52          loadPageVerifyTitle2(html);
53      }
54  
55      /**
56       * @throws Exception if the test fails.
57       */
58      @Test
59      @Alerts("TypeError")
60      public void ctor() throws Exception {
61          final String html = DOCTYPE_HTML
62                  + "<html><head>\n"
63                  + "<script>\n"
64                  + LOG_TITLE_FUNCTION
65                  + "function test() {\n"
66                  + "  try {\n"
67                  + "    var xhr = new XMLHttpRequestEventTarget();\n"
68                  + "    log(xhr);\n"
69                  + "  } catch(e) { logEx(e); }\n"
70                  + "}\n"
71                  + "</script>\n"
72                  + "</head>\n"
73                  + "<body onload='test()'></body></html>";
74          loadPageVerifyTitle2(html);
75      }
76  
77      /**
78       * @throws Exception if the test fails.
79       */
80      @Test
81      @Alerts(DEFAULT = {"[object Object]", "undefined", "undefined",
82                         "function get onabort() { [native code] }",
83                         "function set onabort() { [native code] }",
84                         "true", "true"},
85              FF = {"[object Object]", "undefined", "undefined",
86                    "function onabort() { [native code] }",
87                    "function onabort() { [native code] }",
88                    "true", "true"},
89              FF_ESR = {"[object Object]", "undefined", "undefined",
90                        "function onabort() { [native code] }",
91                        "function onabort() { [native code] }",
92                        "true", "true"})
93      @HtmlUnitNYI(CHROME = {"[object Object]", "undefined", "undefined",
94                             "function onabort() { [native code] }",
95                             "function onabort() { [native code] }",
96                             "true", "true"},
97              EDGE = {"[object Object]", "undefined", "undefined",
98                      "function onabort() { [native code] }",
99                      "function onabort() { [native code] }",
100                     "true", "true"})
101     public void getOwnPropertyDescriptor_onabort() throws Exception {
102         getOwnPropertyDescriptor("onabort");
103     }
104 
105     /**
106      * @throws Exception if the test fails.
107      */
108     @Test
109     @Alerts(DEFAULT = {"[object Object]", "undefined", "undefined",
110                        "function get onerror() { [native code] }",
111                        "function set onerror() { [native code] }",
112                        "true", "true"},
113             FF = {"[object Object]", "undefined", "undefined",
114                   "function onerror() { [native code] }",
115                   "function onerror() { [native code] }",
116                   "true", "true"},
117             FF_ESR = {"[object Object]", "undefined", "undefined",
118                       "function onerror() { [native code] }",
119                       "function onerror() { [native code] }",
120                       "true", "true"})
121     @HtmlUnitNYI(CHROME = {"[object Object]", "undefined", "undefined",
122                            "function onerror() { [native code] }",
123                            "function onerror() { [native code] }",
124                            "true", "true"},
125             EDGE = {"[object Object]", "undefined", "undefined",
126                     "function onerror() { [native code] }",
127                     "function onerror() { [native code] }",
128                     "true", "true"})
129     public void getOwnPropertyDescriptor_onerror() throws Exception {
130         getOwnPropertyDescriptor("onerror");
131     }
132 
133     /**
134      * @throws Exception if the test fails.
135      */
136     @Test
137     @Alerts(DEFAULT = {"[object Object]", "undefined", "undefined",
138                        "function get onload() { [native code] }",
139                        "function set onload() { [native code] }",
140                        "true", "true"},
141             FF = {"[object Object]", "undefined", "undefined",
142                   "function onload() { [native code] }",
143                   "function onload() { [native code] }",
144                   "true", "true"},
145             FF_ESR = {"[object Object]", "undefined", "undefined",
146                       "function onload() { [native code] }",
147                       "function onload() { [native code] }",
148                       "true", "true"})
149     @HtmlUnitNYI(CHROME = {"[object Object]", "undefined", "undefined",
150                            "function onload() { [native code] }",
151                            "function onload() { [native code] }",
152                            "true", "true"},
153             EDGE = {"[object Object]", "undefined", "undefined",
154                     "function onload() { [native code] }",
155                     "function onload() { [native code] }",
156                     "true", "true"})
157     public void getOwnPropertyDescriptor_onload() throws Exception {
158         getOwnPropertyDescriptor("onload");
159     }
160 
161     /**
162      * @throws Exception if the test fails.
163      */
164     @Test
165     @Alerts(DEFAULT = {"[object Object]", "undefined", "undefined",
166                        "function get onloadstart() { [native code] }",
167                        "function set onloadstart() { [native code] }",
168                        "true", "true"},
169             FF = {"[object Object]", "undefined", "undefined",
170                   "function onloadstart() { [native code] }",
171                   "function onloadstart() { [native code] }",
172                   "true", "true"},
173             FF_ESR = {"[object Object]", "undefined", "undefined",
174                       "function onloadstart() { [native code] }",
175                       "function onloadstart() { [native code] }",
176                       "true", "true"})
177     @HtmlUnitNYI(CHROME = {"[object Object]", "undefined", "undefined",
178                            "function onloadstart() { [native code] }",
179                            "function onloadstart() { [native code] }",
180                            "true", "true"},
181             EDGE = {"[object Object]", "undefined", "undefined",
182                     "function onloadstart() { [native code] }",
183                     "function onloadstart() { [native code] }",
184                     "true", "true"})
185     public void getOwnPropertyDescriptor_onloadstart() throws Exception {
186         getOwnPropertyDescriptor("onloadstart");
187     }
188 
189     /**
190      * @throws Exception if the test fails.
191      */
192     @Test
193     @Alerts(DEFAULT = {"[object Object]", "undefined", "undefined",
194                        "function get onloadend() { [native code] }",
195                        "function set onloadend() { [native code] }",
196                        "true", "true"},
197             FF = {"[object Object]", "undefined", "undefined",
198                   "function onloadend() { [native code] }",
199                   "function onloadend() { [native code] }",
200                   "true", "true"},
201             FF_ESR = {"[object Object]", "undefined", "undefined",
202                       "function onloadend() { [native code] }",
203                       "function onloadend() { [native code] }",
204                       "true", "true"})
205     @HtmlUnitNYI(CHROME = {"[object Object]", "undefined", "undefined",
206                            "function onloadend() { [native code] }",
207                            "function onloadend() { [native code] }",
208                            "true", "true"},
209             EDGE = {"[object Object]", "undefined", "undefined",
210                     "function onloadend() { [native code] }",
211                     "function onloadend() { [native code] }",
212                     "true", "true"})
213     public void getOwnPropertyDescriptor_onloadend() throws Exception {
214         getOwnPropertyDescriptor("onloadend");
215     }
216 
217     /**
218      * @throws Exception if the test fails.
219      */
220     @Test
221     @Alerts(DEFAULT = {"[object Object]", "undefined", "undefined",
222                        "function get onprogress() { [native code] }",
223                        "function set onprogress() { [native code] }",
224                        "true", "true"},
225             FF = {"[object Object]", "undefined", "undefined",
226                   "function onprogress() { [native code] }",
227                   "function onprogress() { [native code] }",
228                   "true", "true"},
229             FF_ESR = {"[object Object]", "undefined", "undefined",
230                       "function onprogress() { [native code] }",
231                       "function onprogress() { [native code] }",
232                       "true", "true"})
233     @HtmlUnitNYI(CHROME = {"[object Object]", "undefined", "undefined",
234                            "function onprogress() { [native code] }",
235                            "function onprogress() { [native code] }",
236                            "true", "true"},
237             EDGE = {"[object Object]", "undefined", "undefined",
238                     "function onprogress() { [native code] }",
239                     "function onprogress() { [native code] }",
240                     "true", "true"})
241     public void getOwnPropertyDescriptor_onprogress() throws Exception {
242         getOwnPropertyDescriptor("onprogress");
243     }
244 
245     /**
246      * @throws Exception if the test fails.
247      */
248     @Test
249     @Alerts("undefined")
250     public void getOwnPropertyDescriptor_onreadystatechange() throws Exception {
251         getOwnPropertyDescriptor("onreadystatechange");
252     }
253 
254     /**
255      * @throws Exception if the test fails.
256      */
257     @Test
258     @Alerts(DEFAULT = {"[object Object]", "undefined", "undefined",
259                        "function get ontimeout() { [native code] }",
260                        "function set ontimeout() { [native code] }",
261                        "true", "true"},
262             FF = {"[object Object]", "undefined", "undefined",
263                   "function ontimeout() { [native code] }",
264                   "function ontimeout() { [native code] }",
265                   "true", "true"},
266             FF_ESR = {"[object Object]", "undefined", "undefined",
267                       "function ontimeout() { [native code] }",
268                       "function ontimeout() { [native code] }",
269                       "true", "true"})
270     @HtmlUnitNYI(CHROME = {"[object Object]", "undefined", "undefined",
271                            "function ontimeout() { [native code] }",
272                            "function ontimeout() { [native code] }",
273                            "true", "true"},
274             EDGE = {"[object Object]", "undefined", "undefined",
275                     "function ontimeout() { [native code] }",
276                     "function ontimeout() { [native code] }",
277                     "true", "true"})
278     public void getOwnPropertyDescriptor_ontimeout() throws Exception {
279         getOwnPropertyDescriptor("ontimeout");
280     }
281 
282     private void getOwnPropertyDescriptor(final String event) throws Exception {
283         final String html = DOCTYPE_HTML
284             + "<html>\n"
285             + "  <head>\n"
286             + "    <script>\n"
287             + LOG_TITLE_FUNCTION
288             + "      var request;\n"
289             + "      function test() {\n"
290             + "        var desc = Object.getOwnPropertyDescriptor("
291                                 + "XMLHttpRequestEventTarget.prototype, '" + event + "');\n"
292             + "        log(desc);\n"
293             + "        if(!desc) { return; }\n"
294 
295             + "        log(desc.value);\n"
296             + "        log(desc.writable);\n"
297             + "        log(desc.get);\n"
298             + "        log(desc.set);\n"
299             + "        log(desc.configurable);\n"
300             + "        log(desc.enumerable);\n"
301             + "      }\n"
302             + "    </script>\n"
303             + "  </head>\n"
304             + "  <body onload='test()'>\n"
305             + "  </body>\n"
306             + "</html>";
307 
308         loadPageVerifyTitle2(html);
309     }
310 }