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.general;
16  
17  import java.util.ArrayList;
18  import java.util.Arrays;
19  import java.util.Collection;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.htmlunit.TestCaseTest;
25  import org.htmlunit.WebDriverTestCase;
26  import org.htmlunit.junit.BrowserParameterizedRunner;
27  import org.htmlunit.junit.BrowserParameterizedRunner.Default;
28  import org.htmlunit.junit.annotation.Alerts;
29  import org.junit.Ignore;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.junit.runners.Parameterized.Parameter;
33  import org.junit.runners.Parameterized.Parameters;
34  
35  /**
36   * Test the host class constructors.
37   *
38   * @author Ronald Brill
39   */
40  @RunWith(BrowserParameterizedRunner.class)
41  @Ignore("Work in progress")
42  public class HostConstructorTest extends WebDriverTestCase {
43  
44      private static final HashSet<String> PASSING = new HashSet<>(Arrays.asList(
45              "Animation",
46              "Blob",
47              "DOMParser",
48              "FileReader",
49              "FormData",
50              "MediaSource",
51              "MessageChannel",
52              "XMLHttpRequest",
53              "XMLSerializer"
54              ));
55  
56      /**
57       * Returns the parameterized data.
58       * @return the parameterized data
59       * @throws Exception if an error occurs
60       */
61      @Parameters
62      public static Collection<Object[]> data() throws Exception {
63          final List<Object[]> list = new ArrayList<>();
64          final Set<String> strings = TestCaseTest.getAllConfiguredJsClassNames();
65          for (final String className : strings) {
66              list.add(new Object[] {className});
67          }
68          return list;
69      }
70  
71      /**
72       * The class name.
73       */
74      @Parameter
75      public String className_;
76  
77      /**
78       * The default test.
79       * @throws Exception if an error occurs
80       */
81      @Test
82      @Default
83      public void test() throws Exception {
84          setExpectedAlerts(getExpectedString(className_));
85  
86          test(className_);
87      }
88  
89      private void test(final String className) throws Exception {
90          final String html = DOCTYPE_HTML
91                  + "<html><head></head>\n"
92                  + "<body>"
93                  + "<script>\n"
94                  + LOG_TITLE_FUNCTION
95  
96                  + "  try {\n"
97                  + "    log(new " + className + "());\n"
98                  + "  } catch(e) { logEx(e) }\n"
99                  + "</script>\n"
100                 + "</body></html>";
101 
102         loadPageVerifyTitle2(html);
103     }
104 
105     private String getExpectedString(final String className) throws Exception {
106         if (PASSING.contains(className)) {
107             return "[object " + className_ + "]";
108         }
109 
110         return "ReferenceError";
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     protected boolean isWebClientCached() {
118         return true;
119     }
120 
121     /**
122      * @throws Exception if the test fails
123      */
124     @Test
125     @Alerts("[object Animation]")
126     public void _Animation() throws Exception {
127         test("Animation");
128     }
129 
130     /**
131      * @throws Exception if the test fails
132      */
133     @Test
134     @Alerts("[object HTMLAudioElement]")
135     public void _Audio() throws Exception {
136         test("Audio");
137     }
138 
139     /**
140      * @throws Exception if the test fails
141      */
142     @Test
143     @Alerts("[object AudioContext]")
144     public void _AudioContext() throws Exception {
145         test("AudioContext");
146     }
147 
148     /**
149      * @throws Exception if the test fails
150      */
151     @Test
152     @Alerts("[object CSSStyleSheet]")
153     public void _CSSStyleSheet() throws Exception {
154         test("CSSStyleSheet");
155     }
156 
157     /**
158      * @throws Exception if the test fails
159      */
160     @Test
161     @Alerts("[object Comment]")
162     public void _Comment() throws Exception {
163         test("Comment");
164     }
165 
166     /**
167      * @throws Exception if the test fails
168      */
169     @Test
170     @Alerts("matrix(1, 0, 0, 1, 0, 0)")
171     public void _DOMMatrix() throws Exception {
172         test("DOMMatrix");
173     }
174 
175     /**
176      * @throws Exception if the test fails
177      */
178     @Test
179     @Alerts("matrix(1, 0, 0, 1, 0, 0)")
180     public void _DOMMatrixReadOnly() throws Exception {
181         test("DOMMatrixReadOnly");
182     }
183 
184     /**
185      * @throws Exception if the test fails
186      */
187     @Test
188     @Alerts("Error")
189     public void _DOMException() throws Exception {
190         test("DOMException");
191     }
192 
193     /**
194      * @throws Exception if the test fails
195      */
196     @Test
197     @Alerts("[object DOMPoint]")
198     public void _DOMPoint() throws Exception {
199         test("DOMPoint");
200     }
201 
202     /**
203      * @throws Exception if the test fails
204      */
205     @Test
206     @Alerts("[object DOMPointReadOnly]")
207     public void _DOMPointReadOnly() throws Exception {
208         test("DOMPointReadOnly");
209     }
210 
211     /**
212      * @throws Exception if the test fails
213      */
214     @Test
215     @Alerts("[object DOMRect]")
216     public void _DOMRect() throws Exception {
217         test("DOMRect");
218     }
219 
220     /**
221      * @throws Exception if the test fails
222      */
223     @Test
224     @Alerts("[object DOMRectReadOnly]")
225     public void _DOMRectReadOnly() throws Exception {
226         test("DOMRectReadOnly");
227     }
228 
229     /**
230      * @throws Exception if the test fails
231      */
232     @Test
233     @Alerts("[object DataTransfer]")
234     public void _DataTransfer() throws Exception {
235         test("DataTransfer");
236     }
237 
238     /**
239      * @throws Exception if the test fails
240      */
241     @Test
242     @Alerts("[object Document]")
243     public void _Document() throws Exception {
244         test("Document");
245     }
246 
247     /**
248      * @throws Exception if the test fails
249      */
250     @Test
251     @Alerts("[object DocumentFragment]")
252     public void _DocumentFragment() throws Exception {
253         test("DocumentFragment");
254     }
255 
256     /**
257      * @throws Exception if the test fails
258      */
259     @Test
260     @Alerts("exception")
261     public void _Enumerator() throws Exception {
262         test("Enumerator");
263     }
264 
265     /**
266      * @throws Exception if the test fails
267      */
268     @Test
269     @Alerts("[object EventTarget]")
270     public void _EventTarget() throws Exception {
271         test("EventTarget");
272     }
273 
274     /**
275      * @throws Exception if the test fails
276      */
277     @Test
278     @Alerts("[object Headers]")
279     public void _Headers() throws Exception {
280         test("Headers");
281     }
282 
283     /**
284      * @throws Exception if the test fails
285      */
286     @Test
287     @Alerts("[object HTMLImageElement]")
288     public void _Image() throws Exception {
289         test("Image");
290     }
291 
292     /**
293      * @throws Exception if the test fails
294      */
295     @Test
296     @Alerts(DEFAULT = "[object InputDeviceCapabilities]",
297             FF = "exception",
298             FF_ESR = "exception")
299     public void _InputDeviceCapabilities() throws Exception {
300         test("InputDeviceCapabilities");
301     }
302 
303     /**
304      * @throws Exception if the test fails
305      */
306     @Test
307     @Alerts("[object MediaStream]")
308     public void _MediaStream() throws Exception {
309         test("MediaStream");
310     }
311 
312     /**
313      * @throws Exception if the test fails
314      */
315     @Test
316     @Alerts(DEFAULT = "exception",
317             FF = "[object mozRTCPeerConnection]",
318             FF_ESR = "[object mozRTCPeerConnection]")
319     public void _mozRTCPeerConnection() throws Exception {
320         test("mozRTCPeerConnection");
321     }
322 
323     /**
324      * @throws Exception if the test fails
325      */
326     @Test
327     @Alerts("[object HTMLOptionElement]")
328     public void _Option() throws Exception {
329         test("Option");
330     }
331 
332     /**
333      * @throws Exception if the test fails
334      */
335     @Test
336     @Alerts("[object Path2D]")
337     public void _Path2D() throws Exception {
338         test("Path2D");
339     }
340 
341     /**
342      * @throws Exception if the test fails
343      */
344     @Test
345     @Alerts("")
346     public void _Range() throws Exception {
347         test("Range");
348     }
349 
350     /**
351      * @throws Exception if the test fails
352      */
353     @Test
354     @Alerts("[object ReadableStream]")
355     public void _ReadableStream() throws Exception {
356         test("ReadableStream");
357     }
358 
359     /**
360      * @throws Exception if the test fails
361      */
362     @Test
363     @Alerts("[object Response]")
364     public void _Response() throws Exception {
365         test("Response");
366     }
367 
368     /**
369      * @throws Exception if the test fails
370      */
371     @Test
372     @Alerts("[object RTCPeerConnection]")
373     public void _RTCPeerConnection() throws Exception {
374         test("RTCPeerConnection");
375     }
376 
377     /**
378      * @throws Exception if the test fails
379      */
380     @Test
381     @Alerts(DEFAULT = "[object RTCSessionDescription]",
382             FF = "exception",
383             FF_ESR = "exception")
384     public void _RTCSessionDescription() throws Exception {
385         test("RTCSessionDescription");
386     }
387 
388     /**
389      * @throws Exception if the test fails
390      */
391     @Test
392     @Alerts("[object SpeechSynthesisUtterance]")
393     public void _SpeechSynthesisUtterance() throws Exception {
394         test("SpeechSynthesisUtterance");
395     }
396 
397     /**
398      * @throws Exception if the test fails
399      */
400     @Test
401     @Alerts("[object Text]")
402     public void _Text() throws Exception {
403         test("Text");
404     }
405 
406     /**
407      * @throws Exception if the test fails
408      */
409     @Test
410     @Alerts("[object TextDecoder]")
411     public void _TextDecoder() throws Exception {
412         test("TextDecoder");
413     }
414 
415     /**
416      * @throws Exception if the test fails
417      */
418     @Test
419     @Alerts("[object TextEncoder]")
420     public void _TextEncoder() throws Exception {
421         test("TextEncoder");
422     }
423 
424     /**
425      * @throws Exception if the test fails
426      */
427     @Test
428     @Alerts("")
429     public void _URLSearchParams() throws Exception {
430         test("URLSearchParams");
431     }
432 
433     /**
434      * @throws Exception if the test fails
435      */
436     @Test
437     @Alerts("exception")
438     public void _WebGLContextEvent() throws Exception {
439         test("WebGLContextEvent");
440     }
441 
442     /**
443      * @throws Exception if the test fails
444      */
445     @Test
446     @Alerts("matrix(1, 0, 0, 1, 0, 0)")
447     public void _WebKitCSSMatrix() throws Exception {
448         test("WebKitCSSMatrix");
449     }
450 
451     /**
452      * @throws Exception if the test fails
453      */
454     @Test
455     @Alerts(DEFAULT = "[object MediaStream]",
456             FF = "exception",
457             FF_ESR = "exception")
458     public void _webkitMediaStream() throws Exception {
459         test("webkitMediaStream");
460     }
461 
462     /**
463      * @throws Exception if the test fails
464      */
465     @Test
466     @Alerts(DEFAULT = "[object RTCPeerConnection]",
467             FF = "exception",
468             FF_ESR = "exception")
469     public void _webkitRTCPeerConnection() throws Exception {
470         test("webkitRTCPeerConnection");
471     }
472 
473     /**
474      * @throws Exception if the test fails
475      */
476     @Test
477     @Alerts(DEFAULT = "[object SpeechGrammar]",
478             FF = "exception",
479             FF_ESR = "exception")
480     public void _webkitSpeechGrammar() throws Exception {
481         test("webkitSpeechGrammar");
482     }
483 
484     /**
485      * @throws Exception if the test fails
486      */
487     @Test
488     @Alerts(DEFAULT = "[object SpeechGrammarList]",
489             FF = "exception",
490             FF_ESR = "exception")
491     public void _webkitSpeechGrammarList() throws Exception {
492         test("webkitSpeechGrammarList");
493     }
494 
495     /**
496      * @throws Exception if the test fails
497      */
498     @Test
499     @Alerts(DEFAULT = "[object SpeechRecognition]",
500             FF = "exception",
501             FF_ESR = "exception")
502     public void _webkitSpeechRecognition() throws Exception {
503         test("webkitSpeechRecognition");
504     }
505 
506     /**
507      * @throws Exception if the test fails
508      */
509     @Test
510     @Alerts("[object XPathEvaluator]")
511     public void _XPathEvaluator() throws Exception {
512         test("XPathEvaluator");
513     }
514 
515     /**
516      * @throws Exception if the test fails
517      */
518     @Test
519     @Alerts("[object XSLTProcessor]")
520     public void _XSLTProcessor() throws Exception {
521         test("XSLTProcessor");
522     }
523 
524     /**
525      * Test {@link org.htmlunit.javascript.host.abort.AbortController}.
526      *
527      * @throws Exception if an error occurs
528      */
529     @Test
530     @Alerts("[object AbortController]")
531     public void abortController() throws Exception {
532         test("AbortController");
533     }
534 
535     /**
536      * Test {@link org.htmlunit.javascript.host.abort.AbortSignal}.
537      *
538      * @throws Exception if an error occurs
539      */
540     @Test
541     @Alerts("[object AbortSignal]")
542     public void abortSignal() throws Exception {
543         test("AbortSignal");
544     }
545 }