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