Changeset 11000 in josm


Ignore:
Timestamp:
2016-09-14T12:44:42+02:00 (8 years ago)
Author:
michael2402
Message:

Add layer manager test for resetState() and scheduleRemoval()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/LayerManagerTest.java

    r10744 r11000  
    1717import java.util.ArrayList;
    1818import java.util.Arrays;
     19import java.util.Collections;
    1920
    2021import javax.swing.Action;
     
    4142 */
    4243public class LayerManagerTest {
    43 
    4444    /**
    4545     * This is a layer that can be used in tests. It does not do anything and provides a simple, fake implementation.
     
    120120     */
    121121    protected class CapturingLayerChangeListener implements LayerChangeListener {
    122         private LayerAddEvent layerAdded;
    123         private LayerRemoveEvent layerRemoved;
    124         private LayerOrderChangeEvent layerOrderChanged;
     122        protected LayerAddEvent layerAdded;
     123        protected LayerRemoveEvent layerRemoved;
     124        protected LayerOrderChangeEvent layerOrderChanged;
    125125
    126126        @Override
     
    149149    }
    150150
     151    private final class ResetStateChangeListener extends CapturingLayerChangeListener {
     152        int removed = 0;
     153
     154        @Override
     155        public void layerRemoving(LayerRemoveEvent e) {
     156            // count only
     157            removed++;
     158        }
     159    }
     160
    151161    protected LayerManager layerManager;
    152162
     
    473483    }
    474484
     485    /**
     486     * Test {@link LayerRemoveEvent#scheduleRemoval(java.util.Collection)}
     487     */
     488    @Test
     489    public void testLayerRemoveScheduleRemoval() {
     490        TestLayer layer1 = new TestLayer();
     491        TestLayer layer2 = new TestLayer();
     492        layerManager.addLayer(layer1);
     493        layerManager.addLayer(layer2);
     494
     495        layerManager.addLayerChangeListener(new LayerChangeListener() {
     496            @Override
     497            public void layerRemoving(LayerRemoveEvent e) {
     498                    if (e.getRemovedLayer() == layer1) {
     499                        e.scheduleRemoval(Collections.singleton(layer2));
     500                    }
     501            }
     502
     503            @Override
     504            public void layerOrderChanged(LayerOrderChangeEvent e) {
     505                fail();
     506            }
     507
     508            @Override
     509            public void layerAdded(LayerAddEvent e) {
     510                fail();
     511            }
     512        });
     513
     514        layerManager.removeLayer(layer1);
     515        assertEquals(0, layerManager.getLayers().size());
     516    }
     517
     518    /**
     519     * Test {@link LayerManager#resetState()}
     520     */
     521    @Test
     522    public void testResetState() {
     523        ResetStateChangeListener changeListener = new ResetStateChangeListener();
     524        layerManager.addLayer(new TestLayer());
     525        layerManager.addLayerChangeListener(changeListener);
     526        layerManager.addLayer(new TestLayer());
     527        assertEquals(2, layerManager.getLayers().size());
     528        assertNotNull(changeListener.layerAdded);
     529
     530        layerManager.resetState();
     531        changeListener.layerAdded = null;
     532
     533        assertEquals(2, changeListener.removed);
     534        assertEquals(0, layerManager.getLayers().size());
     535        layerManager.addLayer(new TestLayer());
     536        assertNull(changeListener.layerAdded);
     537    }
     538
    475539}
Note: See TracChangeset for help on using the changeset viewer.