Changeset 12098 in josm for trunk/test/unit
- Timestamp:
- 2017-05-10T23:00:16+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/data/osm/event/SelectionEventManagerTest.java
r12060 r12098 3 3 4 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertNull;6 5 7 6 import java.util.Arrays; 8 7 import java.util.Collection; 9 8 import java.util.HashSet; 9 import java.util.List; 10 10 11 11 import org.junit.Rule; … … 14 14 import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation; 15 15 import org.openstreetmap.josm.data.SelectionChangedListener; 16 import org.openstreetmap.josm.data.osm.DataSelectionListener; 16 17 import org.openstreetmap.josm.data.osm.OsmPrimitive; 17 18 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode; 19 import org.openstreetmap.josm.gui.util.GuiHelper; 18 20 import org.openstreetmap.josm.testutils.JOSMTestRules; 19 21 … … 26 28 */ 27 29 public class SelectionEventManagerTest { 28 private final class SelectionListener implements SelectionChangedListener {30 private final class SelectionListener implements SelectionChangedListener, DataSelectionListener { 29 31 private Collection<? extends OsmPrimitive> newSelection; 32 private final String name; 33 34 public SelectionListener(String name) { 35 this.name = name; 36 } 30 37 31 38 @Override 32 39 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) { 33 40 this.newSelection = newSelection; 41 } 42 43 @Override 44 public void selectionChanged(SelectionChangeEvent event) { 45 this.newSelection = event.getSelection(); 34 46 } 35 47 } … … 52 64 assertEquals(testData1.layer, Main.getLayerManager().getEditLayer()); 53 65 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); 57 77 58 78 // active layer, should change 59 79 testData1.layer.data.setSelected(testData1.existingNode.getPrimitiveId()); 60 assert Equals(new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode)), listener.newSelection);80 assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode))); 61 81 62 listener.newSelection = null;63 82 testData1.layer.data.clearSelection(testData1.existingNode.getPrimitiveId()); 64 assert Equals(new HashSet<OsmPrimitive>(Arrays.asList()), listener.newSelection);83 assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList())); 65 84 66 listener.newSelection = null;67 85 testData1.layer.data.addSelected(testData1.existingNode2.getPrimitiveId()); 68 assert Equals(new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode2)), listener.newSelection);86 assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode2))); 69 87 70 88 // changing to other dataset should trigger a empty selection 71 listener.newSelection = null;72 89 Main.getLayerManager().setActiveLayer(testData2.layer); 73 assert Equals(new HashSet<OsmPrimitive>(Arrays.asList()), listener.newSelection);90 assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList())); 74 91 75 92 // This should not trigger anything, since the layer is not active any more. 76 listener.newSelection = null;77 93 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 } 79 117 } 80 118
Note:
See TracChangeset
for help on using the changeset viewer.