Changeset 14358 in josm


Ignore:
Timestamp:
2018-10-23T01:34:26+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #16878 - SaveLayersDialogTest: fix for non-headless mode (patch by ris)

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

    r14273 r14358  
    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
  • trunk/test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java

    r10962 r14358  
    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;
     
    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;
     
    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;
     
    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
     
    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
     
    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
     
    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    }
  • trunk/test/unit/org/openstreetmap/josm/testutils/mockers/JOptionPaneSimpleMocker.java

    r14332 r14358  
    7676            JOptionPane.CANCEL_OPTION,
    7777            JOptionPane.CLOSED_OPTION
     78        },
     79        // it's hard to know much about DEFAULT_OPTION, so we can't really police anything here, so
     80        // including all known options
     81        JOptionPane.DEFAULT_OPTION, new int[] {
     82            JOptionPane.OK_OPTION,
     83            JOptionPane.CANCEL_OPTION,
     84            JOptionPane.CLOSED_OPTION,
     85            JOptionPane.YES_OPTION,
     86            JOptionPane.NO_OPTION
    7887        }
    7988    );
Note: See TracChangeset for help on using the changeset viewer.