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 java.util.concurrent.atomic.AtomicBoolean;
18  
19  import org.htmlunit.WebServerTestCase;
20  import org.htmlunit.html.HtmlPage;
21  import org.htmlunit.junit.BrowserRunner;
22  import org.htmlunit.junit.annotation.Alerts;
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  
26  /**
27   * Tests for {@link BeforeUnloadEvent}.
28   *
29   * @author Ahmed Ashour
30   */
31  @RunWith(BrowserRunner.class)
32  public class BeforeUnloadEvent2Test extends WebServerTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts("Second")
39      public void nothing() throws Exception {
40          onbeforeunload("");
41      }
42  
43      /**
44       * @throws Exception if the test fails
45       */
46      @Test
47      @Alerts("First")
48      public void setString() throws Exception {
49          onbeforeunload("e.returnValue = 'Hello'");
50      }
51  
52      /**
53       * @throws Exception if the test fails
54       */
55      @Test
56      @Alerts("First")
57      public void setNull() throws Exception {
58          onbeforeunload("e.returnValue = null");
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts("First")
66      public void returnString() throws Exception {
67          onbeforeunload("return 'Hello'");
68      }
69  
70      /**
71       * @throws Exception if the test fails
72       */
73      @Test
74      @Alerts("Second")
75      public void returnNull() throws Exception {
76          onbeforeunload("return null");
77      }
78  
79      private void onbeforeunload(final String functionBody) throws Exception {
80          final String html = DOCTYPE_HTML
81              + "<html><head><title>First</title><script>\n"
82              + "  window.onbeforeunload = function (e) {\n"
83              + "    " + functionBody + ";\n"
84              + "  }\n"
85              + "</script></head><body>\n"
86              + "  <a href='" + URL_SECOND + "'>Click Here</a>\n"
87              + "</body></html>";
88  
89          final String html2 = DOCTYPE_HTML + "<html><head><title>Second</title></head><body></body></html>";
90          getMockWebConnection().setResponse(URL_SECOND, html2);
91  
92          final AtomicBoolean called = new AtomicBoolean();
93          getWebClient().setOnbeforeunloadHandler((page, event) -> {
94              called.set(true);
95              return false;
96          });
97          final HtmlPage page = loadPage(html);
98          final HtmlPage page2 = page.getAnchorByText("Click Here").click();
99  
100         final boolean expectedFirst = "First".equals(getExpectedAlerts()[0]);
101         if (expectedFirst) {
102             assertSame(page, page2);
103             assertTrue(called.get());
104         }
105         else {
106             assertNotSame(page, page2);
107             assertFalse(called.get());
108         }
109         assertEquals(getExpectedAlerts()[0], page2.getTitleText());
110     }
111 
112     /**
113      * @throws Exception if the test fails
114      */
115     @Test
116     @Alerts("First")
117     public void setNullReturnString() throws Exception {
118         onbeforeunload("e.returnValue = null;\n"
119                 + "return 'Hello'");
120     }
121 
122     /**
123      * @throws Exception if the test fails
124      */
125     @Test
126     @Alerts("First")
127     public void setStringReturnNull() throws Exception {
128         onbeforeunload("e.returnValue = 'Hello';\n"
129                 + "return null");
130     }
131 
132     /**
133      * @throws Exception if the test fails
134      */
135     @Test
136     @Alerts("First")
137     public void setNullReturnNull() throws Exception {
138         onbeforeunload("e.returnValue = null;\n"
139                 + "return null");
140     }
141 
142     /**
143      * @throws Exception if the test fails
144      */
145     @Test
146     @Alerts("First")
147     public void setStringReturnString() throws Exception {
148         onbeforeunload("e.returnValue = 'Hello';\n"
149                 + "return 'Hello'");
150     }
151 }