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;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import javax.swing.Popup;
21  
22  import org.htmlunit.SimpleWebTestCase;
23  import org.htmlunit.html.HtmlElement;
24  import org.htmlunit.html.HtmlPage;
25  import org.htmlunit.junit.BrowserRunner;
26  import org.htmlunit.junit.annotation.Alerts;
27  import org.junit.Test;
28  import org.junit.runner.RunWith;
29  
30  /**
31   * Tests for {@link Popup}.
32   *
33   * @author Marc Guillemot
34   */
35  @RunWith(BrowserRunner.class)
36  public class PopupTest extends SimpleWebTestCase {
37  
38      /**
39       * Test that the opened window becomes the current one.
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts("Pop-up window is Open")
44      public void popupWindowBecomesCurrent() throws Exception {
45          final String content = DOCTYPE_HTML
46              + "<html><head><title>First</title><body>\n"
47              + "<span id='button' onClick='openPopup()'>Push me</span>\n"
48              + "<script>\n"
49              + "  function openPopup() {\n "
50              + "    window.open('', '_blank', 'width=640, height=600, scrollbars=yes');\n"
51              + "    alert('Pop-up window is Open');\n "
52              + "  }\n"
53              + "</script>\n"
54              + "</body></html>";
55  
56          final List<String> collectedAlerts = new ArrayList<>();
57          final HtmlPage page = loadPage(content, collectedAlerts);
58          final HtmlElement button = page.getHtmlElementById("button");
59  
60          final HtmlPage secondPage = button.click();
61          final String[] expectedAlerts = {"Pop-up window is Open"};
62          assertEquals(expectedAlerts, collectedAlerts);
63          assertEquals("about:blank", secondPage.getUrl());
64          assertSame(secondPage.getEnclosingWindow(), secondPage.getWebClient().getCurrentWindow());
65      }
66  }