| 1 | // License: GPL. For details, see LICENSE file. | 
|---|
| 2 | package org.openstreetmap.josm.actions; | 
|---|
| 3 |  | 
|---|
| 4 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | 
|---|
| 5 | import static org.junit.jupiter.api.Assertions.assertTrue; | 
|---|
| 6 |  | 
|---|
| 7 | import java.awt.GraphicsEnvironment; | 
|---|
| 8 | import java.util.Collections; | 
|---|
| 9 | import java.util.concurrent.ExecutionException; | 
|---|
| 10 | import java.util.concurrent.TimeUnit; | 
|---|
| 11 | import java.util.concurrent.TimeoutException; | 
|---|
| 12 |  | 
|---|
| 13 | import org.junit.jupiter.api.Test; | 
|---|
| 14 | import org.openstreetmap.josm.TestUtils; | 
|---|
| 15 | import org.openstreetmap.josm.command.AddPrimitivesCommand; | 
|---|
| 16 | import org.openstreetmap.josm.data.UserIdentityManager; | 
|---|
| 17 | import org.openstreetmap.josm.data.coor.LatLon; | 
|---|
| 18 | import org.openstreetmap.josm.data.osm.DataSet; | 
|---|
| 19 | import org.openstreetmap.josm.data.osm.Node; | 
|---|
| 20 | import org.openstreetmap.josm.gui.MainApplication; | 
|---|
| 21 | import org.openstreetmap.josm.gui.io.UploadDialog; | 
|---|
| 22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; | 
|---|
| 23 | import org.openstreetmap.josm.gui.util.GuiHelper; | 
|---|
| 24 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences; | 
|---|
| 25 | import org.openstreetmap.josm.testutils.annotations.Main; | 
|---|
| 26 | import org.openstreetmap.josm.testutils.annotations.OsmApi; | 
|---|
| 27 | import org.openstreetmap.josm.testutils.annotations.Projection; | 
|---|
| 28 | import org.openstreetmap.josm.testutils.annotations.Territories; | 
|---|
| 29 | import org.openstreetmap.josm.testutils.mockers.WindowMocker; | 
|---|
| 30 | import org.openstreetmap.josm.tools.Logging; | 
|---|
| 31 |  | 
|---|
| 32 | import mockit.Invocation; | 
|---|
| 33 | import mockit.Mock; | 
|---|
| 34 | import mockit.MockUp; | 
|---|
| 35 |  | 
|---|
| 36 | /** | 
|---|
| 37 | * Test class for {@link UploadAction} | 
|---|
| 38 | * @author Taylor Smock | 
|---|
| 39 | */ | 
|---|
| 40 | @BasicPreferences | 
|---|
| 41 | @Main | 
|---|
| 42 | @OsmApi(OsmApi.APIType.FAKE) | 
|---|
| 43 | @Projection | 
|---|
| 44 | // Territories is needed due to test pollution. One of the listeners | 
|---|
| 45 | // that may get registered on SelectionEventManager requires | 
|---|
| 46 | // Territories. Rather unfortunately, we also need the external data to | 
|---|
| 47 | // avoid the NPE. | 
|---|
| 48 | @Territories(Territories.Initialize.ALL) | 
|---|
| 49 | class UploadActionTest { | 
|---|
| 50 | /** | 
|---|
| 51 | * Non-regression test for JOSM #21476. | 
|---|
| 52 | */ | 
|---|
| 53 | @Test | 
|---|
| 54 | void testNonRegression21476() throws ExecutionException, InterruptedException, TimeoutException { | 
|---|
| 55 | TestUtils.assumeWorkingJMockit(); | 
|---|
| 56 | Logging.clearLastErrorAndWarnings(); | 
|---|
| 57 | new WindowMocker(); | 
|---|
| 58 | new UploadDialogMock(); | 
|---|
| 59 | // Set up the preconditions. This test acts more like an integration test, in so far as it also tests | 
|---|
| 60 | // unrelated code. | 
|---|
| 61 | UserIdentityManager.getInstance().setAnonymous(); | 
|---|
| 62 | final DataSet testData = new DataSet(); | 
|---|
| 63 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(testData, "testNonRegression21476", null)); | 
|---|
| 64 | final Node toAdd = new Node(new LatLon(20, 20)); | 
|---|
| 65 | toAdd.put("shop", "butcher"); | 
|---|
| 66 | final AddPrimitivesCommand command = new AddPrimitivesCommand(Collections.singletonList(toAdd.save()), testData); | 
|---|
| 67 | command.executeCommand(); | 
|---|
| 68 | final UploadAction uploadAction = new UploadAction(); | 
|---|
| 69 | uploadAction.updateEnabledState(); | 
|---|
| 70 | assertTrue(uploadAction.isEnabled()); | 
|---|
| 71 | // Perform the actual "test" -- we don't want it to throw an exception | 
|---|
| 72 | assertDoesNotThrow(() -> uploadAction.actionPerformed(null)); | 
|---|
| 73 | // Sync threads | 
|---|
| 74 | GuiHelper.runInEDT(() -> { | 
|---|
| 75 | // sync edt | 
|---|
| 76 | }); | 
|---|
| 77 | try { | 
|---|
| 78 | MainApplication.worker.submit(() -> { | 
|---|
| 79 | // sync worker | 
|---|
| 80 | }).get(1, TimeUnit.SECONDS); | 
|---|
| 81 | assertTrue(Logging.getLastErrorAndWarnings().isEmpty()); | 
|---|
| 82 | } finally { | 
|---|
| 83 | Logging.clearLastErrorAndWarnings(); | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | private static class UploadDialogMock extends MockUp<UploadDialog> { | 
|---|
| 88 | @Mock | 
|---|
| 89 | public void pack(final Invocation invocation) { | 
|---|
| 90 | if (!GraphicsEnvironment.isHeadless()) { | 
|---|
| 91 | invocation.proceed(); | 
|---|
| 92 | } | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | @Mock | 
|---|
| 96 | public void setVisible(final Invocation invocation, final boolean visible) { | 
|---|
| 97 | if (!GraphicsEnvironment.isHeadless()) { | 
|---|
| 98 | invocation.proceed(visible); | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | @Mock | 
|---|
| 103 | public final boolean isCanceled(final Invocation invocation) { | 
|---|
| 104 | if (!GraphicsEnvironment.isHeadless()) { | 
|---|
| 105 | return Boolean.TRUE.equals(invocation.proceed()); | 
|---|
| 106 | } | 
|---|
| 107 | return true; | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|