Add some additional unit testing for sanity

Add tests for selection code

The dialog that tells people that they cannot add more than a certain
number of objects is no longer hit, since the user cannot select more
than that amount.

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2020-04-01 10:58:05 -06:00
rodzic 05f3650794
commit c2fd6eaeba
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
7 zmienionych plików z 146 dodań i 24 usunięć

Wyświetl plik

@ -22,7 +22,6 @@ import javax.swing.JCheckBoxMenuItem;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import org.openstreetmap.josm.actions.ExpertToggleAction;
import org.openstreetmap.josm.data.Bounds;
@ -39,6 +38,7 @@ import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListen
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
import org.openstreetmap.josm.gui.mappaint.StyleSource;
import org.openstreetmap.josm.gui.util.GuiHelper;
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.spi.preferences.Config;
@ -223,12 +223,12 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
.map(Way.class::cast).anyMatch(w -> w.containsNode(n)))) {
selection = event.getSelection();
} else {
selection = event.getSelection().stream().distinct().sorted(new OsmComparator(event.getOldSelection()))
.limit(maximumAdditionSelection)
OsmComparator comparator = new OsmComparator(event.getOldSelection());
selection = event.getSelection().stream().distinct().sorted(comparator).limit(maximumAdditionSelection)
.limit(event.getOldSelection().size() + Math.max(1L, maximumAdditionSelection / 10L))
.collect(Collectors.toList());
}
SwingUtilities.invokeLater(() -> getDataSet().setSelected(selection));
GuiHelper.runInEDT(() -> getDataSet().setSelected(selection));
}
}

Wyświetl plik

