source: josm/trunk/test/unit/org/openstreetmap/josm/gui/MainApplicationTest.java@ 14235

Last change on this file since 14235 was 14168, checked in by Don-vip, 6 years ago

fix #16656 - appveyor: tweak configuration and skip troublesome tests to get suite completing (patch by ris)

  • Property svn:eol-style set to native
File size: 12.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNotNull;
7import static org.junit.Assert.assertNull;
8import static org.junit.Assert.assertTrue;
9import static org.junit.Assume.assumeFalse;
10
11import java.awt.BorderLayout;
12import java.awt.event.KeyEvent;
13import java.io.ByteArrayOutputStream;
14import java.io.IOException;
15import java.io.PrintStream;
16import java.net.MalformedURLException;
17import java.nio.charset.StandardCharsets;
18import java.nio.file.Paths;
19import java.util.Arrays;
20import java.util.Collection;
21import java.util.List;
22import java.util.concurrent.ExecutionException;
23import java.util.concurrent.Future;
24
25import javax.swing.JComponent;
26import javax.swing.JPanel;
27import javax.swing.UIManager;
28
29import org.junit.Rule;
30import org.junit.Test;
31import org.openstreetmap.josm.TestUtils;
32import org.openstreetmap.josm.actions.AboutAction;
33import org.openstreetmap.josm.data.Version;
34import org.openstreetmap.josm.data.osm.DataSet;
35import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
36import org.openstreetmap.josm.gui.layer.GpxLayer;
37import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
38import org.openstreetmap.josm.plugins.PluginHandler;
39import org.openstreetmap.josm.plugins.PluginHandlerTestIT;
40import org.openstreetmap.josm.plugins.PluginInformation;
41import org.openstreetmap.josm.plugins.PluginListParseException;
42import org.openstreetmap.josm.plugins.PluginListParser;
43import org.openstreetmap.josm.spi.preferences.Config;
44import org.openstreetmap.josm.testutils.JOSMTestRules;
45import org.openstreetmap.josm.tools.Logging;
46import org.openstreetmap.josm.tools.PlatformManager;
47import org.openstreetmap.josm.tools.Shortcut;
48
49import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
50
51/**
52 * Unit tests of {@link MainApplication} class.
53 */
54public class MainApplicationTest {
55
56 /**
57 * Setup test.
58 */
59 @Rule
60 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
61 public JOSMTestRules test = new JOSMTestRules().main().projection().https().devAPI().timeout(20000);
62
63 /**
64 * Make sure {@link MainApplication#contentPanePrivate} is initialized.
65 */
66 public static void initContentPane() {
67 if (MainApplication.contentPanePrivate == null) {
68 MainApplication.contentPanePrivate = new JPanel(new BorderLayout());
69 }
70 }
71
72 /**
73 * Returns {@link MainApplication#contentPanePrivate} (not public).
74 * @return {@link MainApplication#contentPanePrivate}
75 */
76 public static JComponent getContentPane() {
77 return MainApplication.contentPanePrivate;
78 }
79
80 /**
81 * Make sure {@code MainApplication.mainPanel} is initialized.
82 * @param reAddListeners {@code true} to re-add listeners
83 */
84 public static void initMainPanel(boolean reAddListeners) {
85 if (MainApplication.mainPanel == null) {
86 MainApplication.mainPanel = new MainPanel(MainApplication.getLayerManager());
87 }
88 if (reAddListeners) {
89 MainApplication.mainPanel.reAddListeners();
90 }
91 }
92
93 /**
94 * Make sure {@code MainApplication.menu} is initialized.
95 */
96 public static void initMainMenu() {
97 MainApplication.menu = new MainMenu();
98 }
99
100 /**
101 * Make sure {@link MainApplication#toolbar} is initialized.
102 */
103 public static void initToolbar() {
104 if (MainApplication.toolbar == null) {
105 MainApplication.toolbar = new ToolbarPreferences();
106 }
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 public 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 public 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 public 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 public void testUpdateAndLoadPlugins() throws PluginListParseException {
166 final String old = System.getProperty("josm.plugins");
167 try {
168 System.setProperty("josm.plugins", "buildings_tools,plastic_laf");
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("plastic_laf")));
177 plugins = MainApplication.updateAndLoadEarlyPlugins(null, monitor);
178 }
179 assertEquals(2, plugins.size());
180 assertNotNull(PluginHandler.getPlugin("plastic_laf"));
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 public 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://svn.openstreetmap.org/applications/editors/josm/dist/"+plugin+".jar",
206 "");
207 }
208
209 /**
210 * Unit test of {@link MainApplication#postConstructorProcessCmdLine} - empty case.
211 */
212 @Test
213 public 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 public 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 public 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 public 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 public 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 public 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 AboutAction about = new AboutAction();
297 assertEquals(about, MainApplication.getRegisteredActionShortcut(about.getShortcut()));
298 }
299
300 /**
301 * Unit test of {@link MainApplication#addMapFrameListener} and {@link MainApplication#removeMapFrameListener}.
302 */
303 @Test
304 public 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 public void testEnumDownloadParamType() {
317 TestUtils.superficialEnumCodeCoverage(DownloadParamType.class);
318 }
319}
Note: See TracBrowser for help on using the repository browser.