From e64efcd9734365927905ee327de78a7eba8e2cc7 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sat, 13 Oct 2018 19:29:36 +0100
Subject: [PATCH v1 3/3] MergeLayerActionTest, DownloadWmsAlongTrackActionTest:
fix for non-headless mode by properly mocking dialogs
---
.../josm/actions/AbstractMergeAction.java | 9 +----
.../gui/layer/gpx/DownloadWmsAlongTrackAction.java | 4 +-
.../josm/actions/MergeLayerActionTest.java | 43 +++++++++++++++++++++-
.../layer/gpx/DownloadWmsAlongTrackActionTest.java | 25 +++++++++++++
4 files changed, 70 insertions(+), 11 deletions(-)
diff --git a/src/org/openstreetmap/josm/actions/AbstractMergeAction.java b/src/org/openstreetmap/josm/actions/AbstractMergeAction.java
index ebcfdae9a..33b68f83b 100644
|
a
|
b
|
public abstract class AbstractMergeAction extends JosmAction {
|
| 109 | 109 | JPanel pnl = new JPanel(new GridBagLayout()); |
| 110 | 110 | pnl.add(new JLabel(label), GBC.eol()); |
| 111 | 111 | pnl.add(layerList, GBC.eol().fill(GBC.HORIZONTAL)); |
| 112 | | if (GraphicsEnvironment.isHeadless()) { |
| 113 | | // return first layer in headless mode, for unit tests |
| 114 | | return targetLayers[0]; |
| 115 | | } |
| | 112 | |
| 116 | 113 | ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), title, buttonText, tr("Cancel")); |
| 117 | 114 | ed.setButtonIcons(buttonIcon, "cancel"); |
| 118 | 115 | ed.setContent(pnl); |
| … |
… |
public abstract class AbstractMergeAction extends JosmAction {
|
| 130 | 127 | protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) { |
| 131 | 128 | String message = tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>", |
| 132 | 129 | Utils.escapeReservedCharactersHTML(sourceLayer.getName())); |
| 133 | | if (!GraphicsEnvironment.isHeadless()) { |
| 134 | | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), message, tr("No target layers"), JOptionPane.WARNING_MESSAGE); |
| 135 | | } |
| | 130 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), message, tr("No target layers"), JOptionPane.WARNING_MESSAGE); |
| 136 | 131 | } |
| 137 | 132 | } |
diff --git a/src/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackAction.java b/src/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackAction.java
index b9cc6cb48..9de3c676f 100644
|
a
|
b
|
public class DownloadWmsAlongTrackAction extends AbstractAction {
|
| 117 | 117 | protected AbstractTileSourceLayer<? extends AbstractTMSTileSource> askedLayer() { |
| 118 | 118 | List<AbstractTileSourceLayer> targetLayers = MainApplication.getLayerManager().getLayersOfType(AbstractTileSourceLayer.class); |
| 119 | 119 | if (targetLayers.isEmpty()) { |
| 120 | | if (!GraphicsEnvironment.isHeadless()) { |
| 121 | | warnNoImageryLayers(); |
| 122 | | } |
| | 120 | warnNoImageryLayers(); |
| 123 | 121 | return null; |
| 124 | 122 | } |
| 125 | 123 | return AbstractMergeAction.askTargetLayer(targetLayers.toArray(new AbstractTileSourceLayer[0]), |
diff --git a/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java b/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java
index 77548165a..7c9a28b27 100644
|
a
|
b
|
|
| 1 | 1 | // License: GPL. For details, see LICENSE file. |
| 2 | 2 | package org.openstreetmap.josm.actions; |
| 3 | 3 | |
| | 4 | import javax.swing.JLabel; |
| | 5 | import javax.swing.JPanel; |
| | 6 | |
| 4 | 7 | import static org.junit.Assert.assertEquals; |
| 5 | 8 | import static org.junit.Assert.assertNull; |
| 6 | 9 | |
| … |
… |
import org.junit.Before;
|
| 8 | 11 | import org.junit.Rule; |
| 9 | 12 | import org.junit.Test; |
| 10 | 13 | import org.openstreetmap.josm.data.osm.DataSet; |
| | 14 | import org.openstreetmap.josm.gui.ExtendedDialog; |
| 11 | 15 | import org.openstreetmap.josm.gui.MainApplication; |
| 12 | 16 | import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer; |
| 13 | 17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
| | 18 | import org.openstreetmap.josm.gui.widgets.JosmComboBox; |
| | 19 | import org.openstreetmap.josm.TestUtils; |
| 14 | 20 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| | 21 | import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker; |
| | 22 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; |
| | 23 | |
| | 24 | import com.google.common.collect.ImmutableMap; |
| 15 | 25 | |
| 16 | 26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 17 | 27 | |
| … |
… |
public class MergeLayerActionTest {
|
| 27 | 37 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 28 | 38 | public JOSMTestRules test = new JOSMTestRules().main(); |
| 29 | 39 | |
| | 40 | public static class MergeLayerExtendedDialogMocker extends ExtendedDialogMocker { |
| | 41 | @Override |
| | 42 | protected void act(final ExtendedDialog instance) { |
| | 43 | ((JosmComboBox) ((JPanel) this.getContent(instance)).getComponent(1)).setSelectedIndex(0); |
| | 44 | } |
| | 45 | |
| | 46 | @Override |
| | 47 | protected String getString(final ExtendedDialog instance) { |
| | 48 | return ((JLabel) ((JPanel) this.getContent(instance)).getComponent(0)).getText(); |
| | 49 | } |
| | 50 | }; |
| | 51 | |
| 30 | 52 | private MergeLayerAction action; |
| 31 | 53 | |
| 32 | 54 | /** |
| … |
… |
public class MergeLayerActionTest {
|
| 57 | 79 | */ |
| 58 | 80 | @Test |
| 59 | 81 | public void testMergeNoTargetLayer() { |
| 60 | | OsmDataLayer layer = new OsmDataLayer(new DataSet(), "", null); |
| | 82 | TestUtils.assumeWorkingJMockit(); |
| | 83 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker( |
| | 84 | ImmutableMap.<String, Object>of("<html>There are no layers the source layer<br>'onion'<br>could be merged to.</html>", 0) |
| | 85 | ); |
| | 86 | |
| | 87 | OsmDataLayer layer = new OsmDataLayer(new DataSet(), "onion", null); |
| 61 | 88 | MainApplication.getLayerManager().addLayer(layer); |
| 62 | 89 | assertEquals(1, MainApplication.getLayerManager().getLayers().size()); |
| 63 | 90 | assertNull(action.merge(layer)); |
| 64 | 91 | assertEquals(1, MainApplication.getLayerManager().getLayers().size()); |
| | 92 | |
| | 93 | assertEquals(1, jopsMocker.getInvocationLog().size()); |
| | 94 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0); |
| | 95 | assertEquals(0, (int) invocationLogEntry[0]); |
| | 96 | assertEquals("No target layers", invocationLogEntry[2]); |
| 65 | 97 | } |
| 66 | 98 | |
| 67 | 99 | /** |
| … |
… |
public class MergeLayerActionTest {
|
| 70 | 102 | */ |
| 71 | 103 | @Test |
| 72 | 104 | public void testMergeTwoEmptyLayers() throws Exception { |
| | 105 | TestUtils.assumeWorkingJMockit(); |
| | 106 | final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker(); |
| | 107 | edMocker.getMockResultMap().put("Please select the target layer.", "Merge"); |
| | 108 | |
| 73 | 109 | OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), "1", null); |
| 74 | 110 | OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "2", null); |
| 75 | 111 | MainApplication.getLayerManager().addLayer(layer1); |
| … |
… |
public class MergeLayerActionTest {
|
| 77 | 113 | assertEquals(2, MainApplication.getLayerManager().getLayers().size()); |
| 78 | 114 | action.merge(layer2).get(); |
| 79 | 115 | assertEquals(1, MainApplication.getLayerManager().getLayers().size()); |
| | 116 | |
| | 117 | assertEquals(1, edMocker.getInvocationLog().size()); |
| | 118 | Object[] invocationLogEntry = edMocker.getInvocationLog().get(0); |
| | 119 | assertEquals(1, (int) invocationLogEntry[0]); |
| | 120 | assertEquals("Select target layer", invocationLogEntry[2]); |
| 80 | 121 | } |
| 81 | 122 | } |
diff --git a/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java b/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java
index 637036c93..8c4ab71ea 100644
|
a
|
b
|
|
| 2 | 2 | package org.openstreetmap.josm.gui.layer.gpx; |
| 3 | 3 | |
| 4 | 4 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| | 5 | import static org.junit.Assert.assertEquals; |
| 5 | 6 | import static org.junit.Assert.assertNotNull; |
| 6 | 7 | import static org.junit.Assert.assertNull; |
| 7 | 8 | import static org.junit.Assert.assertTrue; |
| … |
… |
import static org.junit.Assert.assertTrue;
|
| 9 | 10 | import org.awaitility.Awaitility; |
| 10 | 11 | import org.junit.Rule; |
| 11 | 12 | import org.junit.Test; |
| | 13 | import org.openstreetmap.josm.actions.MergeLayerActionTest.MergeLayerExtendedDialogMocker; |
| 12 | 14 | import org.openstreetmap.josm.data.gpx.GpxData; |
| 13 | 15 | import org.openstreetmap.josm.gui.MainApplication; |
| 14 | 16 | import org.openstreetmap.josm.gui.layer.GpxLayerTest; |
| 15 | 17 | import org.openstreetmap.josm.gui.layer.TMSLayer; |
| 16 | 18 | import org.openstreetmap.josm.gui.layer.gpx.DownloadWmsAlongTrackAction.PrecacheWmsTask; |
| | 19 | import org.openstreetmap.josm.TestUtils; |
| 17 | 20 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| 18 | 21 | import org.openstreetmap.josm.testutils.TileSourceRule; |
| | 22 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; |
| | 23 | |
| | 24 | import com.google.common.collect.ImmutableMap; |
| 19 | 25 | |
| 20 | 26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 21 | 27 | |
| … |
… |
public class DownloadWmsAlongTrackActionTest {
|
| 36 | 42 | */ |
| 37 | 43 | @Test |
| 38 | 44 | public void testNoLayer() { |
| | 45 | TestUtils.assumeWorkingJMockit(); |
| | 46 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker( |
| | 47 | ImmutableMap.<String, Object>of("There are no imagery layers.", 0) |
| | 48 | ); |
| | 49 | |
| 39 | 50 | assertNull(new DownloadWmsAlongTrackAction(new GpxData()).createTask()); |
| | 51 | |
| | 52 | assertEquals(1, jopsMocker.getInvocationLog().size()); |
| | 53 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0); |
| | 54 | assertEquals(0, (int) invocationLogEntry[0]); |
| | 55 | assertEquals("No imagery layers", invocationLogEntry[2]); |
| 40 | 56 | } |
| 41 | 57 | |
| 42 | 58 | /** |
| … |
… |
public class DownloadWmsAlongTrackActionTest {
|
| 45 | 61 | */ |
| 46 | 62 | @Test |
| 47 | 63 | public void testTMSLayer() throws Exception { |
| | 64 | TestUtils.assumeWorkingJMockit(); |
| | 65 | final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker(); |
| | 66 | edMocker.getMockResultMap().put("Please select the imagery layer.", "Download"); |
| | 67 | |
| 48 | 68 | final TileSourceRule tileSourceRule = this.test.getTileSourceRule(); |
| 49 | 69 | |
| 50 | 70 | final TMSLayer layer = new TMSLayer( |
| … |
… |
public class DownloadWmsAlongTrackActionTest {
|
| 64 | 84 | // Ensure we clean the place before leaving, even if test fails. |
| 65 | 85 | MainApplication.getLayerManager().removeLayer(layer); |
| 66 | 86 | } |
| | 87 | |
| | 88 | assertEquals(1, edMocker.getInvocationLog().size()); |
| | 89 | Object[] invocationLogEntry = edMocker.getInvocationLog().get(0); |
| | 90 | assertEquals(1, (int) invocationLogEntry[0]); |
| | 91 | assertEquals("Select imagery layer", invocationLogEntry[2]); |
| 67 | 92 | } |
| 68 | 93 | } |