From 32f46e77764e3c051511007c619cabad21812633 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sun, 21 Oct 2018 16:01:32 +0100
Subject: [PATCH v1 2/2] SaveLayersDialogTest: fix for non-headless mode

---
 .../josm/gui/io/SaveLayersDialog.java              |  5 +-
 .../josm/gui/io/SaveLayersDialogTest.java          | 64 ++++++++++++++++++++++
 2 files changed, 65 insertions(+), 4 deletions(-)

diff --git a/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java b/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java
index 551a83efe..9f694ae09 100644
--- a/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java
+++ b/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java
@@ -275,10 +275,7 @@ public class SaveLayersDialog extends JDialog implements TableModelListener {
 
     private static void warn(String msg, List<SaveLayerInfo> infos, String title) {
         JPanel panel = new LayerListWarningMessagePanel(msg, infos);
-        // For unit test coverage in headless mode
-        if (!GraphicsEnvironment.isHeadless()) {
-            JOptionPane.showConfirmDialog(MainApplication.getMainFrame(), panel, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
-        }
+        JOptionPane.showConfirmDialog(MainApplication.getMainFrame(), panel, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
     }
 
     protected static void warnLayersWithConflictsAndUploadRequest(List<SaveLayerInfo> infos) {
diff --git a/test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java b/test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java
index 4eadf2ae4..383f9cd78 100644
--- a/test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java
+++ b/test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java
@@ -1,17 +1,23 @@
 // License: GPL. For details, see LICENSE file.
 package org.openstreetmap.josm.gui.io;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import java.util.Collections;
 import java.util.List;
+import javax.swing.JComponent;
+import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.JOptionPane;
 
 import org.junit.Rule;
 import org.junit.Test;
 import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
 import org.openstreetmap.josm.testutils.JOSMTestRules;
+import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
@@ -33,24 +39,82 @@ public class SaveLayersDialogTest {
     @Test
     public void testConfirmSaveLayerInfosOK() {
         final List<SaveLayerInfo> list = Collections.singletonList(new SaveLayerInfo(new OsmDataLayer(new DataSet(), null, null)));
+
+        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker() {
+            @Override
+            protected void act(final Object message) {
+                // use this opportunity to assert that our SaveLayerInfo is the single option in the JList
+                @SuppressWarnings("unchecked")
+                final JList<SaveLayerInfo> jList = (JList<SaveLayerInfo>) ((JComponent) message).getComponent(1);
+                assertEquals(1, jList.getModel().getSize());
+                assertEquals(list.get(0), jList.getModel().getElementAt(0));
+            }
+
+            @Override
+            protected String getStringFromMessage(final Object message) {
+                return ((JLabel) ((JComponent) message).getComponent(0)).getText();
+            }
+        };
+
+        jopsMocker.getMockResultMap().put(
+            "<html>1 layer has unresolved conflicts.<br>Either resolve them first or discard the "
+            + "modifications.<br>Layer with conflicts:</html>", JOptionPane.OK_OPTION
+        );
+
         assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
             @Override
             public List<SaveLayerInfo> getLayersWithConflictsAndUploadRequest() {
                 return list;
             }
         }));
+
+        assertEquals(1, jopsMocker.getInvocationLog().size());
+        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
+        assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
+        assertEquals("Unsaved data and conflicts", invocationLogEntry[2]);
+
+        jopsMocker.resetInvocationLog();
+        jopsMocker.getMockResultMap().clear();
+        jopsMocker.getMockResultMap().put(
+            "<html>1 layer needs saving but has no associated file.<br>Either select a file for this "
+            + "layer or discard the changes.<br>Layer without a file:</html>", JOptionPane.OK_OPTION
+        );
+
         assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
             @Override
             public List<SaveLayerInfo> getLayersWithoutFilesAndSaveRequest() {
                 return list;
             }
         }));
+
+        assertEquals(1, jopsMocker.getInvocationLog().size());
+        invocationLogEntry = jopsMocker.getInvocationLog().get(0);
+        assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
+        assertEquals("Unsaved data and missing associated file", invocationLogEntry[2]);
+
+        jopsMocker.resetInvocationLog();
+        jopsMocker.getMockResultMap().clear();
+        jopsMocker.getMockResultMap().put(
+            "<html>1 layer needs saving but has an associated file<br>which cannot be written.<br>Either "
+            + "select another file for this layer or discard the changes.<br>Layer with a non-writable "
+            + "file:</html>", JOptionPane.OK_OPTION
+        );
+
         assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
             @Override
             public List<SaveLayerInfo> getLayersWithIllegalFilesAndSaveRequest() {
                 return list;
             }
         }));
+
+        assertEquals(1, jopsMocker.getInvocationLog().size());
+        invocationLogEntry = jopsMocker.getInvocationLog().get(0);
+        assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
+        assertEquals("Unsaved data non-writable files", invocationLogEntry[2]);
+
+        jopsMocker.resetInvocationLog();
+        jopsMocker.getMockResultMap().clear();
+
         assertTrue(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel()));
     }
 }
-- 
2.11.0

