1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.event;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.annotation.Alerts;
19 import org.junit.jupiter.api.Test;
20
21
22
23
24
25
26
27
28 public class MutationEventTest extends WebDriverTestCase {
29
30 private static final String DUMP_EVENT_FUNCTION = " function dump(event) {\n"
31 + " log(event);\n"
32 + " log(event.type);\n"
33 + " log(event.bubbles);\n"
34 + " log(event.cancelable);\n"
35 + " log(event.composed);\n"
36
37
38 + " }\n";
39
40
41
42
43 @Test
44 @Alerts("ReferenceError")
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 MutationEvent('mutant');\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
64
65 @Test
66 @Alerts("ReferenceError")
67 public void create_ctorWithoutType() throws Exception {
68 final String html = DOCTYPE_HTML
69 + "<html><head><script>\n"
70 + LOG_TITLE_FUNCTION
71 + " function test() {\n"
72 + " try {\n"
73 + " var event = new MutationEvent();\n"
74 + " dump(event);\n"
75 + " } catch(e) { logEx(e) }\n"
76 + " }\n"
77 + DUMP_EVENT_FUNCTION
78 + "</script></head><body onload='test()'>\n"
79 + "</body></html>";
80
81 loadPageVerifyTitle2(html);
82 }
83
84
85
86
87 @Test
88 @Alerts("ReferenceError")
89 public void create_ctorNumericType() throws Exception {
90 final String html = DOCTYPE_HTML
91 + "<html><head><script>\n"
92 + LOG_TITLE_FUNCTION
93 + " function test() {\n"
94 + " try {\n"
95 + " var event = new MutationEvent(42);\n"
96 + " dump(event);\n"
97 + " } catch(e) { logEx(e) }\n"
98 + " }\n"
99 + DUMP_EVENT_FUNCTION
100 + "</script></head><body onload='test()'>\n"
101 + "</body></html>";
102
103 loadPageVerifyTitle2(html);
104 }
105
106
107
108
109 @Test
110 @Alerts("ReferenceError")
111 public void create_ctorNullType() throws Exception {
112 final String html = DOCTYPE_HTML
113 + "<html><head><script>\n"
114 + LOG_TITLE_FUNCTION
115 + " function test() {\n"
116 + " try {\n"
117 + " var event = new MutationEvent(null);\n"
118 + " dump(event);\n"
119 + " } catch(e) { logEx(e) }\n"
120 + " }\n"
121 + DUMP_EVENT_FUNCTION
122 + "</script></head><body onload='test()'>\n"
123 + "</body></html>";
124
125 loadPageVerifyTitle2(html);
126 }
127
128
129
130
131 @Test
132 @Alerts("ReferenceError")
133 public void create_ctorUnknownType() throws Exception {
134 final String html = DOCTYPE_HTML
135 + "<html><head><script>\n"
136 + LOG_TITLE_FUNCTION
137 + " function test() {\n"
138 + " try {\n"
139 + " var event = new MutationEvent(unknown);\n"
140 + " dump(event);\n"
141 + " } catch(e) { logEx(e) }\n"
142 + " }\n"
143 + DUMP_EVENT_FUNCTION
144 + "</script></head><body onload='test()'>\n"
145 + "</body></html>";
146
147 loadPageVerifyTitle2(html);
148 }
149
150
151
152
153 @Test
154 @Alerts("ReferenceError")
155 public void create_ctorArbitraryType() throws Exception {
156 final String html = DOCTYPE_HTML
157 + "<html><head><script>\n"
158 + LOG_TITLE_FUNCTION
159 + " function test() {\n"
160 + " try {\n"
161 + " var event = new MutationEvent('HtmlUnitEvent');\n"
162 + " dump(event);\n"
163 + " } catch(e) { logEx(e) }\n"
164 + " }\n"
165 + DUMP_EVENT_FUNCTION
166 + "</script></head><body onload='test()'>\n"
167 + "</body></html>";
168
169 loadPageVerifyTitle2(html);
170 }
171
172
173
174
175 @Test
176 @Alerts("ReferenceError")
177 public void create_ctorAllDetails() throws Exception {
178 final String html = DOCTYPE_HTML
179 + "<html><head><script>\n"
180 + LOG_TITLE_FUNCTION
181 + " function test() {\n"
182 + " try {\n"
183 + " var event = new MutationEvent('mutant', {\n"
184
185 + " });\n"
186 + " dump(event);\n"
187 + " } catch(e) { logEx(e) }\n"
188 + " }\n"
189 + DUMP_EVENT_FUNCTION
190 + "</script></head><body onload='test()'>\n"
191 + "</body></html>";
192
193 loadPageVerifyTitle2(html);
194 }
195
196
197
198
199 @Test
200 @Alerts("ReferenceError")
201 public void create_ctorAllDetailsMissingData() throws Exception {
202 final String html = DOCTYPE_HTML
203 + "<html><head><script>\n"
204 + LOG_TITLE_FUNCTION
205 + " function test() {\n"
206 + " try {\n"
207 + " var event = new MutationEvent('mutant', {\n"
208 + " });\n"
209 + " dump(event);\n"
210 + " } catch(e) { logEx(e) }\n"
211 + " }\n"
212 + DUMP_EVENT_FUNCTION
213 + "</script></head><body onload='test()'>\n"
214 + "</body></html>";
215
216 loadPageVerifyTitle2(html);
217 }
218
219
220
221
222 @Test
223 @Alerts("ReferenceError")
224 public void create_ctorAllDetailsWrongData() throws Exception {
225 final String html = DOCTYPE_HTML
226 + "<html><head><script>\n"
227 + LOG_TITLE_FUNCTION
228 + " function test() {\n"
229 + " try {\n"
230 + " var event = new MutationEvent('mutant', {\n"
231 + " 'data': ['Html', 'Unit']\n"
232 + " });\n"
233 + " dump(event);\n"
234 + " } catch(e) { logEx(e) }\n"
235 + " }\n"
236 + DUMP_EVENT_FUNCTION
237 + "</script></head><body onload='test()'>\n"
238 + "</body></html>";
239
240 loadPageVerifyTitle2(html);
241 }
242
243
244
245
246 @Test
247 @Alerts("false")
248 public void inWindow() throws Exception {
249 final String html = DOCTYPE_HTML
250 + "<html>\n"
251 + "<head>\n"
252 + " <script>\n"
253 + LOG_TITLE_FUNCTION
254 + " function test() {\n"
255 + " log('MutationEvent' in window);\n"
256 + " }\n"
257 + " </script>\n"
258 + "</head>\n"
259 + "<body onload='test()'>\n"
260 + "</body>\n"
261 + "</html>";
262
263 loadPageVerifyTitle2(html);
264 }
265
266
267
268
269 @Test
270 @Alerts(DEFAULT = {"DOM2: exception", "DOM3: exception"},
271 FF_ESR = {"DOM2: [object MutationEvent]", "DOM3: [object MutationEvent]"})
272 public void createEvent() throws Exception {
273 final String html = DOCTYPE_HTML
274 + "<html><head><script>\n"
275 + " function test() {\n"
276 + LOG_TITLE_FUNCTION
277 + " try {\n"
278 + " log('DOM2: ' + document.createEvent('MutationEvents'));\n"
279 + " } catch(e) {log('DOM2: exception')}\n"
280 + " try {\n"
281 + " log('DOM3: ' + document.createEvent('MutationEvent'));\n"
282 + " } catch(e) {log('DOM3: exception')}\n"
283 + " }\n"
284 + "</script></head><body onload='test()'>\n"
285 + "</body></html>";
286 loadPageVerifyTitle2(html);
287 }
288
289
290
291
292 @Test
293 @Alerts({"false", "undefined", "ReferenceError", "ReferenceError", "ReferenceError",
294 "false", "undefined", "ReferenceError", "ReferenceError", "ReferenceError"})
295 public void windowScope() throws Exception {
296 final String html = DOCTYPE_HTML
297 + "<html></body>\n"
298 + "<script>\n"
299 + LOG_TITLE_FUNCTION
300 + " log('mutationEvent' in window);\n"
301 + " log(window.mutationEvent);\n"
302 + " try { log(mutationEvent); } catch(e) { logEx(e); };\n"
303 + " try { log(mutationEvent.prototype); } catch(e) { logEx(e); };\n"
304 + " try { log(mutationEvent.__proto__); } catch(e) { logEx(e); };\n"
305
306 + " log('MutationEvent' in window);\n"
307 + " log(window.MutationEvent);\n"
308 + " try { log(MutationEvent); } catch(e) { logEx(e); };\n"
309 + " try { log(MutationEvent.prototype); } catch(e) { logEx(e); };\n"
310 + " try { log(MutationEvent.__proto__); } catch(e) { logEx(e); };\n"
311 + "</script>\n"
312 + "</body></html>";
313
314 loadPageVerifyTitle2(html);
315 }
316 }