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