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