Changeset 18804 in josm for trunk/test/unit/org
- Timestamp:
- 2023-08-13T00:20:18+02:00 (15 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditorTest.java
r18799 r18804 2 2 package org.openstreetmap.josm.gui.dialogs.relation; 3 3 4 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 4 5 import static org.junit.jupiter.api.Assertions.assertEquals; 6 import static org.junit.jupiter.api.Assertions.assertFalse; 7 import static org.junit.jupiter.api.Assertions.assertInstanceOf; 5 8 import static org.junit.jupiter.api.Assertions.assertNotNull; 6 9 import static org.junit.jupiter.api.Assertions.assertNull; 7 10 import static org.junit.jupiter.api.Assertions.assertSame; 8 11 import static org.openstreetmap.josm.tools.I18n.tr; 12 13 import java.awt.Container; 14 import java.awt.event.WindowEvent; 9 15 import java.util.Collection; 10 16 import java.util.Collections; 11 17 import java.util.concurrent.atomic.AtomicReference; 18 19 import javax.swing.Action; 20 import javax.swing.JButton; 12 21 import javax.swing.JOptionPane; 13 22 import javax.swing.JPanel; 14 23 24 import mockit.Invocation; 15 25 import mockit.Mock; 16 26 import mockit.MockUp; 27 28 import org.junit.jupiter.api.BeforeEach; 17 29 import org.junit.jupiter.api.Test; 18 30 import org.junit.platform.commons.support.ReflectionSupport; … … 22 34 import org.openstreetmap.josm.data.osm.OsmPrimitive; 23 35 import org.openstreetmap.josm.data.osm.Relation; 36 import org.openstreetmap.josm.data.osm.RelationMember; 24 37 import org.openstreetmap.josm.data.osm.Way; 38 import org.openstreetmap.josm.gui.MainApplication; 39 import org.openstreetmap.josm.gui.SideButton; 40 import org.openstreetmap.josm.gui.dialogs.RelationListDialog; 41 import org.openstreetmap.josm.gui.dialogs.relation.actions.AddSelectedAtStartAction; 25 42 import org.openstreetmap.josm.gui.dialogs.relation.actions.IRelationEditorActionAccess; 26 43 import org.openstreetmap.josm.gui.dialogs.relation.actions.PasteMembersAction; … … 31 48 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 32 49 import org.openstreetmap.josm.testutils.annotations.Main; 50 import org.openstreetmap.josm.testutils.annotations.Projection; 33 51 import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; 34 52 import org.openstreetmap.josm.testutils.mockers.WindowMocker; … … 39 57 @BasicPreferences 40 58 @Main 59 @Projection 41 60 public class GenericRelationEditorTest { 42 61 /** … … 80 99 } 81 100 }; 101 } 102 103 @BeforeEach 104 void setup() { 105 new PasteMembersActionMock(); 106 new WindowMocker(); 82 107 } 83 108 … … 137 162 @Test 138 163 void testNonRegression23091() throws Exception { 139 new MockUp<PasteMembersAction>() {140 @Mock141 protected void updateEnabledState() {142 // Do nothing143 }144 };145 new WindowMocker();146 147 164 DataSet ds = new DataSet(); 148 165 Relation relation = new Relation(1); … … 161 178 assertSame(relation, selection.iterator().next(), "The selection should be the same"); 162 179 } 180 181 /** 182 * Ensure that users can create new relations and modify them. 183 */ 184 @Test 185 void testNonRegression23116() { 186 // Setup the mocks 187 final AtomicReference<RelationEditor> editorReference = new AtomicReference<>(); 188 new MockUp<RelationEditor>() { 189 @Mock public RelationEditor getEditor(Invocation invocation, OsmDataLayer layer, Relation r, 190 Collection<RelationMember> selectedMembers) { 191 editorReference.set(invocation.proceed(layer, r, selectedMembers)); 192 return editorReference.get(); 193 } 194 }; 195 // We want to go through the `setVisible` code, just in case. So we have to mock the window location 196 new MockUp<GenericRelationEditor>() { 197 @Mock public void setVisible(boolean visible) { 198 // Do nothing. Ideally, we would just mock the awt methods called, but that would take a lot of mocking. 199 } 200 }; 201 // Set up the data 202 final DataSet dataSet = new DataSet(); 203 MainApplication.getLayerManager().addLayer(new OsmDataLayer(dataSet, "GenericRelationEditorTest.testNonRegression23116", null)); 204 dataSet.addPrimitive(TestUtils.newNode("")); 205 dataSet.setSelected(dataSet.allPrimitives()); 206 final RelationListDialog relationListDialog = new RelationListDialog(); 207 try { 208 final Action newAction = ((SideButton) getComponent(relationListDialog, 2, 0, 0)).getAction(); 209 assertEquals("class org.openstreetmap.josm.gui.dialogs.RelationListDialog$NewAction", 210 newAction.getClass().toString()); 211 // Now get the buttons we want to push 212 newAction.actionPerformed(null); 213 final GenericRelationEditor editor = assertInstanceOf(GenericRelationEditor.class, editorReference.get()); 214 final JButton okAction = getComponent(editor, 0, 1, 0, 2, 0); 215 assertEquals(tr("Delete"), okAction.getText(), "OK is Delete until the relation actually has data"); 216 assertNotNull(editor); 217 final TagEditorPanel tagEditorPanel = getComponent(editor, 0, 1, 0, 1, 0, 0, 1, 1); 218 // We need at least one tag for the action to not be "Delete". 219 tagEditorPanel.getModel().add("type", "someUnknownTypeHere"); 220 final Action addAtStartAction = assertInstanceOf(AddSelectedAtStartAction.class, 221 ((JButton) getComponent(editor, 0, 1, 0, 1, 0, 0, 2, 0, 2, 1, 2, 0, 0)).getAction()); 222 // Perform the actual test. 223 assertDoesNotThrow(() -> addAtStartAction.actionPerformed(null)); 224 assertDoesNotThrow(() -> okAction.getAction().actionPerformed(null)); 225 assertFalse(dataSet.getRelations().isEmpty()); 226 assertSame(dataSet.getNodes().iterator().next(), 227 dataSet.getRelations().iterator().next().getMember(0).getNode()); 228 } finally { 229 // This avoids an issue with the cleanup code and the mocks for this test 230 if (editorReference.get() != null) { 231 RelationDialogManager.getRelationDialogManager().windowClosed(new WindowEvent(editorReference.get(), 0)); 232 } 233 } 234 } 235 236 @SuppressWarnings("unchecked") 237 private static <T extends Container> T getComponent(Container parent, int... tree) { 238 Container current = parent; 239 for (int i : tree) { 240 current = (Container) current.getComponent(i); 241 } 242 return (T) current; 243 } 244 245 private static class PasteMembersActionMock extends MockUp<PasteMembersAction> { 246 @Mock 247 protected void updateEnabledState() { 248 // Do nothing 249 } 250 } 163 251 }
Note:
See TracChangeset
for help on using the changeset viewer.