diff --git a/src/main/java/org/htmlunit/WebClient.java b/src/main/java/org/htmlunit/WebClient.java index c9dd8308e2c..640ac567b62 100644 --- a/src/main/java/org/htmlunit/WebClient.java +++ b/src/main/java/org/htmlunit/WebClient.java @@ -306,7 +306,7 @@ public WebClient(final BrowserVersion browserVersion, final boolean javaScriptEn loadQueue_ = new ArrayList<>(); // The window must be constructed AFTER the script engine. - currentWindowTracker_ = new CurrentWindowTracker(this); + currentWindowTracker_ = new CurrentWindowTracker(this, true); currentWindow_ = new TopLevelWindow("", this); initMSXMLActiveX(); @@ -2188,9 +2188,11 @@ public void setCache(final Cache cache) { */ private static final class CurrentWindowTracker implements WebWindowListener, Serializable { private final WebClient webClient_; + private final boolean ensureOneTopLevelWindow_; - CurrentWindowTracker(final WebClient webClient) { + CurrentWindowTracker(final WebClient webClient, final boolean ensureOneTopLevelWindow) { webClient_ = webClient; + ensureOneTopLevelWindow_ = ensureOneTopLevelWindow; } /** @@ -2225,6 +2227,10 @@ else if (window == webClient_.getCurrentWindow()) { * Postprocessing to make sure we have always one top level window open. */ public void afterWebWindowClosedListenersProcessed(final WebWindowEvent event) { + if (!ensureOneTopLevelWindow_) { + return; + } + if (webClient_.topLevelWindows_.isEmpty()) { // Must always have at least window, and there are no top-level windows left; must create one. final TopLevelWindow newWindow = new TopLevelWindow("", webClient_); @@ -2299,9 +2305,8 @@ public void close() { scriptEngine_.prepareShutdown(); } - // stop the CurrentWindowTracker - // the CurrentWindowTracker makes sure there is still one window available - currentWindowTracker_ = null; + // stop the CurrentWindowTracker from making sure there is still one window available + currentWindowTracker_ = new CurrentWindowTracker(this, false); // Hint: a new TopLevelWindow may be opened by some JS script while we are closing the others // but the prepareShutdown() call will prevent the new window form getting js support @@ -2312,7 +2317,6 @@ public void close() { try { topLevelWindow.close(true); - topLevelWindows_.remove(topLevelWindow); } catch (final Exception e) { LOG.error("Exception while closing a TopLevelWindow", e); @@ -2339,7 +2343,6 @@ else if (window instanceof DialogWindow) { try { topLevelWindow.close(true); - topLevelWindows_.remove(topLevelWindow); } catch (final Exception e) { LOG.error("Exception while closing a TopLevelWindow", e); @@ -2432,7 +2435,7 @@ public void reset() { initMSXMLActiveX(); // The window must be constructed AFTER the script engine. - currentWindowTracker_ = new CurrentWindowTracker(this); + currentWindowTracker_ = new CurrentWindowTracker(this, true); currentWindow_ = new TopLevelWindow("", this); }