| | 1 | package org.openstreetmap.josm.actions; |
| | 2 | |
| | 3 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| | 4 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| | 5 | import static org.junit.jupiter.api.Assertions.fail; |
| | 6 | |
| | 7 | import java.awt.GraphicsEnvironment; |
| | 8 | import java.util.Collections; |
| | 9 | import java.util.concurrent.TimeUnit; |
| | 10 | |
| | 11 | import org.junit.jupiter.api.Test; |
| | 12 | import org.junit.jupiter.api.extension.RegisterExtension; |
| | 13 | import org.openstreetmap.josm.TestUtils; |
| | 14 | import org.openstreetmap.josm.command.AddPrimitivesCommand; |
| | 15 | import org.openstreetmap.josm.data.UserIdentityManager; |
| | 16 | import org.openstreetmap.josm.data.coor.LatLon; |
| | 17 | import org.openstreetmap.josm.data.osm.DataSet; |
| | 18 | import org.openstreetmap.josm.data.osm.Node; |
| | 19 | import org.openstreetmap.josm.gui.MainApplication; |
| | 20 | import org.openstreetmap.josm.gui.io.UploadDialog; |
| | 21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
| | 22 | import org.openstreetmap.josm.gui.util.GuiHelper; |
| | 23 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| | 24 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences; |
| | 25 | import org.openstreetmap.josm.testutils.mockers.WindowMocker; |
| | 26 | import org.openstreetmap.josm.tools.Logging; |
| | 27 | |
| | 28 | import mockit.Invocation; |
| | 29 | import mockit.Mock; |
| | 30 | import mockit.MockUp; |
| | 31 | |
| | 32 | /** |
| | 33 | * Test class for {@link UploadAction} |
| | 34 | * @author Taylor Smock |
| | 35 | */ |
| | 36 | @BasicPreferences |
| | 37 | class UploadActionTest { |
| | 38 | // Only needed for layer cleanup. And user identity cleanup. And ensuring that data isn't accidentally uploaded. |
| | 39 | @RegisterExtension |
| | 40 | JOSMTestRules josmTestRules = new JOSMTestRules().main().projection().fakeAPI(); |
| | 41 | |
| | 42 | /** |
| | 43 | * Non-regression test for JOSM #21476. |
| | 44 | */ |
| | 45 | @Test |
| | 46 | void testNonRegression21476() { |
| | 47 | TestUtils.assumeWorkingJMockit(); |
| | 48 | Logging.clearLastErrorAndWarnings(); |
| | 49 | new WindowMocker(); |
| | 50 | new UploadDialogMock(); |
| | 51 | // Set up the preconditions. This test acts more like an integration test, in so far as it also tests |
| | 52 | // unrelated code. |
| | 53 | UserIdentityManager.getInstance().setAnonymous(); |
| | 54 | final DataSet testData = new DataSet(); |
| | 55 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(testData, "testNonRegression21476", null)); |
| | 56 | final Node toAdd = new Node(new LatLon(20, 20)); |
| | 57 | toAdd.put("shop", "butcher"); |
| | 58 | final AddPrimitivesCommand command = new AddPrimitivesCommand(Collections.singletonList(toAdd.save()), testData); |
| | 59 | command.executeCommand(); |
| | 60 | final UploadAction uploadAction = new UploadAction(); |
| | 61 | uploadAction.updateEnabledState(); |
| | 62 | assertTrue(uploadAction.isEnabled()); |
| | 63 | // Perform the actual "test" -- we don't want it to throw an exception |
| | 64 | assertDoesNotThrow(() -> uploadAction.actionPerformed(null)); |
| | 65 | // Sync threads |
| | 66 | GuiHelper.runInEDT(() -> {/* sync edt */}); |
| | 67 | try { |
| | 68 | MainApplication.worker.submit(() -> {/* sync worker */}).get(1, TimeUnit.SECONDS); |
| | 69 | assertTrue(Logging.getLastErrorAndWarnings().isEmpty()); |
| | 70 | } catch (Exception exception) { |
| | 71 | fail(exception); |
| | 72 | } finally { |
| | 73 | Logging.clearLastErrorAndWarnings(); |
| | 74 | } |
| | 75 | } |
| | 76 | |
| | 77 | private static class UploadDialogMock extends MockUp<UploadDialog> { |
| | 78 | @Mock |
| | 79 | public void pack(final Invocation invocation) { |
| | 80 | if (!GraphicsEnvironment.isHeadless()) { |
| | 81 | invocation.proceed(); |
| | 82 | } |
| | 83 | } |
| | 84 | |
| | 85 | @Mock |
| | 86 | public void setVisible(final Invocation invocation, final boolean visible) { |
| | 87 | if (!GraphicsEnvironment.isHeadless()) { |
| | 88 | invocation.proceed(visible); |
| | 89 | } |
| | 90 | } |
| | 91 | |
| | 92 | @Mock |
| | 93 | public final boolean isCanceled(final Invocation invocation) { |
| | 94 | if (!GraphicsEnvironment.isHeadless()) { |
| | 95 | return invocation.proceed(); |
| | 96 | } |
| | 97 | return true; |
| | 98 | } |
| | 99 | } |
| | 100 | } |