kopia lustrzana https://github.com/JOSM/MapWithAI
Basic testing for AddMapWithAILayerAction + a fix for an NPE
Signed-off-by: Taylor Smock <taylor.smock@kaart.com>pull/1/head
rodzic
f4ad6b7421
commit
6ec23bb0a9
|
@ -11,6 +11,7 @@ import org.openstreetmap.josm.actions.JosmAction;
|
||||||
import org.openstreetmap.josm.data.osm.DataSet;
|
import org.openstreetmap.josm.data.osm.DataSet;
|
||||||
import org.openstreetmap.josm.data.osm.OsmData;
|
import org.openstreetmap.josm.data.osm.OsmData;
|
||||||
import org.openstreetmap.josm.gui.MainApplication;
|
import org.openstreetmap.josm.gui.MainApplication;
|
||||||
|
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
||||||
import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
||||||
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
||||||
import org.openstreetmap.josm.gui.util.GuiHelper;
|
import org.openstreetmap.josm.gui.util.GuiHelper;
|
||||||
|
@ -22,6 +23,7 @@ import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||||
import org.openstreetmap.josm.tools.ImageProvider;
|
import org.openstreetmap.josm.tools.ImageProvider;
|
||||||
import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
||||||
import org.openstreetmap.josm.tools.ImageResource;
|
import org.openstreetmap.josm.tools.ImageResource;
|
||||||
|
import org.openstreetmap.josm.tools.JosmRuntimeException;
|
||||||
import org.openstreetmap.josm.tools.Logging;
|
import org.openstreetmap.josm.tools.Logging;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,9 +56,16 @@ public class AddMapWithAILayerAction extends JosmAction implements AdaptableActi
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
ImageResource resource = new ImageResource(
|
try {
|
||||||
this.info.getSourceCategory().getIcon(ImageSizes.MENU).getImage());
|
ImageResource resource = new ImageResource(
|
||||||
resource.attachImageIcon(this);
|
this.info.getSourceCategory().getIcon(ImageSizes.MENU).getImage());
|
||||||
|
resource.attachImageIcon(this);
|
||||||
|
} catch (JosmRuntimeException e) {
|
||||||
|
// Eclipse doesn't like giving applications their resources...
|
||||||
|
if (!e.getMessage().contains("failed to locate image")) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,10 +81,10 @@ public class AddMapWithAILayerAction extends JosmAction implements AdaptableActi
|
||||||
if (layer != null && !layer.getData().getDataSourceBounds().isEmpty()) {
|
if (layer != null && !layer.getData().getDataSourceBounds().isEmpty()) {
|
||||||
ds = layer.getDataSet();
|
ds = layer.getDataSet();
|
||||||
boundsSource = ds;
|
boundsSource = ds;
|
||||||
} else if (MainApplication.getLayerManager().getActiveData() != null
|
} else if (getDataLayer() != null && !getDataLayer().getDataSet().getDataSourceBounds().isEmpty()) {
|
||||||
&& !MainApplication.getLayerManager().getActiveData().getDataSourceBounds().isEmpty()) {
|
boundsSource = getDataLayer().getDataSet();
|
||||||
boundsSource = MainApplication.getLayerManager().getActiveData();
|
layer = MapWithAIDataUtils.getLayer(true);
|
||||||
ds = MapWithAIDataUtils.getLayer(true).getDataSet();
|
ds = layer.getDataSet();
|
||||||
} else {
|
} else {
|
||||||
boundsSource = null;
|
boundsSource = null;
|
||||||
ds = null;
|
ds = null;
|
||||||
|
@ -90,7 +99,14 @@ public class AddMapWithAILayerAction extends JosmAction implements AdaptableActi
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
MapWithAIDataUtils.getLayer(false).addDownloadedInfo(info);
|
if (layer != null) {
|
||||||
|
layer.addDownloadedInfo(info);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private OsmDataLayer getDataLayer() {
|
||||||
|
return MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class).stream()
|
||||||
|
.filter(i -> !(i instanceof MapWithAILayer)).findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
// License: GPL. For details, see LICENSE file.
|
||||||
|
package org.openstreetmap.josm.plugins.mapwithai.actions;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import org.awaitility.Awaitility;
|
||||||
|
import org.awaitility.Durations;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
import org.openstreetmap.josm.data.Bounds;
|
||||||
|
import org.openstreetmap.josm.data.DataSource;
|
||||||
|
import org.openstreetmap.josm.data.osm.DataSet;
|
||||||
|
import org.openstreetmap.josm.gui.MainApplication;
|
||||||
|
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
||||||
|
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
|
||||||
|
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer;
|
||||||
|
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||||
|
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||||
|
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
|
||||||
|
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||||
|
|
||||||
|
class AddMapWithAILayerActionTest {
|
||||||
|
@RegisterExtension
|
||||||
|
JOSMTestRules rule = new MapWithAITestRules().wiremock().sources().projection();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test() {
|
||||||
|
MapWithAIInfo info = MapWithAILayerInfo.getInstance().getLayers().stream()
|
||||||
|
.filter(i -> i.getName().equalsIgnoreCase("MapWithAI")).findAny().orElse(null);
|
||||||
|
AddMapWithAILayerAction action = new AddMapWithAILayerAction(info);
|
||||||
|
assertDoesNotThrow(() -> action.actionPerformed(null));
|
||||||
|
OsmDataLayer osmLayer = new OsmDataLayer(new DataSet(), "TEST DATA", null);
|
||||||
|
MainApplication.getLayerManager().addLayer(osmLayer);
|
||||||
|
osmLayer.getDataSet()
|
||||||
|
.addDataSource(new DataSource(new Bounds(39.095376, -108.4495519, 39.0987811, -108.4422314), "TEST"));
|
||||||
|
|
||||||
|
assertNull(MapWithAIDataUtils.getLayer(false));
|
||||||
|
action.updateEnabledState();
|
||||||
|
action.actionPerformed(null);
|
||||||
|
Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> MapWithAIDataUtils.getLayer(false) != null);
|
||||||
|
assertNotNull(MapWithAIDataUtils.getLayer(false));
|
||||||
|
|
||||||
|
MainApplication.getLayerManager().removeLayer(MapWithAIDataUtils.getLayer(false));
|
||||||
|
|
||||||
|
MapWithAILayerInfo.getInstance().remove(info);
|
||||||
|
|
||||||
|
MapWithAILayer layer = MapWithAIDataUtils.getLayer(true);
|
||||||
|
assertTrue(layer.getDataSet().isEmpty());
|
||||||
|
|
||||||
|
action.updateEnabledState();
|
||||||
|
action.actionPerformed(null);
|
||||||
|
Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> !layer.getDataSet().isEmpty());
|
||||||
|
assertFalse(layer.getDataSet().isEmpty());
|
||||||
|
|
||||||
|
MainApplication.getLayerManager().removeLayer(MapWithAIDataUtils.getLayer(false));
|
||||||
|
MapWithAILayer mapwithaiLayer = new MapWithAILayer(new DataSet(), "TEST", null);
|
||||||
|
MainApplication.getLayerManager().addLayer(mapwithaiLayer);
|
||||||
|
|
||||||
|
mapwithaiLayer.getDataSet()
|
||||||
|
.addDataSource(new DataSource(new Bounds(39.095376, -108.4495519, 39.0987811, -108.4422314), ""));
|
||||||
|
action.actionPerformed(null);
|
||||||
|
Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> !mapwithaiLayer.getDataSet().isEmpty());
|
||||||
|
assertFalse(mapwithaiLayer.getDataSet().isEmpty());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -219,7 +219,7 @@ public class MapWithAITestRules extends JOSMTestRules {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
|
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
|
||||||
if (wireMock != null) {
|
if (wireMock != null && !request.getUrl().endsWith("/capabilities")) {
|
||||||
String origBody = response.getBodyAsString();
|
String origBody = response.getBodyAsString();
|
||||||
String newBody = origBody.replaceAll("https?:\\/\\/.*?\\/", wireMock.baseUrl() + "/");
|
String newBody = origBody.replaceAll("https?:\\/\\/.*?\\/", wireMock.baseUrl() + "/");
|
||||||
return Response.Builder.like(response).but().body(newBody).build();
|
return Response.Builder.like(response).but().body(newBody).build();
|
||||||
|
|
Ładowanie…
Reference in New Issue