public void doExecute() throws Exception { final Context context = getContext(); final TopLevelWindow window = (TopLevelWindow) context.getCurrentResponse().getEnclosingWindow().getTopWindow(); window.close(); }
/** * Regression test for bug 2808520: onbeforeunload not called when window.close() is called. * * @throws Exception if an error occurs */ @Test public void onBeforeUnloadCalledOnClose() throws Exception { final String html = "<html><body onbeforeunload='alert(7)'>abc</body></html>"; final List<String> alerts = new ArrayList<String>(); final HtmlPage page = loadPage(html, alerts); assertTrue(alerts.isEmpty()); final TopLevelWindow w = (TopLevelWindow) page.getEnclosingWindow(); w.close(); assertEquals(Arrays.asList("7"), alerts); }
/** * Tests closing the only open window. * * @throws Exception if the test fails */ @Test public void closeTheOnlyWindow() throws Exception { final WebClient webClient = new WebClient(); final EventCatcher eventCatcher = new EventCatcher(); eventCatcher.listenTo(webClient); final WebWindow windowToClose = webClient.getCurrentWindow(); ((TopLevelWindow) windowToClose).close(); final List<WebWindowEvent> expectedEvents = Arrays.asList(new WebWindowEvent(windowToClose, WebWindowEvent.CLOSE, null, null)); assertEquals(expectedEvents, eventCatcher.getEvents()); // Since this was the only open window, a new window should have // been created when this one was closed. Verify this. assertNotNull(webClient.getCurrentWindow()); assertNotSame(webClient.getCurrentWindow(), windowToClose); assertEquals(1, webClient.getWebWindows().size()); }