Ticket #16878: v1-0002-SaveLayersDialogTest-fix-for-non-headless-mode.patch

File v1-0002-SaveLayersDialogTest-fix-for-non-headless-mode.patch, 6.3 KB (added by ris, 7 years ago)
  • src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

    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 b public class SaveLayersDialog extends JDialog implements TableModelListener {  
    275275
    276276    private static void warn(String msg, List<SaveLayerInfo> infos, String title) {
    277277        JPanel panel = new LayerListWarningMessagePanel(msg, infos);
    278         // For unit test coverage in headless mode
    279         if (!GraphicsEnvironment.isHeadless()) {
    280             JOptionPane.showConfirmDialog(MainApplication.getMainFrame(), panel, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
    281         }
     278        JOptionPane.showConfirmDialog(MainApplication.getMainFrame(), panel, title, JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
    282279    }
    283280
    284281    protected static void warnLayersWithConflictsAndUploadRequest(List<SaveLayerInfo> infos) {
  • test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java

    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 b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.io;
    33
     4import static org.junit.Assert.assertEquals;
    45import static org.junit.Assert.assertFalse;
    56import static org.junit.Assert.assertTrue;
    67
    78import java.util.Collections;
    89import java.util.List;
     10import javax.swing.JComponent;
     11import javax.swing.JLabel;
     12import javax.swing.JList;
     13import javax.swing.JOptionPane;
    914
    1015import org.junit.Rule;
    1116import org.junit.Test;
    1217import org.openstreetmap.josm.data.osm.DataSet;
    1318import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1419import org.openstreetmap.josm.testutils.JOSMTestRules;
     20import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
    1521
    1622import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1723
    public class SaveLayersDialogTest {  
    3339    @Test
    3440    public void testConfirmSaveLayerInfosOK() {
    3541        final List<SaveLayerInfo> list = Collections.singletonList(new SaveLayerInfo(new OsmDataLayer(new DataSet(), null, null)));
     42
     43        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker() {
     44            @Override
     45            protected void act(final Object message) {
     46                // use this opportunity to assert that our SaveLayerInfo is the single option in the JList
     47                @SuppressWarnings("unchecked")
     48                final JList<SaveLayerInfo> jList = (JList<SaveLayerInfo>) ((JComponent) message).getComponent(1);
     49                assertEquals(1, jList.getModel().getSize());
     50                assertEquals(list.get(0), jList.getModel().getElementAt(0));
     51            }
     52
     53            @Override
     54            protected String getStringFromMessage(final Object message) {
     55                return ((JLabel) ((JComponent) message).getComponent(0)).getText();
     56            }
     57        };
     58
     59        jopsMocker.getMockResultMap().put(
     60            "<html>1 layer has unresolved conflicts.<br>Either resolve them first or discard the "
     61            + "modifications.<br>Layer with conflicts:</html>", JOptionPane.OK_OPTION
     62        );
     63
    3664        assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
    3765            @Override
    3866            public List<SaveLayerInfo> getLayersWithConflictsAndUploadRequest() {
    3967                return list;
    4068            }
    4169        }));
     70
     71        assertEquals(1, jopsMocker.getInvocationLog().size());
     72        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
     73        assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
     74        assertEquals("Unsaved data and conflicts", invocationLogEntry[2]);
     75
     76        jopsMocker.resetInvocationLog();
     77        jopsMocker.getMockResultMap().clear();
     78        jopsMocker.getMockResultMap().put(
     79            "<html>1 layer needs saving but has no associated file.<br>Either select a file for this "
     80            + "layer or discard the changes.<br>Layer without a file:</html>", JOptionPane.OK_OPTION
     81        );
     82
    4283        assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
    4384            @Override
    4485            public List<SaveLayerInfo> getLayersWithoutFilesAndSaveRequest() {
    4586                return list;
    4687            }
    4788        }));
     89
     90        assertEquals(1, jopsMocker.getInvocationLog().size());
     91        invocationLogEntry = jopsMocker.getInvocationLog().get(0);
     92        assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
     93        assertEquals("Unsaved data and missing associated file", invocationLogEntry[2]);
     94
     95        jopsMocker.resetInvocationLog();
     96        jopsMocker.getMockResultMap().clear();
     97        jopsMocker.getMockResultMap().put(
     98            "<html>1 layer needs saving but has an associated file<br>which cannot be written.<br>Either "
     99            + "select another file for this layer or discard the changes.<br>Layer with a non-writable "
     100            + "file:</html>", JOptionPane.OK_OPTION
     101        );
     102
    48103        assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
    49104            @Override
    50105            public List<SaveLayerInfo> getLayersWithIllegalFilesAndSaveRequest() {
    51106                return list;
    52107            }
    53108        }));
     109
     110        assertEquals(1, jopsMocker.getInvocationLog().size());
     111        invocationLogEntry = jopsMocker.getInvocationLog().get(0);
     112        assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
     113        assertEquals("Unsaved data non-writable files", invocationLogEntry[2]);
     114
     115        jopsMocker.resetInvocationLog();
     116        jopsMocker.getMockResultMap().clear();
     117
    54118        assertTrue(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel()));
    55119    }
    56120}