source: josm/trunk/test/unit/org/openstreetmap/josm/gui/MapViewStateTest.java @ 12076

Last change on this file since 12076 was 12076, checked in by bastiK, 22 months ago

see #14740 - update test

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.junit.Assert.assertEquals;
5
6import java.awt.geom.AffineTransform;
7import java.awt.geom.Point2D;
8import java.util.Arrays;
9import java.util.function.Function;
10
11import org.junit.Before;
12import org.junit.BeforeClass;
13import org.junit.Test;
14import org.openstreetmap.josm.JOSMFixture;
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.coor.EastNorth;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
19import org.openstreetmap.josm.gui.MapViewState.MapViewRectangle;
20
21/**
22 * Test {@link MapViewState}
23 * @author Michael Zangl
24 */
25public class MapViewStateTest {
26
27    private static final int WIDTH = 301;
28    private static final int HEIGHT = 200;
29    private MapViewState state;
30
31    /**
32     * Setup test.
33     */
34    @BeforeClass
35    public static void setUpBeforeClass() {
36        JOSMFixture.createUnitTestFixture().init();
37    }
38
39    /**
40     * Create the default state.
41     */
42    @Before
43    public void setUp() {
44        state = MapViewState.createDefaultState(WIDTH, HEIGHT);
45    }
46
47    private void doTestGetCenter(Function<MapViewState, MapViewPoint> getter, Function<Integer, Double> divider) {
48        MapViewPoint center = getter.apply(state);
49        assertHasViewCoords(divider.apply(WIDTH), divider.apply(HEIGHT), center);
50
51        MapViewState newState = state.movedTo(center, new EastNorth(3, 4));
52
53        // state should not change, but new state should.
54        center = getter.apply(state);
55        assertHasViewCoords(divider.apply(WIDTH), divider.apply(HEIGHT), center);
56
57        center = getter.apply(newState);
58        assertEquals("east", 3, center.getEastNorth().east(), 0.01);
59        assertEquals("north", 4, center.getEastNorth().north(), 0.01);
60    }
61
62    /**
63     * Test {@link MapViewState#getCenter()} returns map view center.
64     */
65    @Test
66    public void testGetCenter() {
67        doTestGetCenter(s -> s.getCenter(), t -> t / 2d);
68    }
69
70    private static void assertHasViewCoords(double x, double y, MapViewPoint center) {
71        assertEquals("x", x, center.getInViewX(), 0.01);
72        assertEquals("y", y, center.getInViewY(), 0.01);
73        assertEquals("x", x, center.getInView().getX(), 0.01);
74        assertEquals("y", y, center.getInView().getY(), 0.01);
75    }
76
77    /**
78     * Test {@link MapViewState#getForView(double, double)}
79     */
80    @Test
81    public void testGetForView() {
82        MapViewPoint corner = state.getForView(0, 0);
83        assertHasViewCoords(0, 0, corner);
84
85        MapViewPoint middle = state.getForView(120, 130);
86        assertHasViewCoords(120, 130, middle);
87
88        MapViewPoint fraction = state.getForView(0.12, 0.7);
89        assertHasViewCoords(0.12, 0.7, fraction);
90
91        MapViewPoint negative = state.getForView(-17, -30);
92        assertHasViewCoords(-17, -30, negative);
93    }
94
95    /**
96     * Test {@link MapViewState#getViewWidth()} and {@link MapViewState#getViewHeight()}
97     */
98    @Test
99    public void testGetViewSize() {
100        assertEquals(WIDTH, state.getViewWidth(), 0.01);
101        assertEquals(HEIGHT, state.getViewHeight(), 0.01);
102    }
103
104    /**
105     * Tests that all coordinate conversions for the point work.
106     */
107    @Test
108    public void testPointConversions() {
109        MapViewPoint p = state.getForView(WIDTH / 2d, HEIGHT / 2d);
110        assertHasViewCoords(WIDTH / 2d, HEIGHT / 2d, p);
111
112        EastNorth eastnorth = p.getEastNorth();
113        LatLon shouldLatLon = Main.getProjection().getWorldBoundsLatLon().getCenter();
114        EastNorth shouldEastNorth = Main.getProjection().latlon2eastNorth(shouldLatLon);
115        assertEquals("east", shouldEastNorth.east(), eastnorth.east(), 0.01);
116        assertEquals("north", shouldEastNorth.north(), eastnorth.north(), 0.01);
117        MapViewPoint reversed = state.getPointFor(shouldEastNorth);
118        assertHasViewCoords(WIDTH / 2d, HEIGHT / 2d, reversed);
119
120        LatLon latlon = p.getLatLon();
121        assertEquals("lat", shouldLatLon.lat(), latlon.lat(), 0.01);
122        assertEquals("lon", shouldLatLon.lon(), latlon.lon(), 0.01);
123
124        MapViewPoint p2 = state.getPointFor(new EastNorth(2, 3));
125        assertEquals("east", 2, p2.getEastNorth().east(), 0.01);
126        assertEquals("north", 3, p2.getEastNorth().north(), 0.01);
127    }
128
129    /**
130     * Test {@link MapViewState#getAffineTransform()}
131     */
132    @Test
133    public void testGetAffineTransform() {
134        for (EastNorth en : Arrays.asList(new EastNorth(100, 100), new EastNorth(0, 0), new EastNorth(300, 200),
135                new EastNorth(-1, -2.5))) {
136            MapViewPoint should = state.getPointFor(en);
137            AffineTransform transform = state.getAffineTransform();
138            Point2D result = transform.transform(new Point2D.Double(en.getX(), en.getY()), null);
139
140            assertEquals("x", should.getInViewX(), result.getX(), 0.01);
141            assertEquals("y", should.getInViewY(), result.getY(), 0.01);
142        }
143    }
144
145    /**
146     * Test {@link MapViewState#OUTSIDE_BOTTOM} and similar constants.
147     */
148    @Test
149    public void testOutsideFlags() {
150        assertEquals(1, Integer.bitCount(MapViewState.OUTSIDE_BOTTOM));
151        assertEquals(1, Integer.bitCount(MapViewState.OUTSIDE_TOP));
152        assertEquals(1, Integer.bitCount(MapViewState.OUTSIDE_LEFT));
153        assertEquals(1, Integer.bitCount(MapViewState.OUTSIDE_RIGHT));
154        assertEquals(4, Integer.bitCount(MapViewState.OUTSIDE_BOTTOM | MapViewState.OUTSIDE_TOP
155                | MapViewState.OUTSIDE_LEFT | MapViewState.OUTSIDE_RIGHT));
156    }
157
158    /**
159     * Test {@link MapViewPoint#getOutsideRectangleFlags(MapViewRectangle)}
160     */
161    @Test
162    public void testPointGetOutsideRectangleFlags() {
163        MapViewRectangle rect = state.getForView(0, 0).rectTo(state.getForView(10, 10));
164        assertEquals(0, state.getForView(1, 1).getOutsideRectangleFlags(rect));
165        assertEquals(0, state.getForView(1, 5).getOutsideRectangleFlags(rect));
166        assertEquals(0, state.getForView(9, 1).getOutsideRectangleFlags(rect));
167        assertEquals(0, state.getForView(10 - 1e-10, 1e-10).getOutsideRectangleFlags(rect));
168        assertEquals(0, state.getForView(10 - 1e-10, 10 - 1e-10).getOutsideRectangleFlags(rect));
169
170
171        assertEquals(MapViewState.OUTSIDE_TOP, state.getForView(1, -11).getOutsideRectangleFlags(rect));
172        assertEquals(MapViewState.OUTSIDE_TOP, state.getForView(1, -1e20).getOutsideRectangleFlags(rect));
173
174        assertEquals(MapViewState.OUTSIDE_BOTTOM, state.getForView(1, 11).getOutsideRectangleFlags(rect));
175        assertEquals(MapViewState.OUTSIDE_BOTTOM, state.getForView(1, 1e20).getOutsideRectangleFlags(rect));
176
177        assertEquals(MapViewState.OUTSIDE_LEFT, state.getForView(-11, 1).getOutsideRectangleFlags(rect));
178        assertEquals(MapViewState.OUTSIDE_LEFT, state.getForView(-1e20, 1).getOutsideRectangleFlags(rect));
179        assertEquals(MapViewState.OUTSIDE_RIGHT, state.getForView(11, 1).getOutsideRectangleFlags(rect));
180        assertEquals(MapViewState.OUTSIDE_RIGHT, state.getForView(1e20, 1).getOutsideRectangleFlags(rect));
181
182        assertEquals(MapViewState.OUTSIDE_RIGHT | MapViewState.OUTSIDE_TOP, state.getForView(11, -11).getOutsideRectangleFlags(rect));
183        assertEquals(MapViewState.OUTSIDE_RIGHT | MapViewState.OUTSIDE_BOTTOM, state.getForView(11, 11).getOutsideRectangleFlags(rect));
184        assertEquals(MapViewState.OUTSIDE_LEFT | MapViewState.OUTSIDE_TOP, state.getForView(-11, -11).getOutsideRectangleFlags(rect));
185        assertEquals(MapViewState.OUTSIDE_LEFT | MapViewState.OUTSIDE_BOTTOM, state.getForView(-11, 11).getOutsideRectangleFlags(rect));
186    }
187
188    /**
189     * Test {@link MapViewPoint#oneNormInView(MapViewPoint)}
190     */
191    @Test
192    public void testPointOneNormInView() {
193        MapViewPoint p = state.getForView(5, 15);
194        assertEquals(0, p.oneNormInView(p), 1e-10);
195        assertEquals(6, p.oneNormInView(state.getForView(-1, 15)), 1e-10);
196        assertEquals(5, p.oneNormInView(state.getForView(5, 20)), 1e-10);
197        assertEquals(22, p.oneNormInView(state.getForView(-1, -1)), 1e-10);
198        assertEquals(40, p.oneNormInView(state.getForView(30, 30)), 1e-10);
199    }
200
201    /**
202     * Test {@link MapViewState.MapViewViewPoint#toString()} and {@link MapViewState.MapViewEastNorthPoint#toString()}
203     */
204    @Test
205    public void testToString() {
206        assertEquals("MapViewViewPoint [x=1.0, y=2.0]",
207                state.getForView(1, 2).toString());
208        assertEquals("MapViewEastNorthPoint [eastNorth=EastNorth[e=0.0, n=0.0]]",
209                state.getPointFor(new EastNorth(0, 0)).toString());
210    }
211}
Note: See TracBrowser for help on using the repository browser.