Ignore:
Timestamp:
2017-11-04T17:11:15+01:00 (6 years ago)
Author:
Don-vip
Message:

fix #15508 - add TileSourceRule, use in MinimapDialogTest (patch by ris, modified) + update WireMock to 2.10.1

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java

    r12564 r13078  
    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;
     8
     9import java.awt.Color;
     10import java.awt.Component;
     11import java.awt.Graphics2D;
     12import java.awt.image.BufferedImage;
     13
     14import javax.swing.JMenuItem;
     15import javax.swing.JPopupMenu;
    616
    717import org.junit.Rule;
    818import org.junit.Test;
     19import org.openstreetmap.josm.TestUtils;
     20import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
     21import org.openstreetmap.josm.gui.bbox.SourceButton;
    922import org.openstreetmap.josm.testutils.JOSMTestRules;
    1023
     
    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().platform().projection().fakeImagery();
    2437
    2538    /**
     
    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    /**
     82     * Tests to switch imagery source.
     83     * @throws Exception if any error occurs
     84     */
     85    @Test
     86    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();
     104        // 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");
     121        // 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");
     139        // 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));
     152    }
    36153}
Note: See TracChangeset for help on using the changeset viewer.