Ticket #23057: 23057.patch

File 23057.patch, 10.6 KB (added by taylor.smock, 17 months ago)
  • src/org/openstreetmap/josm/gui/dialogs/layer/DeleteLayerAction.java

    Subject: [PATCH] #23057
    ---
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/gui/dialogs/layer/DeleteLayerAction.java b/src/org/openstreetmap/josm/gui/dialogs/layer/DeleteLayerAction.java
    a b  
    55
    66import java.awt.Component;
    77import java.awt.event.ActionEvent;
     8import java.util.HashMap;
    89import java.util.List;
     10import java.util.Map;
    911
    1012import javax.swing.AbstractAction;
    1113import javax.swing.JMenuItem;
     
    4547            return;
    4648        if (!SaveLayersDialog.saveUnsavedModifications(selectedLayers, SaveLayersDialog.Reason.DELETE))
    4749            return;
     50        final Map<Integer, Layer> layerMap = model.selectedIndices().filter(i -> model.getLayer(i) != null)
     51                .collect(HashMap::new, (map, value) -> map.put(value, model.getLayer(value)), HashMap::putAll);
    4852        for (Layer l: selectedLayers) {
    4953            if (model.getLayerManager().containsLayer(l)) {
    5054                // it may happen that this call removes other layers.
     
    5256                model.getLayerManager().removeLayer(l);
    5357            }
    5458        }
     59        // Set the next active layer to the next visible layer
     60        if (layerMap.size() == 1) {
     61            final int selected = Math.min(layerMap.keySet().iterator().next(), model.getRowCount() - 1);
     62            int currentLayerIndex = selected;
     63            Layer layer = model.getLayer(currentLayerIndex);
     64            // If the user has the last layer selected, we need to wrap around.
     65            boolean reversed = false;
     66            while (layer != null && !layer.isVisible() && currentLayerIndex < model.getRowCount() && currentLayerIndex >= 0) {
     67                if (reversed) {
     68                    currentLayerIndex--;
     69                } else {
     70                    currentLayerIndex++;
     71                }
     72                if (currentLayerIndex == model.getRowCount()) {
     73                    reversed = true;
     74                    currentLayerIndex = selected;
     75                }
     76                layer = model.getLayer(currentLayerIndex);
     77            }
     78            if (layer != null) {
     79                model.getLayerManager().setActiveLayer(layer);
     80                // Reset the selection
     81                model.getSelectionModel().setSelectionInterval(selected, selected);
     82            }
     83        }
    5584    }
    5685
    5786    @Override
  • test/unit/org/openstreetmap/josm/actions/DeleteLayerActionTest.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/test/unit/org/openstreetmap/josm/actions/DeleteLayerActionTest.java b/test/unit/org/openstreetmap/josm/actions/DeleteLayerActionTest.java
    a b  
    44import static org.junit.jupiter.api.Assertions.assertNotNull;
    55import static org.junit.jupiter.api.Assertions.assertNull;
    66
    7 import org.junit.jupiter.api.extension.RegisterExtension;
    87import org.junit.jupiter.api.Test;
    98import org.openstreetmap.josm.data.osm.DataSet;
    109import org.openstreetmap.josm.gui.MainApplication;
    1110import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    12 import org.openstreetmap.josm.testutils.JOSMTestRules;
    13 
    14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     11import org.openstreetmap.josm.testutils.annotations.Main;
     12import org.openstreetmap.josm.testutils.annotations.Projection;
    1513
    1614/**
    1715 * Unit tests for class {@link DeleteLayerAction}.
    1816 */
     17@Main
     18@Projection
    1919final class DeleteLayerActionTest {
    20 
    21     /**
    22      * Setup test.
    23      */
    24     @RegisterExtension
    25     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    26     public static JOSMTestRules test = new JOSMTestRules().main().projection();
    27 
    2820    /**
    2921     * Unit test of {@link DeleteLayerAction#actionPerformed}
    3022     */
  • new file test/unit/org/openstreetmap/josm/gui/dialogs/layer/DeleteLayerActionTest.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/layer/DeleteLayerActionTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/layer/DeleteLayerActionTest.java
    new file mode 100644
    - +  
     1package org.openstreetmap.josm.gui.dialogs.layer;
     2
     3import static org.junit.jupiter.api.Assertions.assertAll;
     4import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertFalse;
     6import static org.junit.jupiter.api.Assertions.assertInstanceOf;
     7import static org.junit.jupiter.api.Assertions.assertNotNull;
     8import static org.junit.jupiter.api.Assertions.assertNotSame;
     9import static org.junit.jupiter.api.Assertions.assertSame;
     10import static org.junit.jupiter.api.Assertions.assertTrue;
     11
     12import java.util.Objects;
     13import java.util.concurrent.atomic.AtomicInteger;
     14import java.util.function.Supplier;
     15
     16import org.junit.jupiter.api.BeforeEach;
     17import org.junit.jupiter.api.Test;
     18import org.openstreetmap.josm.data.osm.DataSet;
     19import org.openstreetmap.josm.gui.MainApplication;
     20import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
     21import org.openstreetmap.josm.gui.layer.Layer;
     22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     23import org.openstreetmap.josm.testutils.annotations.Main;
     24import org.openstreetmap.josm.testutils.annotations.Projection;
     25
     26/**
     27 * Test class for {@link DeleteLayerAction}
     28 */
     29@Main
     30@Projection
     31class DeleteLayerActionTest {
     32    private LayerListDialog.LayerListModel model;
     33    private DeleteLayerAction deleteLayerAction;
     34    private ShowHideLayerAction showHideLayerAction;
     35
     36    @BeforeEach
     37    void setup() {
     38        final AtomicInteger counter = new AtomicInteger();
     39        final Supplier<OsmDataLayer> layerSupplier = () -> new OsmDataLayer(new DataSet(), "testActiveLayer" + counter.getAndIncrement(), null);
     40        for (int i = 0; i < 10; i++) {
     41            MainApplication.getLayerManager().addLayer(layerSupplier.get());
     42        }
     43        final LayerListDialog layerListDialog = LayerListDialog.getInstance();
     44        this.model = layerListDialog.getModel();
     45        this.deleteLayerAction = layerListDialog.createDeleteLayerAction();
     46        this.showHideLayerAction = layerListDialog.createShowHideLayerAction();
     47    }
     48
     49    @Test
     50    void testSetActiveLayerOnlyOneVisible0to8() {
     51        hideRange(0, 8);
     52        assertEquals(9, model.getSelectedLayers().size());
     53        assertEquals(1, model.getLayers().stream().filter(Layer::isVisible).count());
     54        deleteLayerAction.actionPerformed(null);
     55        assertEquals(1, model.getSelectedLayers().size());
     56        final Layer layer = assertInstanceOf(OsmDataLayer.class, model.getLayer(0));
     57        assertNotNull(layer);
     58        assertTrue(layer.isVisible());
     59        assertSame(layer, model.getLayerManager().getActiveLayer());
     60        assertEquals("testActiveLayer0", layer.getName());
     61    }
     62
     63    @Test
     64    void testSetActiveLayerOnlyOneVisible1to9() {
     65        hideRange(1, 9);
     66        assertEquals(9, model.getSelectedLayers().size());
     67        assertEquals(1, model.getLayers().stream().filter(Layer::isVisible).count());
     68        deleteLayerAction.actionPerformed(null);
     69        assertEquals(1, model.getSelectedLayers().size());
     70        final Layer layer = assertInstanceOf(OsmDataLayer.class, model.getLayer(0));
     71        assertNotNull(layer);
     72        assertTrue(layer.isVisible());
     73        assertSame(layer, model.getLayerManager().getActiveLayer());
     74        assertEquals("testActiveLayer9", layer.getName());
     75    }
     76
     77    @Test
     78    void testRemoveMiddleActiveWithSurroundingHiddenLayers() {
     79        hideRange(3, 3);
     80        hideRange(5, 5);
     81        final Layer toRemove = model.getLayer(4);
     82        assertNotNull(toRemove);
     83        assertTrue(toRemove.isVisible());
     84        assertFalse(Objects.requireNonNull(model.getLayer(3)).isVisible());
     85        assertFalse(Objects.requireNonNull(model.getLayer(5)).isVisible());
     86        model.getLayerManager().setActiveLayer(toRemove);
     87        model.setSelectedLayer(toRemove);
     88        deleteLayerAction.actionPerformed(null);
     89        assertSame(model.getLayerManager().getActiveLayer(), model.getLayer(5));
     90        assertEquals("testActiveLayer3", Objects.requireNonNull(model.getLayer(5)).getName());
     91        assertAll(model.getLayers().stream().map(layer -> () -> assertNotSame(toRemove, layer)));
     92    }
     93
     94    @Test
     95    void testRemoveTopActiveWithSurroundingHiddenLayers() {
     96        hideRange(1, 1);
     97        final Layer toRemove = model.getLayer(0);
     98        assertNotNull(toRemove);
     99        assertTrue(toRemove.isVisible());
     100        assertFalse(Objects.requireNonNull(model.getLayer(1)).isVisible());
     101        model.getLayerManager().setActiveLayer(toRemove);
     102        model.setSelectedLayer(toRemove);
     103        deleteLayerAction.actionPerformed(null);
     104        assertSame(model.getLayerManager().getActiveLayer(), model.getLayer(1));
     105        assertEquals("testActiveLayer7", Objects.requireNonNull(model.getLayer(1)).getName());
     106        assertAll(model.getLayers().stream().map(layer -> () -> assertNotSame(toRemove, layer)));
     107    }
     108
     109    @Test
     110    void testRemoveBottomActiveWithSurroundingHiddenLayers() {
     111        hideRange(8, 8);
     112        final Layer toRemove = model.getLayer(9);
     113        assertNotNull(toRemove);
     114        assertTrue(toRemove.isVisible());
     115        assertFalse(Objects.requireNonNull(model.getLayer(8)).isVisible());
     116        model.getLayerManager().setActiveLayer(toRemove);
     117        model.setSelectedLayer(toRemove);
     118        deleteLayerAction.actionPerformed(null);
     119        assertSame(model.getLayerManager().getActiveLayer(), model.getLayer(7));
     120        assertEquals("testActiveLayer2", Objects.requireNonNull(model.getLayer(7)).getName());
     121        assertAll(model.getLayers().stream().map(layer -> () -> assertNotSame(toRemove, layer)));
     122    }
     123
     124    private void hideRange(int start, int end) {
     125        model.getSelectionModel().setSelectionInterval(start, end);
     126        showHideLayerAction.actionPerformed(null);
     127    }
     128}
     129 No newline at end of file