source: josm/trunk/test/unit/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelperTest.java@ 9504

Last change on this file since 9504 was 9395, checked in by simon04, 8 years ago

Extent the unit test for GpxDrawHelper

File size: 6.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.gpx;
3
4import static org.junit.Assert.assertEquals;
5
6import java.io.FileNotFoundException;
7import java.io.IOException;
8import java.util.ArrayList;
9import java.util.Iterator;
10import java.util.List;
11
12import org.junit.BeforeClass;
13import org.junit.Test;
14import org.openstreetmap.josm.JOSMFixture;
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.TestUtils;
17import org.openstreetmap.josm.data.gpx.GpxData;
18import org.openstreetmap.josm.data.gpx.WayPoint;
19import org.openstreetmap.josm.io.GpxReaderTest;
20import org.openstreetmap.josm.tools.ColorHelper;
21import org.xml.sax.SAXException;
22
23/**
24 * Unit tests of {@link GpxDrawHelper} class.
25 */
26public class GpxDrawHelperTest {
27
28 /**
29 * Setup test.
30 */
31 @BeforeClass
32 public static void setUpBeforeClass() {
33 JOSMFixture.createUnitTestFixture().init(false);
34 }
35
36 /**
37 * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/12312">#12312</a>.
38 * @throws IOException if any I/O error occurs
39 * @throws FileNotFoundException if the data file is not found
40 * @throws SAXException if any SAX error occurs
41 */
42 @Test
43 public void testTicket12312() throws FileNotFoundException, IOException, SAXException {
44 Main.pref.put("draw.rawgps.colors.dynamic.layer 12312", true);
45 Main.pref.putInteger("draw.rawgps.colors.layer 12312", GpxDrawHelper.ColorMode.VELOCITY.toIndex());
46 final List<String> colors = calculateColors(TestUtils.getRegressionDataFile(12312, "single_trackpoint.gpx"), "12312", 1);
47 assertEquals("[#FF00FF]", colors.toString());
48 }
49
50 /**
51 * Tests coloring of an example track using the default color.
52 * @throws IOException if any I/O error occurs
53 * @throws FileNotFoundException if the data file is not found
54 * @throws SAXException if any SAX error occurs
55 */
56 @Test
57 public void testNone() throws IOException, SAXException {
58 final List<String> colors = calculateColors("data_nodist/2094047.gpx", "000", 10);
59 assertEquals("[#FF00FF, #FF00FF, #FF00FF, #FF00FF, #FF00FF, #FF00FF, #FF00FF, #FF00FF, #FF00FF, #FF00FF]", colors.toString());
60 }
61
62 /**
63 * Tests coloring of an example track using its velocity.
64 * @throws IOException if any I/O error occurs
65 * @throws FileNotFoundException if the data file is not found
66 * @throws SAXException if any SAX error occurs
67 */
68 @Test
69 public void testVelocity() throws IOException, SAXException {
70 Main.pref.putInteger("draw.rawgps.colors.layer 001", GpxDrawHelper.ColorMode.VELOCITY.toIndex());
71 final List<String> colors = calculateColors("data_nodist/2094047.gpx", "001", 10);
72 assertEquals("[#FF00FF, #FFAD00, #FFA800, #FFA800, #FF9E00, #FF9400, #FF7000, #FF7000, #FF8000, #FF9400]", colors.toString());
73 }
74
75 /**
76 * Tests coloring of an example track using its velocity with a dynamic scale
77 * @throws IOException if any I/O error occurs
78 * @throws FileNotFoundException if the data file is not found
79 * @throws SAXException if any SAX error occurs
80 */
81 @Test
82 public void testVelocityDynamic() throws IOException, SAXException {
83 Main.pref.putInteger("draw.rawgps.colors.layer 002", GpxDrawHelper.ColorMode.VELOCITY.toIndex());
84 Main.pref.put("draw.rawgps.colors.dynamic.layer 002", true);
85 final List<String> colors = calculateColors("data_nodist/2094047.gpx", "002", 10);
86 assertEquals("[#FF00FF, #00FFE0, #00FFC2, #00FFC2, #00FF75, #00FF3D, #99FF00, #94FF00, #38FF00, #00FF38]", colors.toString());
87 }
88
89 /**
90 * Tests coloring of an example track using its direction.
91 * @throws IOException if any I/O error occurs
92 * @throws FileNotFoundException if the data file is not found
93 * @throws SAXException if any SAX error occurs
94 */
95 @Test
96 public void testDirection() throws IOException, SAXException {
97 Main.pref.putInteger("draw.rawgps.colors.layer 003", GpxDrawHelper.ColorMode.DIRECTION.toIndex());
98 final List<String> colors = calculateColors("data_nodist/2094047.gpx", "003", 10);
99 assertEquals("[#FF00FF, #E8EC25, #EDEA26, #EDE625, #ECD622, #ECBC1E, #E8600E, #E73C09, #E8540C, #EA9116]", colors.toString());
100 }
101
102 /**
103 * Tests coloring of an example track using its direction.
104 * @throws IOException if any I/O error occurs
105 * @throws FileNotFoundException if the data file is not found
106 * @throws SAXException if any SAX error occurs
107 */
108 @Test
109 public void testTime() throws IOException, SAXException {
110 Main.pref.putInteger("draw.rawgps.colors.layer 003", GpxDrawHelper.ColorMode.TIME.toIndex());
111 final List<String> colors = calculateColors("data_nodist/2094047.gpx", "003", 10);
112 assertEquals("[#FF00FF, #FF0000, #FF0000, #FF0500, #FF0500, #FF0A00, #FF0A00, #FF1F00, #FF2E00, #FF3300]", colors.toString());
113 }
114
115 /**
116 *
117 * @param fileName the GPX filename to parse
118 * @param layerName the layer name used to fetch the color settings, see {@link GpxDrawHelper#readPreferences(java.lang.String)}
119 * @param n the number of waypoints of the first track/segment to analyze
120 * @return the HTML color codes for the first {@code n} points
121 * @throws IOException if any I/O error occurs
122 * @throws FileNotFoundException if the data file is not found
123 * @throws SAXException if any SAX error occurs
124 */
125 static List<String> calculateColors(String fileName, String layerName, int n) throws IOException, SAXException {
126 final GpxData data = GpxReaderTest.parseGpxData(fileName);
127 final GpxDrawHelper gdh = new GpxDrawHelper(data);
128 gdh.readPreferences(layerName);
129 gdh.calculateColors();
130 final Iterator<WayPoint> wayPointIterator = data.tracks.iterator().next().getSegments().iterator().next().getWayPoints().iterator();
131 final List<String> colorCodes = new ArrayList<>(n);
132 while (colorCodes.size() < n) {
133 colorCodes.add(ColorHelper.color2html(wayPointIterator.next().customColoring));
134 }
135 return colorCodes;
136 }
137}
Note: See TracBrowser for help on using the repository browser.