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 public class BeforeUnloadEventTest extends WebDriverTestCase {
28
29 private static final String DUMP_EVENT_FUNCTION = " function dump(event) {\n"
30 + " if (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 + " log(event.returnValue);\n"
37 + " } else {\n"
38 + " log('no event');\n"
39 + " }\n"
40 + " }\n";
41
42
43
44
45 @Test
46 @Alerts("TypeError")
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 BeforeUnloadEvent('beforeunload');\n"
54 + " } catch(e) { logEx(e) }\n"
55 + " }\n"
56 + "</script></head><body onload='test()'>\n"
57 + "</body></html>";
58
59 loadPageVerifyTitle2(html);
60 }
61
62
63
64
65 @Test
66 @Alerts({"[object BeforeUnloadEvent]", "", "false", "false", "false", ""})
67 public void create_createEvent() 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 = document.createEvent('BeforeUnloadEvent');\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({"[object BeforeUnloadEvent]", "beforeunload", "true", "false", "false", ""})
89 public void initEvent() 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 = document.createEvent('BeforeUnloadEvent');\n"
96 + " event.initEvent('beforeunload', true, false);\n"
97 + " dump(event);\n"
98 + " } catch(e) { logEx(e) }\n"
99 + " }\n"
100 + DUMP_EVENT_FUNCTION
101 + "</script></head><body onload='test()'>\n"
102 + "</body></html>";
103
104 loadPageVerifyTitle2(html);
105 }
106
107
108
109
110 @Test
111 @Alerts({"[object BeforeUnloadEvent]", "beforeunload", "true", "false", "false", ""})
112 public void dispatchEvent() throws Exception {
113 final String html = DOCTYPE_HTML
114 + "<html><head><script>\n"
115 + LOG_TITLE_FUNCTION
116 + " function test() {\n"
117 + " try {\n"
118 + " var event = document.createEvent('BeforeUnloadEvent');\n"
119 + " event.initEvent('beforeunload', true, false);\n"
120 + " dispatchEvent(event);\n"
121 + " } catch(e) { logEx(e); }\n"
122 + " }\n"
123 + DUMP_EVENT_FUNCTION
124 + " window.onbeforeunload = dump;\n"
125 + "</script></head><body onload='test()'>\n"
126 + "</body></html>";
127
128 loadPageVerifyTitle2(html);
129 }
130
131
132
133
134 @Test
135 @Alerts({"[object Event]", "beforeunload", "true", "false", "false", "true"})
136 public void dispatchEvent_event() throws Exception {
137 final String html = DOCTYPE_HTML
138 + "<html><head><script>\n"
139 + LOG_TITLE_FUNCTION
140 + " function test() {\n"
141 + " try {\n"
142 + " var event = document.createEvent('Event');\n"
143 + " event.initEvent('beforeunload', true, false);\n"
144 + " dispatchEvent(event);\n"
145 + " } catch(e) { logEx(e); }\n"
146 + " }\n"
147 + DUMP_EVENT_FUNCTION
148 + " window.onbeforeunload = dump;\n"
149 + "</script></head><body onload='test()'>\n"
150 + "</body></html>";
151
152 loadPageVerifyTitle2(html);
153 }
154
155
156
157
158 @Test
159 @Alerts("supported")
160 public void onBeforeUnload_supported() throws Exception {
161 final String html = DOCTYPE_HTML
162 + "<html><head><script>\n"
163 + LOG_TITLE_FUNCTION
164 + " function test() {\n"
165 + " if ('onbeforeunload' in window) { log('supported') }\n"
166 + " }\n"
167 + "</script></head><body onload='test()'>\n"
168 + "</body></html>";
169
170 loadPageVerifyTitle2(html);
171 }
172 }