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.junit.Test;
21 import org.junit.runner.RunWith;
22
23
24
25
26
27
28
29 @RunWith(BrowserRunner.class)
30 public class BeforeUnloadEventTest extends WebDriverTestCase {
31
32 private static final String DUMP_EVENT_FUNCTION = " function dump(event) {\n"
33 + " if (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.returnValue);\n"
40 + " } else {\n"
41 + " log('no event');\n"
42 + " }\n"
43 + " }\n";
44
45
46
47
48 @Test
49 @Alerts("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 BeforeUnloadEvent('beforeunload');\n"
57 + " } catch(e) { logEx(e) }\n"
58 + " }\n"
59 + "</script></head><body onload='test()'>\n"
60 + "</body></html>";
61
62 loadPageVerifyTitle2(html);
63 }
64
65
66
67
68 @Test
69 @Alerts({"[object BeforeUnloadEvent]", "", "false", "false", "false", ""})
70 public void create_createEvent() throws Exception {
71 final String html = DOCTYPE_HTML
72 + "<html><head><script>\n"
73 + LOG_TITLE_FUNCTION
74 + " function test() {\n"
75 + " try {\n"
76 + " var event = document.createEvent('BeforeUnloadEvent');\n"
77 + " dump(event);\n"
78 + " } catch(e) { logEx(e); }\n"
79 + " }\n"
80 + DUMP_EVENT_FUNCTION
81 + "</script></head><body onload='test()'>\n"
82 + "</body></html>";
83
84 loadPageVerifyTitle2(html);
85 }
86
87
88
89
90 @Test
91 @Alerts({"[object BeforeUnloadEvent]", "beforeunload", "true", "false", "false", ""})
92 public void initEvent() throws Exception {
93 final String html = DOCTYPE_HTML
94 + "<html><head><script>\n"
95 + LOG_TITLE_FUNCTION
96 + " function test() {\n"
97 + " try {\n"
98 + " var event = document.createEvent('BeforeUnloadEvent');\n"
99 + " event.initEvent('beforeunload', true, false);\n"
100 + " dump(event);\n"
101 + " } catch(e) { logEx(e) }\n"
102 + " }\n"
103 + DUMP_EVENT_FUNCTION
104 + "</script></head><body onload='test()'>\n"
105 + "</body></html>";
106
107 loadPageVerifyTitle2(html);
108 }
109
110
111
112
113 @Test
114 @Alerts({"[object BeforeUnloadEvent]", "beforeunload", "true", "false", "false", ""})
115 public void dispatchEvent() throws Exception {
116 final String html = DOCTYPE_HTML
117 + "<html><head><script>\n"
118 + LOG_TITLE_FUNCTION
119 + " function test() {\n"
120 + " try {\n"
121 + " var event = document.createEvent('BeforeUnloadEvent');\n"
122 + " event.initEvent('beforeunload', true, false);\n"
123 + " dispatchEvent(event);\n"
124 + " } catch(e) { logEx(e); }\n"
125 + " }\n"
126 + DUMP_EVENT_FUNCTION
127 + " window.onbeforeunload = dump;\n"
128 + "</script></head><body onload='test()'>\n"
129 + "</body></html>";
130
131 loadPageVerifyTitle2(html);
132 }
133
134
135
136
137 @Test
138 @Alerts({"[object Event]", "beforeunload", "true", "false", "false", "true"})
139 public void dispatchEvent_event() 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 = document.createEvent('Event');\n"
146 + " event.initEvent('beforeunload', true, false);\n"
147 + " dispatchEvent(event);\n"
148 + " } catch(e) { logEx(e); }\n"
149 + " }\n"
150 + DUMP_EVENT_FUNCTION
151 + " window.onbeforeunload = dump;\n"
152 + "</script></head><body onload='test()'>\n"
153 + "</body></html>";
154
155 loadPageVerifyTitle2(html);
156 }
157
158
159
160
161 @Test
162 @Alerts("supported")
163 public void onBeforeUnload_supported() throws Exception {
164 final String html = DOCTYPE_HTML
165 + "<html><head><script>\n"
166 + LOG_TITLE_FUNCTION
167 + " function test() {\n"
168 + " if ('onbeforeunload' in window) { log('supported') }\n"
169 + " }\n"
170 + "</script></head><body onload='test()'>\n"
171 + "</body></html>";
172
173 loadPageVerifyTitle2(html);
174 }
175 }