1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
9 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
10 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
11 | import static org.junit.jupiter.api.Assumptions.assumeFalse;
|
---|
12 |
|
---|
13 | import java.awt.BorderLayout;
|
---|
14 | import java.awt.event.KeyEvent;
|
---|
15 | import java.io.ByteArrayOutputStream;
|
---|
16 | import java.io.IOException;
|
---|
17 | import java.io.PrintStream;
|
---|
18 | import java.net.MalformedURLException;
|
---|
19 | import java.net.URL;
|
---|
20 | import java.nio.charset.StandardCharsets;
|
---|
21 | import java.nio.file.Paths;
|
---|
22 | import java.util.Arrays;
|
---|
23 | import java.util.Collection;
|
---|
24 | import java.util.Collections;
|
---|
25 | import java.util.List;
|
---|
26 | import java.util.concurrent.ExecutionException;
|
---|
27 | import java.util.concurrent.Future;
|
---|
28 | import java.util.concurrent.atomic.AtomicReference;
|
---|
29 | import java.util.jar.Attributes;
|
---|
30 |
|
---|
31 | import javax.swing.JComponent;
|
---|
32 | import javax.swing.JPanel;
|
---|
33 | import javax.swing.UIDefaults;
|
---|
34 | import javax.swing.UIManager;
|
---|
35 | import javax.swing.plaf.metal.MetalLookAndFeel;
|
---|
36 |
|
---|
37 | import org.awaitility.Awaitility;
|
---|
38 | import org.awaitility.Durations;
|
---|
39 | import org.junit.jupiter.api.Test;
|
---|
40 | import org.junit.jupiter.api.Timeout;
|
---|
41 | import org.openstreetmap.josm.TestUtils;
|
---|
42 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
43 | import org.openstreetmap.josm.actions.OpenLocationAction;
|
---|
44 | import org.openstreetmap.josm.data.Version;
|
---|
45 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
46 | import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
|
---|
47 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
48 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
49 | import org.openstreetmap.josm.gui.preferences.display.LafPreference;
|
---|
50 | import org.openstreetmap.josm.plugins.PluginClassLoader;
|
---|
51 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
52 | import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
|
---|
53 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
54 | import org.openstreetmap.josm.plugins.PluginListParseException;
|
---|
55 | import org.openstreetmap.josm.plugins.PluginListParser;
|
---|
56 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
57 | import org.openstreetmap.josm.testutils.annotations.HTTPS;
|
---|
58 | import org.openstreetmap.josm.testutils.annotations.Main;
|
---|
59 | import org.openstreetmap.josm.testutils.annotations.OsmApi;
|
---|
60 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
---|
61 | import org.openstreetmap.josm.tools.Logging;
|
---|
62 | import org.openstreetmap.josm.tools.PlatformManager;
|
---|
63 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
64 | import org.openstreetmap.josm.tools.bugreport.BugReportQueue;
|
---|
65 |
|
---|
66 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
67 | import mockit.Mock;
|
---|
68 | import mockit.MockUp;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Unit tests of {@link MainApplication} class.
|
---|
72 | */
|
---|
73 | @HTTPS
|
---|
74 | @Main
|
---|
75 | @OsmApi(OsmApi.APIType.DEV)
|
---|
76 | @Projection
|
---|
77 | @Timeout(20)
|
---|
78 | public class MainApplicationTest {
|
---|
79 | /**
|
---|
80 | * Make sure {@link MainApplication#contentPanePrivate} is initialized.
|
---|
81 | */
|
---|
82 | public static void initContentPane() {
|
---|
83 | // init every time to avoid "Keystroke %s is already assigned to %s"
|
---|
84 | MainApplication.contentPanePrivate = new JPanel(new BorderLayout());
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Returns {@link MainApplication#contentPanePrivate} (not public).
|
---|
89 | * @return {@link MainApplication#contentPanePrivate}
|
---|
90 | */
|
---|
91 | public static JComponent getContentPane() {
|
---|
92 | return MainApplication.contentPanePrivate;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Make sure {@code MainApplication.mainPanel} is initialized.
|
---|
97 | * @param reAddListeners {@code true} to re-add listeners
|
---|
98 | */
|
---|
99 | public static void initMainPanel(boolean reAddListeners) {
|
---|
100 | if (MainApplication.mainPanel == null) {
|
---|
101 | MainApplication.mainPanel = new MainPanel(MainApplication.getLayerManager());
|
---|
102 | }
|
---|
103 | if (reAddListeners) {
|
---|
104 | MainApplication.mainPanel.reAddListeners();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Make sure {@code MainApplication.menu} is initialized.
|
---|
110 | */
|
---|
111 | public static void initMainMenu() {
|
---|
112 | MainApplication.menu = new MainMenu();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Make sure {@link MainApplication#toolbar} is initialized.
|
---|
117 | */
|
---|
118 | public static void initToolbar() {
|
---|
119 | // init every time to avoid "Registered toolbar action"
|
---|
120 | MainApplication.toolbar = new ToolbarPreferences();
|
---|
121 | }
|
---|
122 |
|
---|
123 | @SuppressFBWarnings(value = "DM_DEFAULT_ENCODING")
|
---|
124 | private void testShow(final String arg, String expected) throws InterruptedException, IOException {
|
---|
125 | PrintStream old = System.out;
|
---|
126 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
---|
127 | System.setOut(new PrintStream(baos));
|
---|
128 | Thread t = new Thread(() -> MainApplication.main(new String[] {arg}));
|
---|
129 | t.start();
|
---|
130 | t.join();
|
---|
131 | System.out.flush();
|
---|
132 | assertEquals(expected, baos.toString(StandardCharsets.UTF_8.name()).trim());
|
---|
133 | } finally {
|
---|
134 | System.setOut(old);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Test of {@link MainApplication#main} with argument {@code --version}.
|
---|
140 | * @throws Exception in case of error
|
---|
141 | */
|
---|
142 | @Test
|
---|
143 | void testShowVersion() throws Exception {
|
---|
144 | testShow("--version", Version.getInstance().getAgentString());
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Test of {@link MainApplication#main} with argument {@code --help}.
|
---|
149 | * @throws Exception in case of error
|
---|
150 | */
|
---|
151 | @Test
|
---|
152 | void testShowHelp() throws Exception {
|
---|
153 | testShow("--help", MainApplication.getHelp().trim());
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Unit test of {@link DownloadParamType#paramType} method.
|
---|
158 | */
|
---|
159 | @Test
|
---|
160 | void testParamType() {
|
---|
161 | assertEquals(DownloadParamType.bounds, DownloadParamType.paramType("48.000,16.000,48.001,16.001"));
|
---|
162 | assertEquals(DownloadParamType.fileName, DownloadParamType.paramType("data.osm"));
|
---|
163 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file:///home/foo/data.osm"));
|
---|
164 | assertEquals(DownloadParamType.fileUrl, DownloadParamType.paramType("file://C:\\Users\\foo\\data.osm"));
|
---|
165 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("http://somewhere.com/data.osm"));
|
---|
166 | assertEquals(DownloadParamType.httpUrl, DownloadParamType.paramType("https://somewhere.com/data.osm"));
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Test of {@link MainApplication#updateAndLoadEarlyPlugins} and {@link MainApplication#loadLatePlugins} methods.
|
---|
171 | * @throws PluginListParseException if an error occurs
|
---|
172 | */
|
---|
173 | @Test
|
---|
174 | void testUpdateAndLoadPlugins() throws PluginListParseException {
|
---|
175 | final String old = System.getProperty("josm.plugins");
|
---|
176 | try {
|
---|
177 | System.setProperty("josm.plugins", "buildings_tools,log4j");
|
---|
178 | SplashProgressMonitor monitor = new SplashProgressMonitor("foo", e -> {
|
---|
179 | // Do nothing
|
---|
180 | });
|
---|
181 | Collection<PluginInformation> plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
|
---|
182 | if (plugins.isEmpty()) {
|
---|
183 | PluginHandlerTestIT.downloadPlugins(Arrays.asList(
|
---|
184 | newPluginInformation("buildings_tools"),
|
---|
185 | newPluginInformation("log4j")));
|
---|
186 | plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
|
---|
187 | }
|
---|
188 | assertEquals(2, plugins.size());
|
---|
189 | assertNotNull(PluginHandler.getPlugin("log4j"));
|
---|
190 | assertNull(PluginHandler.getPlugin("buildings_tools"));
|
---|
191 | MainApplication.loadLatePlugins(null, monitor, plugins);
|
---|
192 | assertNotNull(PluginHandler.getPlugin("buildings_tools"));
|
---|
193 | } finally {
|
---|
194 | if (old != null) {
|
---|
195 | System.setProperty("josm.plugins", old);
|
---|
196 | } else {
|
---|
197 | System.clearProperty("josm.plugins");
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Unit test of {@link MainApplication#setupUIManager}.
|
---|
204 | */
|
---|
205 | @Test
|
---|
206 | void testSetupUIManager() {
|
---|
207 | TestUtils.assumeWorkingJMockit();
|
---|
208 | assumeFalse(PlatformManager.isPlatformWindows() && "True".equals(System.getenv("APPVEYOR")));
|
---|
209 | assertDoesNotThrow(MainApplication::setupUIManager);
|
---|
210 | assertEquals(Config.getPref().get("laf", PlatformManager.getPlatform().getDefaultStyle()),
|
---|
211 | UIManager.getLookAndFeel().getClass().getCanonicalName());
|
---|
212 | try {
|
---|
213 | LafPreference.LAF.put(BadLaf.class.getName());
|
---|
214 | new PluginHandlerMock();
|
---|
215 | AtomicReference<Throwable> exceptionAtomicReference = new AtomicReference<>();
|
---|
216 | BugReportQueue.getInstance().setBugReportHandler((e, index) -> {
|
---|
217 | exceptionAtomicReference.set(e.getCause());
|
---|
218 | return BugReportQueue.SuppressionMode.NONE;
|
---|
219 | });
|
---|
220 | assertDoesNotThrow(MainApplication::setupUIManager);
|
---|
221 |
|
---|
222 | /* We cannot sync on the worker or EDT threads since the bug report handler makes a new thread */
|
---|
223 | Awaitility.await().atMost(Durations.ONE_HUNDRED_MILLISECONDS)
|
---|
224 | .pollDelay(Durations.ONE_MILLISECOND)
|
---|
225 | .until(() -> !BugReportQueue.getInstance().exceptionHandlingInProgress());
|
---|
226 | assertNotNull(exceptionAtomicReference.get());
|
---|
227 | assertInstanceOf(UnsupportedOperationException.class, exceptionAtomicReference.get());
|
---|
228 | // The LAF only resets on restart, so don't bother checking that it switched back in UIManager
|
---|
229 | assertEquals(LafPreference.LAF.getDefaultValue(), LafPreference.LAF.get());
|
---|
230 | } finally {
|
---|
231 | BugReportQueue.getInstance().setBugReportHandler(BugReportQueue.FALLBACK_BUGREPORT_HANDLER);
|
---|
232 | // Make certain we reset the LAF
|
---|
233 | LafPreference.LAF.remove();
|
---|
234 | assertDoesNotThrow(MainApplication::setupUIManager);
|
---|
235 | assertEquals(Config.getPref().get("laf", PlatformManager.getPlatform().getDefaultStyle()),
|
---|
236 | UIManager.getLookAndFeel().getClass().getCanonicalName());
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | private static PluginInformation newPluginInformation(String plugin) throws PluginListParseException {
|
---|
241 | return PluginListParser.createInfo(plugin+".jar", "https://josm.openstreetmap.de/osmsvn/applications/editors/josm/dist/"+plugin+".jar",
|
---|
242 | new Attributes());
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - empty case.
|
---|
247 | */
|
---|
248 | @Test
|
---|
249 | void testPostConstructorProcessCmdLineEmpty() {
|
---|
250 | // Check the method accepts no arguments
|
---|
251 | assertDoesNotThrow(() -> MainApplication.postConstructorProcessCmdLine(new ProgramArguments()));
|
---|
252 | }
|
---|
253 |
|
---|
254 | private static void doTestPostConstructorProcessCmdLine(String download, String downloadGps, boolean gpx) {
|
---|
255 | assertNull(MainApplication.getLayerManager().getEditDataSet());
|
---|
256 | for (Future<?> f : MainApplication.postConstructorProcessCmdLine(new ProgramArguments("--download=" + download,
|
---|
257 | "--downloadgps=" + downloadGps,
|
---|
258 | "--selection=type: node"))) {
|
---|
259 | try {
|
---|
260 | f.get();
|
---|
261 | } catch (InterruptedException | ExecutionException e) {
|
---|
262 | Logging.error(e);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | DataSet ds = MainApplication.getLayerManager().getEditDataSet();
|
---|
266 | assertNotNull(ds);
|
---|
267 | assertFalse(ds.getSelected().isEmpty());
|
---|
268 | MainApplication.getLayerManager().removeLayer(MainApplication.getLayerManager().getEditLayer());
|
---|
269 | if (gpx) {
|
---|
270 | List<GpxLayer> gpxLayers = MainApplication.getLayerManager().getLayersOfType(GpxLayer.class);
|
---|
271 | assertEquals(1, gpxLayers.size());
|
---|
272 | MainApplication.getLayerManager().removeLayer(gpxLayers.iterator().next());
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with bounds.
|
---|
278 | * This test assumes the DEV API contains nodes around 0,0 and GPX tracks around London
|
---|
279 | */
|
---|
280 | @Test
|
---|
281 | void testPostConstructorProcessCmdLineBounds() {
|
---|
282 | doTestPostConstructorProcessCmdLine(
|
---|
283 | "-47.20,-126.75,-47.10,-126.65",
|
---|
284 | "51.35,-0.4,51.60,0.2", true);
|
---|
285 | }
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with http/https URLs.
|
---|
289 | * This test assumes the DEV API contains nodes around -47.15, -126.7 (R'lyeh) and GPX tracks around London
|
---|
290 | */
|
---|
291 | @Test
|
---|
292 | void testPostConstructorProcessCmdLineHttpUrl() {
|
---|
293 | doTestPostConstructorProcessCmdLine(
|
---|
294 | "https://api06.dev.openstreetmap.org/api/0.6/map?bbox=-126.75,-47.20,-126.65,-47.10",
|
---|
295 | "https://master.apis.dev.openstreetmap.org/api/0.6/trackpoints?bbox=-0.4,51.35,0.2,51.6&page=0", true);
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file URLs.
|
---|
300 | * @throws MalformedURLException if an error occurs
|
---|
301 | */
|
---|
302 | @Test
|
---|
303 | void testPostConstructorProcessCmdLineFileUrl() throws MalformedURLException {
|
---|
304 | doTestPostConstructorProcessCmdLine(
|
---|
305 | Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toUri().toURL().toExternalForm(),
|
---|
306 | Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toUri().toURL().toExternalForm(), false);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - nominal case with file names.
|
---|
311 | */
|
---|
312 | @Test
|
---|
313 | void testPostConstructorProcessCmdLineFilename() {
|
---|
314 | doTestPostConstructorProcessCmdLine(
|
---|
315 | Paths.get(TestUtils.getTestDataRoot() + "multipolygon.osm").toFile().getAbsolutePath(),
|
---|
316 | Paths.get(TestUtils.getTestDataRoot() + "minimal.gpx").toFile().getAbsolutePath(), false);
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Unit test of {@link MainApplication#getRegisteredActionShortcut}.
|
---|
321 | */
|
---|
322 | @Test
|
---|
323 | void testGetRegisteredActionShortcut() {
|
---|
324 | Shortcut noKeystroke = Shortcut.registerShortcut("no", "keystroke", 0, 0);
|
---|
325 | assertNull(noKeystroke.getKeyStroke());
|
---|
326 | assertNull(MainApplication.getRegisteredActionShortcut(noKeystroke));
|
---|
327 | Shortcut noAction = Shortcut.registerShortcut("foo", "bar", KeyEvent.VK_AMPERSAND, Shortcut.SHIFT);
|
---|
328 | assertNotNull(noAction.getKeyStroke());
|
---|
329 | assertNull(MainApplication.getRegisteredActionShortcut(noAction));
|
---|
330 | JosmAction action = new OpenLocationAction();
|
---|
331 | assertEquals(action, MainApplication.getRegisteredActionShortcut(action.getShortcut()));
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Unit test of {@link MainApplication#addMapFrameListener} and {@link MainApplication#removeMapFrameListener}.
|
---|
336 | */
|
---|
337 | @Test
|
---|
338 | void testMapFrameListener() {
|
---|
339 | MapFrameListener listener = (o, n) -> { };
|
---|
340 | assertTrue(MainApplication.addMapFrameListener(listener));
|
---|
341 | assertFalse(MainApplication.addMapFrameListener(null));
|
---|
342 | assertTrue(MainApplication.removeMapFrameListener(listener));
|
---|
343 | assertFalse(MainApplication.removeMapFrameListener(null));
|
---|
344 | }
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Unit test of {@link DownloadParamType} enum.
|
---|
348 | */
|
---|
349 | @Test
|
---|
350 | void testEnumDownloadParamType() {
|
---|
351 | TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * This class exists to test a failure in non-default UI loading
|
---|
356 | */
|
---|
357 | public static class BadLaf extends MetalLookAndFeel {
|
---|
358 | @Override
|
---|
359 | public UIDefaults getDefaults() {
|
---|
360 | throw new UnsupportedOperationException("Test failure loading");
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * A mock class for returning a fake plugin class loader for {@link #testSetupUIManager()}
|
---|
366 | */
|
---|
367 | public static class PluginHandlerMock extends MockUp<PluginHandler> {
|
---|
368 | @Mock
|
---|
369 | public static Collection<PluginClassLoader> getPluginClassLoaders() {
|
---|
370 | return Collections.singleton(new PluginClassLoader(new URL[0], BadLaf.class.getClassLoader(), Collections.emptyList()));
|
---|
371 | }
|
---|
372 | }
|
---|
373 | }
|
---|