View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for {@link BeforeUnloadEvent}.
25   *
26   * @author Frank Danek
27   * @author Ronald Brill
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       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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      * @throws Exception if the test fails
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      * @throws Exception if the test fails
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      * @throws Exception if the test fails
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 }