Changeset 14300 in josm for trunk/test


Ignore:
Timestamp:
2018-10-06T22:01:03+02:00 (6 years ago)
Author:
Don-vip
Message:

fix #16809 - SlippyMapBBoxChooser: include tile sources from current layers (patch by ris, modified)

File:
1 edited

Legend:

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

    r14201 r14300  
    33
    44import static java.util.concurrent.TimeUnit.MILLISECONDS;
     5import static org.junit.Assert.assertArrayEquals;
    56import static org.junit.Assert.assertEquals;
    67import static org.junit.Assert.assertFalse;
     
    1415import java.awt.event.ComponentEvent;
    1516import java.awt.image.BufferedImage;
     17import java.util.ArrayList;
    1618import java.util.Arrays;
    1719import java.util.Map;
     
    3133import org.openstreetmap.josm.data.DataSource;
    3234import org.openstreetmap.josm.data.osm.DataSet;
     35import org.openstreetmap.josm.data.imagery.ImageryInfo;
     36import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
    3337import org.openstreetmap.josm.data.projection.ProjectionRegistry;
    3438import org.openstreetmap.josm.data.projection.Projections;
     
    3741import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
    3842import org.openstreetmap.josm.gui.bbox.SourceButton;
     43import org.openstreetmap.josm.gui.layer.ImageryLayer;
    3944import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
    4045import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    128133    }
    129134
     135    protected void assertSourceLabelsVisible(final String... labels) {
     136        GuiHelper.runInEDTAndWaitWithException(() -> {
     137            final ArrayList<String> menuLabels = new ArrayList<>();
     138            final JPopupMenu menu = this.sourceButton.getPopupMenu();
     139            for (Component c: menu.getComponents()) {
     140                if (c instanceof JPopupMenu.Separator) {
     141                    break;
     142                }
     143                menuLabels.add(((JMenuItem) c).getText());
     144            }
     145
     146            assertArrayEquals(
     147                labels,
     148                menuLabels.toArray()
     149            );
     150        });
     151    }
     152
    130153    private MinimapDialog minimap;
    131154    private SlippyMapBBoxChooser slippyMap;
     
    218241
    219242        assertEquals("Green Tiles", Config.getPref().get("slippy_map_chooser.mapstyle", "Fail"));
     243    }
     244
     245    /**
     246     * Tests that the apparently-selected TileSource survives the tile sources being refreshed.
     247     * @throws Exception if any error occurs
     248     */
     249    @Test
     250    public void testRefreshSourcesRetainsSelection() throws Exception {
     251        // relevant prefs starting out empty, should choose the first source and have shown download area enabled
     252        // (not that there's a data layer for it to use)
     253
     254        this.setUpMiniMap();
     255
     256        this.clickSourceMenuItemByLabel("Magenta Tiles");
     257        this.assertSingleSelectedSourceLabel("Magenta Tiles");
     258
     259        // call paint to trigger new tile fetch
     260        this.paintSlippyMap();
     261
     262        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished);
     263
     264        this.paintSlippyMap();
     265
     266        assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0));
     267
     268        this.slippyMap.refreshTileSources();
     269
     270        this.assertSingleSelectedSourceLabel("Magenta Tiles");
     271
     272        // call paint to trigger new tile fetch
     273        this.paintSlippyMap();
     274
     275        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished);
     276
     277        this.paintSlippyMap();
     278
     279        assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0));
     280    }
     281
     282    /**
     283     * Tests that the currently selected source being removed from ImageryLayerInfo will remain present and
     284     * selected in the source menu even after the tile sources have been refreshed.
     285     * @throws Exception if any error occurs
     286     */
     287    @Test
     288    public void testRemovedSourceStillSelected() throws Exception {
     289        // relevant prefs starting out empty, should choose the first source and have shown download area enabled
     290        // (not that there's a data layer for it to use)
     291
     292        this.setUpMiniMap();
     293
     294        this.clickSourceMenuItemByLabel("Green Tiles");
     295
     296        ImageryLayerInfo.instance.remove(
     297            ImageryLayerInfo.instance.getLayers().stream().filter(i -> i.getName().equals("Green Tiles")).findAny().get()
     298        );
     299
     300        this.assertSingleSelectedSourceLabel("Green Tiles");
     301
     302        this.slippyMap.refreshTileSources();
     303
     304        this.assertSingleSelectedSourceLabel("Green Tiles");
     305
     306        // call paint to trigger new tile fetch
     307        this.paintSlippyMap();
     308
     309        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished);
     310
     311        this.paintSlippyMap();
     312
     313        assertEquals(0xff00ff00, paintedSlippyMap.getRGB(0, 0));
     314    }
     315
     316    /**
     317     * Tests the tile source list includes sources only present in the LayerManager
     318     * @throws Exception if any error occurs
     319     */
     320    @Test
     321    public void testTileSourcesFromCurrentLayers() throws Exception {
     322        // relevant prefs starting out empty, should choose the first (ImageryLayerInfo) source and have shown download area enabled
     323        // (not that there's a data layer for it to use)
     324
     325        final ImageryInfo magentaTilesInfo = ImageryLayerInfo.instance.getLayers().stream().filter(
     326            i -> i.getName().equals("Magenta Tiles")
     327        ).findAny().get();
     328        final ImageryInfo blackTilesInfo = ImageryLayerInfo.instance.getLayers().stream().filter(
     329            i -> i.getName().equals("Black Tiles")
     330        ).findAny().get();
     331
     332        // first we will remove "Magenta Tiles" from ImageryLayerInfo
     333        ImageryLayerInfo.instance.remove(magentaTilesInfo);
     334
     335        this.setUpMiniMap();
     336
     337        assertSourceLabelsVisible(
     338            "White Tiles",
     339            "Black Tiles",
     340            "Green Tiles"
     341        );
     342
     343        final ImageryLayer magentaTilesLayer = ImageryLayer.create(magentaTilesInfo);
     344        GuiHelper.runInEDT(() -> MainApplication.getLayerManager().addLayer(magentaTilesLayer));
     345
     346        assertSourceLabelsVisible(
     347            "White Tiles",
     348            "Black Tiles",
     349            "Green Tiles",
     350            "Magenta Tiles"
     351        );
     352
     353        this.clickSourceMenuItemByLabel("Magenta Tiles");
     354        this.assertSingleSelectedSourceLabel("Magenta Tiles");
     355
     356        // call paint to trigger new tile fetch
     357        this.paintSlippyMap();
     358
     359        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished);
     360
     361        this.paintSlippyMap();
     362
     363        assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0));
     364
     365        final ImageryLayer blackTilesLayer = ImageryLayer.create(blackTilesInfo);
     366        GuiHelper.runInEDT(() -> MainApplication.getLayerManager().addLayer(blackTilesLayer));
     367
     368        assertSourceLabelsVisible(
     369            "White Tiles",
     370            "Black Tiles",
     371            "Green Tiles",
     372            "Magenta Tiles"
     373        );
     374
     375        this.clickSourceMenuItemByLabel("Black Tiles");
     376        this.assertSingleSelectedSourceLabel("Black Tiles");
     377
     378        // call paint to trigger new tile fetch
     379        this.paintSlippyMap();
     380
     381        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished);
     382
     383        this.paintSlippyMap();
     384
     385        assertEquals(0xff000000, paintedSlippyMap.getRGB(0, 0));
     386
     387        // removing magentaTilesLayer while it is *not* the selected TileSource should make it disappear
     388        // immediately
     389        GuiHelper.runInEDT(() -> MainApplication.getLayerManager().removeLayer(magentaTilesLayer));
     390
     391        assertSourceLabelsVisible(
     392            "White Tiles",
     393            "Black Tiles",
     394            "Green Tiles"
     395        );
     396        this.assertSingleSelectedSourceLabel("Black Tiles");
     397
     398        final ImageryLayer magentaTilesLayer2 = ImageryLayer.create(magentaTilesInfo);
     399        GuiHelper.runInEDT(() -> MainApplication.getLayerManager().addLayer(magentaTilesLayer2));
     400
     401        assertSourceLabelsVisible(
     402            "White Tiles",
     403            "Black Tiles",
     404            "Green Tiles",
     405            "Magenta Tiles"
     406        );
     407
     408        this.clickSourceMenuItemByLabel("Magenta Tiles");
     409        this.assertSingleSelectedSourceLabel("Magenta Tiles");
     410
     411        // call paint to trigger new tile fetch
     412        this.paintSlippyMap();
     413
     414        Awaitility.await().atMost(1000, MILLISECONDS).until(this.slippyMapTasksFinished);
     415
     416        this.paintSlippyMap();
     417
     418        assertEquals(0xffff00ff, paintedSlippyMap.getRGB(0, 0));
     419
     420        // removing magentaTilesLayer while it *is* the selected TileSource...
     421        GuiHelper.runInEDT(() -> MainApplication.getLayerManager().removeLayer(magentaTilesLayer2));
     422
     423        assertSourceLabelsVisible(
     424            "White Tiles",
     425            "Black Tiles",
     426            "Green Tiles",
     427            "Magenta Tiles"
     428        );
     429        this.assertSingleSelectedSourceLabel("Magenta Tiles");
     430
     431        this.clickSourceMenuItemByLabel("Green Tiles");
     432        this.assertSingleSelectedSourceLabel("Green Tiles");
     433        assertSourceLabelsVisible(
     434            "White Tiles",
     435            "Black Tiles",
     436            "Green Tiles"
     437        );
     438
     439        // removing blackTilesLayer shouldn't remove it from the menu as it is already in ImageryLayerInfo
     440        GuiHelper.runInEDT(() -> MainApplication.getLayerManager().removeLayer(blackTilesLayer));
     441
     442        this.assertSingleSelectedSourceLabel("Green Tiles");
     443        assertSourceLabelsVisible(
     444            "White Tiles",
     445            "Black Tiles",
     446            "Green Tiles"
     447        );
    220448    }
    221449
Note: See TracChangeset for help on using the changeset viewer.