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

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

see #15182 - deprecate shortcut handling and mapframe listener methods in Main. Replacement: same methods in gui.MainApplication

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