Ticket #15599: v2-0002-MinimapDialogTest-perform-event-related-operation.patch

File v2-0002-MinimapDialogTest-perform-event-related-operation.patch, 6.0 KB (added by ris, 8 years ago)
  • test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java

    From 43973ad26cf7a7f009e6e423d70ed66cd3f8c89e Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Sat, 25 Nov 2017 20:38:52 +0000
    Subject: [PATCH v2 2/3] MinimapDialogTest: perform event-related operations in
     EDT thread
    
    ---
     .../josm/gui/dialogs/MinimapDialogTest.java        | 76 +++++++++++++++-------
     1 file changed, 51 insertions(+), 25 deletions(-)
    
    diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
    index e302a7c51..b4ac23820 100644
    a b import org.openstreetmap.josm.Main;  
    2424import org.openstreetmap.josm.TestUtils;
    2525import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
    2626import org.openstreetmap.josm.gui.bbox.SourceButton;
     27import org.openstreetmap.josm.gui.util.GuiHelper;
    2728import org.openstreetmap.josm.testutils.JOSMTestRules;
    2829
    2930import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    public class MinimapDialogTest {  
    5455        assertFalse(dlg.isVisible());
    5556    }
    5657
    57     protected void assertSingleSelectedSourceLabel(String label) {
     58    @FunctionalInterface
     59    protected interface ThrowingRunnable {
     60        void run() throws Throwable;
     61    }
     62
     63    protected static Runnable uncheckExceptions(final ThrowingRunnable tr) {
     64        return (() -> {
     65            try {
     66                tr.run();
     67            } catch (Throwable e) {
     68                throw new RuntimeException(e);
     69            }
     70        });
     71    }
     72
     73    protected void assertSingleSelectedSourceLabel(final String label) {
    5874        JPopupMenu menu = this.sourceButton.getPopupMenu();
    5975        boolean found = false;
    6076        for (Component c: menu.getComponents()) {
    public class MinimapDialogTest {  
    7389        assertTrue("Selected source not found in menu", found);
    7490    }
    7591
    76     protected JMenuItem getSourceMenuItemByLabel(String label) {
    77         JPopupMenu menu = this.sourceButton.getPopupMenu();
    78         for (Component c: menu.getComponents()) {
    79             if (JPopupMenu.Separator.class.isInstance(c)) {
    80                 break;
    81             } else if (((JMenuItem) c).getText() == label) {
    82                 return (JMenuItem) c;
    83             }
    84             // else continue...
     92    protected void clickSourceMenuItemByLabel(final String label) {
     93        try {
     94            GuiHelper.runInEDTAndWaitWithException(() -> {
     95                JPopupMenu menu = this.sourceButton.getPopupMenu();
     96                for (Component c: menu.getComponents()) {
     97                    if (JPopupMenu.Separator.class.isInstance(c)) {
     98                        // sources should all come before any separators
     99                        break;
     100                    } else if (((JMenuItem) c).getText() == label) {
     101                        ((JMenuItem) c).doClick();
     102                        return;
     103                    }
     104                    // else continue...
     105                }
     106                fail();
     107            });
     108        } catch (Throwable e) {
     109            // need to turn this *back* into an AssertionFailedError
     110            fail(String.format("Failed to find menu item with label %s: %s", label, e));
    85111        }
    86         fail("Failed to find menu item with label " + label);
    87         return null;
    88112    }
    89113
    90114    protected MinimapDialog minimap;
    public class MinimapDialogTest {  
    94118
    95119    protected static BufferedImage paintedSlippyMap;
    96120
    97     protected void setUpMiniMap() throws Exception {
    98         this.minimap = new MinimapDialog();
    99         this.minimap.setSize(300, 200);
    100         this.minimap.showDialog();
    101         this.slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(this.minimap, "slippyMap");
    102         this.sourceButton = (SourceButton) TestUtils.getPrivateField(this.slippyMap, "iSourceButton");
     121    protected void setUpMiniMap() {
     122        GuiHelper.runInEDTAndWaitWithException(uncheckExceptions(() -> {
     123            this.minimap = new MinimapDialog();
     124            this.minimap.setSize(300, 200);
     125            this.minimap.showDialog();
     126            this.slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(this.minimap, "slippyMap");
     127            this.sourceButton = (SourceButton) TestUtils.getPrivateField(this.slippyMap, "iSourceButton");
    103128
    104         this.slippyMapTasksFinished = () -> !this.slippyMap.getTileController().getTileLoader().hasOutstandingTasks();
     129            // get minimap in a paintable state
     130            this.minimap.addNotify();
     131            this.minimap.doLayout();
     132        }));
    105133
    106         // get minimap in a paintable state
    107         this.minimap.addNotify();
    108         this.minimap.doLayout();
     134        this.slippyMapTasksFinished = () -> !this.slippyMap.getTileController().getTileLoader().hasOutstandingTasks();
    109135    }
    110136
    111137    protected void paintSlippyMap() {
    public class MinimapDialogTest {  
    152178
    153179        this.assertSingleSelectedSourceLabel("White Tiles");
    154180
    155         this.getSourceMenuItemByLabel("Magenta Tiles").doClick();
     181        this.clickSourceMenuItemByLabel("Magenta Tiles");
    156182        this.assertSingleSelectedSourceLabel("Magenta Tiles");
    157183        // call paint to trigger new tile fetch
    158184        this.paintSlippyMap();
    public class MinimapDialogTest {  
    163189
    164190        assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0));
    165191
    166         this.getSourceMenuItemByLabel("Green Tiles").doClick();
     192        this.clickSourceMenuItemByLabel("Green Tiles");
    167193        this.assertSingleSelectedSourceLabel("Green Tiles");
    168194        // call paint to trigger new tile fetch
    169195        this.paintSlippyMap();
    public class MinimapDialogTest {  
    198224
    199225        assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0));
    200226
    201         this.getSourceMenuItemByLabel("Magenta Tiles").doClick();
     227        this.clickSourceMenuItemByLabel("Magenta Tiles");
    202228        this.assertSingleSelectedSourceLabel("Magenta Tiles");
    203229
    204230        assertEquals("Magenta Tiles", Main.pref.get("slippy_map_chooser.mapstyle", "Fail"));