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

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

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
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.jupiter.api.Assertions.assertEquals;
5
6import java.io.FileNotFoundException;
7import java.io.IOException;
8import java.util.Collections;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12import java.util.stream.Collectors;
13
14import org.junit.jupiter.api.extension.RegisterExtension;
15import org.junit.jupiter.api.Test;
16import org.openstreetmap.josm.TestUtils;
17import org.openstreetmap.josm.data.gpx.GpxData;
18import org.openstreetmap.josm.gui.layer.GpxLayer;
19import org.openstreetmap.josm.gui.layer.gpx.GpxDrawHelper.ColorMode;
20import org.openstreetmap.josm.io.GpxReaderTest;
21import org.openstreetmap.josm.testutils.JOSMTestRules;
22import org.openstreetmap.josm.tools.ColorHelper;
23import org.xml.sax.SAXException;
24
25import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
26
27/**
28 * Unit tests of {@link GpxDrawHelper} class.
29 */
30class GpxDrawHelperTest {
31
32 /**
33 * Setup test.
34 */
35 @RegisterExtension
36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37 public JOSMTestRules test = new JOSMTestRules();
38
39 /**
40 * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/12312">#12312</a>.
41 * @throws IOException if any I/O error occurs
42 * @throws FileNotFoundException if the data file is not found
43 * @throws SAXException if any SAX error occurs
44 */
45 @Test
46 void testTicket12312() throws FileNotFoundException, IOException, SAXException {
47 final Map<String, String> prefs = new HashMap<String, String>() {{
48 put("colormode.dynamic-range", "true");
49 put("colormode", Integer.toString(ColorMode.VELOCITY.toIndex()));
50 }};
51 final List<String> colors = calculateColors(TestUtils.getRegressionDataFile(12312, "single_trackpoint.gpx"), prefs, 1);
52 assertEquals("[null]", colors.toString());
53 }
54
55 /**
56 * Tests coloring of an example track using the default color.
57 * @throws IOException if any I/O error occurs
58 * @throws FileNotFoundException if the data file is not found
59 * @throws SAXException if any SAX error occurs
60 */
61 @Test
62 void testNone() throws IOException, SAXException {
63 final List<String> colors = calculateColors("nodist/data/2094047.gpx", Collections.emptyMap(), 10);
64 assertEquals("[#000000, #000000, #000000, #000000, #000000, #000000, #000000, #000000, #000000, #000000]", colors.toString());
65 }
66
67 /**
68 * Tests coloring of an example track using its velocity.
69 * @throws IOException if any I/O error occurs
70 * @throws FileNotFoundException if the data file is not found
71 * @throws SAXException if any SAX error occurs
72 */
73 @Test
74 void testVelocity() throws IOException, SAXException {
75 final Map<String, String> prefs = Collections.singletonMap("colormode", Integer.toString(ColorMode.VELOCITY.toIndex()));
76 final List<String> colors = calculateColors("nodist/data/2094047.gpx", prefs, 10);
77 assertEquals("[#000000, #FFAD00, #FFA800, #FFA800, #FF9E00, #FF9400, #FF7000, #FF7000, #FF8000, #FF9400]", colors.toString());
78 }
79
80 /**
81 * Tests coloring of an example track using its velocity with a dynamic scale
82 * @throws IOException if any I/O error occurs
83 * @throws FileNotFoundException if the data file is not found
84 * @throws SAXException if any SAX error occurs
85 */
86 @Test
87 void testVelocityDynamic() throws IOException, SAXException {
88 final Map<String, String> prefs = new HashMap<String, String>() {{
89 put("colormode.dynamic-range", "true");
90 put("colormode", Integer.toString(ColorMode.VELOCITY.toIndex()));
91 }};
92 final List<String> colors = calculateColors("nodist/data/2094047.gpx", prefs, 10);
93 assertEquals("[#000000, #00FFE0, #00FFC2, #00FFC2, #00FF75, #00FF3D, #99FF00, #94FF00, #38FF00, #00FF38]", colors.toString());
94 }
95
96 /**
97 * Tests coloring of an example track using its direction.
98 * @throws IOException if any I/O error occurs
99 * @throws FileNotFoundException if the data file is not found
100 * @throws SAXException if any SAX error occurs
101 */
102 @Test
103 void testDirection() throws IOException, SAXException {
104 final Map<String, String> prefs = Collections.singletonMap("colormode", Integer.toString(ColorMode.DIRECTION.toIndex()));
105 final List<String> colors = calculateColors("nodist/data/2094047.gpx", prefs, 10);
106 assertEquals("[#000000, #EAEC25, #EDEA26, #EDE525, #ECD322, #EBB81D, #E85A0D, #E73708, #E84D0B, #EA8A15]", colors.toString());
107 }
108
109 /**
110 * Tests coloring of an example track using its direction.
111 * @throws IOException if any I/O error occurs
112 * @throws FileNotFoundException if the data file is not found
113 * @throws SAXException if any SAX error occurs
114 */
115 @Test
116 void testTime() throws IOException, SAXException {
117 final Map<String, String> prefs = Collections.singletonMap("colormode", Integer.toString(ColorMode.TIME.toIndex()));
118 final List<String> colors = calculateColors("nodist/data/2094047.gpx", prefs, 10);
119 assertEquals("[#000000, #FF0000, #FF0000, #FF0500, #FF0500, #FF0A00, #FF0A00, #FF1F00, #FF2E00, #FF3300]", colors.toString());
120 }
121
122 /**
123 *
124 * @param fileName the GPX filename to parse
125 * @param layerPrefs a HashMap representing the layer specific preferences
126 * @param n the number of waypoints of the first track/segment to analyze
127 * @return the HTML color codes for the first {@code n} points
128 * @throws IOException if any I/O error occurs
129 * @throws FileNotFoundException if the data file is not found
130 * @throws SAXException if any SAX error occurs
131 */
132 static List<String> calculateColors(String fileName, Map<String, String> layerPrefs, int n) throws IOException, SAXException {
133 final GpxData data = GpxReaderTest.parseGpxData(fileName);
134 data.getLayerPrefs().putAll(layerPrefs);
135 final GpxLayer layer = new GpxLayer(data);
136 final GpxDrawHelper gdh = new GpxDrawHelper(layer);
137 gdh.readPreferences();
138 gdh.calculateColors();
139 return data.getTrackPoints().limit(n).map(p -> ColorHelper.color2html(p.customColoring)).collect(Collectors.toList());
140 }
141}
Note: See TracBrowser for help on using the repository browser.