From a9f6e53860adfdad655bf54cd6afced224d21bcd Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Mon, 15 Oct 2018 20:45:47 +0100
Subject: [PATCH v1] ChooseTrackVisibilityActionTest: fix for non-headless mode
 by properly mocking dialogs

---
 .../gui/layer/gpx/ChooseTrackVisibilityAction.java | 38 ++++++++++------------
 .../openstreetmap/josm/gui/layer/GpxLayerTest.java |  2 +-
 .../layer/gpx/ChooseTrackVisibilityActionTest.java | 24 ++++++++++++++
 3 files changed, 43 insertions(+), 21 deletions(-)

diff --git a/src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java b/src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java
index ee49c6eaf..fd9d8a909 100644
--- a/src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java
+++ b/src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java
@@ -262,26 +262,24 @@ public class ChooseTrackVisibilityAction extends AbstractAction {
         msg.add(scrollPane, GBC.eol().fill(GBC.BOTH));
 
         int v = 1;
-        if (!GraphicsEnvironment.isHeadless()) {
-            // build dialog
-            ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), tr("Set track visibility for {0}", layer.getName()),
-                    tr("Show all"), tr("Show selected only"), tr("Cancel"));
-            ed.setButtonIcons("eye", "dialogs/filter", "cancel");
-            ed.setContent(msg, false);
-            ed.setDefaultButton(2);
-            ed.setCancelButton(3);
-            ed.configureContextsensitiveHelp("/Action/ChooseTrackVisibility", true);
-            ed.setRememberWindowGeometry(getClass().getName() + ".geometry",
-                    WindowGeometry.centerInWindow(MainApplication.getMainFrame(), new Dimension(1000, 500)));
-            ed.showDialog();
-            dateFilter.saveInPrefs();
-            v = ed.getValue();
-            // cancel for unknown buttons and copy back original settings
-            if (v != 1 && v != 2) {
-                layer.trackVisibility = Arrays.copyOf(trackVisibilityBackup, layer.trackVisibility.length);
-                MainApplication.getMap().repaint();
-                return;
-            }
+        // build dialog
+        ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), tr("Set track visibility for {0}", layer.getName()),
+                tr("Show all"), tr("Show selected only"), tr("Cancel"));
+        ed.setButtonIcons("eye", "dialogs/filter", "cancel");
+        ed.setContent(msg, false);
+        ed.setDefaultButton(2);
+        ed.setCancelButton(3);
+        ed.configureContextsensitiveHelp("/Action/ChooseTrackVisibility", true);
+        ed.setRememberWindowGeometry(getClass().getName() + ".geometry",
+                WindowGeometry.centerInWindow(MainApplication.getMainFrame(), new Dimension(1000, 500)));
+        ed.showDialog();
+        dateFilter.saveInPrefs();
+        v = ed.getValue();
+        // cancel for unknown buttons and copy back original settings
+        if (v != 1 && v != 2) {
+            layer.trackVisibility = Arrays.copyOf(trackVisibilityBackup, layer.trackVisibility.length);
+            MainApplication.getMap().repaint();
+            return;
         }
         // set visibility (1 = show all, 2 = filter). If no tracks are selected
         // set all of them visible and...
diff --git a/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java b/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java
index 0cfc549e5..1688a8755 100644
--- a/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java
+++ b/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java
@@ -63,7 +63,7 @@ public class GpxLayerTest {
      * @throws SAXException if any SAX error occurs
      */
     public static GpxLayer getMinimalGpxLayer() throws IOException, SAXException {
-        return new GpxLayer(getMinimalGpxData());
+        return new GpxLayer(getMinimalGpxData(), "Bananas");
     }
 
     /**
diff --git a/test/unit/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityActionTest.java b/test/unit/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityActionTest.java
index 64636c0e3..4a5289b03 100644
--- a/test/unit/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityActionTest.java
+++ b/test/unit/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityActionTest.java
@@ -1,10 +1,17 @@
 // License: GPL. For details, see LICENSE file.
 package org.openstreetmap.josm.gui.layer.gpx;
 
+import static org.junit.Assert.assertEquals;
+
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
 import org.junit.Rule;
 import org.junit.Test;
+import org.openstreetmap.josm.gui.ExtendedDialog;
 import org.openstreetmap.josm.gui.layer.GpxLayerTest;
 import org.openstreetmap.josm.testutils.JOSMTestRules;
+import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
@@ -26,6 +33,23 @@ public class ChooseTrackVisibilityActionTest {
      */
     @Test
     public void testAction() throws Exception {
+        final ExtendedDialogMocker edMocker = new ExtendedDialogMocker() {
+            protected String getString(final ExtendedDialog instance) {
+                return ((JLabel) ((JPanel) this.getContent(instance)).getComponent(2)).getText();
+            }
+        };
+        edMocker.getMockResultMap().put(
+            "<html>Select all tracks that you want to be displayed. You can drag select a range of " +
+            "tracks or use CTRL+Click to select specific ones. The map is updated live in the " +
+            "background. Open the URLs by double clicking them.</html>",
+            "Show all"
+        );
+
         new ChooseTrackVisibilityAction(GpxLayerTest.getMinimalGpxLayer()).actionPerformed(null);
+
+        assertEquals(1, edMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
+        assertEquals(1, (int) invocationLogEntry[0]);
+        assertEquals("Set track visibility for Bananas", invocationLogEntry[2]);
     }
 }
-- 
2.11.0