@ -30,7 +30,6 @@ import org.openstreetmap.josm.tools.Shortcut;
public class MapWithAIMoveAction extends JosmAction {
/** UID for abstract action */
private static final long serialVersionUID = 319374598;
private transient Notification lastNotification;
/** The maximum number of objects is this times the maximum add */
public static final int MAX_ADD_MULTIPLIER = 10;
@ -69,9 +68,6 @@ public class MapWithAIMoveAction extends JosmAction {
.collect(Collectors.toList());
ds.clearSelection(nodes);
nodes.stream().map(Node::getReferrers).forEach(ds::addSelected);
if (ds.getSelected().size() > maxAddition && !(maxAddition == 0 && ExpertToggleAction.isExpert())) {
createMaxAddedDialog(maxAddition, ds.getSelected().size());
}
final Collection<OsmPrimitive> selected = limitCollection(ds, maxAddition);
final OsmDataLayer editLayer = getOsmDataLayer();
if ((editLayer != null && !selected.isEmpty()
@ -116,21 +112,6 @@ public class MapWithAIMoveAction extends JosmAction {
.orElse(null);
}
private void createMaxAddedDialog(int maxAddition, int triedToAdd) {
final Notification notification = new Notification();
final StringBuilder message = new StringBuilder();
message.append(MapWithAIPlugin.NAME).append(": ").append(tr("maximum additions per action are "))
.append(maxAddition).append(", ").append(tr("tried to add ")).append(triedToAdd).append('.');
notification.setContent(message.toString());
notification.setDuration(Notification.TIME_DEFAULT);
notification.setIcon(JOptionPane.INFORMATION_MESSAGE);
notification.show();
if (lastNotification != null) {
lastNotification.setDuration(0);
}
lastNotification = notification;
}
@Override
protected void updateEnabledState() {
setEnabled(checkIfActionEnabled());

Wyświetl plik

@ -55,7 +55,7 @@ public class ReplacementPreferenceTable extends PreferencesTable {
: null;
}
private static boolean askAddSetting(JComponent gui, JPanel p) {
protected static boolean askAddSetting(JComponent gui, JPanel p) {
return new ExtendedDialog(gui, tr("Add setting"), tr("OK"), tr("Cancel")).setContent(p)
.setButtonIcons("ok", "cancel").showDialog().getValue() == 1;
}

Wyświetl plik

@ -12,6 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openstreetmap.josm.tools.I18n.tr;
import java.awt.Component;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -19,6 +20,7 @@ import java.util.List;
import javax.swing.Action;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.awaitility.Durations;
import org.junit.After;
@ -30,17 +32,21 @@ import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.DataSource;
import org.openstreetmap.josm.data.UndoRedoHandler;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.BBox;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.gui.mappaint.StyleSource;
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer.ContinuousDownloadAction;
import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAILayerInfoTest;
import org.openstreetmap.josm.spi.preferences.Config;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import com.github.tomakehurst.wiremock.WireMockServer;
@ -141,6 +147,22 @@ public class MapWithAILayerTest {
}
}
@Test
public void testSelection() throws InvocationTargetException, InterruptedException {
MapWithAILayer mapWithAILayer = MapWithAIDataUtils.getLayer(true);
DataSet ds = mapWithAILayer.getDataSet();
new GetDataRunnable(Arrays.asList(new BBox(-5.7400005, 34.4524384, -5.6686014, 34.5513153)), ds, null).fork()
.join();
assertTrue(ds.getSelected().isEmpty());
SwingUtilities.invokeAndWait(() -> ds.setSelected(ds.allNonDeletedCompletePrimitives()));
assertEquals(1, ds.getSelected().size());
OsmPrimitive prim = ds.getSelected().iterator().next();
assertTrue(prim instanceof Way);
SwingUtilities.invokeAndWait(() -> ds.setSelected(((Way) prim).getNodes()));
assertEquals(((Way) prim).getNodes().size(), ds.getSelected().size());
assertTrue(((Way) prim).getNodes().parallelStream().allMatch(ds::isSelected));
}
@Test
public void testGetData() {
final MapWithAILayer mapWithAILayer = MapWithAIDataUtils.getLayer(true);
@ -183,4 +205,23 @@ public class MapWithAILayerTest {
assertTrue(actions.length > 0);
assertEquals(ContinuousDownloadAction.class, layer.getMenuEntries()[actions.length - 3].getClass());
}
@Test
public void testLayerSwitch() {
MapWithAIDataUtils.addMapWithAIPaintStyles();
Layer osm = new OsmDataLayer(new DataSet(), "TEST", null);
MainApplication.getLayerManager().addLayer(osm);
MainApplication.getLayerManager().addLayer(layer);
MainApplication.getLayerManager().setActiveLayer(layer);
StyleSource pref = MapWithAIDataUtils.getMapWithAIPaintStyle();
layer.activeOrEditLayerChanged(null);
assertTrue(pref.active);
MainApplication.getLayerManager().setActiveLayer(osm);
layer.activeOrEditLayerChanged(null);
assertTrue(pref.active);
Config.getPref().putBoolean(MapWithAIPlugin.NAME + ".boolean:toggle_with_layer", true);
layer.activeOrEditLayerChanged(null);
assertFalse(pref.active);
}
}

Wyświetl plik

@ -17,12 +17,17 @@ import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.Notification;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.gui.util.GuiHelper;
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.ConnectedCommand;
import org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation.DuplicateCommand;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.mockers.WindowMocker;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import mockit.Mock;
import mockit.MockUp;
public class MapWithAIMoveActionTest {
MapWithAIMoveAction moveAction;
@ -106,4 +111,32 @@ public class MapWithAIMoveActionTest {
assertTrue(way1.lastNode().hasKey(ConnectedCommand.KEY), "The conn key should exist");
assertFalse(way1.lastNode().isDeleted(), "way1 should no longer be deleted");
}
private static class NotificationMocker extends MockUp<Notification> {
public boolean shown;
@Mock
public void show() {
shown = true;
}
}
@Test
public void testMaxAddNotification() {
TestUtils.assumeWorkingJMockit();
new WindowMocker();
NotificationMocker notification = new NotificationMocker();
DataSet ds = MapWithAIDataUtils.getLayer(true).getDataSet();
MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(), "TEST", null));
MapWithAIPreferenceHelper.setMaximumAddition(1, false);
for (int i = 0; i < 40; i++) {
ds.addPrimitive(new Node(LatLon.ZERO));
}
for (int i = 0; i < 11; i++) {
GuiHelper.runInEDTAndWait(() -> ds.setSelected(ds.allNonDeletedPrimitives().iterator().next()));
moveAction.actionPerformed(null);
}
assertTrue(notification.shown);
notification.shown = false;
}
}

Wyświetl plik

@ -7,6 +7,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Collections;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -15,6 +16,7 @@ import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@ -41,6 +43,12 @@ public class StubEndsTestTest {
ds.addPrimitive(nonStaticWay);
tester = new StubEndsTest();
tester.startTest(NullProgressMonitor.INSTANCE);
}
@After
public void tearDown() {
tester.endTest();
}
@Test

Wyświetl plik

@ -0,0 +1,59 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.gui.preferences;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.openstreetmap.josm.TestUtils;
import org.openstreetmap.josm.gui.preferences.advanced.PrefEntry;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.mockers.WindowMocker;
import mockit.Mock;
import mockit.MockUp;
/**
* @author Taylor Smock
*
*/
public class ReplacementPreferenceTableTest {
@Rule
public JOSMTestRules rule = new JOSMTestRules().preferences();
ReplacementPreferenceTable test;
private static final class ReplacementPreferenceTableMock extends MockUp<ReplacementPreferenceTable> {
private boolean set;
public ReplacementPreferenceTableMock(boolean set) {
this.set = set;
}
@Mock
protected boolean askAddSetting(JComponent gui, JPanel p) {
return set;
}
}
@Before
public void setUp() {
TestUtils.assumeWorkingJMockit();
new WindowMocker();
test = new ReplacementPreferenceTable(new ArrayList<PrefEntry>());
}
@Test
public void test() {
new ReplacementPreferenceTableMock(true);
assertNotNull(test.addPreference(new JPanel()));
new ReplacementPreferenceTableMock(false);
assertNull(test.addPreference(new JPanel()));
}
}