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