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

Last change on this file since 12643 was 12643, checked in by Don-vip, 8 years ago

see #15182 - deprecate Main.main.menu. Replacement: gui.MainApplication.getMenu()

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