kopia lustrzana https://github.com/JOSM/MapWithAI
Expand tests for MapWithAIAction
Signed-off-by: Taylor Smock <taylor.smock@kaart.com>pull/1/head
rodzic
a470f9f95c
commit
f659ca1f46
|
@ -49,21 +49,42 @@ public class MapWithAIAction extends JosmAction {
|
|||
.collect(Collectors.toList());
|
||||
final OsmDataLayer layer = getOsmLayer(osmLayers);
|
||||
if (layer != null && MapWithAIDataUtils.getMapWithAIData(MapWithAIDataUtils.getLayer(true), layer)) {
|
||||
createMessageDialog();
|
||||
Notification notification = createMessageDialog();
|
||||
if (notification != null) {
|
||||
notification.show();
|
||||
}
|
||||
} else if (layer != null && hasLayer) {
|
||||
toggleLayer(layer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static OsmDataLayer getOsmLayer(List<OsmDataLayer> osmLayers) {
|
||||
return osmLayers.size() == 1
|
||||
? osmLayers.get(0)
|
||||
: AbstractMergeAction.askTargetLayer(osmLayers.toArray(new OsmDataLayer[0]),
|
||||
tr("Please select the target layer"), tr("Select target layer"), tr("OK"), "download");
|
||||
/**
|
||||
* Get the osm layer that the user wants to use to get data from (doesn't ask if
|
||||
* user only has one data layer)
|
||||
*
|
||||
* @param osmLayers The list of osm data layers
|
||||
* @return The layer that the user selects
|
||||
*/
|
||||
protected static OsmDataLayer getOsmLayer(List<OsmDataLayer> osmLayers) {
|
||||
OsmDataLayer returnLayer = null;
|
||||
if (osmLayers.size() == 1) {
|
||||
returnLayer = osmLayers.get(0);
|
||||
} else if (!osmLayers.isEmpty()) {
|
||||
AbstractMergeAction.askTargetLayer(osmLayers.toArray(new OsmDataLayer[0]),
|
||||
tr("Please select the target layer"), tr("Select target layer"), tr("OK"), "download");
|
||||
}
|
||||
return returnLayer;
|
||||
}
|
||||
|
||||
private static void toggleLayer(Layer toLayer) {
|
||||
/**
|
||||
* Toggle the layer (the toLayer is the layer to switch to, if currently active
|
||||
* it will switch to the MapWithAI layer, if the MapWithAI layer is currently
|
||||
* active it will switch to the layer passed)
|
||||
*
|
||||
* @param toLayer The {@link Layer} to switch to
|
||||
*/
|
||||
protected static void toggleLayer(Layer toLayer) {
|
||||
final OsmDataLayer mapwithai = MapWithAIDataUtils.getLayer(false);
|
||||
final Layer currentLayer = MainApplication.getLayerManager().getActiveLayer();
|
||||
if (currentLayer != null) {
|
||||
|
@ -80,10 +101,16 @@ public class MapWithAIAction extends JosmAction {
|
|||
setEnabled(getLayerManager().getEditDataSet() != null);
|
||||
}
|
||||
|
||||
public void createMessageDialog() {
|
||||
/**
|
||||
* Create a message dialog to notify the user if data is available in their
|
||||
* downloaded region
|
||||
*
|
||||
* @return A Notification to show ({@link Notification#show})
|
||||
*/
|
||||
public static Notification createMessageDialog() {
|
||||
final MapWithAILayer layer = MapWithAIDataUtils.getLayer(false);
|
||||
if (layer != null) {
|
||||
final Notification notification = new Notification();
|
||||
final Notification notification = layer == null ? null : new Notification();
|
||||
if (notification != null) {
|
||||
final List<Bounds> bounds = new ArrayList<>(layer.getDataSet().getDataSourceBounds());
|
||||
if (bounds.isEmpty()) {
|
||||
MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class).stream()
|
||||
|
@ -110,7 +137,7 @@ public class MapWithAIAction extends JosmAction {
|
|||
notification.setContent(message.toString());
|
||||
notification.setDuration(Notification.TIME_DEFAULT);
|
||||
notification.setIcon(JOptionPane.INFORMATION_MESSAGE);
|
||||
notification.show();
|
||||
}
|
||||
return notification;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,26 @@
|
|||
// License: GPL. For details, see LICENSE file.
|
||||
package org.openstreetmap.josm.plugins.mapwithai.backend;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.awaitility.Awaitility;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
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.Notification;
|
||||
import org.openstreetmap.josm.gui.layer.Layer;
|
||||
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
|
||||
|
@ -29,21 +40,63 @@ public class MapWithAIActionTest {
|
|||
|
||||
@Test
|
||||
public void testEnabled() {
|
||||
Assert.assertFalse(action.isEnabled());
|
||||
assertFalse(action.isEnabled());
|
||||
MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(), "temporary", null));
|
||||
Assert.assertTrue(action.isEnabled());
|
||||
assertTrue(action.isEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDownload() {
|
||||
Assert.assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
|
||||
assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
|
||||
action.actionPerformed(null);
|
||||
Assert.assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
|
||||
assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
|
||||
|
||||
MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(), "temporary", null));
|
||||
action.actionPerformed(null);
|
||||
Awaitility.await().timeout(8, TimeUnit.SECONDS)
|
||||
.until(() -> 1 == MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).size());
|
||||
Assert.assertEquals(1, MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).size());
|
||||
.until(() -> 1 == MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).size());
|
||||
assertEquals(1, MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).size());
|
||||
|
||||
assertSame(MapWithAIDataUtils.getLayer(false), MainApplication.getLayerManager().getActiveLayer());
|
||||
action.actionPerformed(null);
|
||||
assertNotSame(MapWithAIDataUtils.getLayer(false), MainApplication.getLayerManager().getActiveLayer());
|
||||
MainApplication.getLayerManager().removeLayer(MainApplication.getLayerManager().getActiveLayer());
|
||||
action.actionPerformed(null);
|
||||
assertSame(MapWithAIDataUtils.getLayer(false), MainApplication.getLayerManager().getActiveLayer());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToggleLayer() {
|
||||
MapWithAIAction.toggleLayer(null);
|
||||
Layer layer = new OsmDataLayer(new DataSet(), "Test layer", null);
|
||||
assertNull(MainApplication.getLayerManager().getActiveLayer());
|
||||
MainApplication.getLayerManager().addLayer(layer);
|
||||
assertSame(layer, MainApplication.getLayerManager().getActiveLayer());
|
||||
MapWithAIAction.toggleLayer(layer);
|
||||
assertSame(layer, MainApplication.getLayerManager().getActiveLayer());
|
||||
MapWithAIDataUtils.getLayer(true);
|
||||
// Adding the MapWithAI layer switches to it
|
||||
assertSame(MapWithAIDataUtils.getLayer(false), MainApplication.getLayerManager().getActiveLayer());
|
||||
MapWithAIAction.toggleLayer(layer);
|
||||
assertSame(layer, MainApplication.getLayerManager().getActiveLayer());
|
||||
MapWithAIAction.toggleLayer(layer);
|
||||
assertSame(MapWithAIDataUtils.getLayer(false), MainApplication.getLayerManager().getActiveLayer());
|
||||
MapWithAIAction.toggleLayer(null);
|
||||
assertSame(MapWithAIDataUtils.getLayer(false), MainApplication.getLayerManager().getActiveLayer());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNotification() {
|
||||
Notification notification = MapWithAIAction.createMessageDialog();
|
||||
assertNull(notification);
|
||||
|
||||
MapWithAILayer layer = MapWithAIDataUtils.getLayer(true);
|
||||
notification = MapWithAIAction.createMessageDialog();
|
||||
assertNotNull(notification);
|
||||
DataSource source = new DataSource(new Bounds(38.8876078, -77.012173, 38.892087, -77.0059234), "test area");
|
||||
layer.getDataSet().addDataSource(source);
|
||||
notification = MapWithAIAction.createMessageDialog();
|
||||
assertNotNull(notification);
|
||||
assertEquals(Notification.TIME_DEFAULT, notification.getDuration());
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue