Default continuous download to on

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2020-06-30 09:44:21 -06:00
rodzic 1414061c23
commit e336dedb42
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
3 zmienionych plików z 25 dodań i 26 usunięć

Wyświetl plik

@ -22,7 +22,7 @@ import org.openstreetmap.josm.tools.Destroyable;
*/ */
public final class DownloadListener implements DataSourceListener, Destroyable { public final class DownloadListener implements DataSourceListener, Destroyable {
private final WeakReference<DataSet> ds; final WeakReference<DataSet> ds;
private static final Collection<DownloadListener> LISTENERS = new HashSet<>(); private static final Collection<DownloadListener> LISTENERS = new HashSet<>();
public DownloadListener(DataSet dataSet) { public DownloadListener(DataSet dataSet) {

Wyświetl plik

@ -56,7 +56,7 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
private Integer maximumAddition; private Integer maximumAddition;
private MapWithAIInfo url; private MapWithAIInfo url;
private Boolean switchLayers; private Boolean switchLayers;
private boolean continuousDownload; private boolean continuousDownload = true;
private final Lock lock; private final Lock lock;
/** /**
@ -284,15 +284,22 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
super(tr("Continuous download")); super(tr("Continuous download"));
new ImageProvider("download").getResource().attachImageIcon(this, true); new ImageProvider("download").getResource().attachImageIcon(this, true);
this.layer = layer; this.layer = layer;
updateListeners();
} }
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
layer.continuousDownload = !layer.continuousDownload; layer.continuousDownload = !layer.continuousDownload;
updateListeners();
}
void updateListeners() {
if (layer.continuousDownload) { if (layer.continuousDownload) {
for (OsmDataLayer data : MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class)) { for (OsmDataLayer data : MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class)) {
if (!(data instanceof MapWithAILayer)) {
new DownloadListener(data.getDataSet()); new DownloadListener(data.getDataSet());
} }
}
} else { } else {
DownloadListener.destroyAll(); DownloadListener.destroyAll();
} }

Wyświetl plik

@ -17,6 +17,7 @@ import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.DataSource; import org.openstreetmap.josm.data.DataSource;
import org.openstreetmap.josm.data.osm.DataSet; import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules; import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.testutils.JOSMTestRules; import org.openstreetmap.josm.testutils.JOSMTestRules;
@ -28,25 +29,29 @@ public class DownloadListenerTest {
public JOSMTestRules rule = new MapWithAITestRules().sources().wiremock().preferences().projection(); public JOSMTestRules rule = new MapWithAITestRules().sources().wiremock().preferences().projection();
@Test @Test
public void testDataSourceChange() public void testDataSourceChange() {
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
DataSet ds = new DataSet(); DataSet ds = new DataSet();
DownloadListener listener = new DownloadListener(ds); MainApplication.getLayerManager().addLayer(new OsmDataLayer(ds, "Test Data", null));
Bounds bounds = MapWithAIDataUtilsTest.getTestBounds(); Bounds bounds = MapWithAIDataUtilsTest.getTestBounds();
MapWithAILayer layer = MapWithAIDataUtils.getLayer(true); MapWithAILayer layer = MapWithAIDataUtils.getLayer(true);
MapWithAILayer.ContinuousDownloadAction continuousDownload = new MapWithAILayer.ContinuousDownloadAction(layer);
// Now defaults to on, so we need to toggle.
continuousDownload.actionPerformed(null);
Awaitility.await().atMost(Durations.ONE_SECOND) Awaitility.await().atMost(Durations.ONE_SECOND)
.until(() -> MainApplication.getLayerManager().containsLayer(layer)); .until(() -> MainApplication.getLayerManager().containsLayer(layer));
// Test when MapWithAI layer isn't continuous downloading // Test when MapWithAI layer isn't continuous downloading
ds.addDataSource(new DataSource(bounds, "Test bounds")); ds.addDataSource(new DataSource(bounds, "Test bounds"));
listener.dataSourceChange(null);
Awaitility.await().pollDelay(Durations.ONE_HUNDRED_MILLISECONDS).atMost(Durations.ONE_SECOND) Awaitility.await().pollDelay(Durations.ONE_HUNDRED_MILLISECONDS).atMost(Durations.ONE_SECOND)
.until(() -> layer.getDataSet().getDataSources().isEmpty()); .until(() -> layer.getDataSet().getDataSources().isEmpty());
assertTrue(layer.getDataSet().getDataSourceBounds().isEmpty()); assertTrue(layer.getDataSet().getDataSourceBounds().isEmpty());
MapWithAILayer.ContinuousDownloadAction continuousDownload = new MapWithAILayer.ContinuousDownloadAction(layer);
continuousDownload.actionPerformed(null); continuousDownload.actionPerformed(null);
// Test when MapWithAI layer isn't continuous downloading // Test when MapWithAI layer is continuous downloading
ds.addDataSource(new DataSource(bounds, "Test bounds 2")); ds.addDataSource(new DataSource(bounds, "Test bounds 2"));
assertTrue(layer.getDataSet().isEmpty()); assertTrue(layer.getDataSet().isEmpty());
@ -54,31 +59,18 @@ public class DownloadListenerTest {
assertFalse(layer.getDataSet().isEmpty()); assertFalse(layer.getDataSet().isEmpty());
MainApplication.getLayerManager().removeLayer(layer); MainApplication.getLayerManager().removeLayer(layer);
Field listenerDs = DownloadListener.class.getDeclaredField("ds");
listenerDs.setAccessible(true);
@SuppressWarnings("unchecked")
WeakReference<DataSet> lds = (WeakReference<DataSet>) listenerDs.get(listener);
assertNotNull(lds.get());
ds.addDataSource(new DataSource(bounds, "Test bounds 3"));
assertNull(lds.get());
} }
@Test @Test
public void testDestroy() public void testDestroy() {
throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
DataSet ds = new DataSet(); DataSet ds = new DataSet();
DownloadListener listener = new DownloadListener(ds); DownloadListener listener = new DownloadListener(ds);
Field listenerDs = DownloadListener.class.getDeclaredField("ds");
listenerDs.setAccessible(true);
@SuppressWarnings("unchecked") assertNotNull(listener.ds.get());
WeakReference<DataSet> lds = (WeakReference<DataSet>) listenerDs.get(listener);
assertNotNull(lds.get());
listener.destroy(); listener.destroy();
assertNull(lds.get()); assertNull(listener.ds.get());
listener.destroy(); listener.destroy();
assertNull(lds.get()); assertNull(listener.ds.get());
} }
@Test @Test