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 InputEvent}.
24   *
25   * @author Ronald Brill
26   */
27  public class InputEventTest 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  
36              + "    var details = [event.data, event.inputType, event.isComposing].join(',');\n"
37              + "    log(details);\n"
38              + "  }\n";
39  
40      /**
41       * @throws Exception if the test fails
42       */
43      @Test
44      @Alerts({"[object InputEvent]", "type", "false", "false", "false", ",,false"})
45      public void create_ctor() throws Exception {
46          final String html = DOCTYPE_HTML
47                  + "<html><head><script>\n"
48                  + LOG_TITLE_FUNCTION
49                  + "  function test() {\n"
50                  + "    try {\n"
51                  + "      var event = new InputEvent('type');\n"
52                  + "      dump(event);\n"
53                  + "    } catch(e) { logEx(e) }\n"
54                  + "  }\n"
55                  + DUMP_EVENT_FUNCTION
56                  + "</script></head><body onload='test()'>\n"
57                  + "</body></html>";
58  
59          loadPageVerifyTitle2(html);
60      }
61  
62      /**
63       * @throws Exception if the test fails
64       */
65      @Test
66      @Alerts("TypeError")
67      @HtmlUnitNYI(CHROME = {"[object InputEvent]", "undefined", "false", "false", "false",
68                             ",,false"},
69              EDGE = {"[object InputEvent]", "undefined", "false", "false", "false",
70                      ",,false"},
71              FF = {"[object InputEvent]", "undefined", "false", "false", "false",
72                    ",,false"},
73              FF_ESR = {"[object InputEvent]", "undefined", "false", "false", "false",
74                        ",,false"})
75      public void create_ctorWithoutType() throws Exception {
76          final String html = DOCTYPE_HTML
77              + "<html><head><script>\n"
78              + LOG_TITLE_FUNCTION
79              + "  function test() {\n"
80              + "    try {\n"
81              + "      var event = new InputEvent();\n"
82              + "      dump(event);\n"
83              + "    } catch(e) { logEx(e) }\n"
84              + "  }\n"
85              + DUMP_EVENT_FUNCTION
86              + "</script></head><body onload='test()'>\n"
87              + "</body></html>";
88  
89          loadPageVerifyTitle2(html);
90      }
91  
92      /**
93       * @throws Exception if the test fails
94       */
95      @Test
96      @Alerts({"[object InputEvent]", "42", "false", "false", "false", ",,false"})
97      public void create_ctorNumericType() throws Exception {
98          final String html = DOCTYPE_HTML
99              + "<html><head><script>\n"
100             + LOG_TITLE_FUNCTION
101             + "  function test() {\n"
102             + "    try {\n"
103             + "      var event = new InputEvent(42);\n"
104             + "      dump(event);\n"
105             + "    } catch(e) { logEx(e) }\n"
106             + "  }\n"
107             + DUMP_EVENT_FUNCTION
108             + "</script></head><body onload='test()'>\n"
109             + "</body></html>";
110 
111         loadPageVerifyTitle2(html);
112     }
113 
114     /**
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts({"[object InputEvent]", "null", "false", "false", "false", ",,false"})
119     public void create_ctorNullType() throws Exception {
120         final String html = DOCTYPE_HTML
121             + "<html><head><script>\n"
122             + LOG_TITLE_FUNCTION
123             + "  function test() {\n"
124             + "    try {\n"
125             + "      var event = new InputEvent(null);\n"
126             + "      dump(event);\n"
127             + "    } catch(e) { logEx(e) }\n"
128             + "  }\n"
129             + DUMP_EVENT_FUNCTION
130             + "</script></head><body onload='test()'>\n"
131             + "</body></html>";
132 
133         loadPageVerifyTitle2(html);
134     }
135 
136     /**
137      * @throws Exception if the test fails
138      */
139     @Test
140     @Alerts("ReferenceError")
141     public void create_ctorUnknownType() throws Exception {
142         final String html = DOCTYPE_HTML
143             + "<html><head><script>\n"
144             + LOG_TITLE_FUNCTION
145             + "  function test() {\n"
146             + "    try {\n"
147             + "      var event = new InputEvent(unknown);\n"
148             + "      dump(event);\n"
149             + "    } catch(e) { logEx(e) }\n"
150             + "  }\n"
151             + DUMP_EVENT_FUNCTION
152             + "</script></head><body onload='test()'>\n"
153             + "</body></html>";
154 
155         loadPageVerifyTitle2(html);
156     }
157 
158     /**
159      * @throws Exception if the test fails
160      */
161     @Test
162     @Alerts({"[object InputEvent]", "HtmlUnitEvent", "false", "false", "false", ",,false"})
163     public void create_ctorArbitraryType() throws Exception {
164         final String html = DOCTYPE_HTML
165             + "<html><head><script>\n"
166             + LOG_TITLE_FUNCTION
167             + "  function test() {\n"
168             + "    try {\n"
169             + "      var event = new InputEvent('HtmlUnitEvent');\n"
170             + "      dump(event);\n"
171             + "    } catch(e) { logEx(e) }\n"
172             + "  }\n"
173             + DUMP_EVENT_FUNCTION
174             + "</script></head><body onload='test()'>\n"
175             + "</body></html>";
176 
177         loadPageVerifyTitle2(html);
178     }
179 
180     /**
181      * @throws Exception if the test fails
182      */
183     @Test
184     @Alerts(DEFAULT = {"[object InputEvent]", "input", "false", "false", "false",
185                        "data,inputType,true"},
186             CHROME = {"[object InputEvent]", "input", "false", "false", "false",
187                       "data,,true"},
188             EDGE = {"[object InputEvent]", "input", "false", "false", "false",
189                     "data,,true"})
190     public void create_ctorAllDetails() throws Exception {
191         final String html = DOCTYPE_HTML
192             + "<html><head><script>\n"
193             + LOG_TITLE_FUNCTION
194             + "  function test() {\n"
195             + "    try {\n"
196             + "      var event = new InputEvent('input', "
197                              + "{ inputType: 'inputType', data: 'data', isComposing: true });\n"
198             + "      dump(event);\n"
199             + "    } catch(e) { logEx(e) }\n"
200             + "  }\n"
201             + DUMP_EVENT_FUNCTION
202             + "</script></head><body onload='test()'>\n"
203             + "</body></html>";
204 
205         loadPageVerifyTitle2(html);
206     }
207 
208     /**
209      * @throws Exception if the test fails
210      */
211     @Test
212     @Alerts({"[object InputEvent]", "input", "false", "false", "false", ",,true"})
213     public void create_ctorSomeDetails() throws Exception {
214         final String html = DOCTYPE_HTML
215             + "<html><head><script>\n"
216             + LOG_TITLE_FUNCTION
217             + "  function test() {\n"
218             + "    try {\n"
219             + "      var event = new InputEvent('input', "
220                              + "{ isComposing: true });\n"
221             + "      dump(event);\n"
222             + "    } catch(e) { logEx(e) }\n"
223             + "  }\n"
224             + DUMP_EVENT_FUNCTION
225             + "</script></head><body onload='test()'>\n"
226             + "</body></html>";
227 
228         loadPageVerifyTitle2(html);
229     }
230 
231     /**
232      * @throws Exception if the test fails
233      */
234     @Test
235     @Alerts({"[object InputEvent]", "input", "false", "false", "false", ",,false"})
236     public void create_ctorMissingData() throws Exception {
237         final String html = DOCTYPE_HTML
238             + "<html><head><script>\n"
239             + LOG_TITLE_FUNCTION
240             + "  function test() {\n"
241             + "    try {\n"
242             + "      var event = new InputEvent('input', {\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({"[object InputEvent]", "input", "false", "false", "false", ",,false"})
259     public void create_ctorNullData() throws Exception {
260         final String html = DOCTYPE_HTML
261             + "<html><head><script>\n"
262             + LOG_TITLE_FUNCTION
263             + "  function test() {\n"
264             + "    try {\n"
265             + "      var event = new InputEvent('input', null);\n"
266             + "      dump(event);\n"
267             + "    } catch(e) { logEx(e) }\n"
268             + "  }\n"
269             + DUMP_EVENT_FUNCTION
270             + "</script></head><body onload='test()'>\n"
271             + "</body></html>";
272 
273         loadPageVerifyTitle2(html);
274     }
275 
276     /**
277      * @throws Exception if the test fails
278      */
279     @Test
280     @Alerts({"[object InputEvent]", "input", "false", "false", "false", ",,false"})
281     public void create_ctorUndefinedData() throws Exception {
282         final String html = DOCTYPE_HTML
283             + "<html><head><script>\n"
284             + LOG_TITLE_FUNCTION
285             + "  function test() {\n"
286             + "    try {\n"
287             + "      var event = new InputEvent('input', undefined);\n"
288             + "      dump(event);\n"
289             + "    } catch(e) { logEx(e) }\n"
290             + "  }\n"
291             + DUMP_EVENT_FUNCTION
292             + "</script></head><body onload='test()'>\n"
293             + "</body></html>";
294 
295         loadPageVerifyTitle2(html);
296     }
297 
298     /**
299      * @throws Exception if the test fails
300      */
301     @Test
302     @Alerts({"[object InputEvent]", "input", "false", "false", "false", "Html,Unit,,false"})
303     public void create_ctorWrongData() throws Exception {
304         final String html = DOCTYPE_HTML
305             + "<html><head><script>\n"
306             + LOG_TITLE_FUNCTION
307             + "  function test() {\n"
308             + "    try {\n"
309             + "      var event = new InputEvent('input', {\n"
310             + "        'data': ['Html', 'Unit']\n"
311             + "      });\n"
312             + "      dump(event);\n"
313             + "    } catch(e) { logEx(e) }\n"
314             + "  }\n"
315             + DUMP_EVENT_FUNCTION
316             + "</script></head><body onload='test()'>\n"
317             + "</body></html>";
318 
319         loadPageVerifyTitle2(html);
320     }
321 }