Ticket #15508: v1-0004-MinimapDialogTest-add-testSourceSwitching.patch

File v1-0004-MinimapDialogTest-add-testSourceSwitching.patch, 6.0 KB (added by ris, 6 years ago)
  • test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java

    From a9282d0559114b0d34d8dba23200148ccca2e265 Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Tue, 31 Oct 2017 22:09:21 +0000
    Subject: [PATCH 4/4] MinimapDialogTest: add testSourceSwitching
    
    ---
     .../josm/gui/dialogs/MinimapDialogTest.java        | 124 ++++++++++++++++++++-
     1 file changed, 122 insertions(+), 2 deletions(-)
    
    diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java
    index 36eee87b3..5e7a92666 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.dialogs;
    33
     4import static org.junit.Assert.assertEquals;
    45import static org.junit.Assert.assertFalse;
    56import static org.junit.Assert.assertTrue;
     7import static org.junit.Assert.fail;
    68
    79import org.junit.Rule;
    810import org.junit.Test;
     11import org.openstreetmap.josm.TestUtils;
    912import org.openstreetmap.josm.testutils.JOSMTestRules;
     13import org.openstreetmap.josm.gui.MainApplication;
     14import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
     15import org.openstreetmap.josm.gui.bbox.SourceButton;
     16
     17import java.awt.Color;
     18import java.awt.Component;
     19import java.awt.Graphics2D;
     20import java.awt.image.BufferedImage;
     21
     22import javax.swing.JMenuItem;
     23import javax.swing.JPopupMenu;
    1024
    1125import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1226
    import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;  
    1428 * Unit tests of {@link MinimapDialog} class.
    1529 */
    1630public class MinimapDialogTest {
    17 
    1831    /**
    1932     * Setup tests
    2033     */
    2134    @Rule
    2235    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    23     public JOSMTestRules test = new JOSMTestRules().platform();
     36    public JOSMTestRules josmTestRules = new JOSMTestRules().main().commands().platform().projection().fakeImagery();
    2437
    2538    /**
    2639     * Unit test of {@link MinimapDialog} class.
    public class MinimapDialogTest {  
    3346        dlg.hideDialog();
    3447        assertFalse(dlg.isVisible());
    3548    }
     49
     50    private static void assertSingleSelectedSourceLabel(JPopupMenu menu, String label) {
     51        boolean found = false;
     52        for (Component c: menu.getComponents()) {
     53            if (JPopupMenu.Separator.class.isInstance(c)) {
     54                break;
     55            } else {
     56                boolean equalText = ((JMenuItem) c).getText() == label;
     57                boolean isSelected = ((JMenuItem) c).isSelected();
     58                assertEquals(equalText, isSelected);
     59                if (equalText) {
     60                    assertFalse("Second selected source found", found);
     61                    found = true;
     62                }
     63            }
     64        }
     65        assertTrue("Selected source not found in menu", found);
     66    }
     67
     68    private static JMenuItem getSourceMenuItemByLabel(JPopupMenu menu, String label) {
     69        for (Component c: menu.getComponents()) {
     70            if (JPopupMenu.Separator.class.isInstance(c)) {
     71                break;
     72            } else if (((JMenuItem) c).getText() == label) {
     73                return (JMenuItem) c;
     74            }
     75            // else continue...
     76        }
     77        fail("Failed to find menu item with label " + label);
     78        return null;
     79    }
     80
     81    @Test
     82    public void testSourceSwitching() throws Throwable {
     83        MinimapDialog dlg = new MinimapDialog();
     84        dlg.setSize(300, 200);
     85        dlg.showDialog();
     86        SlippyMapBBoxChooser slippyMap = (SlippyMapBBoxChooser)TestUtils.getPrivateField(dlg, "slippyMap");
     87        SourceButton sourceButton = (SourceButton)TestUtils.getPrivateField(slippyMap, "iSourceButton");
     88
     89        // get dlg in a paintable state
     90        dlg.addNotify();
     91        dlg.doLayout();
     92
     93        BufferedImage image = new BufferedImage(
     94            slippyMap.getSize().width,
     95            slippyMap.getSize().height,
     96            BufferedImage.TYPE_INT_RGB
     97        );
     98
     99        Graphics2D g = image.createGraphics();
     100        // an initial paint operation is required to trigger the tile fetches
     101        slippyMap.paintAll(g);
     102        g.setBackground(Color.BLUE);
     103        g.clearRect(0, 0, image.getWidth(), image.getHeight());
     104        g.dispose();
     105
     106        Thread.sleep(3500);
     107
     108        g = image.createGraphics();
     109        slippyMap.paintAll(g);
     110
     111        assertEquals(0xffffffff, image.getRGB(0, 0));
     112
     113        assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "White Tiles");
     114
     115        getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Magenta Tiles").doClick();
     116        assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Magenta Tiles");
     117        // call paint to trigger new tile fetch
     118        slippyMap.paintAll(g);
     119
     120        // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over
     121        // any state
     122        g.setBackground(Color.BLUE);
     123        g.clearRect(0, 0, image.getWidth(), image.getHeight());
     124        g.dispose();
     125
     126        Thread.sleep(500);
     127
     128        g = image.createGraphics();
     129        slippyMap.paintAll(g);
     130
     131        assertEquals(0xffff00ff, image.getRGB(0, 0));
     132
     133        getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Green Tiles").doClick();
     134        assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Green Tiles");
     135        // call paint to trigger new tile fetch
     136        slippyMap.paintAll(g);
     137
     138        g.setBackground(Color.BLUE);
     139        g.clearRect(0, 0, image.getWidth(), image.getHeight());
     140        g.dispose();
     141
     142        Thread.sleep(500);
     143
     144        g = image.createGraphics();
     145        slippyMap.paintAll(g);
     146
     147        assertEquals(0xff00ff00, image.getRGB(0, 0));
     148
     149//      useful for debugging
     150//         try {
     151//             javax.imageio.ImageIO.write(image, "png", new java.io.File("painted.png"));
     152//         } catch (java.io.IOException ioe) {
     153//             System.err.println("Failed writing image");
     154//         }
     155    }
    36156}