Changeset 18866 in josm for trunk/test/unit/org
- Timestamp:
- 2023-10-12T17:42:13+02:00 (13 months ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditorTest.java
r18804 r18866 9 9 import static org.junit.jupiter.api.Assertions.assertNull; 10 10 import static org.junit.jupiter.api.Assertions.assertSame; 11 import static org.junit.jupiter.api.Assertions.fail; 11 12 import static org.openstreetmap.josm.tools.I18n.tr; 12 13 14 import java.awt.Component; 13 15 import java.awt.Container; 16 import java.awt.event.MouseEvent; 17 import java.awt.event.MouseListener; 14 18 import java.awt.event.WindowEvent; 19 import java.util.ArrayDeque; 20 import java.util.Arrays; 15 21 import java.util.Collection; 16 22 import java.util.Collections; 17 23 import java.util.concurrent.atomic.AtomicReference; 24 import java.util.stream.Collectors; 18 25 19 26 import javax.swing.Action; 20 27 import javax.swing.JButton; 28 import javax.swing.JLabel; 21 29 import javax.swing.JOptionPane; 22 30 import javax.swing.JPanel; 23 24 import mockit.Invocation;25 import mockit.Mock;26 import mockit.MockUp;27 31 28 32 import org.junit.jupiter.api.BeforeEach; … … 36 40 import org.openstreetmap.josm.data.osm.RelationMember; 37 41 import org.openstreetmap.josm.data.osm.Way; 42 import org.openstreetmap.josm.gui.ExtendedDialog; 38 43 import org.openstreetmap.josm.gui.MainApplication; 39 44 import org.openstreetmap.josm.gui.SideButton; … … 45 50 import org.openstreetmap.josm.gui.tagging.TagEditorPanel; 46 51 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField; 52 import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset; 47 53 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetHandler; 54 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetLabel; 55 import org.openstreetmap.josm.spi.preferences.Config; 48 56 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 49 57 import org.openstreetmap.josm.testutils.annotations.Main; 50 58 import org.openstreetmap.josm.testutils.annotations.Projection; 59 import org.openstreetmap.josm.testutils.annotations.TaggingPresets; 60 import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker; 51 61 import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; 52 62 import org.openstreetmap.josm.testutils.mockers.WindowMocker; 63 import org.openstreetmap.josm.tools.JosmRuntimeException; 64 65 import mockit.Invocation; 66 import mockit.Mock; 67 import mockit.MockUp; 53 68 54 69 /** … … 105 120 new PasteMembersActionMock(); 106 121 new WindowMocker(); 122 } 123 124 private static AtomicReference<RelationEditor> setupGuiMocks() { 125 AtomicReference<RelationEditor> editorReference = new AtomicReference<>(); 126 new MockUp<RelationEditor>() { 127 @Mock public RelationEditor getEditor(Invocation invocation, OsmDataLayer layer, Relation r, 128 Collection<RelationMember> selectedMembers) { 129 editorReference.set(invocation.proceed(layer, r, selectedMembers)); 130 return editorReference.get(); 131 } 132 }; 133 // We want to go through the `setVisible` code, just in case. So we have to mock the window location 134 new MockUp<GenericRelationEditor>() { 135 @Mock public void setVisible(boolean visible) { 136 // Do nothing. Ideally, we would just mock the awt methods called, but that would take a lot of mocking. 137 } 138 }; 139 return editorReference; 107 140 } 108 141 … … 185 218 void testNonRegression23116() { 186 219 // 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 }; 220 final AtomicReference<RelationEditor> editorReference = setupGuiMocks(); 201 221 // Set up the data 202 222 final DataSet dataSet = new DataSet(); … … 234 254 } 235 255 256 /** 257 * Ensure that users can create new relations with a preset available and open the preset. 258 * See {@link TaggingPreset#showAndApply} for where a relation may exist without a dataset. 259 */ 260 @BasicPreferences 261 @TaggingPresets 262 @Test 263 void testNonRegression23196() { 264 // This happens when the preset validator is enabled (Preferences -> `Tagging Presets` -> `Run data validator on user input`) 265 Config.getPref().putBoolean("taggingpreset.validator", true); 266 // Setup the mocks 267 final AtomicReference<RelationEditor> editorReference = setupGuiMocks(); 268 new ExtendedDialogMocker(Collections.singletonMap("Change 1 object", "Apply Preset")) { 269 @Override 270 protected String getString(ExtendedDialog instance) { 271 return instance.getTitle(); 272 } 273 }; 274 // Set up the data 275 final DataSet dataSet = new DataSet(); 276 final OsmDataLayer layer = new OsmDataLayer(dataSet, "GenericRelationEditorTest.testNonRegression23196", null); 277 MainApplication.getLayerManager().addLayer(layer); 278 dataSet.addPrimitive(TestUtils.newNode("")); 279 dataSet.setSelected(dataSet.allPrimitives()); 280 try { 281 RelationEditor.getEditor(layer, TestUtils.newRelation("type=multipolygon"), 282 dataSet.getSelected().stream().map(p -> new RelationMember("", p)).collect(Collectors.toList())); 283 final GenericRelationEditor editor = assertInstanceOf(GenericRelationEditor.class, editorReference.get()); 284 TaggingPresetLabel label = getComponentByNameOrText(TaggingPresetLabel.class, editor, "Relations/Multipolygon …"); 285 final MouseEvent mouseEvent = new MouseEvent(label, 0, 0, 0, 0, 0, 0, false); 286 for (MouseListener listener : label.getMouseListeners()) { 287 assertDoesNotThrow(() -> listener.mouseClicked(mouseEvent)); 288 } 289 } finally { 290 // This avoids an issue with the cleanup code and the mocks for this test 291 if (editorReference.get() != null) { 292 RelationDialogManager.getRelationDialogManager().windowClosed(new WindowEvent(editorReference.get(), 0)); 293 } 294 } 295 } 296 297 private static <T extends Component> T getComponentByNameOrText(Class<T> clazz, Container parent, String name) { 298 final ArrayDeque<Component> componentDeque = new ArrayDeque<>(Collections.singletonList(parent)); 299 while (!componentDeque.isEmpty()) { 300 final Component current = componentDeque.pop(); 301 if (current instanceof Container) { 302 componentDeque.addAll(Arrays.asList(((Container) current).getComponents())); 303 } 304 if (clazz.isInstance(current)) { 305 T component = clazz.cast(current); 306 if (name.equals(component.getName())) { 307 return component; 308 } else if (component instanceof JLabel && name.equals(((JLabel) component).getText())) { 309 return component; 310 } 311 } 312 } 313 fail("Component with name " + name + " not found"); 314 throw new JosmRuntimeException("This should never happen due to the fail line"); 315 } 316 236 317 @SuppressWarnings("unchecked") 237 318 private static <T extends Container> T getComponent(Container parent, int... tree) { -
trunk/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java
r18690 r18866 171 171 } 172 172 return resultField; 173 }174 175 @Mock176 private void setupDialog(final Invocation invocation) {177 if (!GraphicsEnvironment.isHeadless()) {178 invocation.proceed();179 }180 // else do nothing - WindowMocker-ed Windows doesn't work well enough for some of the181 // component constructions182 173 } 183 174 -
trunk/test/unit/org/openstreetmap/josm/testutils/mockers/WindowMocker.java
r14081 r18866 2 2 package org.openstreetmap.josm.testutils.mockers; 3 3 4 import java.awt.Component; 4 5 import java.awt.Frame; 5 6 import java.awt.GraphicsConfiguration; … … 37 38 private void $init(final Invocation invocation, final Window window, final GraphicsConfiguration gc) { 38 39 } 40 41 @Mock 42 public void pack() { 43 } 44 45 @Mock 46 public void setLocationRelativeTo(final Component c) { 47 } 39 48 }
Note:
See TracChangeset
for help on using the changeset viewer.