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.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
26
27
28
29
30 @RunWith(BrowserRunner.class)
31 public class CustomEventTest 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 + " log(event.detail);\n"
40 + " }\n";
41
42
43
44
45 @Test
46 @Alerts({"[object CustomEvent]", "my", "false", "false", "false", "null"})
47 public void create_ctor() throws Exception {
48 final String html = DOCTYPE_HTML
49 + "<html><head><script>\n"
50 + LOG_TITLE_FUNCTION
51 + " function test() {\n"
52 + " try {\n"
53 + " var event = new CustomEvent('my');\n"
54 + " dump(event);\n"
55 + " } catch(e) { logEx(e) }\n"
56 + " }\n"
57 + DUMP_EVENT_FUNCTION
58 + "</script></head><body onload='test()'>\n"
59 + "</body></html>";
60
61 loadPageVerifyTitle2(html);
62 }
63
64
65
66
67 @Test
68 @Alerts("TypeError")
69 @HtmlUnitNYI(CHROME = {"[object CustomEvent]", "undefined", "false", "false", "false", "null"},
70 EDGE = {"[object CustomEvent]", "undefined", "false", "false", "false", "null"},
71 FF = {"[object CustomEvent]", "undefined", "false", "false", "false", "null"},
72 FF_ESR = {"[object CustomEvent]", "undefined", "false", "false", "false", "null"})
73 public void create_ctorWithoutType() throws Exception {
74 final String html = DOCTYPE_HTML
75 + "<html><head><script>\n"
76 + LOG_TITLE_FUNCTION
77 + " function test() {\n"
78 + " try {\n"
79 + " var event = new CustomEvent();\n"
80 + " dump(event);\n"
81 + " } catch(e) { logEx(e) }\n"
82 + " }\n"
83 + DUMP_EVENT_FUNCTION
84 + "</script></head><body onload='test()'>\n"
85 + "</body></html>";
86
87 loadPageVerifyTitle2(html);
88 }
89
90
91
92
93 @Test
94 @Alerts({"[object CustomEvent]", "42", "false", "false", "false", "null"})
95 public void create_ctorNumericType() throws Exception {
96 final String html = DOCTYPE_HTML
97 + "<html><head><script>\n"
98 + LOG_TITLE_FUNCTION
99 + " function test() {\n"
100 + " try {\n"
101 + " var event = new CustomEvent(42);\n"
102 + " dump(event);\n"
103 + " } catch(e) { logEx(e) }\n"
104 + " }\n"
105 + DUMP_EVENT_FUNCTION
106 + "</script></head><body onload='test()'>\n"
107 + "</body></html>";
108
109 loadPageVerifyTitle2(html);
110 }
111
112
113
114
115 @Test
116 @Alerts({"[object CustomEvent]", "null", "false", "false", "false", "null"})
117 public void create_ctorNullType() throws Exception {
118 final String html = DOCTYPE_HTML
119 + "<html><head><script>\n"
120 + LOG_TITLE_FUNCTION
121 + " function test() {\n"
122 + " try {\n"
123 + " var event = new CustomEvent(null);\n"
124 + " dump(event);\n"
125 + " } catch(e) { logEx(e) }\n"
126 + " }\n"
127 + DUMP_EVENT_FUNCTION
128 + "</script></head><body onload='test()'>\n"
129 + "</body></html>";
130
131 loadPageVerifyTitle2(html);
132 }
133
134
135
136
137 @Test
138 @Alerts("ReferenceError")
139 public void create_ctorUnknownType() throws Exception {
140 final String html = DOCTYPE_HTML
141 + "<html><head><script>\n"
142 + LOG_TITLE_FUNCTION
143 + " function test() {\n"
144 + " try {\n"
145 + " var event = new CustomEvent(unknown);\n"
146 + " dump(event);\n"
147 + " } catch(e) { logEx(e) }\n"
148 + " }\n"
149 + DUMP_EVENT_FUNCTION
150 + "</script></head><body onload='test()'>\n"
151 + "</body></html>";
152
153 loadPageVerifyTitle2(html);
154 }
155
156
157
158
159 @Test
160 @Alerts({"[object CustomEvent]", "HtmlUnitEvent", "false", "false", "false", "null"})
161 public void create_ctorArbitraryType() throws Exception {
162 final String html = DOCTYPE_HTML
163 + "<html><head><script>\n"
164 + LOG_TITLE_FUNCTION
165 + " function test() {\n"
166 + " try {\n"
167 + " var event = new CustomEvent('HtmlUnitEvent');\n"
168 + " dump(event);\n"
169 + " } catch(e) { logEx(e) }\n"
170 + " }\n"
171 + DUMP_EVENT_FUNCTION
172 + "</script></head><body onload='test()'>\n"
173 + "</body></html>";
174
175 loadPageVerifyTitle2(html);
176 }
177
178
179
180
181 @Test
182 @Alerts({"[object CustomEvent]", "click", "false", "false", "false", "abcd"})
183 public void create_ctorAllDetails() throws Exception {
184 final String html = DOCTYPE_HTML
185 + "<html><head><script>\n"
186 + LOG_TITLE_FUNCTION
187 + " function test() {\n"
188 + " try {\n"
189 + " var event = new CustomEvent('click', {\n"
190 + " 'detail': 'abcd'"
191 + " });\n"
192 + " dump(event);\n"
193 + " } catch(e) { logEx(e) }\n"
194 + " }\n"
195 + DUMP_EVENT_FUNCTION
196 + "</script></head><body onload='test()'>\n"
197 + "</body></html>";
198
199 loadPageVerifyTitle2(html);
200 }
201
202
203
204
205 @Test
206 @Alerts("function")
207 public void initCustomEvent() throws Exception {
208 final String html = DOCTYPE_HTML
209 + "<html><head><script>\n"
210 + LOG_TITLE_FUNCTION
211 + " function test() {\n"
212 + " try {\n"
213 + " var e = document.createEvent('CustomEvent');\n"
214 + " log(typeof e.initCustomEvent);\n"
215 + " } catch(e) { logEx(e) }\n"
216 + " }\n"
217 + "</script></head><body onload='test()'>\n"
218 + "</body></html>";
219 loadPageVerifyTitle2(html);
220 }
221
222
223
224
225 @Test
226 @Alerts({"true", "details", "I was here"})
227 public void dispatchEvent() throws Exception {
228 final String html = DOCTYPE_HTML
229 + "<html><head>\n"
230 + "<script>\n"
231 + LOG_TITLE_FUNCTION
232 + "function test() {\n"
233 + " var listener = function(x) {\n"
234 + " log(x == myEvent);\n"
235 + " log(x.detail);\n"
236 + " x.foo = 'I was here';\n"
237 + " }\n"
238 + " document.addEventListener('HTMLImportsLoaded', listener);\n"
239
240 + " var myEvent = document.createEvent('CustomEvent');\n"
241 + " myEvent.initCustomEvent('HTMLImportsLoaded', true, true, 'details');\n"
242 + " document.dispatchEvent(myEvent);\n"
243 + " log(myEvent.foo);\n"
244 + "}\n"
245 + "</script>\n"
246 + "</head><body onload='test()'>\n"
247 + "</body></html>";
248
249 loadPageVerifyTitle2(html);
250 }
251
252
253
254
255 @Test
256 @Alerts({"true", "details", "I was here"})
257 public void dispatchEventOnDomText() throws Exception {
258 final String html = DOCTYPE_HTML
259 + "<html><head>\n"
260 + "<script>\n"
261 + LOG_TITLE_FUNCTION
262 + "function test() {\n"
263 + " var listener = function(x) {\n"
264 + " log(x == myEvent);\n"
265 + " log(x.detail);\n"
266 + " x.foo = 'I was here';\n"
267 + " }\n"
268 + " var txt = document.getElementById('myDiv').firstChild;\n"
269 + " txt.addEventListener('MyEvent', listener);\n"
270
271 + " var myEvent = document.createEvent('CustomEvent');\n"
272 + " myEvent.initCustomEvent('MyEvent', true, true, 'details');\n"
273 + " txt.dispatchEvent(myEvent);\n"
274 + " log(myEvent.foo);\n"
275 + "}\n"
276 + "</script>\n"
277 + "</head>\n"
278 + "<body onload='test()'>\n"
279 + " <div id='myDiv'>Hallo HtmlUnit</div>\n"
280 + "</body></html>";
281
282 loadPageVerifyTitle2(html);
283 }
284 }