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

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

see #13001 - replace calls to Main.main.[add|remove]Layer by Main.getLayerManager().[add|remove]Layer

  • Property svn:eol-style set to native
File size: 7.4 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.assertTrue;
7
8import java.awt.Color;
9import java.io.IOException;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.HashMap;
13
14import javax.swing.JScrollPane;
15
16import org.junit.BeforeClass;
17import org.junit.Test;
18import org.openstreetmap.josm.JOSMFixture;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.TestUtils;
21import org.openstreetmap.josm.data.gpx.GpxData;
22import org.openstreetmap.josm.data.gpx.ImmutableGpxTrack;
23import org.openstreetmap.josm.data.gpx.WayPoint;
24import org.openstreetmap.josm.data.projection.Projections;
25import org.openstreetmap.josm.gui.widgets.HtmlPanel;
26import org.openstreetmap.josm.io.GpxReaderTest;
27import org.xml.sax.SAXException;
28
29/**
30 * Unit tests of {@link GpxLayer} class.
31 */
32public class GpxLayerTest {
33
34 /**
35 * Setup tests
36 */
37 @BeforeClass
38 public static void setUpBeforeClass() {
39 JOSMFixture.createUnitTestFixture().init(true);
40 }
41
42 private static String getHtml(GpxLayer layer) {
43 return ((HtmlPanel) ((JScrollPane) layer.getInfoComponent()).getViewport().getView()).getEditorPane().getText();
44 }
45
46 /**
47 * Returns minimal GPX data.
48 * @return minimal GPX data, with a single waypoint, a single track composed of a single segment
49 * @throws IOException if any I/O error occurs
50 * @throws SAXException if any SAX error occurs
51 */
52 public static GpxData getMinimalGpxData() throws IOException, SAXException {
53 return GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + "minimal.gpx");
54 }
55
56 /**
57 * Returns minimal GPX layer.
58 * @return minimal GPX layer, with a single waypoint, a single track composed of a single segment
59 * @throws IOException if any I/O error occurs
60 * @throws SAXException if any SAX error occurs
61 */
62 public static GpxLayer getMinimalGpxLayer() throws IOException, SAXException {
63 return new GpxLayer(getMinimalGpxData());
64 }
65
66 /**
67 * Unit test of {@link GpxLayer#GpxLayer}.
68 * @throws Exception if any error occurs
69 */
70 @Test
71 public void testGpxLayer() throws Exception {
72 GpxLayer layer = new GpxLayer(new GpxData(), "foo", false);
73 assertEquals("foo", layer.getName());
74 assertFalse(layer.isLocalFile());
75 assertEquals(Color.MAGENTA, layer.getColor(false));
76 assertEquals("<html>0 tracks, 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer.getToolTipText());
77
78 GpxLayer layer2 = new GpxLayer(new GpxData(), "bar", true);
79 assertEquals("bar", layer2.getName());
80 assertTrue(layer2.isLocalFile());
81 assertEquals(Color.MAGENTA, layer2.getColor(true));
82 assertEquals("<html>0 tracks, 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer2.getToolTipText());
83
84 assertFalse(layer.isChanged());
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
173 assertEquals("1/3/16 12:59 PM - 1:00 PM (0:00)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
174 }
175
176 /**
177 * Unit test of {@link GpxLayer#mergeFrom}.
178 * @throws Exception if any error occurs
179 */
180 @Test
181 public void testMergeFrom() throws Exception {
182 GpxLayer layer = new GpxLayer(new GpxData());
183 assertTrue(layer.data.isEmpty());
184 layer.mergeFrom(getMinimalGpxLayer());
185 assertFalse(layer.data.isEmpty());
186 assertEquals(1, layer.data.tracks.size());
187 assertEquals(1, layer.data.waypoints.size());
188 }
189
190 /**
191 * Unit test of {@link GpxLayer#paint}.
192 * @throws Exception if any error occurs
193 */
194 @Test
195 public void testPaint() throws Exception {
196 GpxLayer layer = getMinimalGpxLayer();
197 try {
198 Main.getLayerManager().addLayer(layer);
199 assertTrue(layer.getMenuEntries().length > 0);
200 layer.paint(TestUtils.newGraphics(), Main.map.mapView, layer.data.getMetaBounds());
201 } finally {
202 Main.getLayerManager().removeLayer(layer);
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.