Changeset 12098 in josm for trunk/test/unit


Ignore:
Timestamp:
2017-05-10T23:00:16+02:00 (8 years ago)
Author:
michael2402
Message:

Allow to globally add the new selection listeners that get detailed events.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/osm/event/SelectionEventManagerTest.java

    r12060 r12098  
    33
    44import static org.junit.Assert.assertEquals;
    5 import static org.junit.Assert.assertNull;
    65
    76import java.util.Arrays;
    87import java.util.Collection;
    98import java.util.HashSet;
     9import java.util.List;
    1010
    1111import org.junit.Rule;
     
    1414import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
    1515import org.openstreetmap.josm.data.SelectionChangedListener;
     16import org.openstreetmap.josm.data.osm.DataSelectionListener;
    1617import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1718import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
     19import org.openstreetmap.josm.gui.util.GuiHelper;
    1820import org.openstreetmap.josm.testutils.JOSMTestRules;
    1921
     
    2628 */
    2729public class SelectionEventManagerTest {
    28     private final class SelectionListener implements SelectionChangedListener {
     30    private final class SelectionListener implements SelectionChangedListener, DataSelectionListener {
    2931        private Collection<? extends OsmPrimitive> newSelection;
     32        private final String name;
     33
     34        public SelectionListener(String name) {
     35            this.name = name;
     36        }
    3037
    3138        @Override
    3239        public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
    3340            this.newSelection = newSelection;
     41        }
     42
     43        @Override
     44        public void selectionChanged(SelectionChangeEvent event) {
     45            this.newSelection = event.getSelection();
    3446        }
    3547    }
     
    5264        assertEquals(testData1.layer, Main.getLayerManager().getEditLayer());
    5365
    54         SelectionListener listener = new SelectionListener();
    55         SelectionEventManager.getInstance().addSelectionListener(listener, FireMode.IMMEDIATELY);
    56         assertNull(listener.newSelection);
     66        SelectionListener listener1 = new SelectionListener("IMMEDIATELY");
     67        SelectionListener listener2 = new SelectionListener("IN_EDT_CONSOLIDATED");
     68        SelectionListener listener3 = new SelectionListener("normal");
     69        SelectionListener listener4 = new SelectionListener("edt");
     70        SelectionEventManager instance = SelectionEventManager.getInstance();
     71        instance.addSelectionListener(listener1, FireMode.IMMEDIATELY);
     72        instance.addSelectionListener(listener2, FireMode.IN_EDT_CONSOLIDATED);
     73        instance.addSelectionListener(listener3);
     74        instance.addSelectionListenerForEdt(listener4);
     75        List<SelectionListener> listeners = Arrays.asList(listener1, listener2, listener3, listener4);
     76        assertSelectionEquals(listeners, null);
    5777
    5878        // active layer, should change
    5979        testData1.layer.data.setSelected(testData1.existingNode.getPrimitiveId());
    60         assertEquals(new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode)), listener.newSelection);
     80        assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode)));
    6181
    62         listener.newSelection = null;
    6382        testData1.layer.data.clearSelection(testData1.existingNode.getPrimitiveId());
    64         assertEquals(new HashSet<OsmPrimitive>(Arrays.asList()), listener.newSelection);
     83        assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList()));
    6584
    66         listener.newSelection = null;
    6785        testData1.layer.data.addSelected(testData1.existingNode2.getPrimitiveId());
    68         assertEquals(new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode2)), listener.newSelection);
     86        assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode2)));
    6987
    7088        // changing to other dataset should trigger a empty selection
    71         listener.newSelection = null;
    7289        Main.getLayerManager().setActiveLayer(testData2.layer);
    73         assertEquals(new HashSet<OsmPrimitive>(Arrays.asList()), listener.newSelection);
     90        assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList()));
    7491
    7592        // This should not trigger anything, since the layer is not active any more.
    76         listener.newSelection = null;
    7793        testData1.layer.data.clearSelection(testData1.existingNode.getPrimitiveId());
    78         assertNull(listener.newSelection);
     94        assertSelectionEquals(listeners, null);
     95
     96        testData2.layer.data.setSelected(testData2.existingNode.getPrimitiveId());
     97        assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList(testData2.existingNode)));
     98
     99        // removal
     100        instance.removeSelectionListener((SelectionChangedListener) listener1);
     101        instance.removeSelectionListener((SelectionChangedListener) listener2);
     102        instance.removeSelectionListener((DataSelectionListener) listener3);
     103        instance.removeSelectionListener((DataSelectionListener) listener4);
     104
     105        // no event triggered now
     106        testData2.layer.data.setSelected(testData2.existingNode2.getPrimitiveId());
     107        assertSelectionEquals(listeners, null);
     108    }
     109
     110    private void assertSelectionEquals(List<SelectionListener> listeners, Object should) {
     111        // sync
     112        GuiHelper.runInEDTAndWait(() -> {});
     113        for (SelectionListener listener : listeners) {
     114            assertEquals(listener.name, should, listener.newSelection);
     115            listener.newSelection = null;
     116        }
    79117    }
    80118
Note: See TracChangeset for help on using the changeset viewer.