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.event;
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 CustomEvent}.
24   *
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  public class CustomEventTest extends WebDriverTestCase {
29  
30      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
31              + "    log(event);\n"
32              + "    log(event.type);\n"
33              + "    log(event.bubbles);\n"
34              + "    log(event.cancelable);\n"
35              + "    log(event.composed);\n"
36              + "    log(event.detail);\n"
37              + "  }\n";
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts({"[object CustomEvent]", "my", "false", "false", "false", "null"})
44      public void create_ctor() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head><script>\n"
47              + LOG_TITLE_FUNCTION
48              + "  function test() {\n"
49              + "    try {\n"
50              + "      var event = new CustomEvent('my');\n"
51              + "      dump(event);\n"
52              + "    } catch(e) { logEx(e) }\n"
53              + "  }\n"
54              + DUMP_EVENT_FUNCTION
55              + "</script></head><body onload='test()'>\n"
56              + "</body></html>";
57  
58          loadPageVerifyTitle2(html);
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts("TypeError")
66      @HtmlUnitNYI(CHROME = {"[object CustomEvent]", "undefined", "false", "false", "false", "null"},
67                  EDGE = {"[object CustomEvent]", "undefined", "false", "false", "false", "null"},
68                  FF = {"[object CustomEvent]", "undefined", "false", "false", "false", "null"},
69                  FF_ESR = {"[object CustomEvent]", "undefined", "false", "false", "false", "null"})
70      public void create_ctorWithoutType() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html><head><script>\n"
73              + LOG_TITLE_FUNCTION
74              + "  function test() {\n"
75              + "    try {\n"
76              + "      var event = new CustomEvent();\n"
77              + "      dump(event);\n"
78              + "    } catch(e) { logEx(e) }\n"
79              + "  }\n"
80              + DUMP_EVENT_FUNCTION
81              + "</script></head><body onload='test()'>\n"
82              + "</body></html>";
83  
84          loadPageVerifyTitle2(html);
85      }
86  
87      /**
88       * @throws Exception if the test fails
89       */
90      @Test
91      @Alerts({"[object CustomEvent]", "42", "false", "false", "false", "null"})
92      public void create_ctorNumericType() throws Exception {
93          final String html = DOCTYPE_HTML
94              + "<html><head><script>\n"
95              + LOG_TITLE_FUNCTION
96              + "  function test() {\n"
97              + "    try {\n"
98              + "      var event = new CustomEvent(42);\n"
99              + "      dump(event);\n"
100             + "    } catch(e) { logEx(e) }\n"
101             + "  }\n"
102             + DUMP_EVENT_FUNCTION
103             + "</script></head><body onload='test()'>\n"
104             + "</body></html>";
105 
106         loadPageVerifyTitle2(html);
107     }
108 
109     /**
110      * @throws Exception if the test fails
111      */
112     @Test
113     @Alerts({"[object CustomEvent]", "null", "false", "false", "false", "null"})
114     public void create_ctorNullType() throws Exception {
115         final String html = DOCTYPE_HTML
116             + "<html><head><script>\n"
117             + LOG_TITLE_FUNCTION
118             + "  function test() {\n"
119             + "    try {\n"
120             + "      var event = new CustomEvent(null);\n"
121             + "      dump(event);\n"
122             + "    } catch(e) { logEx(e) }\n"
123             + "  }\n"
124             + DUMP_EVENT_FUNCTION
125             + "</script></head><body onload='test()'>\n"
126             + "</body></html>";
127 
128         loadPageVerifyTitle2(html);
129     }
130 
131     /**
132      * @throws Exception if the test fails
133      */
134     @Test
135     @Alerts("ReferenceError")
136     public void create_ctorUnknownType() throws Exception {
137         final String html = DOCTYPE_HTML
138             + "<html><head><script>\n"
139             + LOG_TITLE_FUNCTION
140             + "  function test() {\n"
141             + "    try {\n"
142             + "      var event = new CustomEvent(unknown);\n"
143             + "      dump(event);\n"
144             + "    } catch(e) { logEx(e) }\n"
145             + "  }\n"
146             + DUMP_EVENT_FUNCTION
147             + "</script></head><body onload='test()'>\n"
148             + "</body></html>";
149 
150         loadPageVerifyTitle2(html);
151     }
152 
153     /**
154      * @throws Exception if the test fails
155      */
156     @Test
157     @Alerts({"[object CustomEvent]", "HtmlUnitEvent", "false", "false", "false", "null"})
158     public void create_ctorArbitraryType() throws Exception {
159         final String html = DOCTYPE_HTML
160             + "<html><head><script>\n"
161             + LOG_TITLE_FUNCTION
162             + "  function test() {\n"
163             + "    try {\n"
164             + "      var event = new CustomEvent('HtmlUnitEvent');\n"
165             + "      dump(event);\n"
166             + "    } catch(e) { logEx(e) }\n"
167             + "  }\n"
168             + DUMP_EVENT_FUNCTION
169             + "</script></head><body onload='test()'>\n"
170             + "</body></html>";
171 
172         loadPageVerifyTitle2(html);
173     }
174 
175     /**
176      * @throws Exception if the test fails
177      */
178     @Test
179     @Alerts({"[object CustomEvent]", "click", "false", "false", "false", "abcd"})
180     public void create_ctorAllDetails() throws Exception {
181         final String html = DOCTYPE_HTML
182             + "<html><head><script>\n"
183             + LOG_TITLE_FUNCTION
184             + "  function test() {\n"
185             + "    try {\n"
186             + "      var event = new CustomEvent('click', {\n"
187             + "        'detail': 'abcd'"
188             + "      });\n"
189             + "      dump(event);\n"
190             + "    } catch(e) { logEx(e) }\n"
191             + "  }\n"
192             + DUMP_EVENT_FUNCTION
193             + "</script></head><body onload='test()'>\n"
194             + "</body></html>";
195 
196         loadPageVerifyTitle2(html);
197     }
198 
199     /**
200      * @throws Exception if the test fails
201      */
202     @Test
203     @Alerts("function")
204     public void initCustomEvent() throws Exception {
205         final String html = DOCTYPE_HTML
206             + "<html><head><script>\n"
207             + LOG_TITLE_FUNCTION
208             + "  function test() {\n"
209             + "    try {\n"
210             + "      var e = document.createEvent('CustomEvent');\n"
211             + "      log(typeof e.initCustomEvent);\n"
212             + "    } catch(e) { logEx(e) }\n"
213             + "  }\n"
214             + "</script></head><body onload='test()'>\n"
215             + "</body></html>";
216         loadPageVerifyTitle2(html);
217     }
218 
219     /**
220      * @throws Exception if an error occurs
221      */
222     @Test
223     @Alerts({"true", "details", "I was here"})
224     public void dispatchEvent() throws Exception {
225         final String html = DOCTYPE_HTML
226             + "<html><head>\n"
227             + "<script>\n"
228             + LOG_TITLE_FUNCTION
229             + "function test() {\n"
230             + "  var listener = function(x) {\n"
231             + "    log(x == myEvent);\n"
232             + "    log(x.detail);\n"
233             + "    x.foo = 'I was here';\n"
234             + "  }\n"
235             + "  document.addEventListener('HTMLImportsLoaded', listener);\n"
236 
237             + "  var myEvent = document.createEvent('CustomEvent');\n"
238             + "  myEvent.initCustomEvent('HTMLImportsLoaded', true, true, 'details');\n"
239             + "  document.dispatchEvent(myEvent);\n"
240             + "  log(myEvent.foo);\n"
241             + "}\n"
242             + "</script>\n"
243             + "</head><body onload='test()'>\n"
244             + "</body></html>";
245 
246         loadPageVerifyTitle2(html);
247     }
248 
249     /**
250      * @throws Exception if an error occurs
251      */
252     @Test
253     @Alerts({"true", "details", "I was here"})
254     public void dispatchEventOnDomText() throws Exception {
255         final String html = DOCTYPE_HTML
256             + "<html><head>\n"
257             + "<script>\n"
258             + LOG_TITLE_FUNCTION
259             + "function test() {\n"
260             + "  var listener = function(x) {\n"
261             + "    log(x == myEvent);\n"
262             + "    log(x.detail);\n"
263             + "    x.foo = 'I was here';\n"
264             + "  }\n"
265             + "  var txt = document.getElementById('myDiv').firstChild;\n"
266             + "  txt.addEventListener('MyEvent', listener);\n"
267 
268             + "  var myEvent = document.createEvent('CustomEvent');\n"
269             + "  myEvent.initCustomEvent('MyEvent', true, true, 'details');\n"
270             + "  txt.dispatchEvent(myEvent);\n"
271             + "  log(myEvent.foo);\n"
272             + "}\n"
273             + "</script>\n"
274             + "</head>\n"
275             + "<body onload='test()'>\n"
276             + "  <div id='myDiv'>Hallo HtmlUnit</div>\n"
277             + "</body></html>";
278 
279         loadPageVerifyTitle2(html);
280     }
281 }