1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
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.assertNull;
|
---|
9 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
10 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
11 |
|
---|
12 | import java.awt.Color;
|
---|
13 | import java.awt.Component;
|
---|
14 | import java.io.IOException;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.HashMap;
|
---|
18 | import java.util.Locale;
|
---|
19 | import java.util.TimeZone;
|
---|
20 |
|
---|
21 | import javax.swing.JScrollPane;
|
---|
22 |
|
---|
23 | import org.junit.jupiter.api.BeforeEach;
|
---|
24 | import org.junit.jupiter.api.Test;
|
---|
25 | import org.openstreetmap.josm.TestUtils;
|
---|
26 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
27 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
28 | import org.openstreetmap.josm.data.gpx.IGpxTrackSegment;
|
---|
29 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
30 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
31 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
32 | import org.openstreetmap.josm.data.projection.CustomProjection;
|
---|
33 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
34 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
35 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
36 | import org.openstreetmap.josm.io.GpxReaderTest;
|
---|
37 | import org.openstreetmap.josm.testutils.annotations.I18n;
|
---|
38 | import org.openstreetmap.josm.testutils.annotations.Main;
|
---|
39 | import org.openstreetmap.josm.testutils.annotations.MeasurementSystem;
|
---|
40 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
---|
41 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
42 | import org.xml.sax.SAXException;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Unit tests of {@link GpxLayer} class.
|
---|
46 | */
|
---|
47 | @I18n
|
---|
48 | @Main
|
---|
49 | @MeasurementSystem
|
---|
50 | @Projection
|
---|
51 | public class GpxLayerTest {
|
---|
52 | /**
|
---|
53 | * Setup test.
|
---|
54 | */
|
---|
55 | @BeforeEach
|
---|
56 | void setUp() {
|
---|
57 | Locale.setDefault(Locale.ROOT);
|
---|
58 | DateUtils.PROP_ISO_DATES.put(true);
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static String getHtml(GpxLayer layer) {
|
---|
62 | return ((HtmlPanel) ((JScrollPane) layer.getInfoComponent()).getViewport().getView()).getEditorPane().getText();
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Returns minimal GPX data.
|
---|
67 | * @return minimal GPX data, with a single waypoint, a single track composed of a single segment
|
---|
68 | * @throws IOException if any I/O error occurs
|
---|
69 | * @throws SAXException if any SAX error occurs
|
---|
70 | */
|
---|
71 | public static GpxData getMinimalGpxData() throws IOException, SAXException {
|
---|
72 | return GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + "minimal.gpx");
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Returns minimal GPX layer.
|
---|
77 | * @return minimal GPX layer, with a single waypoint, a single track composed of a single segment
|
---|
78 | * @throws IOException if any I/O error occurs
|
---|
79 | * @throws SAXException if any SAX error occurs
|
---|
80 | */
|
---|
81 | public static GpxLayer getMinimalGpxLayer() throws IOException, SAXException {
|
---|
82 | return new GpxLayer(getMinimalGpxData(), "Bananas");
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Unit test of {@link GpxLayer#GpxLayer}.
|
---|
87 | */
|
---|
88 | @Test
|
---|
89 | void testGpxLayer() {
|
---|
90 | GpxLayer layer = new GpxLayer(new GpxData(), "foo", false);
|
---|
91 | GpxTrack trk = new GpxTrack(new ArrayList<IGpxTrackSegment>(), new HashMap<>());
|
---|
92 | trk.getExtensions().add("gpxd", "color", "#FF0000");
|
---|
93 | layer.data.addTrack(trk);
|
---|
94 |
|
---|
95 | assertEquals("foo", layer.getName());
|
---|
96 | assertFalse(layer.isLocalFile());
|
---|
97 | assertEquals(layer.getColor(), Color.RED);
|
---|
98 | assertEquals("<html>1 track (0 segments), 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer.getToolTipText());
|
---|
99 |
|
---|
100 | GpxLayer layer2 = new GpxLayer(new GpxData(), "bar", true);
|
---|
101 | assertEquals("bar", layer2.getName());
|
---|
102 | assertTrue(layer2.isLocalFile());
|
---|
103 | assertNull(layer2.getColor());
|
---|
104 | assertEquals("<html>0 tracks (0 segments), 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer2.getToolTipText());
|
---|
105 |
|
---|
106 | assertTrue(layer.checkSaveConditions());
|
---|
107 | assertTrue(layer.isInfoResizable());
|
---|
108 | assertTrue(layer.isSavable());
|
---|
109 | assertTrue(layer.isMergable(layer2));
|
---|
110 |
|
---|
111 | layer.projectionChanged(null, null);
|
---|
112 | layer.projectionChanged(null, Projections.getProjectionByCode("EPSG:3857"));
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Unit test of {@link GpxLayer#getInfoComponent}.
|
---|
117 | * @throws Exception if any error occurs
|
---|
118 | */
|
---|
119 | @Test
|
---|
120 | void testGetInfoComponent() throws Exception {
|
---|
121 | assertEquals("<html>\n"+
|
---|
122 | " <head>\n" +
|
---|
123 | " <style type=\"text/css\">\n" +
|
---|
124 | " <!--\n" +
|
---|
125 | " td { padding-top: 4px; padding-bottom: 4px; padding-right: 16px; padding-left: 16px }\n" +
|
---|
126 | " -->\n" +
|
---|
127 | " </style>\n" +
|
---|
128 | " \n" +
|
---|
129 | " </head>\n" +
|
---|
130 | " <body>\n" +
|
---|
131 | " Length: 0.01 m<br>0 routes, 0 waypoints<br>\n" +
|
---|
132 | " </body>\n" +
|
---|
133 | "</html>\n",
|
---|
134 | getHtml(new GpxLayer(new GpxData())));
|
---|
135 |
|
---|
136 | assertEquals("<html>\n"+
|
---|
137 | " <head>\n" +
|
---|
138 | " <style type=\"text/css\">\n" +
|
---|
139 | " <!--\n" +
|
---|
140 | " td { padding-top: 4px; padding-bottom: 4px; padding-right: 16px; padding-left: 16px }\n" +
|
---|
141 | " -->\n" +
|
---|
142 | " </style>\n" +
|
---|
143 | " \n" +
|
---|
144 | " </head>\n" +
|
---|
145 | " <body>\n" +
|
---|
146 | " Creator: MapSource 6.16.3<br>\n\n" +
|
---|
147 | " <table>\n" +
|
---|
148 | " <tr align=\"center\">\n" +
|
---|
149 | " <td colspan=\"5\">\n" +
|
---|
150 | " 1 track, 1 track segments\n" +
|
---|
151 | " </td>\n" +
|
---|
152 | " </tr>\n" +
|
---|
153 | " <tr align=\"center\">\n" +
|
---|
154 | " <td>\n" +
|
---|
155 | " Name\n" +
|
---|
156 | " </td>\n" +
|
---|
157 | " <td>\n" +
|
---|
158 | " Description\n" +
|
---|
159 | " </td>\n" +
|
---|
160 | " <td>\n" +
|
---|
161 | " Timespan\n" +
|
---|
162 | " </td>\n" +
|
---|
163 | " <td>\n" +
|
---|
164 | " Length\n" +
|
---|
165 | " </td>\n" +
|
---|
166 | " <td>\n" +
|
---|
167 | " Number of<br>Segments\n" +
|
---|
168 | " </td>\n" +
|
---|
169 | " <td>\n" +
|
---|
170 | " URL\n" +
|
---|
171 | " </td>\n" +
|
---|
172 | " </tr>\n" +
|
---|
173 | " <tr>\n" +
|
---|
174 | " <td>\n" +
|
---|
175 | " 2016-01-03 20:40:14\n" +
|
---|
176 | " </td>\n" +
|
---|
177 | " <td>\n" +
|
---|
178 | " \n" +
|
---|
179 | " </td>\n" +
|
---|
180 | " <td>\n" +
|
---|
181 | " 2016-01-03 11:59:58 – 12:00:00 (2.0 s)\n" +
|
---|
182 | " </td>\n" +
|
---|
183 | " <td>\n" +
|
---|
184 | " 12.0 m\n" +
|
---|
185 | " </td>\n" +
|
---|
186 | " <td>\n" +
|
---|
187 | " 1\n" +
|
---|
188 | " </td>\n" +
|
---|
189 | " <td>\n" +
|
---|
190 | " \n" +
|
---|
191 | " </td>\n" +
|
---|
192 | " </tr>\n" +
|
---|
193 | " </table>\n" +
|
---|
194 | " <br>\n" +
|
---|
195 | " <br>\n" +
|
---|
196 | " Length: 12.0 m<br>0 routes, 1 waypoint<br>\n" +
|
---|
197 | " </body>\n" +
|
---|
198 | "</html>\n",
|
---|
199 | getHtml(getMinimalGpxLayer()));
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Unit test of {@link GpxLayer#getTimespanForTrack}.
|
---|
204 | * @throws Exception if any error occurs
|
---|
205 | */
|
---|
206 | @Test
|
---|
207 | void testGetTimespanForTrack() throws Exception {
|
---|
208 | assertEquals("", GpxLayer.getTimespanForTrack(
|
---|
209 | new GpxTrack(new ArrayList<Collection<WayPoint>>(), new HashMap<>())));
|
---|
210 |
|
---|
211 | assertEquals("2016-01-03 11:59:58 \u2013 12:00:00 (2.0 s)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
|
---|
212 |
|
---|
213 | TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
|
---|
214 | assertEquals("2016-01-03 12:59:58 \u2013 13:00:00 (2.0 s)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Unit test of {@link GpxLayer#mergeFrom}.
|
---|
219 | * @throws Exception if any error occurs
|
---|
220 | */
|
---|
221 | @Test
|
---|
222 | void testMergeFrom() throws Exception {
|
---|
223 | GpxLayer layer = new GpxLayer(new GpxData());
|
---|
224 | assertTrue(layer.data.isEmpty());
|
---|
225 | layer.mergeFrom(getMinimalGpxLayer());
|
---|
226 | assertFalse(layer.data.isEmpty());
|
---|
227 | assertEquals(1, layer.data.tracks.size());
|
---|
228 | assertEquals(1, layer.data.waypoints.size());
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Test that {@link GpxLayer#mergeFrom} throws IAE for invalid arguments
|
---|
233 | */
|
---|
234 | @Test
|
---|
235 | void testMergeFromIAE() {
|
---|
236 | final GpxLayer gpxLayer = new GpxLayer(new GpxData());
|
---|
237 | final OsmDataLayer osmDataLayer = new OsmDataLayer(new DataSet(), "testMergeFromIAE", null);
|
---|
238 | assertThrows(IllegalArgumentException.class, () -> gpxLayer.mergeFrom(osmDataLayer));
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Unit test of {@link GpxLayer#paint}.
|
---|
243 | * @throws Exception if any error occurs
|
---|
244 | */
|
---|
245 | @Test
|
---|
246 | void testPaint() throws Exception {
|
---|
247 | GpxLayer layer = getMinimalGpxLayer();
|
---|
248 | try {
|
---|
249 | MainApplication.getLayerManager().addLayer(layer);
|
---|
250 | assertTrue(layer.getMenuEntries().length > 0);
|
---|
251 | layer.paint(TestUtils.newGraphics(), MainApplication.getMap().mapView, layer.data.getMetaBounds());
|
---|
252 | } finally {
|
---|
253 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Unit test of {@link GpxLayer#getChangesetSourceTag}.
|
---|
259 | */
|
---|
260 | @Test
|
---|
261 | void testGetChangesetSourceTag() {
|
---|
262 | assertEquals("survey", new GpxLayer(new GpxData(), "", true).getChangesetSourceTag());
|
---|
263 | assertNull(new GpxLayer(new GpxData(), "", false).getChangesetSourceTag());
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Checks that potential operations that could be called after destroy() are harmless
|
---|
268 | */
|
---|
269 | @Test
|
---|
270 | void testRobustnessAfterDestroy() {
|
---|
271 | GpxData data = new GpxData();
|
---|
272 | GpxLayer layer = new GpxLayer(data, "1", false);
|
---|
273 | GpxLayer otherLayer = new GpxLayer(new GpxData(), "2", false);
|
---|
274 | assertEquals(data, layer.getData());
|
---|
275 | assertTrue(layer.isMergable(otherLayer));
|
---|
276 | assertTrue(layer.hasColor());
|
---|
277 | assertTrue(layer.isSavable());
|
---|
278 | assertTrue(layer.checkSaveConditions());
|
---|
279 | assertFalse(layer.isModified());
|
---|
280 | assertFalse(layer.requiresSaveToFile());
|
---|
281 | assertNull(layer.getChangesetSourceTag());
|
---|
282 | assertNull(layer.getAssociatedFile());
|
---|
283 |
|
---|
284 | layer.destroy();
|
---|
285 |
|
---|
286 | assertNull(layer.getData());
|
---|
287 | assertNull(layer.getColor());
|
---|
288 | assertFalse(layer.hasColor());
|
---|
289 | assertFalse(layer.isMergable(otherLayer));
|
---|
290 | assertFalse(layer.isSavable());
|
---|
291 | assertFalse(layer.checkSaveConditions());
|
---|
292 | assertFalse(layer.isModified());
|
---|
293 | assertFalse(layer.requiresSaveToFile());
|
---|
294 | assertNull(layer.getChangesetSourceTag());
|
---|
295 | assertNull(layer.getAssociatedFile());
|
---|
296 | Object infoComponent = layer.getInfoComponent();
|
---|
297 | Component view = assertInstanceOf(JScrollPane.class, infoComponent).getViewport().getView();
|
---|
298 | String text = assertInstanceOf(HtmlPanel.class, view).getEditorPane().getText().trim();
|
---|
299 | assertTrue(text.startsWith("<html>"), text);
|
---|
300 | assertTrue(text.endsWith("</html>"), text);
|
---|
301 | assertEquals("<html><br></html>", layer.getToolTipText());
|
---|
302 | assertDoesNotThrow(layer::jumpToNextMarker);
|
---|
303 | assertDoesNotThrow(layer::jumpToPreviousMarker);
|
---|
304 | assertDoesNotThrow(() -> layer.visitBoundingBox(new BoundingXYVisitor()));
|
---|
305 | assertDoesNotThrow(() -> layer.filterTracksByDate(null, null, false));
|
---|
306 | assertDoesNotThrow(() -> layer.projectionChanged(new CustomProjection(), new CustomProjection()));
|
---|
307 | }
|
---|
308 | }
|
---|