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 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
7 |
|
---|
8 | import java.awt.GraphicsEnvironment;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.concurrent.TimeUnit;
|
---|
11 |
|
---|
12 | import org.junit.jupiter.api.Test;
|
---|
13 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
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.JOSMTestRules;
|
---|
25 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
26 | import org.openstreetmap.josm.testutils.annotations.Main;
|
---|
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 | @Projection
|
---|
43 | // Territories is needed due to test pollution. One of the listeners
|
---|
44 | // that may get registered on SelectionEventManager requires
|
---|
45 | // Territories. Rather unfortunately, we also need the external data to
|
---|
46 | // avoid the NPE.
|
---|
47 | @Territories(Territories.Initialize.ALL)
|
---|
48 | class UploadActionTest {
|
---|
49 | // Only needed for layer cleanup. And user identity cleanup. And ensuring that data isn't accidentally uploaded.
|
---|
50 | // Note that the setUp method can be replaced by the @Territories extension, when that is merged.
|
---|
51 | @RegisterExtension
|
---|
52 | static JOSMTestRules josmTestRules = new JOSMTestRules().fakeAPI();
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Non-regression test for JOSM #21476.
|
---|
56 | */
|
---|
57 | @Test
|
---|
58 | void testNonRegression21476() {
|
---|
59 | TestUtils.assumeWorkingJMockit();
|
---|
60 | Logging.clearLastErrorAndWarnings();
|
---|
61 | new WindowMocker();
|
---|
62 | new UploadDialogMock();
|
---|
63 | // Set up the preconditions. This test acts more like an integration test, in so far as it also tests
|
---|
64 | // unrelated code.
|
---|
65 | UserIdentityManager.getInstance().setAnonymous();
|
---|
66 | final DataSet testData = new DataSet();
|
---|
67 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(testData, "testNonRegression21476", null));
|
---|
68 | final Node toAdd = new Node(new LatLon(20, 20));
|
---|
69 | toAdd.put("shop", "butcher");
|
---|
70 | final AddPrimitivesCommand command = new AddPrimitivesCommand(Collections.singletonList(toAdd.save()), testData);
|
---|
71 | command.executeCommand();
|
---|
72 | final UploadAction uploadAction = new UploadAction();
|
---|
73 | uploadAction.updateEnabledState();
|
---|
74 | assertTrue(uploadAction.isEnabled());
|
---|
75 | // Perform the actual "test" -- we don't want it to throw an exception
|
---|
76 | assertDoesNotThrow(() -> uploadAction.actionPerformed(null));
|
---|
77 | // Sync threads
|
---|
78 | GuiHelper.runInEDT(() -> {
|
---|
79 | // sync edt
|
---|
80 | });
|
---|
81 | try {
|
---|
82 | MainApplication.worker.submit(() -> {
|
---|
83 | // sync worker
|
---|
84 | }).get(1, TimeUnit.SECONDS);
|
---|
85 | assertTrue(Logging.getLastErrorAndWarnings().isEmpty());
|
---|
86 | } catch (Exception exception) {
|
---|
87 | fail(exception);
|
---|
88 | } finally {
|
---|
89 | Logging.clearLastErrorAndWarnings();
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | private static class UploadDialogMock extends MockUp<UploadDialog> {
|
---|
94 | @Mock
|
---|
95 | public void pack(final Invocation invocation) {
|
---|
96 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
97 | invocation.proceed();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Mock
|
---|
102 | public void setVisible(final Invocation invocation, final boolean visible) {
|
---|
103 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
104 | invocation.proceed(visible);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Mock
|
---|
109 | public final boolean isCanceled(final Invocation invocation) {
|
---|
110 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
111 | return invocation.proceed();
|
---|
112 | }
|
---|
113 | return true;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|