From e64efcd9734365927905ee327de78a7eba8e2cc7 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 13 Oct 2018 19:29:36 +0100
Subject: [PATCH v1 3/3] MergeLayerActionTest, DownloadWmsAlongTrackActionTest:
 fix for non-headless mode by properly mocking dialogs

---
 .../josm/actions/AbstractMergeAction.java          |  9 +----
 .../gui/layer/gpx/DownloadWmsAlongTrackAction.java |  4 +-
 .../josm/actions/MergeLayerActionTest.java         | 43 +++++++++++++++++++++-
 .../layer/gpx/DownloadWmsAlongTrackActionTest.java | 25 +++++++++++++
 4 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/src/org/openstreetmap/josm/actions/AbstractMergeAction.java b/src/org/openstreetmap/josm/actions/AbstractMergeAction.java
index ebcfdae9a..33b68f83b 100644
--- a/src/org/openstreetmap/josm/actions/AbstractMergeAction.java
+++ b/src/org/openstreetmap/josm/actions/AbstractMergeAction.java
@@ -109,10 +109,7 @@ public abstract class AbstractMergeAction extends JosmAction {
         JPanel pnl = new JPanel(new GridBagLayout());
         pnl.add(new JLabel(label), GBC.eol());
         pnl.add(layerList, GBC.eol().fill(GBC.HORIZONTAL));
-        if (GraphicsEnvironment.isHeadless()) {
-            // return first layer in headless mode, for unit tests
-            return targetLayers[0];
-        }
+
         ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), title, buttonText, tr("Cancel"));
         ed.setButtonIcons(buttonIcon, "cancel");
         ed.setContent(pnl);
@@ -130,8 +127,6 @@ public abstract class AbstractMergeAction extends JosmAction {
     protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) {
         String message = tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>",
                 Utils.escapeReservedCharactersHTML(sourceLayer.getName()));
-        if (!GraphicsEnvironment.isHeadless()) {
-            JOptionPane.showMessageDialog(MainApplication.getMainFrame(), message, tr("No target layers"), JOptionPane.WARNING_MESSAGE);
-        }
+        JOptionPane.showMessageDialog(MainApplication.getMainFrame(), message, tr("No target layers"), JOptionPane.WARNING_MESSAGE);
     }
 }
