Changeset 9395 in josm for trunk/test


Ignore:
Timestamp:
2016-01-10T18:40:36+01:00 (9 years ago)
Author:
simon04
Message:

Extent the unit test for GpxDrawHelper

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelperTest.java

    r9339 r9395  
    22package org.openstreetmap.josm.gui.layer.gpx;
    33
    4 import java.io.FileInputStream;
     4import static org.junit.Assert.assertEquals;
     5
    56import java.io.FileNotFoundException;
    67import java.io.IOException;
    7 import java.io.InputStream;
     8import java.util.ArrayList;
     9import java.util.Iterator;
     10import java.util.List;
    811
    912import org.junit.BeforeClass;
     
    1215import org.openstreetmap.josm.Main;
    1316import org.openstreetmap.josm.TestUtils;
    14 import org.openstreetmap.josm.io.GpxReader;
     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;
    1521import org.xml.sax.SAXException;
    1622
     
    3642    @Test
    3743    public void testTicket12312() throws FileNotFoundException, IOException, SAXException {
    38         try (InputStream in = new FileInputStream((TestUtils.getRegressionDataFile(12312, "single_trackpoint.gpx")))) {
    39             GpxReader reader = new GpxReader(in);
    40             reader.parse(false);
    41             GpxDrawHelper gdh = new GpxDrawHelper(reader.getGpxData());
    42             Main.pref.put("draw.rawgps.colors.dynamic.layer 12312", true);
    43             Main.pref.putInteger("draw.rawgps.colors.layer 12312", 1); // ColorMode.VELOCITY
    44             gdh.readPreferences("12312");
    45             gdh.calculateColors();
     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));
    46134        }
     135        return colorCodes;
    47136    }
    48137}
Note: See TracChangeset for help on using the changeset viewer.