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.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link MutationEvent}.
25   *
26   * @author Ahmed Ashour
27   * @author Frank Danek
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class MutationEventTest extends WebDriverTestCase {
32  
33      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
34              + "    log(event);\n"
35              + "    log(event.type);\n"
36              + "    log(event.bubbles);\n"
37              + "    log(event.cancelable);\n"
38              + "    log(event.composed);\n"
39  
40              // TODO all properties
41              + "  }\n";
42  
43      /**
44       * @throws Exception if the test fails
45       */
46      @Test
47      @Alerts(DEFAULT = "ReferenceError",
48              FF = "TypeError",
49              FF_ESR = "TypeError")
50      public void create_ctor() throws Exception {
51          final String html = DOCTYPE_HTML
52              + "<html><head><script>\n"
53              + LOG_TITLE_FUNCTION
54              + "  function test() {\n"
55              + "    try {\n"
56              + "      var event = new MutationEvent('mutant');\n"
57              + "      dump(event);\n"
58              + "    } catch(e) { logEx(e) }\n"
59              + "  }\n"
60              + DUMP_EVENT_FUNCTION
61              + "</script></head><body onload='test()'>\n"
62              + "</body></html>";
63  
64          loadPageVerifyTitle2(html);
65      }
66  
67      /**
68       * @throws Exception if the test fails
69       */
70      @Test
71      @Alerts(DEFAULT = "ReferenceError",
72              FF = "TypeError",
73              FF_ESR = "TypeError")
74      public void create_ctorWithoutType() throws Exception {
75          final String html = DOCTYPE_HTML
76              + "<html><head><script>\n"
77              + LOG_TITLE_FUNCTION
78              + "  function test() {\n"
79              + "    try {\n"
80              + "      var event = new MutationEvent();\n"
81              + "      dump(event);\n"
82              + "    } catch(e) { logEx(e) }\n"
83              + "  }\n"
84              + DUMP_EVENT_FUNCTION
85              + "</script></head><body onload='test()'>\n"
86              + "</body></html>";
87  
88          loadPageVerifyTitle2(html);
89      }
90  
91      /**
92       * @throws Exception if the test fails
93       */
94      @Test
95      @Alerts(DEFAULT = "ReferenceError",
96              FF = "TypeError",
97              FF_ESR = "TypeError")
98      public void create_ctorNumericType() throws Exception {
99          final String html = DOCTYPE_HTML
100             + "<html><head><script>\n"
101             + LOG_TITLE_FUNCTION
102             + "  function test() {\n"
103             + "    try {\n"
104             + "      var event = new MutationEvent(42);\n"
105             + "      dump(event);\n"
106             + "    } catch(e) { logEx(e) }\n"
107             + "  }\n"
108             + DUMP_EVENT_FUNCTION
109             + "</script></head><body onload='test()'>\n"
110             + "</body></html>";
111 
112         loadPageVerifyTitle2(html);
113     }
114 
115     /**
116      * @throws Exception if the test fails
117      */
118     @Test
119     @Alerts(DEFAULT = "ReferenceError",
120             FF = "TypeError",
121             FF_ESR = "TypeError")
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 MutationEvent(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 MutationEvent(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(DEFAULT = "ReferenceError",
166             FF = "TypeError",
167             FF_ESR = "TypeError")
168     public void create_ctorArbitraryType() throws Exception {
169         final String html = DOCTYPE_HTML
170             + "<html><head><script>\n"
171             + LOG_TITLE_FUNCTION
172             + "  function test() {\n"
173             + "    try {\n"
174             + "      var event = new MutationEvent('HtmlUnitEvent');\n"
175             + "      dump(event);\n"
176             + "    } catch(e) { logEx(e) }\n"
177             + "  }\n"
178             + DUMP_EVENT_FUNCTION
179             + "</script></head><body onload='test()'>\n"
180             + "</body></html>";
181 
182         loadPageVerifyTitle2(html);
183     }
184 
185     /**
186      * @throws Exception if the test fails
187      */
188     @Test
189     @Alerts(DEFAULT = "ReferenceError",
190             FF = "TypeError",
191             FF_ESR = "TypeError")
192     public void create_ctorAllDetails() throws Exception {
193         final String html = DOCTYPE_HTML
194             + "<html><head><script>\n"
195             + LOG_TITLE_FUNCTION
196             + "  function test() {\n"
197             + "    try {\n"
198             + "      var event = new MutationEvent('mutant', {\n"
199             // + "        'data': 'mozart'\n"
200             + "      });\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(DEFAULT = "ReferenceError",
216             FF = "TypeError",
217             FF_ESR = "TypeError")
218     public void create_ctorAllDetailsMissingData() throws Exception {
219         final String html = DOCTYPE_HTML
220             + "<html><head><script>\n"
221             + LOG_TITLE_FUNCTION
222             + "  function test() {\n"
223             + "    try {\n"
224             + "      var event = new MutationEvent('mutant', {\n"
225             + "      });\n"
226             + "      dump(event);\n"
227             + "    } catch(e) { logEx(e) }\n"
228             + "  }\n"
229             + DUMP_EVENT_FUNCTION
230             + "</script></head><body onload='test()'>\n"
231             + "</body></html>";
232 
233         loadPageVerifyTitle2(html);
234     }
235 
236     /**
237      * @throws Exception if the test fails
238      */
239     @Test
240     @Alerts(DEFAULT = "ReferenceError",
241             FF = "TypeError",
242             FF_ESR = "TypeError")
243     public void create_ctorAllDetailsWrongData() throws Exception {
244         final String html = DOCTYPE_HTML
245             + "<html><head><script>\n"
246             + LOG_TITLE_FUNCTION
247             + "  function test() {\n"
248             + "    try {\n"
249             + "      var event = new MutationEvent('mutant', {\n"
250             + "        'data': ['Html', 'Unit']\n"
251             + "      });\n"
252             + "      dump(event);\n"
253             + "    } catch(e) { logEx(e) }\n"
254             + "  }\n"
255             + DUMP_EVENT_FUNCTION
256             + "</script></head><body onload='test()'>\n"
257             + "</body></html>";
258 
259         loadPageVerifyTitle2(html);
260     }
261 
262     /**
263      * @throws Exception if the test fails
264      */
265     @Test
266     @Alerts(DEFAULT = "false",
267             FF = "true",
268             FF_ESR = "true")
269     public void inWindow() throws Exception {
270         final String html = DOCTYPE_HTML
271             + "<html>\n"
272             + "<head>\n"
273             + "  <script>\n"
274             + LOG_TITLE_FUNCTION
275             + "    function test() {\n"
276             + "      log('MutationEvent' in window);\n"
277             + "    }\n"
278             + "  </script>\n"
279             + "</head>\n"
280             + "<body onload='test()'>\n"
281             + "</body>\n"
282             + "</html>";
283 
284         loadPageVerifyTitle2(html);
285     }
286 
287     /**
288      * @throws Exception if the test fails
289      */
290     @Test
291     @Alerts(DEFAULT = {"DOM2: exception", "DOM3: exception"},
292             FF = {"DOM2: [object MutationEvent]", "DOM3: [object MutationEvent]"},
293             FF_ESR = {"DOM2: [object MutationEvent]", "DOM3: [object MutationEvent]"})
294     public void createEvent() throws Exception {
295         final String html = DOCTYPE_HTML
296             + "<html><head><script>\n"
297             + "  function test() {\n"
298             + LOG_TITLE_FUNCTION
299             + "    try {\n"
300             + "      log('DOM2: ' + document.createEvent('MutationEvents'));\n"
301             + "    } catch(e) {log('DOM2: exception')}\n"
302             + "    try {\n"
303             + "      log('DOM3: ' + document.createEvent('MutationEvent'));\n"
304             + "    } catch(e) {log('DOM3: exception')}\n"
305             + "  }\n"
306             + "</script></head><body onload='test()'>\n"
307             + "</body></html>";
308         loadPageVerifyTitle2(html);
309     }
310 
311 }