source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/event/SelectionEventManagerTest.java @ 12098

Last change on this file since 12098 was 12098, checked in by michael2402, 22 months ago

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

File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm.event;
3
4import static org.junit.Assert.assertEquals;
5
6import java.util.Arrays;
7import java.util.Collection;
8import java.util.HashSet;
9import java.util.List;
10
11import org.junit.Rule;
12import org.junit.Test;
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
15import org.openstreetmap.josm.data.SelectionChangedListener;
16import org.openstreetmap.josm.data.osm.DataSelectionListener;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * Tests the {@link SelectionEventManager}
26 * @author Michael Zangl
27 * @since 12048
28 */
29public class SelectionEventManagerTest {
30    private final class SelectionListener implements SelectionChangedListener, DataSelectionListener {
31        private Collection<? extends OsmPrimitive> newSelection;
32        private final String name;
33
34        public SelectionListener(String name) {
35            this.name = name;
36        }
37
38        @Override
39        public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
40            this.newSelection = newSelection;
41        }
42
43        @Override
44        public void selectionChanged(SelectionChangeEvent event) {
45            this.newSelection = event.getSelection();
46        }
47    }
48
49    /**
50     */
51    @Rule
52    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
53    public JOSMTestRules test = new JOSMTestRules().preferences();
54
55    /**
56     * Tests that events in the active layer are propagated.
57     */
58    @Test
59    public void testEventPropagation() {
60        // automatically adds the layers
61        CommandTestDataWithRelation testData1 = new CommandTestDataWithRelation();
62        CommandTestDataWithRelation testData2 = new CommandTestDataWithRelation();
63        Main.getLayerManager().setActiveLayer(testData1.layer);
64        assertEquals(testData1.layer, Main.getLayerManager().getEditLayer());
65
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);
77
78        // active layer, should change
79        testData1.layer.data.setSelected(testData1.existingNode.getPrimitiveId());
80        assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode)));
81
82        testData1.layer.data.clearSelection(testData1.existingNode.getPrimitiveId());
83        assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList()));
84
85        testData1.layer.data.addSelected(testData1.existingNode2.getPrimitiveId());
86        assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList(testData1.existingNode2)));
87
88        // changing to other dataset should trigger a empty selection
89        Main.getLayerManager().setActiveLayer(testData2.layer);
90        assertSelectionEquals(listeners, new HashSet<OsmPrimitive>(Arrays.asList()));
91
92        // This should not trigger anything, since the layer is not active any more.
93        testData1.layer.data.clearSelection(testData1.existingNode.getPrimitiveId());
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        }
117    }
118
119}
Note: See TracBrowser for help on using the repository browser.