source: josm/trunk/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java@ 15924

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

fix #18761 - only consider source=survey for local GPX files, not the public ones

  • Property svn:eol-style set to native
File size: 9.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8
9import java.awt.Color;
10import java.io.IOException;
11import java.util.ArrayList;
12import java.util.Collection;
13import java.util.HashMap;
14import java.util.TimeZone;
15
16import javax.swing.JScrollPane;
17
18import org.junit.Rule;
19import org.junit.Test;
20import org.openstreetmap.josm.TestUtils;
21import org.openstreetmap.josm.data.gpx.GpxData;
22import org.openstreetmap.josm.data.gpx.GpxTrack;
23import org.openstreetmap.josm.data.gpx.IGpxTrackSegment;
24import org.openstreetmap.josm.data.gpx.WayPoint;
25import org.openstreetmap.josm.data.osm.DataSet;
26import org.openstreetmap.josm.data.projection.Projections;
27import org.openstreetmap.josm.gui.MainApplication;
28import org.openstreetmap.josm.gui.widgets.HtmlPanel;
29import org.openstreetmap.josm.io.GpxReaderTest;
30import org.openstreetmap.josm.testutils.JOSMTestRules;
31import org.xml.sax.SAXException;
32
33import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
34
35/**
36 * Unit tests of {@link GpxLayer} class.
37 */
38public class GpxLayerTest {
39
40 /**
41 * Setup tests
42 */
43 @Rule
44 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
45 public JOSMTestRules test = new JOSMTestRules().main().projection().i18n().metricSystem();
46
47 private static String getHtml(GpxLayer layer) {
48 return ((HtmlPanel) ((JScrollPane) layer.getInfoComponent()).getViewport().getView()).getEditorPane().getText();
49 }
50
51 /**
52 * Returns minimal GPX data.
53 * @return minimal GPX data, with a single waypoint, a single track composed of a single segment
54 * @throws IOException if any I/O error occurs
55 * @throws SAXException if any SAX error occurs
56 */
57 public static GpxData getMinimalGpxData() throws IOException, SAXException {
58 return GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + "minimal.gpx");
59 }
60
61 /**
62 * Returns minimal GPX layer.
63 * @return minimal GPX layer, with a single waypoint, a single track composed of a single segment
64 * @throws IOException if any I/O error occurs
65 * @throws SAXException if any SAX error occurs
66 */
67 public static GpxLayer getMinimalGpxLayer() throws IOException, SAXException {
68 return new GpxLayer(getMinimalGpxData(), "Bananas");
69 }
70
71 /**
72 * Unit test of {@link GpxLayer#GpxLayer}.
73 * @throws Exception if any error occurs
74 */
75 @Test
76 public void testGpxLayer() throws Exception {
77 GpxLayer layer = new GpxLayer(new GpxData(), "foo", false);
78 GpxTrack trk = new GpxTrack(new ArrayList<IGpxTrackSegment>(), new HashMap<>());
79 trk.getExtensions().add("gpxd", "color", "#FF0000");
80 layer.data.addTrack(trk);
81
82 assertEquals("foo", layer.getName());
83 assertFalse(layer.isLocalFile());
84 assertEquals(layer.getColor(), Color.RED);
85 assertEquals("<html>1 track (0 segments), 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer.getToolTipText());
86
87 GpxLayer layer2 = new GpxLayer(new GpxData(), "bar", true);
88 assertEquals("bar", layer2.getName());
89 assertTrue(layer2.isLocalFile());
90 assertNull(layer2.getColor());
91 assertEquals("<html>0 tracks (0 segments), 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer2.getToolTipText());
92
93 assertTrue(layer.checkSaveConditions());
94 assertTrue(layer.isInfoResizable());
95 assertTrue(layer.isSavable());
96 assertTrue(layer.isMergable(layer2));
97
98 layer.projectionChanged(null, null);
99 layer.projectionChanged(null, Projections.getProjectionByCode("EPSG:3857"));
100 }
101
102 /**
103 * Unit test of {@link GpxLayer#getInfoComponent}.
104 * @throws Exception if any error occurs
105 */
106 @Test
107 public void testGetInfoComponent() throws Exception {
108 assertEquals("<html>\n"+
109 " <head>\n" +
110 " <style type=\"text/css\">\n" +
111 " <!--\n" +
112 " td { padding-top: 4px; padding-bottom: 4px; padding-right: 16px; padding-left: 16px }\n" +
113 " -->\n" +
114 " </style>\n" +
115 " \n" +
116 " </head>\n" +
117 " <body>\n" +
118 " Length: 0.01 m<br>0 routes, 0 waypoints<br>\n" +
119 " </body>\n" +
120 "</html>\n",
121 getHtml(new GpxLayer(new GpxData())));
122
123 assertEquals("<html>\n"+
124 " <head>\n" +
125 " <style type=\"text/css\">\n" +
126 " <!--\n" +
127 " td { padding-top: 4px; padding-bottom: 4px; padding-right: 16px; padding-left: 16px }\n" +
128 " -->\n" +
129 " </style>\n" +
130 " \n" +
131 " </head>\n" +
132 " <body>\n" +
133 " Creator: MapSource 6.16.3<br>\n\n" +
134 " <table>\n" +
135 " <tr align=\"center\">\n" +
136 " <td colspan=\"5\">\n" +
137 " 1 track, 1 track segments\n" +
138 " </td>\n" +
139 " </tr>\n" +
140 " <tr align=\"center\">\n" +
141 " <td>\n" +
142 " Name\n" +
143 " </td>\n" +
144 " <td>\n" +
145 " Description\n" +
146 " </td>\n" +
147 " <td>\n" +
148 " Timespan\n" +
149 " </td>\n" +
150 " <td>\n" +
151 " Length\n" +
152 " </td>\n" +
153 " <td>\n" +
154 " Number of<br>Segments\n" +
155 " </td>\n" +
156 " <td>\n" +
157 " URL\n" +
158 " </td>\n" +
159 " </tr>\n" +
160 " <tr>\n" +
161 " <td>\n" +
162 " 2016-01-03 20:40:14\n" +
163 " </td>\n" +
164 " <td>\n" +
165 " \n" +
166 " </td>\n" +
167 " <td>\n" +
168 " 1/3/16 11:59 AM - 12:00 PM (0:00)\n" +
169 " </td>\n" +
170 " <td>\n" +
171 " 12.0 m\n" +
172 " </td>\n" +
173 " <td>\n" +
174 " 1\n" +
175 " </td>\n" +
176 " <td>\n" +
177 " \n" +
178 " </td>\n" +
179 " </tr>\n" +
180 " </table>\n" +
181 " <br>\n" +
182 " <br>\n" +
183 " Length: 12.0 m<br>0 routes, 1 waypoint<br>\n" +
184 " </body>\n" +
185 "</html>\n",
186 getHtml(getMinimalGpxLayer()));
187 }
188
189 /**
190 * Unit test of {@link GpxLayer#getTimespanForTrack}.
191 * @throws Exception if any error occurs
192 */
193 @Test
194 public void testGetTimespanForTrack() throws Exception {
195 assertEquals("", GpxLayer.getTimespanForTrack(
196 new GpxTrack(new ArrayList<Collection<WayPoint>>(), new HashMap<String, Object>())));
197
198 assertEquals("1/3/16 11:59 AM - 12:00 PM (0:00)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
199
200 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
201 assertEquals("1/3/16 12:59 PM - 1:00 PM (0:00)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
202 }
203
204 /**
205 * Unit test of {@link GpxLayer#mergeFrom}.
206 * @throws Exception if any error occurs
207 */
208 @Test
209 public void testMergeFrom() throws Exception {
210 GpxLayer layer = new GpxLayer(new GpxData());
211 assertTrue(layer.data.isEmpty());
212 layer.mergeFrom(getMinimalGpxLayer());
213 assertFalse(layer.data.isEmpty());
214 assertEquals(1, layer.data.tracks.size());
215 assertEquals(1, layer.data.waypoints.size());
216 }
217
218 /**
219 * Test that {@link GpxLayer#mergeFrom} throws IAE for invalid arguments
220 */
221 @Test(expected = IllegalArgumentException.class)
222 public void testMergeFromIAE() {
223 new GpxLayer(new GpxData()).mergeFrom(new OsmDataLayer(new DataSet(), "", null));
224 }
225
226 /**
227 * Unit test of {@link GpxLayer#paint}.
228 * @throws Exception if any error occurs
229 */
230 @Test
231 public void testPaint() throws Exception {
232 GpxLayer layer = getMinimalGpxLayer();
233 try {
234 MainApplication.getLayerManager().addLayer(layer);
235 assertTrue(layer.getMenuEntries().length > 0);
236 layer.paint(TestUtils.newGraphics(), MainApplication.getMap().mapView, layer.data.getMetaBounds());
237 } finally {
238 MainApplication.getLayerManager().removeLayer(layer);
239 }
240 }
241
242 /**
243 * Unit test of {@link GpxLayer#getChangesetSourceTag}.
244 */
245 @Test
246 public void testGetChangesetSourceTag() {
247 assertEquals("survey", new GpxLayer(new GpxData(), "", true).getChangesetSourceTag());
248 assertNull(new GpxLayer(new GpxData(), "", false).getChangesetSourceTag());
249 }
250}
Note: See TracBrowser for help on using the repository browser.