[9666] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
| 3 |
|
---|
| 4 | import static org.junit.Assert.assertEquals;
|
---|
| 5 |
|
---|
| 6 | import java.io.FileNotFoundException;
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.util.List;
|
---|
[12156] | 9 | import java.util.stream.Collectors;
|
---|
[9666] | 10 |
|
---|
[12564] | 11 | import org.junit.Rule;
|
---|
[9666] | 12 | import org.junit.Test;
|
---|
| 13 | import org.openstreetmap.josm.TestUtils;
|
---|
| 14 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
[12157] | 15 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
[9666] | 16 | import org.openstreetmap.josm.io.GpxReaderTest;
|
---|
[12849] | 17 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[12564] | 18 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
[9666] | 19 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
| 20 | import org.xml.sax.SAXException;
|
---|
| 21 |
|
---|
[12564] | 22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
| 23 |
|
---|
[9666] | 24 | /**
|
---|
| 25 | * Unit tests of {@link GpxDrawHelper} class.
|
---|
| 26 | */
|
---|
| 27 | public class GpxDrawHelperTest {
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * Setup test.
|
---|
| 31 | */
|
---|
[12564] | 32 | @Rule
|
---|
| 33 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
| 34 | public JOSMTestRules test = new JOSMTestRules();
|
---|
[9666] | 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 {
|
---|
[12849] | 44 | Config.getPref().putBoolean("draw.rawgps.colors.dynamic.layer 12312", true);
|
---|
| 45 | Config.getPref().putInt("draw.rawgps.colors.layer 12312", GpxDrawHelper.ColorMode.VELOCITY.toIndex());
|
---|
[9666] | 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 {
|
---|
[12849] | 70 | Config.getPref().putInt("draw.rawgps.colors.layer 001", GpxDrawHelper.ColorMode.VELOCITY.toIndex());
|
---|
[9666] | 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 {
|
---|
[12849] | 83 | Config.getPref().putInt("draw.rawgps.colors.layer 002", GpxDrawHelper.ColorMode.VELOCITY.toIndex());
|
---|
| 84 | Config.getPref().putBoolean("draw.rawgps.colors.dynamic.layer 002", true);
|
---|
[9666] | 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 {
|
---|
[12849] | 97 | Config.getPref().putInt("draw.rawgps.colors.layer 003", GpxDrawHelper.ColorMode.DIRECTION.toIndex());
|
---|
[9666] | 98 | final List<String> colors = calculateColors("data_nodist/2094047.gpx", "003", 10);
|
---|
[9809] | 99 | assertEquals("[#FF00FF, #EAEC25, #EDEA26, #EDE525, #ECD322, #EBB81D, #E85A0D, #E73708, #E84D0B, #EA8A15]", colors.toString());
|
---|
[9666] | 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 {
|
---|
[12849] | 110 | Config.getPref().putInt("draw.rawgps.colors.layer 003", GpxDrawHelper.ColorMode.TIME.toIndex());
|
---|
[9666] | 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);
|
---|
[12157] | 127 | final GpxLayer layer = new GpxLayer(data);
|
---|
| 128 | final GpxDrawHelper gdh = new GpxDrawHelper(layer);
|
---|
[9666] | 129 | gdh.readPreferences(layerName);
|
---|
| 130 | gdh.calculateColors();
|
---|
[12156] | 131 | return data.getTrackPoints().limit(n).map(p -> ColorHelper.color2html(p.customColoring)).collect(Collectors.toList());
|
---|
[9666] | 132 | }
|
---|
| 133 | }
|
---|