| 1 | package org.openstreetmap.josm.gui.dialogs.layer; |
| 2 | |
| 3 | import static org.junit.jupiter.api.Assertions.assertAll; |
| 4 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 7 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 8 | import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 9 | import static org.junit.jupiter.api.Assertions.assertSame; |
| 10 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 11 | |
| 12 | import java.util.Objects; |
| 13 | import java.util.concurrent.atomic.AtomicInteger; |
| 14 | import java.util.function.Supplier; |
| 15 | |
| 16 | import org.junit.jupiter.api.BeforeEach; |
| 17 | import org.junit.jupiter.api.Test; |
| 18 | import org.openstreetmap.josm.data.osm.DataSet; |
| 19 | import org.openstreetmap.josm.gui.MainApplication; |
| 20 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog; |
| 21 | import org.openstreetmap.josm.gui.layer.Layer; |
| 22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
| 23 | import org.openstreetmap.josm.testutils.annotations.Main; |
| 24 | import org.openstreetmap.josm.testutils.annotations.Projection; |
| 25 | |
| 26 | /** |
| 27 | * Test class for {@link DeleteLayerAction} |
| 28 | */ |
| 29 | @Main |
| 30 | @Projection |
| 31 | class 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 |