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 {
|
| 275 | 275 | |
| 276 | 276 | private static void warn(String msg, List<SaveLayerInfo> infos, String title) { |
| 277 | 277 | 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); |
| 282 | 279 | } |
| 283 | 280 | |
| 284 | 281 | 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
|
b
|
|
| 1 | 1 | // License: GPL. For details, see LICENSE file. |
| 2 | 2 | package org.openstreetmap.josm.gui.io; |
| 3 | 3 | |
| | 4 | import static org.junit.Assert.assertEquals; |
| 4 | 5 | import static org.junit.Assert.assertFalse; |
| 5 | 6 | import static org.junit.Assert.assertTrue; |
| 6 | 7 | |
| 7 | 8 | import java.util.Collections; |
| 8 | 9 | import java.util.List; |
| | 10 | import javax.swing.JComponent; |
| | 11 | import javax.swing.JLabel; |
| | 12 | import javax.swing.JList; |
| | 13 | import javax.swing.JOptionPane; |
| 9 | 14 | |
| 10 | 15 | import org.junit.Rule; |
| 11 | 16 | import org.junit.Test; |
| 12 | 17 | import org.openstreetmap.josm.data.osm.DataSet; |
| 13 | 18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
| 14 | 19 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| | 20 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; |
| 15 | 21 | |
| 16 | 22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 17 | 23 | |
| … |
… |
public class SaveLayersDialogTest {
|
| 33 | 39 | @Test |
| 34 | 40 | public void testConfirmSaveLayerInfosOK() { |
| 35 | 41 | 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 | |
| 36 | 64 | assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() { |
| 37 | 65 | @Override |
| 38 | 66 | public List<SaveLayerInfo> getLayersWithConflictsAndUploadRequest() { |
| 39 | 67 | return list; |
| 40 | 68 | } |
| 41 | 69 | })); |
| | 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 | |
| 42 | 83 | assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() { |
| 43 | 84 | @Override |
| 44 | 85 | public List<SaveLayerInfo> getLayersWithoutFilesAndSaveRequest() { |
| 45 | 86 | return list; |
| 46 | 87 | } |
| 47 | 88 | })); |
| | 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 | |
| 48 | 103 | assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() { |
| 49 | 104 | @Override |
| 50 | 105 | public List<SaveLayerInfo> getLayersWithIllegalFilesAndSaveRequest() { |
| 51 | 106 | return list; |
| 52 | 107 | } |
| 53 | 108 | })); |
| | 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 | |
| 54 | 118 | assertTrue(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel())); |
| 55 | 119 | } |
| 56 | 120 | } |