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

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

    From 6abaa76a6cf430e4d08651b272f70d67f9d0e4e6 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        | 123 ++++++++++++++++++++-
     1 file changed, 121 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..69ea8e9dd 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.bbox.SlippyMapBBoxChooser;
     14import org.openstreetmap.josm.gui.bbox.SourceButton;
     15
     16import java.awt.Color;
     17import java.awt.Component;
     18import java.awt.Graphics2D;
     19import java.awt.image.BufferedImage;
     20
     21import javax.swing.JMenuItem;
     22import javax.swing.JPopupMenu;
    1023
    1124import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1225
    import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;  
    1427 * Unit tests of {@link MinimapDialog} class.
    1528 */
    1629public class MinimapDialogTest {
    17 
    1830    /**
    1931     * Setup tests
    2032     */
    2133    @Rule
    2234    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    23     public JOSMTestRules test = new JOSMTestRules().platform();
     35    public JOSMTestRules josmTestRules = new JOSMTestRules().main().commands().platform().projection().fakeImagery();
    2436
    2537    /**
    2638     * Unit test of {@link MinimapDialog} class.
    public class MinimapDialogTest {  
    3345        dlg.hideDialog();
    3446        assertFalse(dlg.isVisible());
    3547    }
     48
     49    private static void assertSingleSelectedSourceLabel(JPopupMenu menu, String label) {
     50        boolean found = false;
     51        for (Component c: menu.getComponents()) {
     52            if (JPopupMenu.Separator.class.isInstance(c)) {
     53                break;
     54            } else {
     55                boolean equalText = ((JMenuItem) c).getText() == label;
     56                boolean isSelected = ((JMenuItem) c).isSelected();
     57                assertEquals(equalText, isSelected);
     58                if (equalText) {
     59                    assertFalse("Second selected source found", found);
     60                    found = true;
     61                }
     62            }
     63        }
     64        assertTrue("Selected source not found in menu", found);
     65    }
     66
     67    private static JMenuItem getSourceMenuItemByLabel(JPopupMenu menu, String label) {
     68        for (Component c: menu.getComponents()) {
     69            if (JPopupMenu.Separator.class.isInstance(c)) {
     70                break;
     71            } else if (((JMenuItem) c).getText() == label) {
     72                return (JMenuItem) c;
     73            }
     74            // else continue...
     75        }
     76        fail("Failed to find menu item with label " + label);
     77        return null;
     78    }
     79
     80    @Test
     81    public void testSourceSwitching() throws Throwable {
     82        MinimapDialog dlg = new MinimapDialog();
     83        dlg.setSize(300, 200);
     84        dlg.showDialog();
     85        SlippyMapBBoxChooser slippyMap = (SlippyMapBBoxChooser) TestUtils.getPrivateField(dlg, "slippyMap");
     86        SourceButton sourceButton = (SourceButton) TestUtils.getPrivateField(slippyMap, "iSourceButton");
     87
     88        // get dlg in a paintable state
     89        dlg.addNotify();
     90        dlg.doLayout();
     91
     92        BufferedImage image = new BufferedImage(
     93            slippyMap.getSize().width,
     94            slippyMap.getSize().height,
     95            BufferedImage.TYPE_INT_RGB
     96        );
     97
     98        Graphics2D g = image.createGraphics();
     99        // an initial paint operation is required to trigger the tile fetches
     100        slippyMap.paintAll(g);
     101        g.setBackground(Color.BLUE);
     102        g.clearRect(0, 0, image.getWidth(), image.getHeight());
     103        g.dispose();
     104
     105        Thread.sleep(500);
     106
     107        g = image.createGraphics();
     108        slippyMap.paintAll(g);
     109
     110        assertEquals(0xffffffff, image.getRGB(0, 0));
     111
     112        assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "White Tiles");
     113
     114        getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Magenta Tiles").doClick();
     115        assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Magenta Tiles");
     116        // call paint to trigger new tile fetch
     117        slippyMap.paintAll(g);
     118
     119        // clear background to a recognizably "wrong" color & dispose our Graphics2D so we don't risk carrying over
     120        // any state
     121        g.setBackground(Color.BLUE);
     122        g.clearRect(0, 0, image.getWidth(), image.getHeight());
     123        g.dispose();
     124
     125        Thread.sleep(500);
     126
     127        g = image.createGraphics();
     128        slippyMap.paintAll(g);
     129
     130        assertEquals(0xffff00ff, image.getRGB(0, 0));
     131
     132        getSourceMenuItemByLabel(sourceButton.getPopupMenu(), "Green Tiles").doClick();
     133        assertSingleSelectedSourceLabel(sourceButton.getPopupMenu(), "Green Tiles");
     134        // call paint to trigger new tile fetch
     135        slippyMap.paintAll(g);
     136
     137        g.setBackground(Color.BLUE);
     138        g.clearRect(0, 0, image.getWidth(), image.getHeight());
     139        g.dispose();
     140
     141        Thread.sleep(500);
     142
     143        g = image.createGraphics();
     144        slippyMap.paintAll(g);
     145
     146        assertEquals(0xff00ff00, image.getRGB(0, 0));
     147
     148        // useful for debugging
     149        // try {
     150        //     javax.imageio.ImageIO.write(image, "png", new java.io.File("painted.png"));
     151        // } catch (java.io.IOException ioe) {
     152        //     System.err.println("Failed writing image");
     153        // }
     154    }
    36155}