Changeset 13078 in josm for trunk


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

Location:
trunk
Files:
2 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/.classpath

    r13011 r13078  
    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.7.1.jar"/>
     24        <classpathentry kind="lib" path="test/lib/wiremock-standalone-2.10.1.jar"/>
    2525        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    2626        <classpathentry exported="true" kind="con" path="GROOVY_SUPPORT"/>
  • trunk/test/unit/org/openstreetmap/josm/TestUtils.java

    r12726 r13078  
    171171
    172172    /**
     173     * Returns a private static field value.
     174     * @param cls object class
     175     * @param fieldName private field name
     176     * @return private field value
     177     * @throws ReflectiveOperationException if a reflection operation error occurs
     178     */
     179    public static Object getPrivateStaticField(Class<?> cls, String fieldName) throws ReflectiveOperationException {
     180        Field f = cls.getDeclaredField(fieldName);
     181        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
     182            f.setAccessible(true);
     183            return null;
     184        });
     185        return f.get(null);
     186    }
     187
     188    /**
    173189     * Returns an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
    174190     * but does not show the progress.
  • 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}
  • trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    r13021 r13078  
    22package org.openstreetmap.josm.testutils;
    33
     4import java.awt.Color;
    45import java.io.File;
    56import java.io.IOException;
     
    5758    private APIType useAPI = APIType.NONE;
    5859    private String i18n = null;
     60    private TileSourceRule tileSourceRule;
    5961    private boolean platform;
    6062    private boolean useProjection;
     
    243245
    244246    /**
     247     * Replace imagery sources with a default set of mock tile sources
     248     *
     249     * @return this instance, for easy chaining
     250     */
     251    public JOSMTestRules fakeImagery() {
     252        return this.fakeImagery(
     253            new TileSourceRule(
     254                true,
     255                true,
     256                true,
     257                new TileSourceRule.ColorSource(Color.WHITE, "White Tiles", 256),
     258                new TileSourceRule.ColorSource(Color.BLACK, "Black Tiles", 256),
     259                new TileSourceRule.ColorSource(Color.MAGENTA, "Magenta Tiles", 256),
     260                new TileSourceRule.ColorSource(Color.GREEN, "Green Tiles", 256)
     261            )
     262        );
     263    }
     264
     265    /**
     266     * Replace imagery sources with those from specific mock tile server setup
     267     * @param tileSourceRule Tile source rule
     268     *
     269     * @return this instance, for easy chaining
     270     */
     271    public JOSMTestRules fakeImagery(TileSourceRule tileSourceRule) {
     272        this.preferences();
     273        this.tileSourceRule = tileSourceRule;
     274        return this;
     275    }
     276
     277    /**
    245278     * Use the {@link Main#main}, {@code Main.contentPanePrivate}, {@code Main.mainPanel},
    246279     *         {@link Main#menu}, {@link Main#toolbar} global variables in this test.
     
    257290    public Statement apply(Statement base, Description description) {
    258291        Statement statement = base;
     292        // counter-intuitively, Statements which need to have their setup routines performed *after* another one need to
     293        // be added into the chain *before* that one, so that it ends up on the "inside".
    259294        if (timeout > 0) {
    260295            // TODO: new DisableOnDebug(timeout)
    261296            statement = new FailOnTimeoutStatement(statement, timeout);
    262297        }
     298
     299        // this half of TileSourceRule's initialization must happen after josm is set up
     300        if (this.tileSourceRule != null) {
     301            statement = this.tileSourceRule.applyRegisterLayers(statement, description);
     302        }
     303
    263304        statement = new CreateJosmEnvironment(statement);
    264305        if (josmHome != null) {
    265306            statement = josmHome.apply(statement, description);
     307        }
     308
     309        // run mock tile server as the outermost Statement (started first) so it can hopefully be initializing in
     310        // parallel with other setup
     311        if (this.tileSourceRule != null) {
     312            statement = this.tileSourceRule.applyRunServer(statement, description);
    266313        }
    267314        return statement;
     
    407454        MainApplication.getLayerManager().resetState();
    408455        eventManager.resetState();
     456    }
     457
     458    /**
     459     * @return TileSourceRule which is automatically started by this rule
     460     */
     461    public TileSourceRule getTileSourceRule() {
     462        return this.tileSourceRule;
    409463    }
    410464
Note: See TracChangeset for help on using the changeset viewer.