Changeset 11867 in josm for trunk/test/unit


Ignore:
Timestamp:
2017-04-09T16:00:43+02:00 (8 years ago)
Author:
bastiK
Message:

see #7427 - fix some unit tests

Location:
trunk/test/unit/org
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/CustomMatchers.java

    r10691 r11867  
    44import java.awt.geom.Point2D;
    55import java.util.Collection;
     6import java.util.Objects;
    67import java.util.function.Predicate;
    78
     
    8889     */
    8990    public static Matcher<? super Point2D> is(final Point2D expected) {
    90         return new CustomTypeSafeMatcher<Point2D>("the same Point2D") {
     91        return new CustomTypeSafeMatcher<Point2D>(Objects.toString(expected)) {
    9192            @Override
    9293            protected boolean matchesSafely(Point2D actual) {
     
    102103     */
    103104    public static Matcher<? super LatLon> is(final LatLon expected) {
    104         return new CustomTypeSafeMatcher<LatLon>("the same LatLon") {
     105        return new CustomTypeSafeMatcher<LatLon>(Objects.toString(expected)) {
    105106            @Override
    106107            protected boolean matchesSafely(LatLon actual) {
     
    117118     */
    118119    public static Matcher<? super EastNorth> is(final EastNorth expected) {
    119         return new CustomTypeSafeMatcher<EastNorth>("the same EastNorth") {
     120        return new CustomTypeSafeMatcher<EastNorth>(Objects.toString(expected)) {
    120121            @Override
    121122            protected boolean matchesSafely(EastNorth actual) {
  • trunk/test/unit/org/openstreetmap/josm/gui/NavigatableComponentTest.java

    r11838 r11867  
    88import java.awt.Rectangle;
    99import java.awt.geom.Point2D;
     10import java.util.Objects;
    1011
    1112import javax.swing.JPanel;
    1213
    1314import org.CustomMatchers;
     15import org.hamcrest.CustomTypeSafeMatcher;
     16import org.hamcrest.Matcher;
    1417import org.junit.Before;
    1518import org.junit.Rule;
     
    112115        component.zoomTo(new LatLon(10, 10));
    113116        Point2D shouldBeCenter = component.getPoint2D(new LatLon(10, 10));
    114         // center may move 0.5 pixels for alignment, see {@link NavigatableComponent#zoomTo(EastNorth, double, boolean)}
     117        // 0.5 pixel tolerance, see isAfterZoom
    115118        assertEquals(shouldBeCenter.getX(), WIDTH / 2., 0.5);
    116119        assertEquals(shouldBeCenter.getY(), HEIGHT / 2., 0.5);
     
    128131        component.zoomToFactor(0.5);
    129132        assertEquals(initialScale / 2, component.getScale(), 0.00000001);
    130         assertThat(component.getCenter(), CustomMatchers.is(center));
     133        assertThat(component.getCenter(), isAfterZoom(center, component.getScale()));
    131134        component.zoomToFactor(2);
    132135        assertEquals(initialScale, component.getScale(), 0.00000001);
    133         assertThat(component.getCenter(), CustomMatchers.is(center));
     136        assertThat(component.getCenter(), isAfterZoom(center, component.getScale()));
    134137
    135138        // zoomToFactor(EastNorth, double)
     
    137140        component.zoomToFactor(newCenter, 0.5);
    138141        assertEquals(initialScale / 2, component.getScale(), 0.00000001);
    139         assertEquals(newCenter, component.getCenter());
     142        assertThat(component.getCenter(), isAfterZoom(newCenter, component.getScale()));
    140143        component.zoomToFactor(newCenter, 2);
    141144        assertEquals(initialScale, component.getScale(), 0.00000001);
    142         assertEquals(newCenter, component.getCenter());
     145        assertThat(component.getCenter(), isAfterZoom(newCenter, component.getScale()));
    143146    }
    144147
     
    168171        component.zoomToFactor(0, 0, 0.5);
    169172        assertEquals(initialScale / 2, component.getScale(), 0.00000001);
    170         assertThat(component.getEastNorth(0, 0), CustomMatchers.is(testPoint1));
     173        assertThat(component.getEastNorth(0, 0), isAfterZoom(testPoint1, component.getScale()));
    171174        component.zoomToFactor(0, 0, 2);
    172175        assertEquals(initialScale, component.getScale(), 0.00000001);
    173         assertThat(component.getEastNorth(0, 0), CustomMatchers.is(testPoint1));
     176        assertThat(component.getEastNorth(0, 0), isAfterZoom(testPoint1, component.getScale()));
    174177
    175178        component.zoomToFactor(200, 150, 0.5);
    176179        assertEquals(initialScale / 2, component.getScale(), 0.00000001);
    177         assertThat(component.getEastNorth(200, 150), CustomMatchers.is(testPoint2));
     180        assertThat(component.getEastNorth(200, 150), isAfterZoom(testPoint2, component.getScale()));
    178181        component.zoomToFactor(200, 150, 2);
    179182        assertEquals(initialScale, component.getScale(), 0.00000001);
    180         assertThat(component.getEastNorth(200, 150), CustomMatchers.is(testPoint2));
     183        assertThat(component.getEastNorth(200, 150), isAfterZoom(testPoint2, component.getScale()));
    181184
    182185    }
     
    206209    }
    207210
     211    /**
     212     * Check that EastNorth is the same as expected after zooming the NavigatableComponent.
     213     *
     214     * Adds tolerance of 0.5 pixel for pixel grid alignment, see
     215     * {@link NavigatableComponent#zoomTo(EastNorth, double, boolean)}
     216     * @param expected expected
     217     * @param scale current scale
     218     * @return Matcher object
     219     */
     220    private Matcher<EastNorth> isAfterZoom(EastNorth expected, double scale) {
     221        return new CustomTypeSafeMatcher<EastNorth>(Objects.toString(expected)) {
     222            @Override
     223            protected boolean matchesSafely(EastNorth actual) {
     224                // compare pixels (east/north divided by scale)
     225                return Math.abs((expected.getX() - actual.getX()) / scale) <= 0.5
     226                        && Math.abs((expected.getY() - actual.getY()) / scale) <= 0.5;
     227            }
     228        };
     229    }
     230
    208231}
Note: See TracChangeset for help on using the changeset viewer.