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 CompositionEvent}.
24   *
25   * @author Ronald Brill
26   */
27  public class CompositionEventTest 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              + "    log(event.view == window);\n"
37  
38              + "    log(event.data);\n"
39              + "  }\n";
40  
41      /**
42       * @throws Exception if the test fails
43       */
44      @Test
45      @Alerts({"[object CompositionEvent]", "composition", "false", "false", "false", "false", ""})
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 CompositionEvent('composition');\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 CompositionEvent]", "undefined", "false", "false", "false", "false", ""},
69              EDGE = {"[object CompositionEvent]", "undefined", "false", "false", "false", "false", ""},
70              FF = {"[object CompositionEvent]", "undefined", "false", "false", "false", "false", ""},
71              FF_ESR = {"[object CompositionEvent]", "undefined", "false", "false", "false", "false", ""})
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 CompositionEvent();\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 CompositionEvent]", "42", "false", "false", "false", "false", ""})
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 CompositionEvent(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 CompositionEvent]", "null", "false", "false", "false", "false", ""})
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 CompositionEvent(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 CompositionEvent(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 CompositionEvent]", "HtmlUnitEvent", "false", "false", "false", "false", ""})
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 CompositionEvent('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 CompositionEvent]", "composition", "false", "false", "false", "false", "mozart"})
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 event = new CompositionEvent('composition', {\n"
189             + "        'data': 'mozart'\n"
190             + "      });\n"
191             + "      dump(event);\n"
192             + "    } catch(e) { logEx(e) }\n"
193             + "  }\n"
194             + DUMP_EVENT_FUNCTION
195             + "</script></head><body onload='test()'>\n"
196             + "</body></html>";
197 
198         loadPageVerifyTitle2(html);
199     }
200 
201     /**
202      * @throws Exception if the test fails
203      */
204     @Test
205     @Alerts({"[object CompositionEvent]", "composition", "false", "false", "false", "false", ""})
206     public void create_ctorAllDetailsMissingData() throws Exception {
207         final String html = DOCTYPE_HTML
208             + "<html><head><script>\n"
209             + LOG_TITLE_FUNCTION
210             + "  function test() {\n"
211             + "    try {\n"
212             + "      var event = new CompositionEvent('composition', {\n"
213             + "      });\n"
214             + "      dump(event);\n"
215             + "    } catch(e) { logEx(e) }\n"
216             + "  }\n"
217             + DUMP_EVENT_FUNCTION
218             + "</script></head><body onload='test()'>\n"
219             + "</body></html>";
220 
221         loadPageVerifyTitle2(html);
222     }
223 
224     /**
225      * @throws Exception if the test fails
226      */
227     @Test
228     @Alerts({"[object CompositionEvent]", "composition", "false", "false", "false", "false", "Html,Unit"})
229     public void create_ctorAllDetailsWrongData() throws Exception {
230         final String html = DOCTYPE_HTML
231             + "<html><head><script>\n"
232             + LOG_TITLE_FUNCTION
233             + "  function test() {\n"
234             + "    try {\n"
235             + "      var event = new CompositionEvent('composition', {\n"
236             + "        'data': ['Html', 'Unit']\n"
237             + "      });\n"
238             + "      dump(event);\n"
239             + "    } catch(e) { logEx(e) }\n"
240             + "  }\n"
241             + DUMP_EVENT_FUNCTION
242             + "</script></head><body onload='test()'>\n"
243             + "</body></html>";
244 
245         loadPageVerifyTitle2(html);
246     }
247 
248     /**
249      * @throws Exception if the test fails
250      */
251     @Test
252     @Alerts("true")
253     public void inWindow() throws Exception {
254         final String html = DOCTYPE_HTML
255             + "<html>\n"
256             + "<head>\n"
257             + "  <script>\n"
258             + LOG_TITLE_FUNCTION
259             + "    function test() {\n"
260             + "      log('CompositionEvent' in window);\n"
261             + "    }\n"
262             + "  </script>\n"
263             + "</head>\n"
264             + "<body onload='test()'>\n"
265             + "</body>\n"
266             + "</html>";
267 
268         loadPageVerifyTitle2(html);
269     }
270 }