Ticket #16845: v1-0001-ChooseTrackVisibilityActionTest-fix-for-non-headl.patch

File v1-0001-ChooseTrackVisibilityActionTest-fix-for-non-headl.patch, 6.0 KB (added by ris, 6 years ago)
  • src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java

    From a9f6e53860adfdad655bf54cd6afced224d21bcd Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Mon, 15 Oct 2018 20:45:47 +0100
    Subject: [PATCH v1] ChooseTrackVisibilityActionTest: fix for non-headless mode
     by properly mocking dialogs
    
    ---
     .../gui/layer/gpx/ChooseTrackVisibilityAction.java | 38 ++++++++++------------
     .../openstreetmap/josm/gui/layer/GpxLayerTest.java |  2 +-
     .../layer/gpx/ChooseTrackVisibilityActionTest.java | 24 ++++++++++++++
     3 files changed, 43 insertions(+), 21 deletions(-)
    
    diff --git a/src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java b/src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java
    index ee49c6eaf..fd9d8a909 100644
    a b public class ChooseTrackVisibilityAction extends AbstractAction {  
    262262        msg.add(scrollPane, GBC.eol().fill(GBC.BOTH));
    263263
    264264        int v = 1;
    265         if (!GraphicsEnvironment.isHeadless()) {
    266             // build dialog
    267             ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), tr("Set track visibility for {0}", layer.getName()),
    268                     tr("Show all"), tr("Show selected only"), tr("Cancel"));
    269             ed.setButtonIcons("eye", "dialogs/filter", "cancel");
    270             ed.setContent(msg, false);
    271             ed.setDefaultButton(2);
    272             ed.setCancelButton(3);
    273             ed.configureContextsensitiveHelp("/Action/ChooseTrackVisibility", true);
    274             ed.setRememberWindowGeometry(getClass().getName() + ".geometry",
    275                     WindowGeometry.centerInWindow(MainApplication.getMainFrame(), new Dimension(1000, 500)));
    276             ed.showDialog();
    277             dateFilter.saveInPrefs();
    278             v = ed.getValue();
    279             // cancel for unknown buttons and copy back original settings
    280             if (v != 1 && v != 2) {
    281                 layer.trackVisibility = Arrays.copyOf(trackVisibilityBackup, layer.trackVisibility.length);
    282                 MainApplication.getMap().repaint();
    283                 return;
    284             }
     265        // build dialog
     266        ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), tr("Set track visibility for {0}", layer.getName()),
     267                tr("Show all"), tr("Show selected only"), tr("Cancel"));
     268        ed.setButtonIcons("eye", "dialogs/filter", "cancel");
     269        ed.setContent(msg, false);
     270        ed.setDefaultButton(2);
     271        ed.setCancelButton(3);
     272        ed.configureContextsensitiveHelp("/Action/ChooseTrackVisibility", true);
     273        ed.setRememberWindowGeometry(getClass().getName() + ".geometry",
     274                WindowGeometry.centerInWindow(MainApplication.getMainFrame(), new Dimension(1000, 500)));
     275        ed.showDialog();
     276        dateFilter.saveInPrefs();
     277        v = ed.getValue();
     278        // cancel for unknown buttons and copy back original settings
     279        if (v != 1 && v != 2) {
     280            layer.trackVisibility = Arrays.copyOf(trackVisibilityBackup, layer.trackVisibility.length);
     281            MainApplication.getMap().repaint();
     282            return;
    285283        }
    286284        // set visibility (1 = show all, 2 = filter). If no tracks are selected
    287285        // set all of them visible and...
  • test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java

    diff --git a/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java b/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java
    index 0cfc549e5..1688a8755 100644
    a b public class GpxLayerTest {  
    6363     * @throws SAXException if any SAX error occurs
    6464     */
    6565    public static GpxLayer getMinimalGpxLayer() throws IOException, SAXException {
    66         return new GpxLayer(getMinimalGpxData());
     66        return new GpxLayer(getMinimalGpxData(), "Bananas");
    6767    }
    6868
    6969    /**
  • test/unit/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityActionTest.java

    diff --git a/test/unit/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityActionTest.java b/test/unit/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityActionTest.java
    index 64636c0e3..4a5289b03 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.layer.gpx;
    33
     4import static org.junit.Assert.assertEquals;
     5
     6import javax.swing.JLabel;
     7import javax.swing.JPanel;
     8
    49import org.junit.Rule;
    510import org.junit.Test;
     11import org.openstreetmap.josm.gui.ExtendedDialog;
    612import org.openstreetmap.josm.gui.layer.GpxLayerTest;
    713import org.openstreetmap.josm.testutils.JOSMTestRules;
     14import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
    815
    916import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1017
    public class ChooseTrackVisibilityActionTest {  
    2633     */
    2734    @Test
    2835    public void testAction() throws Exception {
     36        final ExtendedDialogMocker edMocker = new ExtendedDialogMocker() {
     37            protected String getString(final ExtendedDialog instance) {
     38                return ((JLabel) ((JPanel) this.getContent(instance)).getComponent(2)).getText();
     39            }
     40        };
     41        edMocker.getMockResultMap().put(
     42            "<html>Select all tracks that you want to be displayed. You can drag select a range of " +
     43            "tracks or use CTRL+Click to select specific ones. The map is updated live in the " +
     44            "background. Open the URLs by double clicking them.</html>",
     45            "Show all"
     46        );
     47
    2948        new ChooseTrackVisibilityAction(GpxLayerTest.getMinimalGpxLayer()).actionPerformed(null);
     49
     50        assertEquals(1, edMocker.getInvocationLog().size());
     51        Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
     52        assertEquals(1, (int) invocationLogEntry[0]);
     53        assertEquals("Set track visibility for Bananas", invocationLogEntry[2]);
    3054    }
    3155}