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