From 98ddb963f200e9c0640aa900b40656f0aefd0261 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Mon, 26 Mar 2018 20:23:51 +0100
Subject: [PATCH v2 08/28] JOSMTestRules: attempt to close all windows left
 open after each test

---
 .../openstreetmap/josm/testutils/JOSMTestRules.java    | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
index 03d7cb3b8..c56aad0aa 100644
--- a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
+++ b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
@@ -2,11 +2,14 @@
 package org.openstreetmap.josm.testutils;
 
 import java.awt.Color;
+import java.awt.Window;
+import java.awt.event.WindowEvent;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.security.GeneralSecurityException;
 import java.text.MessageFormat;
+import java.util.Arrays;
 import java.util.TimeZone;
 
 import org.junit.rules.TemporaryFolder;
@@ -575,6 +578,21 @@ public class JOSMTestRules implements TestRule {
             TestUtils.setPrivateStaticField(Version.class, "instance", this.originalVersion);
         }
 
+        Window[] windows = Window.getWindows();
+        if (windows.length != 0) {
+            Logging.info(
+                "Attempting to close {0} windows left open by tests: {1}",
+                windows.length,
+                Arrays.toString(windows)
+            );
+        }
+        GuiHelper.runInEDTAndWait(() -> {
+            for (Window window : windows) {
+                window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
+                window.dispose();
+            }
+        });
+
         // Parts of JOSM uses weak references - destroy them.
         System.gc();
     }
-- 
2.11.0

