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

Last change on this file since 12849 was 12849, checked in by bastiK, 7 years ago

see #15229 - use Config in tests

  • Property svn:eol-style set to native
File size: 6.0 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.List;
9import java.util.stream.Collectors;
10
11import org.junit.Rule;
12import org.junit.Test;
13import org.openstreetmap.josm.TestUtils;
14import org.openstreetmap.josm.data.gpx.GpxData;
15import org.openstreetmap.josm.gui.layer.GpxLayer;
16import org.openstreetmap.josm.io.GpxReaderTest;
17import org.openstreetmap.josm.spi.preferences.Config;
18import org.openstreetmap.josm.testutils.JOSMTestRules;
19import org.openstreetmap.josm.tools.ColorHelper;
20import org.xml.sax.SAXException;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * Unit tests of {@link GpxDrawHelper} class.
26 */
27public class GpxDrawHelperTest {
28
29 /**
30 * Setup test.
31 */
32 @Rule
33 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
34 public JOSMTestRules test = new JOSMTestRules();
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 Config.getPref().putBoolean("draw.rawgps.colors.dynamic.layer 12312", true);
45 Config.getPref().putInt("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 Config.getPref().putInt("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 Config.getPref().putInt("draw.rawgps.colors.layer 002", GpxDrawHelper.ColorMode.VELOCITY.toIndex());
84 Config.getPref().putBoolean("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 Config.getPref().putInt("draw.rawgps.colors.layer 003", GpxDrawHelper.ColorMode.DIRECTION.toIndex());
98 final List<String> colors = calculateColors("data_nodist/2094047.gpx", "003", 10);
99 assertEquals("[#FF00FF, #EAEC25, #EDEA26, #EDE525, #ECD322, #EBB81D, #E85A0D, #E73708, #E84D0B, #EA8A15]", 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 Config.getPref().putInt("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 GpxLayer layer = new GpxLayer(data);
128 final GpxDrawHelper gdh = new GpxDrawHelper(layer);
129 gdh.readPreferences(layerName);
130 gdh.calculateColors();
131 return data.getTrackPoints().limit(n).map(p -> ColorHelper.color2html(p.customColoring)).collect(Collectors.toList());
132 }
133}
Note: See TracBrowser for help on using the repository browser.