From 3db488ff6c7b370f554988969fefe869998bbbb9 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 25 Nov 2017 20:38:52 +0000
Subject: [PATCH v1 5/6] MinimapDialogTest: perform event-related operations in
 EDT thread

---
 .../josm/gui/dialogs/MinimapDialogTest.java        | 74 +++++++++++++++-------
 1 file changed, 50 insertions(+), 24 deletions(-)

diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
index 708685fa3..4ef49923a 100644
--- a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
+++ b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
@@ -24,6 +24,7 @@ import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.TestUtils;
 import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
 import org.openstreetmap.josm.gui.bbox.SourceButton;
+import org.openstreetmap.josm.gui.util.GuiHelper;
 import org.openstreetmap.josm.testutils.JOSMTestRules;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -54,6 +55,21 @@ public class MinimapDialogTest {
         assertFalse(dlg.isVisible());
     }
 
+    @FunctionalInterface
+    protected interface ThrowingRunnable {
+        void run() throws Throwable;
+    }
+
+    protected static Runnable uncheckExceptions(final ThrowingRunnable tr) {
+        return (() -> {
+            try {
+                tr.run();
+            } catch (Throwable e) {
+                throw new RuntimeException(e);
+            }
+        });
+    }
+
     protected void assertSingleSelectedSourceLabel(final String label) {
         JPopupMenu menu = this.sourceButton.getPopupMenu();
         boolean found = false;
@@ -73,18 +89,26 @@ public class MinimapDialogTest {
         assertTrue("Selected source not found in menu", found);
     }
 
-    protected JMenuItem getSourceMenuItemByLabel(final String label) {
-        JPopupMenu menu = this.sourceButton.getPopupMenu();
-        for (Component c: menu.getComponents()) {
-            if (JPopupMenu.Separator.class.isInstance(c)) {
-                break;
-            } else if (((JMenuItem) c).getText() == label) {
-                return (JMenuItem) c;
-            }
-            // else continue...
+    protected void clickSourceMenuItemByLabel(final String label) {
+        try {
+            GuiHelper.runInEDTAndWaitWithException(() -> {
+                JPopupMenu menu = this.sourceButton.getPopupMenu();
+                for (Component c: menu.getComponents()) {
+                    if (JPopupMenu.Separator.class.isInstance(c)) {
+                        // sources should all come before any separators
+                        break;
+                    } else if (((JMenuItem) c).getText() == label) {
+                        ((JMenuItem) c).doClick();
+                        return;
+                    }
+                    // else continue...
+                }
+                fail();
+            });
+        } catch (Throwable e) {
+            // need to turn this *back* into an AssertionFailedError
+            fail(String.format("Failed to find menu item with label %s: %s", label, e));
         }
-        fail("Failed to find menu item with label " + label);
-        return null;
     }
 
     protected MinimapDialog minimap;
@@ -94,18 +118,20 @@ public class MinimapDialogTest {
 
     protected static BufferedImage paintedSlippyMap;
 
-    protected void setUpMiniMap() throws Exception {
-        this.minimap = new MinimapDialog();
-        this.minimap.setSize(300, 200);
-        this.minimap.showDialog();
-        this.slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(this.minimap, "slippyMap");
-        this.sourceButton = (SourceButton) TestUtils.getPrivateField(this.slippyMap, "iSourceButton");
+    protected void setUpMiniMap() {
+        GuiHelper.runInEDTAndWaitWithException(uncheckExceptions(() -> {
+            this.minimap = new MinimapDialog();
+            this.minimap.setSize(300, 200);
+            this.minimap.showDialog();
+            this.slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(this.minimap, "slippyMap");
+            this.sourceButton = (SourceButton) TestUtils.getPrivateField(this.slippyMap, "iSourceButton");
 
-        this.slippyMapTasksFinished = () -> !this.slippyMap.getTileController().getTileLoader().hasOutstandingTasks();
+            // get minimap in a paintable state
+            this.minimap.addNotify();
+            this.minimap.doLayout();
+        }));
 
-        // get minimap in a paintable state
-        this.minimap.addNotify();
-        this.minimap.doLayout();
+        this.slippyMapTasksFinished = () -> !this.slippyMap.getTileController().getTileLoader().hasOutstandingTasks();
     }
 
     protected void paintSlippyMap() {
@@ -152,7 +178,7 @@ public class MinimapDialogTest {
 
         this.assertSingleSelectedSourceLabel("White Tiles");
 
-        this.getSourceMenuItemByLabel("Magenta Tiles").doClick();
+        this.clickSourceMenuItemByLabel("Magenta Tiles");
         this.assertSingleSelectedSourceLabel("Magenta Tiles");
         // call paint to trigger new tile fetch
         this.paintSlippyMap();
@@ -163,7 +189,7 @@ public class MinimapDialogTest {
 
         assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0));
 
-        this.getSourceMenuItemByLabel("Green Tiles").doClick();
+        this.clickSourceMenuItemByLabel("Green Tiles");
         this.assertSingleSelectedSourceLabel("Green Tiles");
         // call paint to trigger new tile fetch
         this.paintSlippyMap();
@@ -198,7 +224,7 @@ public class MinimapDialogTest {
 
         assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0));
 
-        this.getSourceMenuItemByLabel("Magenta Tiles").doClick();
+        this.clickSourceMenuItemByLabel("Magenta Tiles");
         this.assertSingleSelectedSourceLabel("Magenta Tiles");
 
         assertEquals("Magenta Tiles", Main.pref.get("slippy_map_chooser.mapstyle", "Fail"));
-- 
2.11.0