diff --git a/src/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackAction.java b/src/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackAction.java
index b9cc6cb48..9de3c676f 100644
--- a/src/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackAction.java
+++ b/src/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackAction.java
@@ -117,9 +117,7 @@ public class DownloadWmsAlongTrackAction extends AbstractAction {
     protected AbstractTileSourceLayer<? extends AbstractTMSTileSource> askedLayer() {
         List<AbstractTileSourceLayer> targetLayers = MainApplication.getLayerManager().getLayersOfType(AbstractTileSourceLayer.class);
         if (targetLayers.isEmpty()) {
-            if (!GraphicsEnvironment.isHeadless()) {
-                warnNoImageryLayers();
-            }
+            warnNoImageryLayers();
             return null;
         }
         return AbstractMergeAction.askTargetLayer(targetLayers.toArray(new AbstractTileSourceLayer[0]),
diff --git a/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java b/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java
index 77548165a..7c9a28b27 100644
--- a/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java
+++ b/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java
@@ -1,6 +1,9 @@
 // License: GPL. For details, see LICENSE file.
 package org.openstreetmap.josm.actions;
 
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 
@@ -8,10 +11,17 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.gui.ExtendedDialog;
 import org.openstreetmap.josm.gui.MainApplication;
 import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
+import org.openstreetmap.josm.gui.widgets.JosmComboBox;
+import org.openstreetmap.josm.TestUtils;
 import org.openstreetmap.josm.testutils.JOSMTestRules;
+import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
+import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
+
+import com.google.common.collect.ImmutableMap;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
@@ -27,6 +37,18 @@ public class MergeLayerActionTest {
     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     public JOSMTestRules test = new JOSMTestRules().main();
 
+    public static class MergeLayerExtendedDialogMocker extends ExtendedDialogMocker {
+        @Override
+        protected void act(final ExtendedDialog instance) {
+            ((JosmComboBox) ((JPanel) this.getContent(instance)).getComponent(1)).setSelectedIndex(0);
+        }
+
+        @Override
+        protected String getString(final ExtendedDialog instance) {
+            return ((JLabel) ((JPanel) this.getContent(instance)).getComponent(0)).getText();
+        }
+    };
+
     private MergeLayerAction action;
 
     /**
@@ -57,11 +79,21 @@ public class MergeLayerActionTest {
      */
     @Test
     public void testMergeNoTargetLayer() {
-        OsmDataLayer layer = new OsmDataLayer(new DataSet(), "", null);
+        TestUtils.assumeWorkingJMockit();
+        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
+            ImmutableMap.<String, Object>of("<html>There are no layers the source layer<br>'onion'<br>could be merged to.</html>", 0)
+        );
+
+        OsmDataLayer layer = new OsmDataLayer(new DataSet(), "onion", null);
         MainApplication.getLayerManager().addLayer(layer);
         assertEquals(1, MainApplication.getLayerManager().getLayers().size());
         assertNull(action.merge(layer));
         assertEquals(1, MainApplication.getLayerManager().getLayers().size());
+
+        assertEquals(1, jopsMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
+        assertEquals(0, (int) invocationLogEntry[0]);
+        assertEquals("No target layers", invocationLogEntry[2]);
     }
 
     /**
@@ -70,6 +102,10 @@ public class MergeLayerActionTest {
      */
     @Test
     public void testMergeTwoEmptyLayers() throws Exception {
+        TestUtils.assumeWorkingJMockit();
+        final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker();
+        edMocker.getMockResultMap().put("Please select the target layer.", "Merge");
+
         OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), "1", null);
         OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "2", null);
         MainApplication.getLayerManager().addLayer(layer1);
@@ -77,5 +113,10 @@ public class MergeLayerActionTest {
         assertEquals(2, MainApplication.getLayerManager().getLayers().size());
         action.merge(layer2).get();
         assertEquals(1, MainApplication.getLayerManager().getLayers().size());
+
+        assertEquals(1, edMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
+        assertEquals(1, (int) invocationLogEntry[0]);
+        assertEquals("Select target layer", invocationLogEntry[2]);
     }
 }
diff --git a/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java b/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java
index 637036c93..8c4ab71ea 100644
--- a/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java
+++ b/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java
@@ -2,6 +2,7 @@
 package org.openstreetmap.josm.gui.layer.gpx;
 
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -9,13 +10,18 @@ import static org.junit.Assert.assertTrue;
 import org.awaitility.Awaitility;
 import org.junit.Rule;
 import org.junit.Test;
+import org.openstreetmap.josm.actions.MergeLayerActionTest.MergeLayerExtendedDialogMocker;
 import org.openstreetmap.josm.data.gpx.GpxData;
 import org.openstreetmap.josm.gui.MainApplication;
 import org.openstreetmap.josm.gui.layer.GpxLayerTest;
 import org.openstreetmap.josm.gui.layer.TMSLayer;
 import org.openstreetmap.josm.gui.layer.gpx.DownloadWmsAlongTrackAction.PrecacheWmsTask;
+import org.openstreetmap.josm.TestUtils;
 import org.openstreetmap.josm.testutils.JOSMTestRules;
 import org.openstreetmap.josm.testutils.TileSourceRule;
+import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
+
+import com.google.common.collect.ImmutableMap;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
@@ -36,7 +42,17 @@ public class DownloadWmsAlongTrackActionTest {
      */
     @Test
     public void testNoLayer() {
+        TestUtils.assumeWorkingJMockit();
+        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
+            ImmutableMap.<String, Object>of("There are no imagery layers.", 0)
+        );
+
         assertNull(new DownloadWmsAlongTrackAction(new GpxData()).createTask());
+
+        assertEquals(1, jopsMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
+        assertEquals(0, (int) invocationLogEntry[0]);
+        assertEquals("No imagery layers", invocationLogEntry[2]);
     }
 
     /**
@@ -45,6 +61,10 @@ public class DownloadWmsAlongTrackActionTest {
      */
     @Test
     public void testTMSLayer() throws Exception {
+        TestUtils.assumeWorkingJMockit();
+        final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker();
+        edMocker.getMockResultMap().put("Please select the imagery layer.", "Download");
+
         final TileSourceRule tileSourceRule = this.test.getTileSourceRule();
 
         final TMSLayer layer = new TMSLayer(
@@ -64,5 +84,10 @@ public class DownloadWmsAlongTrackActionTest {
             // Ensure we clean the place before leaving, even if test fails.
             MainApplication.getLayerManager().removeLayer(layer);
         }
+
+        assertEquals(1, edMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
+        assertEquals(1, (int) invocationLogEntry[0]);
+        assertEquals("Select imagery layer", invocationLogEntry[2]);
     }
 }
-- 
2.11.0

