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