Changeset 18706 in josm for trunk/test/unit/org
- Timestamp:
- 2023-04-20T22:13:25+02:00 (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java
r17275 r18706 6 6 import static org.junit.jupiter.api.Assertions.assertTrue; 7 7 8 import java.awt.Component; 9 import java.awt.GraphicsEnvironment; 10 import java.io.File; 8 11 import java.util.Collections; 9 12 import java.util.List; 13 10 14 import javax.swing.JComponent; 11 15 import javax.swing.JLabel; … … 13 17 import javax.swing.JOptionPane; 14 18 15 import org.junit.jupiter.api.extension.RegisterExtension; 19 import mockit.Invocation; 20 import mockit.Mock; 21 import mockit.MockUp; 16 22 import org.junit.jupiter.api.Test; 23 import org.junit.jupiter.params.ParameterizedTest; 24 import org.junit.jupiter.params.provider.EnumSource; 25 import org.openstreetmap.josm.command.AddPrimitivesCommand; 26 import org.openstreetmap.josm.data.coor.LatLon; 17 27 import org.openstreetmap.josm.data.osm.DataSet; 28 import org.openstreetmap.josm.data.osm.Node; 29 import org.openstreetmap.josm.data.osm.UploadPolicy; 18 30 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 19 import org.openstreetmap.josm.testutils. JOSMTestRules;31 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 20 32 import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; 21 22 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 33 import org.openstreetmap.josm.testutils.mockers.WindowMocker; 23 34 24 35 /** 25 36 * Unit tests of {@link SaveLayersDialog} class. 26 37 */ 38 @BasicPreferences 27 39 class SaveLayersDialogTest { 28 29 /**30 * Setup tests31 */32 @RegisterExtension33 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")34 public JOSMTestRules test = new JOSMTestRules();35 36 40 /** 37 41 * Test of {@link SaveLayersDialog#confirmSaveLayerInfosOK}. … … 118 122 assertTrue(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel())); 119 123 } 124 125 /** 126 * Non-regression test for #22817: No warning when deleting a layer with changes and discourages upload 127 * @param policy The upload policy to test 128 */ 129 @ParameterizedTest 130 @EnumSource(value = UploadPolicy.class) 131 void testNonRegression22817(UploadPolicy policy) { 132 final OsmDataLayer osmDataLayer = new OsmDataLayer(new DataSet(), null, null); 133 osmDataLayer.getDataSet().setUploadPolicy(policy); 134 // BLOCKED files don't have a way to become blocked via the UI, so they must be loaded from disk. 135 if (policy == UploadPolicy.BLOCKED) { 136 osmDataLayer.setAssociatedFile(new File("/dev/null")); 137 } 138 new AddPrimitivesCommand(Collections.singletonList(new Node(LatLon.ZERO).save()), Collections.emptyList(), osmDataLayer.getDataSet()) 139 .executeCommand(); 140 assertTrue(osmDataLayer.getDataSet().isModified()); 141 new WindowMocker(); 142 // Needed since the *first call* is to check whether we are in a headless environment 143 new GraphicsEnvironmentMock(); 144 // Needed since we need to mock out the UI 145 SaveLayersDialogMock saveLayersDialogMock = new SaveLayersDialogMock(); 146 147 assertTrue(SaveLayersDialog.saveUnsavedModifications(Collections.singleton(osmDataLayer), SaveLayersDialog.Reason.DELETE)); 148 assertEquals(1, saveLayersDialogMock.getUserActionCalled, "The user should have been asked for an action on the layer"); 149 } 150 151 private static class GraphicsEnvironmentMock extends MockUp<GraphicsEnvironment> { 152 @Mock 153 public static boolean isHeadless(Invocation invocation) { 154 return false; 155 } 156 } 157 158 private static class SaveLayersDialogMock extends MockUp<SaveLayersDialog> { 159 private final SaveLayersModel model = new SaveLayersModel(); 160 private int getUserActionCalled = 0; 161 @Mock 162 public void $init(Component parent) { 163 // Do nothing 164 } 165 166 @Mock 167 public void prepareForSavingAndUpdatingLayers(final SaveLayersDialog.Reason reason) { 168 // Do nothing 169 } 170 171 @Mock 172 public SaveLayersModel getModel() { 173 return this.model; 174 } 175 176 @Mock 177 public void setVisible(boolean b) { 178 // Do nothing 179 } 180 181 @Mock 182 public SaveLayersDialog.UserAction getUserAction() { 183 this.getUserActionCalled++; 184 return SaveLayersDialog.UserAction.PROCEED; 185 } 186 187 @Mock 188 public void closeDialog() { 189 // Do nothing 190 } 191 } 120 192 }
Note:
See TracChangeset
for help on using the changeset viewer.