Ignore:
Timestamp:
2018-10-14T17:30:28+02:00 (6 years ago)
Author:
Don-vip
Message:

fix #16841 - MergeLayerActionTest, DownloadWmsAlongTrackActionTest: fix for non-headless mode by properly mocking dialogs (patch by ris)

Location:
trunk/test/unit/org/openstreetmap/josm
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java

    r14138 r14332  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.actions;
     3
     4import javax.swing.JLabel;
     5import javax.swing.JPanel;
    36
    47import static org.junit.Assert.assertEquals;
     
    912import org.junit.Test;
    1013import org.openstreetmap.josm.data.osm.DataSet;
     14import org.openstreetmap.josm.gui.ExtendedDialog;
    1115import org.openstreetmap.josm.gui.MainApplication;
    1216import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
    1317import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     18import org.openstreetmap.josm.gui.widgets.JosmComboBox;
     19import org.openstreetmap.josm.TestUtils;
    1420import org.openstreetmap.josm.testutils.JOSMTestRules;
     21import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
     22import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
     23
     24import com.google.common.collect.ImmutableMap;
    1525
    1626import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    2737    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    2838    public JOSMTestRules test = new JOSMTestRules().main();
     39
     40    /**
     41     * MergeLayerExtendedDialog mocker.
     42     */
     43    public static class MergeLayerExtendedDialogMocker extends ExtendedDialogMocker {
     44        @Override
     45        protected void act(final ExtendedDialog instance) {
     46            ((JosmComboBox<?>) ((JPanel) this.getContent(instance)).getComponent(1)).setSelectedIndex(0);
     47        }
     48
     49        @Override
     50        protected String getString(final ExtendedDialog instance) {
     51            return ((JLabel) ((JPanel) this.getContent(instance)).getComponent(0)).getText();
     52        }
     53    }
    2954
    3055    private MergeLayerAction action;
     
    5883    @Test
    5984    public void testMergeNoTargetLayer() {
    60         OsmDataLayer layer = new OsmDataLayer(new DataSet(), "", null);
     85        TestUtils.assumeWorkingJMockit();
     86        final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
     87            ImmutableMap.<String, Object>of("<html>There are no layers the source layer<br>'onion'<br>could be merged to.</html>", 0)
     88        );
     89
     90        OsmDataLayer layer = new OsmDataLayer(new DataSet(), "onion", null);
    6191        MainApplication.getLayerManager().addLayer(layer);
    6292        assertEquals(1, MainApplication.getLayerManager().getLayers().size());
    6393        assertNull(action.merge(layer));
    6494        assertEquals(1, MainApplication.getLayerManager().getLayers().size());
     95
     96        assertEquals(1, jopsMocker.getInvocationLog().size());
     97        Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
     98        assertEquals(0, (int) invocationLogEntry[0]);
     99        assertEquals("No target layers", invocationLogEntry[2]);
    65100    }
    66101
     
    71106    @Test
    72107    public void testMergeTwoEmptyLayers() throws Exception {
     108        TestUtils.assumeWorkingJMockit();
     109        final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker();
     110        edMocker.getMockResultMap().put("Please select the target layer.", "Merge");
     111
    73112        OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), "1", null);
    74113        OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "2", null);
     
    78117        action.merge(layer2).get();
    79118        assertEquals(1, MainApplication.getLayerManager().getLayers().size());
     119
     120        assertEquals(1, edMocker.getInvocationLog().size());
     121        Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
     122        assertEquals(1, (int) invocationLogEntry[0]);
     123        assertEquals("Select target layer", invocationLogEntry[2]);
    80124    }
    81125}
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java

    r14138 r14332  
    33
    44import static java.util.concurrent.TimeUnit.MILLISECONDS;
     5import static org.junit.Assert.assertEquals;
    56import static org.junit.Assert.assertNotNull;
    67import static org.junit.Assert.assertNull;
     
    1011import org.junit.Rule;
    1112import org.junit.Test;
     13import org.openstreetmap.josm.actions.MergeLayerActionTest.MergeLayerExtendedDialogMocker;
    1214import org.openstreetmap.josm.data.gpx.GpxData;
    1315import org.openstreetmap.josm.gui.MainApplication;
     
    1517import org.openstreetmap.josm.gui.layer.TMSLayer;
    1618import org.openstreetmap.josm.gui.layer.gpx.DownloadWmsAlongTrackAction.PrecacheWmsTask;
     19import org.openstreetmap.josm.TestUtils;
    1720import org.openstreetmap.josm.testutils.JOSMTestRules;
    1821import org.openstreetmap.josm.testutils.TileSourceRule;
     22import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
     23
     24import com.google.common.collect.ImmutableMap;
    1925
    2026import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    3743    @Test
    3844    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
    3950        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]);
    4056    }
    4157
     
    4662    @Test
    4763    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
    4868        final TileSourceRule tileSourceRule = this.test.getTileSourceRule();
    4969
     
    6585            MainApplication.getLayerManager().removeLayer(layer);
    6686        }
     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]);
    6792    }
    6893}
  • trunk/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java

    r14052 r14332  
    1111import java.util.WeakHashMap;
    1212
     13import org.openstreetmap.josm.TestUtils;
    1314import org.openstreetmap.josm.gui.ExtendedDialog;
    1415import org.openstreetmap.josm.tools.Logging;
     
    108109    }
    109110
     111    /**
     112     * Target for overriding, similar to {@link #getMockResult} except with the implication it will only
     113     * be invoked once per dialog display, therefore ideal opportunity to perform any mutating actions,
     114     * e.g. making a selection on a widget.
     115     * @param instance dialog instance
     116     */
     117    protected void act(final ExtendedDialog instance) {
     118        // Override in sub-classes
     119    }
     120
    110121    protected Object[] getInvocationLogEntry(final ExtendedDialog instance, final int mockResult) {
    111122        return new Object[] {
     
    114125            instance.getTitle()
    115126        };
     127    }
     128
     129    /**
     130     * A convenience method to access {@link ExtendedDialog#content} without exception-catching boilerplate
     131     * @param instance dialog instance
     132     * @return dialog content component
     133     */
     134    protected Component getContent(final ExtendedDialog instance) {
     135        try {
     136            return (Component) TestUtils.getPrivateField(instance, "content");
     137        } catch (ReflectiveOperationException e) {
     138            throw new RuntimeException(e);
     139        }
    116140    }
    117141
     
    130154            try {
    131155                final ExtendedDialog instance = invocation.getInvokedInstance();
     156                this.act(instance);
    132157                final int mockResult = this.getMockResult(instance);
    133158                // TODO check validity of mockResult?
  • trunk/test/unit/org/openstreetmap/josm/testutils/mockers/HelpAwareOptionPaneMocker.java

    r14201 r14332  
    7676        }
    7777        return this.getMockResultMap().get(messageString);
     78    }
     79
     80    /**
     81     * Target for overriding, similar to {@link #getMockResultForMessage} except with the implication it
     82     * will only be invoked once per dialog display, therefore ideal opportunity to perform any mutating
     83     * actions, e.g. making a selection on a widget.
     84     * @param message message
     85     */
     86    protected void act(final Object message) {
     87        // Override in sub-classes
    7888    }
    7989
     
    134144    ) {
    135145        try {
     146            this.act(msg);
    136147            final Object result = this.getMockResultForMessage(msg);
    137148
  • trunk/test/unit/org/openstreetmap/josm/testutils/mockers/JOptionPaneSimpleMocker.java

    r14052 r14332  
    134134    }
    135135
     136    /**
     137     * Target for overriding, similar to {@link #getMockResultForMessage} except with the implication it
     138     * will only be invoked once per dialog display, therefore ideal opportunity to perform any mutating
     139     * actions, e.g. making a selection on a widget.
     140     * @param message message
     141     */
     142    protected void act(final Object message) {
     143        // Override in sub-classes
     144    }
     145
    136146    protected Object[] getInvocationLogEntry(
    137147        final Object message,
     
    162172    ) {
    163173        try {
     174            this.act(message);
    164175            final Object result = this.getMockResultForMessage(message);
    165176            if (selectionValues == null) {
     
    216227    ) {
    217228        try {
     229            this.act(message);
    218230            // why look up a "result" for a message dialog which can only have one possible result? it's
    219231            // a good opportunity to assert its contents
     
    263275    ) {
    264276        try {
     277            this.act(message);
    265278            final Object result = this.getMockResultForMessage(message);
    266279            if (!(result instanceof Integer && Ints.contains(optionTypePermittedResults.get(optionType), (int) result))) {
Note: See TracChangeset for help on using the changeset viewer.