Changeset 13112 in josm


Ignore:
Timestamp:
2017-11-12T00:54:15+01:00 (7 days ago)
Author:
Don-vip
Message:

fix #15539 - use awaitility in MinimapDialogTest rather than Thread.sleep() (patch by ris)

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/.classpath

    r13110 r13112  
    2222        <classpathentry kind="lib" path="test/lib/unitils-core/unitils-core-3.4.6.jar"/>
    2323        <classpathentry kind="lib" path="test/lib/commons-testing/commons-testing-2.1.0.jar"/>
    24         <classpathentry kind="lib" path="test/lib/wiremock-standalone-2.10.1.jar"/>
     24        <classpathentry exported="true" kind="lib" path="test/lib/wiremock-standalone-2.10.1.jar"/>
     25        <classpathentry exported="true" kind="lib" path="test/lib/awaitility-3.0.0.jar"/>
    2526        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    2627        <classpathentry exported="true" kind="con" path="GROOVY_SUPPORT"/>
  • trunk/netbeans/nbproject/project.properties

    r13110 r13112  
    5959file.reference.system-rules-1.16.1.jar=../test/lib/system-rules-1.16.1.jar
    6060file.reference.wiremock-standalone-2.10.1.jar=../test/lib/wiremock-standalone-2.10.1.jar
     61file.reference.awaitility-3.0.0.jar=../test/lib/awaitility-3.0.0.jar
    6162includes=**/*.java
    6263jar.compress=false
     
    9495    ${file.reference.system-rules-1.16.1.jar}:\
    9596    ${file.reference.wiremock-standalone-2.10.1.jar}:\
     97        ${file.reference.awaitility-3.0.0.jar}:\
    9698    ${file.reference.spotbugs.jar}:\
    9799    ${file.reference.commons-testing-2.1.0.jar}
  • trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoader.java

    r12539 r13112  
    127127    }
    128128
     129    @Override
     130    public boolean hasOutstandingTasks() {
     131        return downloadExecutor.getTaskCount() > downloadExecutor.getCompletedTaskCount();
     132    }
     133
    129134    /**
    130135     * Sets the download executor that will be used to download tiles instead of default one.
  • trunk/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java

    r13078 r13112  
    77import static org.junit.Assert.fail;
    88
     9import static java.util.concurrent.TimeUnit.MILLISECONDS;
     10
    911import java.awt.Color;
    1012import java.awt.Component;
     
    1517import javax.swing.JPopupMenu;
    1618
     19import java.util.concurrent.Callable;
     20
    1721import org.junit.Rule;
    1822import org.junit.Test;
     23import org.openstreetmap.josm.Main;
    1924import org.openstreetmap.josm.TestUtils;
    2025import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
     
    2328
    2429import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     30
     31import org.awaitility.Awaitility;
    2532
    2633/**
     
    7986    }
    8087
     88    protected MinimapDialog minimap;
     89    protected SlippyMapBBoxChooser slippyMap;
     90    protected SourceButton sourceButton;
     91
     92    protected static BufferedImage paintedSlippyMap;
     93
     94    protected void setUpMiniMap() throws Exception {
     95        this.minimap = new MinimapDialog();
     96        this.minimap.setSize(300, 200);
     97        this.minimap.showDialog();
     98        this.slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(this.minimap, "slippyMap");
     99        this.sourceButton = (SourceButton) TestUtils.getPrivateField(this.slippyMap, "iSourceButton");
     100
     101        // get dlg in a paintable state
     102        this.minimap.addNotify();
     103        this.minimap.doLayout();
     104    }
     105
     106    protected void paintSlippyMap() {
     107        if (paintedSlippyMap == null ||
     108            paintedSlippyMap.getWidth() != this.slippyMap.getSize().width ||
     109            paintedSlippyMap.getHeight() != this.slippyMap.getSize().height) {
     110            paintedSlippyMap = new BufferedImage(
     111                this.slippyMap.getSize().width,
     112                this.slippyMap.getSize().height,
     113                BufferedImage.TYPE_INT_RGB
     114            );
     115        } // else reuse existing one - allocation is expensive
     116
     117        // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over
     118        // any state
     119        Graphics2D g = paintedSlippyMap.createGraphics();
     120        g.setBackground(Color.BLUE);
     121        g.clearRect(0, 0, paintedSlippyMap.getWidth(), paintedSlippyMap.getHeight());
     122        g.dispose();
     123
     124        g = paintedSlippyMap.createGraphics();
     125        this.slippyMap.paintAll(g);
     126    }
     127
     128    protected Callable<Boolean> slippyMapTasksFinished() {
     129        return () -> !this.slippyMap.getTileController().getTileLoader().hasOutstandingTasks();
     130    }
     131
    81132    /**
    82133     * Tests to switch imagery source.
     
    85136    @Test
    86137    public void testSourceSwitching() throws Exception {
    87         MinimapDialog dlg = new MinimapDialog();
    88         dlg.setSize(300, 200);
    89         dlg.showDialog();
    90         SlippyMapBBoxChooser slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(dlg, "slippyMap");
    91         SourceButton sourceButton = (SourceButton) TestUtils.getPrivateField(slippyMap, "iSourceButton");
    92 
    93         // get dlg in a paintable state
    94         dlg.addNotify();
    95         dlg.doLayout();
    96 
    97         BufferedImage image = new BufferedImage(
    98             slippyMap.getSize().width,
    99             slippyMap.getSize().height,
    100             BufferedImage.TYPE_INT_RGB
    101         );
    102 
    103         Graphics2D g = image.createGraphics();
     138        // relevant prefs starting out empty, should choose the first source and have shown download area enabled
     139        // (not that there's a data layer for it to use)
     140
     141        this.setUpMiniMap();
     142
    104143        // an initial paint operation is required to trigger the tile fetches
    105         slippyMap.paintAll(g);
    106         g.setBackground(Color.BLUE);
    107         g.clearRect(0, 0, image.getWidth(), image.getHeight());
    108         g.dispose();
    109 
    110         Thread.sleep(500);
    111 
    112         g = image.createGraphics();
    113         slippyMap.paintAll(g);
    114 
    115         assertEquals(0xffffffff, image.getRGB(0, 0));
    116 
    117         assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "White Tiles");
    118 
    119         getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Magenta Tiles").doClick();
    120         assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Magenta Tiles");
     144        this.paintSlippyMap();
     145
     146        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished());
     147
     148        this.paintSlippyMap();
     149
     150        assertEquals(0xffffffff, paintedSlippyMap.getRGB(0, 0));
     151
     152        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "White Tiles");
     153
     154        getSourceMenuItemByLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles").doClick();
     155        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles");
    121156        // call paint to trigger new tile fetch
    122         slippyMap.paintAll(g);
    123 
    124         // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over
    125         // any state
    126         g.setBackground(Color.BLUE);
    127         g.clearRect(0, 0, image.getWidth(), image.getHeight());
    128         g.dispose();
    129 
    130         Thread.sleep(500);
    131 
    132         g = image.createGraphics();
    133         slippyMap.paintAll(g);
    134 
    135         assertEquals(0xffff00ff, image.getRGB(0, 0));
    136 
    137         getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Green Tiles").doClick();
    138         assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Green Tiles");
     157        this.paintSlippyMap();
     158
     159        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished());
     160
     161        this.paintSlippyMap();
     162
     163        assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0));
     164
     165        getSourceMenuItemByLabel(this.sourceButton.getPopupMenu(), "Green Tiles").doClick();
     166        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Green Tiles");
    139167        // call paint to trigger new tile fetch
    140         slippyMap.paintAll(g);
    141 
    142         g.setBackground(Color.BLUE);
    143         g.clearRect(0, 0, image.getWidth(), image.getHeight());
    144         g.dispose();
    145 
    146         Thread.sleep(500);
    147 
    148         g = image.createGraphics();
    149         slippyMap.paintAll(g);
    150 
    151         assertEquals(0xff00ff00, image.getRGB(0, 0));
     168        this.paintSlippyMap();
     169
     170        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished());
     171
     172        this.paintSlippyMap();
     173
     174        assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0));
     175
     176        assertEquals("Green Tiles", Main.pref.get("slippy_map_chooser.mapstyle", "Fail"));
     177    }
     178
     179    /**
     180     * Tests minimap obeys a saved "mapstyle" preference on startup.
     181     * @throws Exception if any error occurs
     182     */
     183    @Test
     184    public void testSourcePrefObeyed() throws Exception {
     185        Main.pref.put("slippy_map_chooser.mapstyle", "Green Tiles");
     186
     187        this.setUpMiniMap();
     188
     189        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Green Tiles");
     190
     191        // an initial paint operation is required to trigger the tile fetches
     192        this.paintSlippyMap();
     193
     194        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished());
     195
     196        this.paintSlippyMap();
     197
     198        assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0));
     199
     200        getSourceMenuItemByLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles").doClick();
     201        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "Magenta Tiles");
     202
     203        assertEquals("Magenta Tiles", Main.pref.get("slippy_map_chooser.mapstyle", "Fail"));
     204    }
     205
     206    /**
     207     * Tests minimap handles an unrecognized "mapstyle" preference on startup
     208     * @throws Exception if any error occurs
     209     */
     210    @Test
     211    public void testSourcePrefInvalid() throws Exception {
     212        Main.pref.put("slippy_map_chooser.mapstyle", "Hooloovoo Tiles");
     213
     214        this.setUpMiniMap();
     215
     216        assertSingleSelectedSourceLabel(this.sourceButton.getPopupMenu(), "White Tiles");
     217
     218        // an initial paint operation is required to trigger the tile fetches
     219        this.paintSlippyMap();
     220
     221        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished());
     222
     223        this.paintSlippyMap();
     224
     225        assertEquals(0xffffffff, paintedSlippyMap.getRGB(0, 0));
    152226    }
    153227}
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java

    r12636 r13112  
    2929    @Rule
    3030    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    31     public JOSMTestRules test = new JOSMTestRules().platform().main().projection();
     31    public JOSMTestRules test = new JOSMTestRules().platform().main().projection().timeout(20000);
    3232
    3333    /**
Note: See TracChangeset for help on using the changeset viewer.