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 CloseEventTest 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.code);\n"
37 + " log(event.reason);\n"
38 + " log(event.wasClean);\n"
39 + " } else {\n"
40 + " log('no event');\n"
41 + " }\n"
42 + " }\n";
43
44
45
46
47 @Test
48 @Alerts({"[object CloseEvent]", "type-close", "false", "false", "false", "0", "", "false"})
49 public void create_ctor() throws Exception {
50 final String html = DOCTYPE_HTML
51 + "<html><head><script>\n"
52 + LOG_TITLE_FUNCTION
53 + " function test() {\n"
54 + " try {\n"
55 + " var event = new CloseEvent('type-close');\n"
56 + " dump(event);\n"
57 + " } catch(e) { logEx(e) }\n"
58 + " }\n"
59 + DUMP_EVENT_FUNCTION
60 + "</script></head><body onload='test()'>\n"
61 + "</body></html>";
62
63 loadPageVerifyTitle2(html);
64 }
65
66
67
68
69 @Test
70 @Alerts({"[object CloseEvent]", "type-close", "true", "false", "false", "42", "test-reason", "true"})
71 public void create_ctorWithDetails() throws Exception {
72 final String html = DOCTYPE_HTML
73 + "<html><head><script>\n"
74 + LOG_TITLE_FUNCTION
75 + " function test() {\n"
76 + " try {\n"
77 + " var event = new CloseEvent('type-close', {\n"
78 + " 'bubbles': true,\n"
79 + " 'reason': 'test-reason',\n"
80 + " 'code': 42,\n"
81 + " 'wasClean': true\n"
82 + " });\n"
83 + " dump(event);\n"
84 + " } catch(e) { logEx(e) }\n"
85 + " }\n"
86 + DUMP_EVENT_FUNCTION
87 + "</script></head><body onload='test()'>\n"
88 + "</body></html>";
89
90 loadPageVerifyTitle2(html);
91 }
92
93
94
95
96 @Test
97 @Alerts(DEFAULT = {"[object CloseEvent]", "", "false", "false", "false", "0", "", "false"},
98 FF = "NotSupportedError/DOMException",
99 FF_ESR = "NotSupportedError/DOMException")
100 public void create_createEvent() throws Exception {
101 final String html = DOCTYPE_HTML
102 + "<html><head><script>\n"
103 + LOG_TITLE_FUNCTION
104 + " function test() {\n"
105 + " try {\n"
106 + " var event = document.createEvent('CloseEvent');\n"
107 + " dump(event);\n"
108 + " } catch(e) { logEx(e) }\n"
109 + " }\n"
110 + DUMP_EVENT_FUNCTION
111 + "</script></head><body onload='test()'>\n"
112 + "</body></html>";
113
114 loadPageVerifyTitle2(html);
115 }
116
117
118
119
120 @Test
121 @Alerts(DEFAULT = "no initCloseEvent",
122 FF = "NotSupportedError/DOMException",
123 FF_ESR = "NotSupportedError/DOMException")
124 public void initCloseEvent() throws Exception {
125 final String html = DOCTYPE_HTML
126 + "<html><head><script>\n"
127 + LOG_TITLE_FUNCTION
128 + " function test() {\n"
129 + " try {\n"
130 + " var event = document.createEvent('CloseEvent');\n"
131 + " if (event.initCloseEvent) {\n"
132 + " event.initCloseEvent('close', true, false, true, 42, 'time to close');\n"
133 + " dump(event);\n"
134 + " } else { log('no initCloseEvent'); }\n"
135 + " } catch(e) { logEx(e) }\n"
136 + " }\n"
137 + DUMP_EVENT_FUNCTION
138 + "</script></head><body onload='test()'>\n"
139 + "</body></html>";
140
141 loadPageVerifyTitle2(html);
142 }
143 }