Ticket #11849: 0001-Added-NavigatableComponentTest.patch

File 0001-Added-NavigatableComponentTest.patch, 9.9 KB (added by michael2402, 9 years ago)
  • test/unit/org/openstreetmap/josm/TestUtils.java

    From 71a3a3876cd654b1324d796f4190ef00094d9928 Mon Sep 17 00:00:00 2001
    From: Michael Zangl <michael.zangl@student.kit.edu>
    Date: Thu, 10 Sep 2015 12:43:44 +0200
    Subject: [PATCH] Added NavigatableComponentTest
    
    ---
     test/unit/org/openstreetmap/josm/TestUtils.java    |  36 ++++
     .../josm/gui/NavigatableComponentTest.java         | 187 +++++++++++++++++++++
     2 files changed, 223 insertions(+)
     create mode 100644 test/unit/org/openstreetmap/josm/gui/NavigatableComponentTest.java
    
    diff --git a/test/unit/org/openstreetmap/josm/TestUtils.java b/test/unit/org/openstreetmap/josm/TestUtils.java
    index 729e511..678fce0 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm;
    33
     4import static org.junit.Assert.assertEquals;
    45import static org.junit.Assert.fail;
    56
     7import java.awt.geom.Point2D;
    68import java.util.Arrays;
    79import java.util.Comparator;
    810
     11import org.openstreetmap.josm.data.coor.EastNorth;
     12import org.openstreetmap.josm.data.coor.LatLon;
     13
    914/**
    1015 * Various utils, useful for unit tests.
    1116 */
    public final class TestUtils {  
    106111        .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
    107112        .toString();
    108113    }
     114
     115    /**
     116     * An assertion that fails if the provided coordinates are not the same (within the default server precision).
     117     * @param expected The expected EastNorth coordinate.
     118     * @param actual The actual value.
     119     */
     120    public static void assertEastNorthEquals(EastNorth expected, EastNorth actual) {
     121        assertEquals("Wrong x coordinate.", expected.getX(), actual.getX(), LatLon.MAX_SERVER_PRECISION);
     122        assertEquals("Wrong y coordinate.", expected.getY(), actual.getY(), LatLon.MAX_SERVER_PRECISION);
     123    }
     124
     125    /**
     126     * An assertion that fails if the provided coordinates are not the same (within the default server precision).
     127     * @param expected The expected LatLon coordinate.
     128     * @param actual The actual value.
     129     */
     130    public static void assertLatLonEquals(LatLon expected, LatLon actual) {
     131        assertEquals("Wrong lat coordinate.", expected.getX(), actual.getX(), LatLon.MAX_SERVER_PRECISION);
     132        assertEquals("Wrong lon coordinate.", expected.getY(), actual.getY(), LatLon.MAX_SERVER_PRECISION);
     133    }
     134
     135    /**
     136     * An assertion that fails if the provided points are not the same.
     137     * @param expected The expected Point2D
     138     * @param actual The actual value.
     139     */
     140    public static void assertPointEquals(Point2D expected, Point2D actual) {
     141        if (expected.distance(actual) > 0.0000001) {
     142            throw new AssertionError("Expected " + expected + " but got " + actual);
     143        }
     144    }
    109145}
  • new file test/unit/org/openstreetmap/josm/gui/NavigatableComponentTest.java

    diff --git a/test/unit/org/openstreetmap/josm/gui/NavigatableComponentTest.java b/test/unit/org/openstreetmap/josm/gui/NavigatableComponentTest.java
    new file mode 100644
    index 0000000..22b796e
    - +  
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.gui;
     3
     4import static org.junit.Assert.assertEquals;
     5import static org.openstreetmap.josm.TestUtils.assertEastNorthEquals;
     6import static org.openstreetmap.josm.TestUtils.assertLatLonEquals;
     7import static org.openstreetmap.josm.TestUtils.assertPointEquals;
     8
     9import java.awt.Rectangle;
     10import java.awt.geom.Point2D;
     11
     12import org.junit.Before;
     13import org.junit.BeforeClass;
     14import org.junit.Test;
     15import org.openstreetmap.josm.JOSMFixture;
     16import org.openstreetmap.josm.Main;
     17import org.openstreetmap.josm.data.Bounds;
     18import org.openstreetmap.josm.data.ProjectionBounds;
     19import org.openstreetmap.josm.data.coor.EastNorth;
     20import org.openstreetmap.josm.data.coor.LatLon;
     21import org.openstreetmap.josm.gui.util.GuiHelper;
     22
     23/**
     24 * Some tests for the {@link NavigatableComponent} class.
     25 * @author Michael Zangl
     26 *
     27 */
     28public class NavigatableComponentTest {
     29
     30    private static final int HEIGHT = 200;
     31    private static final int WIDTH = 300;
     32    private NavigatableComponent component;
     33
     34    /**
     35     * Setup test.
     36     */
     37    @BeforeClass
     38    public static void setUp() {
     39        JOSMFixture.createUnitTestFixture().init();
     40    }
     41
     42    /**
     43     * Create a new, fresh {@link NavigatableComponent}
     44     */
     45    @Before
     46    public void setup() {
     47        component = new NavigatableComponent();
     48        component.setBounds(new Rectangle(WIDTH, HEIGHT));
     49        // wait for the event to be propagated.
     50        GuiHelper.runInEDTAndWait(new Runnable() {
     51            @Override
     52            public void run() {
     53            }
     54        });
     55    }
     56
     57    /**
     58     * Test if the default scale was set correctly.
     59     */
     60    @Test
     61    public void testDefaultScale() {
     62        assertEquals(Main.getProjection().getDefaultZoomInPPD(), component.getScale(), 0.00001);
     63    }
     64
     65    /**
     66     * Tests {@link NavigatableComponent#getPoint2D(EastNorth)}
     67     */
     68    @Test
     69    public void testPoint2DEastNorth() {
     70        assertPointEquals(new Point2D.Double(), component.getPoint2D((EastNorth) null));
     71        Point2D shouldBeCenter = component.getPoint2D(component.getCenter());
     72        assertPointEquals(new Point2D.Double(WIDTH / 2, HEIGHT / 2), shouldBeCenter);
     73
     74        EastNorth testPoint = component.getCenter().add(300 * component.getScale(), 200 * component.getScale());
     75        Point2D testPointConverted = component.getPoint2D(testPoint);
     76        assertPointEquals(new Point2D.Double(WIDTH / 2 + 300, HEIGHT / 2 - 200), testPointConverted);
     77    }
     78
     79    /**
     80     * TODO: Implement this test.
     81     */
     82    @Test
     83    public void testPoint2DLatLon() {
     84        assertPointEquals(new Point2D.Double(), component.getPoint2D((LatLon) null));
     85        // TODO: Really test this.
     86    }
     87
     88    /**
     89     * Tests {@link NavigatableComponent#zoomTo(LatLon)}
     90     */
     91    @Test
     92    public void testZoomToLatLon() {
     93        component.zoomTo(new LatLon(10, 10));
     94        Point2D shouldBeCenter = component.getPoint2D(new LatLon(10, 10));
     95        assertPointEquals(new Point2D.Double(WIDTH / 2, HEIGHT / 2), shouldBeCenter);
     96    }
     97
     98    /**
     99     * Tests {@link NavigatableComponent#zoomToFactor(double)} and {@link NavigatableComponent#zoomToFactor(EastNorth, double)}
     100     */
     101    @Test
     102    public void testZoomToFactor() {
     103        EastNorth center = component.getCenter();
     104        double initialScale = component.getScale();
     105
     106        // zoomToFactor(double)
     107        component.zoomToFactor(0.5);
     108        assertEquals(initialScale / 2, component.getScale(), 0.00000001);
     109        assertEastNorthEquals(center, component.getCenter());
     110        component.zoomToFactor(2);
     111        assertEquals(initialScale, component.getScale(), 0.00000001);
     112        assertEastNorthEquals(center, component.getCenter());
     113
     114        // zoomToFactor(EastNorth, double)
     115        EastNorth newCenter = new EastNorth(10, 20);
     116        component.zoomToFactor(newCenter, 0.5);
     117        assertEquals(initialScale / 2, component.getScale(), 0.00000001);
     118        assertEastNorthEquals(newCenter, component.getCenter());
     119        component.zoomToFactor(newCenter, 2);
     120        assertEquals(initialScale, component.getScale(), 0.00000001);
     121        assertEastNorthEquals(newCenter, component.getCenter());
     122    }
     123
     124    /**
     125     * Tests {@link NavigatableComponent#getEastNorth(int, int)}
     126     */
     127    @Test
     128    public void testGetEastNorth() {
     129        EastNorth center = component.getCenter();
     130        assertEastNorthEquals(center, component.getEastNorth(WIDTH / 2, HEIGHT / 2));
     131
     132        EastNorth testPoint = component.getCenter().add(WIDTH * component.getScale(), HEIGHT * component.getScale());
     133        assertEastNorthEquals(testPoint, component.getEastNorth(3 * WIDTH / 2, -HEIGHT / 2));
     134    }
     135
     136    /**
     137     * Tests {@link NavigatableComponent#zoomToFactor(double, double, double)}
     138     */
     139    @Test
     140    public void testZoomToFactorCenter() {
     141        // zoomToFactor(double, double, double)
     142        // assumes getEastNorth works as expected
     143        EastNorth testPoint1 = component.getEastNorth(0, 0);
     144        EastNorth testPoint2 = component.getEastNorth(200, 150);
     145        double initialScale = component.getScale();
     146
     147        component.zoomToFactor(0, 0, 0.5);
     148        assertEquals(initialScale / 2, component.getScale(), 0.00000001);
     149        assertEastNorthEquals(testPoint1, component.getEastNorth(0, 0));
     150        component.zoomToFactor(0, 0, 2);
     151        assertEquals(initialScale, component.getScale(), 0.00000001);
     152        assertEastNorthEquals(testPoint1, component.getEastNorth(0, 0));
     153
     154        component.zoomToFactor(200, 150, 0.5);
     155        assertEquals(initialScale / 2, component.getScale(), 0.00000001);
     156        assertEastNorthEquals(testPoint2, component.getEastNorth(200, 150));
     157        component.zoomToFactor(200, 150, 2);
     158        assertEquals(initialScale, component.getScale(), 0.00000001);
     159        assertEastNorthEquals(testPoint2, component.getEastNorth(200, 150));
     160
     161    }
     162
     163    /**
     164     * Tests {@link NavigatableComponent#getProjectionBounds()}
     165     */
     166    @Test
     167    public void testGetProjectionBounds() {
     168        ProjectionBounds bounds = component.getProjectionBounds();
     169        assertEastNorthEquals(component.getCenter(), bounds.getCenter());
     170
     171        assertEastNorthEquals(component.getEastNorth(0, HEIGHT), bounds.getMin());
     172        assertEastNorthEquals(component.getEastNorth(WIDTH, 0), bounds.getMax());
     173    }
     174
     175    /**
     176     * Tests {@link NavigatableComponent#getRealBounds()}
     177     */
     178    @Test
     179    public void testGetRealBounds() {
     180        Bounds bounds = component.getRealBounds();
     181        assertLatLonEquals(component.getLatLon(WIDTH / 2, HEIGHT / 2), bounds.getCenter());
     182
     183        assertLatLonEquals(component.getLatLon(0, HEIGHT), bounds.getMin());
     184        assertLatLonEquals(component.getLatLon(WIDTH, 0), bounds.getMax());
     185    }
     186
     187}