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 CompositionEvent}.
26   *
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class CompositionEventTest 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              + "    log(event.view == window);\n"
40  
41              + "    log(event.data);\n"
42              + "  }\n";
43  
44      /**
45       * @throws Exception if the test fails
46       */
47      @Test
48      @Alerts({"[object CompositionEvent]", "composition", "false", "false", "false", "false", ""})
49      public void create_ctor() throws Exception {
50          final String html = DOCTYPE_HTML
51              + "<html><head><script>\n"
52              + LOG_TITLE_FUNCTION
53              + "  function test() {\n"
54              + "    try {\n"
55              + "      var event = new CompositionEvent('composition');\n"
56              + "      dump(event);\n"
57              + "    } catch(e) { logEx(e) }\n"
58              + "  }\n"
59              + DUMP_EVENT_FUNCTION
60              + "</script></head><body onload='test()'>\n"
61              + "</body></html>";
62  
63          loadPageVerifyTitle2(html);
64      }
65  
66      /**
67       * @throws Exception if the test fails
68       */
69      @Test
70      @Alerts("TypeError")
71      @HtmlUnitNYI(CHROME = {"[object CompositionEvent]", "undefined", "false", "false", "false", "false", ""},
72              EDGE = {"[object CompositionEvent]", "undefined", "false", "false", "false", "false", ""},
73              FF = {"[object CompositionEvent]", "undefined", "false", "false", "false", "false", ""},
74              FF_ESR = {"[object CompositionEvent]", "undefined", "false", "false", "false", "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 CompositionEvent();\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 CompositionEvent]", "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 CompositionEvent(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 CompositionEvent]", "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 CompositionEvent(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 CompositionEvent(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 CompositionEvent]", "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 CompositionEvent('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({"[object CompositionEvent]", "composition", "false", "false", "false", "false", "mozart"})
185     public void create_ctorAllDetails() throws Exception {
186         final String html = DOCTYPE_HTML
187             + "<html><head><script>\n"
188             + LOG_TITLE_FUNCTION
189             + "  function test() {\n"
190             + "    try {\n"
191             + "      var event = new CompositionEvent('composition', {\n"
192             + "        'data': 'mozart'\n"
193             + "      });\n"
194             + "      dump(event);\n"
195             + "    } catch(e) { logEx(e) }\n"
196             + "  }\n"
197             + DUMP_EVENT_FUNCTION
198             + "</script></head><body onload='test()'>\n"
199             + "</body></html>";
200 
201         loadPageVerifyTitle2(html);
202     }
203 
204     /**
205      * @throws Exception if the test fails
206      */
207     @Test
208     @Alerts({"[object CompositionEvent]", "composition", "false", "false", "false", "false", ""})
209     public void create_ctorAllDetailsMissingData() throws Exception {
210         final String html = DOCTYPE_HTML
211             + "<html><head><script>\n"
212             + LOG_TITLE_FUNCTION
213             + "  function test() {\n"
214             + "    try {\n"
215             + "      var event = new CompositionEvent('composition', {\n"
216             + "      });\n"
217             + "      dump(event);\n"
218             + "    } catch(e) { logEx(e) }\n"
219             + "  }\n"
220             + DUMP_EVENT_FUNCTION
221             + "</script></head><body onload='test()'>\n"
222             + "</body></html>";
223 
224         loadPageVerifyTitle2(html);
225     }
226 
227     /**
228      * @throws Exception if the test fails
229      */
230     @Test
231     @Alerts({"[object CompositionEvent]", "composition", "false", "false", "false", "false", "Html,Unit"})
232     public void create_ctorAllDetailsWrongData() throws Exception {
233         final String html = DOCTYPE_HTML
234             + "<html><head><script>\n"
235             + LOG_TITLE_FUNCTION
236             + "  function test() {\n"
237             + "    try {\n"
238             + "      var event = new CompositionEvent('composition', {\n"
239             + "        'data': ['Html', 'Unit']\n"
240             + "      });\n"
241             + "      dump(event);\n"
242             + "    } catch(e) { logEx(e) }\n"
243             + "  }\n"
244             + DUMP_EVENT_FUNCTION
245             + "</script></head><body onload='test()'>\n"
246             + "</body></html>";
247 
248         loadPageVerifyTitle2(html);
249     }
250 
251     /**
252      * @throws Exception if the test fails
253      */
254     @Test
255     @Alerts("true")
256     public void inWindow() throws Exception {
257         final String html = DOCTYPE_HTML
258             + "<html>\n"
259             + "<head>\n"
260             + "  <script>\n"
261             + LOG_TITLE_FUNCTION
262             + "    function test() {\n"
263             + "      log('CompositionEvent' in window);\n"
264             + "    }\n"
265             + "  </script>\n"
266             + "</head>\n"
267             + "<body onload='test()'>\n"
268             + "</body>\n"
269             + "</html>";
270 
271         loadPageVerifyTitle2(html);
272     }
273 }