| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.layer.gpx;
|
|---|
| 3 |
|
|---|
| 4 | import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|---|
| 5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
|---|
| 6 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|---|
| 7 | import static org.junit.jupiter.api.Assertions.assertNull;
|
|---|
| 8 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
|---|
| 9 |
|
|---|
| 10 | import java.util.Collections;
|
|---|
| 11 |
|
|---|
| 12 | import org.awaitility.Awaitility;
|
|---|
| 13 | import org.junit.jupiter.api.Test;
|
|---|
| 14 | import org.junit.jupiter.api.Timeout;
|
|---|
| 15 | import org.openstreetmap.josm.TestUtils;
|
|---|
| 16 | import org.openstreetmap.josm.actions.MergeLayerActionTest.MergeLayerExtendedDialogMocker;
|
|---|
| 17 | import org.openstreetmap.josm.data.gpx.GpxData;
|
|---|
| 18 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 19 | import org.openstreetmap.josm.gui.layer.GpxLayerTest;
|
|---|
| 20 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
|---|
| 21 | import org.openstreetmap.josm.gui.layer.gpx.DownloadWmsAlongTrackAction.PrecacheWmsTask;
|
|---|
| 22 | import org.openstreetmap.josm.testutils.annotations.FakeImagery;
|
|---|
| 23 | import org.openstreetmap.josm.testutils.annotations.Main;
|
|---|
| 24 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
|---|
| 25 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Unit tests of {@link DownloadWmsAlongTrackAction} class.
|
|---|
| 29 | */
|
|---|
| 30 | @FakeImagery
|
|---|
| 31 | @Main
|
|---|
| 32 | @Projection
|
|---|
| 33 | @Timeout(20)
|
|---|
| 34 | class DownloadWmsAlongTrackActionTest {
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Test action without layer.
|
|---|
| 38 | */
|
|---|
| 39 | @Test
|
|---|
| 40 | void testNoLayer() {
|
|---|
| 41 | TestUtils.assumeWorkingJMockit();
|
|---|
| 42 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
|
|---|
| 43 | Collections.singletonMap("There are no imagery layers.", 0)
|
|---|
| 44 | );
|
|---|
| 45 |
|
|---|
| 46 | assertNull(new DownloadWmsAlongTrackAction(new GpxData()).createTask());
|
|---|
| 47 |
|
|---|
| 48 | assertEquals(1, jopsMocker.getInvocationLog().size());
|
|---|
| 49 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
|---|
| 50 | assertEquals(0, (int) invocationLogEntry[0]);
|
|---|
| 51 | assertEquals("No imagery layers", invocationLogEntry[2]);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Test action with a TMS layer.
|
|---|
| 56 | * @throws Exception if an error occurs
|
|---|
| 57 | */
|
|---|
| 58 | @Test
|
|---|
| 59 | void testTMSLayer(FakeImagery.FakeImageryWireMockExtension fakeImageryWireMockExtension) throws Exception {
|
|---|
| 60 | TestUtils.assumeWorkingJMockit();
|
|---|
| 61 | final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker();
|
|---|
| 62 | edMocker.getMockResultMap().put("Please select the imagery layer.", "Download");
|
|---|
| 63 |
|
|---|
| 64 | final TMSLayer layer = new TMSLayer(
|
|---|
| 65 | fakeImageryWireMockExtension.getSourcesList().get(0).getImageryInfo(fakeImageryWireMockExtension.getRuntimeInfo().getHttpPort())
|
|---|
| 66 | );
|
|---|
| 67 | try {
|
|---|
| 68 | MainApplication.getLayerManager().addLayer(layer);
|
|---|
| 69 | TMSLayer.getCache().clear();
|
|---|
| 70 | assertTrue(TMSLayer.getCache().getMatching(".*").isEmpty());
|
|---|
| 71 | // Perform action
|
|---|
| 72 | PrecacheWmsTask task = new DownloadWmsAlongTrackAction(GpxLayerTest.getMinimalGpxData()).createTask();
|
|---|
| 73 | assertNotNull(task);
|
|---|
| 74 | task.run();
|
|---|
| 75 | // Ensure cache is (eventually) not empty
|
|---|
| 76 | Awaitility.await().atMost(10000, MILLISECONDS).until(() -> !TMSLayer.getCache().getMatching(".*").isEmpty());
|
|---|
| 77 | } finally {
|
|---|
| 78 | // Ensure we clean the place before leaving, even if test fails.
|
|---|
| 79 | MainApplication.getLayerManager().removeLayer(layer);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | assertEquals(1, edMocker.getInvocationLog().size());
|
|---|
| 83 | Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
|
|---|
| 84 | assertEquals(1, (int) invocationLogEntry[0]);
|
|---|
| 85 | assertEquals("Select imagery layer", invocationLogEntry[2]);
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|