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