1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 |
|
---|
6 | import java.awt.geom.AffineTransform;
|
---|
7 | import java.awt.geom.Point2D;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.function.Function;
|
---|
10 |
|
---|
11 | import org.junit.Before;
|
---|
12 | import org.junit.BeforeClass;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.JOSMFixture;
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
|
---|
19 | import org.openstreetmap.josm.gui.MapViewState.MapViewRectangle;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Test {@link MapViewState}
|
---|
23 | * @author Michael Zangl
|
---|
24 | */
|
---|
25 | public 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 | /**
|
---|
71 | * Test {@link MapViewState#getCenterAtPixel()} returns map view center.
|
---|
72 | */
|
---|
73 | @Test
|
---|
74 | public void testGetCenterAtPixel() {
|
---|
75 | doTestGetCenter(s -> s.getCenterAtPixel(), t -> (double) (t / 2));
|
---|
76 | }
|
---|
77 |
|
---|
78 | private static void assertHasViewCoords(double x, double y, MapViewPoint center) {
|
---|
79 | assertEquals("x", x, center.getInViewX(), 0.01);
|
---|
80 | assertEquals("y", y, center.getInViewY(), 0.01);
|
---|
81 | assertEquals("x", x, center.getInView().getX(), 0.01);
|
---|
82 | assertEquals("y", y, center.getInView().getY(), 0.01);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Test {@link MapViewState#getForView(double, double)}
|
---|
87 | */
|
---|
88 | @Test
|
---|
89 | public void testGetForView() {
|
---|
90 | MapViewPoint corner = state.getForView(0, 0);
|
---|
91 | assertHasViewCoords(0, 0, corner);
|
---|
92 |
|
---|
93 | MapViewPoint middle = state.getForView(120, 130);
|
---|
94 | assertHasViewCoords(120, 130, middle);
|
---|
95 |
|
---|
96 | MapViewPoint fraction = state.getForView(0.12, 0.7);
|
---|
97 | assertHasViewCoords(0.12, 0.7, fraction);
|
---|
98 |
|
---|
99 | MapViewPoint negative = state.getForView(-17, -30);
|
---|
100 | assertHasViewCoords(-17, -30, negative);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Test {@link MapViewState#getViewWidth()} and {@link MapViewState#getViewHeight()}
|
---|
105 | */
|
---|
106 | @Test
|
---|
107 | public void testGetViewSize() {
|
---|
108 | assertEquals(WIDTH, state.getViewWidth(), 0.01);
|
---|
109 | assertEquals(HEIGHT, state.getViewHeight(), 0.01);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Tests that all coordinate conversions for the point work.
|
---|
114 | */
|
---|
115 | @Test
|
---|
116 | public void testPointConversions() {
|
---|
117 | MapViewPoint p = state.getForView(WIDTH / 2d, HEIGHT / 2d);
|
---|
118 | assertHasViewCoords(WIDTH / 2d, HEIGHT / 2d, p);
|
---|
119 |
|
---|
120 | EastNorth eastnorth = p.getEastNorth();
|
---|
121 | LatLon shouldLatLon = Main.getProjection().getWorldBoundsLatLon().getCenter();
|
---|
122 | EastNorth shouldEastNorth = Main.getProjection().latlon2eastNorth(shouldLatLon);
|
---|
123 | assertEquals("east", shouldEastNorth.east(), eastnorth.east(), 0.01);
|
---|
124 | assertEquals("north", shouldEastNorth.north(), eastnorth.north(), 0.01);
|
---|
125 | MapViewPoint reversed = state.getPointFor(shouldEastNorth);
|
---|
126 | assertHasViewCoords(WIDTH / 2d, HEIGHT / 2d, reversed);
|
---|
127 |
|
---|
128 | LatLon latlon = p.getLatLon();
|
---|
129 | assertEquals("lat", shouldLatLon.lat(), latlon.lat(), 0.01);
|
---|
130 | assertEquals("lon", shouldLatLon.lon(), latlon.lon(), 0.01);
|
---|
131 |
|
---|
132 | MapViewPoint p2 = state.getPointFor(new EastNorth(2, 3));
|
---|
133 | assertEquals("east", 2, p2.getEastNorth().east(), 0.01);
|
---|
134 | assertEquals("north", 3, p2.getEastNorth().north(), 0.01);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Test {@link MapViewState#getAffineTransform()}
|
---|
139 | */
|
---|
140 | @Test
|
---|
141 | public void testGetAffineTransform() {
|
---|
142 | for (EastNorth en : Arrays.asList(new EastNorth(100, 100), new EastNorth(0, 0), new EastNorth(300, 200),
|
---|
143 | new EastNorth(-1, -2.5))) {
|
---|
144 | MapViewPoint should = state.getPointFor(en);
|
---|
145 | AffineTransform transform = state.getAffineTransform();
|
---|
146 | Point2D result = transform.transform(new Point2D.Double(en.getX(), en.getY()), null);
|
---|
147 |
|
---|
148 | assertEquals("x", should.getInViewX(), result.getX(), 0.01);
|
---|
149 | assertEquals("y", should.getInViewY(), result.getY(), 0.01);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Test {@link MapViewState#OUTSIDE_BOTTOM} and similar constants.
|
---|
155 | */
|
---|
156 | @Test
|
---|
157 | public void testOutsideFlags() {
|
---|
158 | assertEquals(1, Integer.bitCount(MapViewState.OUTSIDE_BOTTOM));
|
---|
159 | assertEquals(1, Integer.bitCount(MapViewState.OUTSIDE_TOP));
|
---|
160 | assertEquals(1, Integer.bitCount(MapViewState.OUTSIDE_LEFT));
|
---|
161 | assertEquals(1, Integer.bitCount(MapViewState.OUTSIDE_RIGHT));
|
---|
162 | assertEquals(4, Integer.bitCount(MapViewState.OUTSIDE_BOTTOM | MapViewState.OUTSIDE_TOP
|
---|
163 | | MapViewState.OUTSIDE_LEFT | MapViewState.OUTSIDE_RIGHT));
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Test {@link MapViewPoint#getOutsideRectangleFlags(MapViewRectangle)}
|
---|
168 | */
|
---|
169 | @Test
|
---|
170 | public void testPointGetOutsideRectangleFlags() {
|
---|
171 | MapViewRectangle rect = state.getForView(0, 0).rectTo(state.getForView(10, 10));
|
---|
172 | assertEquals(0, state.getForView(1, 1).getOutsideRectangleFlags(rect));
|
---|
173 | assertEquals(0, state.getForView(1, 5).getOutsideRectangleFlags(rect));
|
---|
174 | assertEquals(0, state.getForView(9, 1).getOutsideRectangleFlags(rect));
|
---|
175 | assertEquals(0, state.getForView(10 - 1e-10, 1e-10).getOutsideRectangleFlags(rect));
|
---|
176 | assertEquals(0, state.getForView(10 - 1e-10, 10 - 1e-10).getOutsideRectangleFlags(rect));
|
---|
177 |
|
---|
178 |
|
---|
179 | assertEquals(MapViewState.OUTSIDE_TOP, state.getForView(1, -11).getOutsideRectangleFlags(rect));
|
---|
180 | assertEquals(MapViewState.OUTSIDE_TOP, state.getForView(1, -1e20).getOutsideRectangleFlags(rect));
|
---|
181 |
|
---|
182 | assertEquals(MapViewState.OUTSIDE_BOTTOM, state.getForView(1, 11).getOutsideRectangleFlags(rect));
|
---|
183 | assertEquals(MapViewState.OUTSIDE_BOTTOM, state.getForView(1, 1e20).getOutsideRectangleFlags(rect));
|
---|
184 |
|
---|
185 | assertEquals(MapViewState.OUTSIDE_LEFT, state.getForView(-11, 1).getOutsideRectangleFlags(rect));
|
---|
186 | assertEquals(MapViewState.OUTSIDE_LEFT, state.getForView(-1e20, 1).getOutsideRectangleFlags(rect));
|
---|
187 | assertEquals(MapViewState.OUTSIDE_RIGHT, state.getForView(11, 1).getOutsideRectangleFlags(rect));
|
---|
188 | assertEquals(MapViewState.OUTSIDE_RIGHT, state.getForView(1e20, 1).getOutsideRectangleFlags(rect));
|
---|
189 |
|
---|
190 | assertEquals(MapViewState.OUTSIDE_RIGHT | MapViewState.OUTSIDE_TOP, state.getForView(11, -11).getOutsideRectangleFlags(rect));
|
---|
191 | assertEquals(MapViewState.OUTSIDE_RIGHT | MapViewState.OUTSIDE_BOTTOM, state.getForView(11, 11).getOutsideRectangleFlags(rect));
|
---|
192 | assertEquals(MapViewState.OUTSIDE_LEFT | MapViewState.OUTSIDE_TOP, state.getForView(-11, -11).getOutsideRectangleFlags(rect));
|
---|
193 | assertEquals(MapViewState.OUTSIDE_LEFT | MapViewState.OUTSIDE_BOTTOM, state.getForView(-11, 11).getOutsideRectangleFlags(rect));
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Test {@link MapViewPoint#oneNormInView(MapViewPoint)}
|
---|
198 | */
|
---|
199 | @Test
|
---|
200 | public void testPointOneNormInView() {
|
---|
201 | MapViewPoint p = state.getForView(5, 15);
|
---|
202 | assertEquals(0, p.oneNormInView(p), 1e-10);
|
---|
203 | assertEquals(6, p.oneNormInView(state.getForView(-1, 15)), 1e-10);
|
---|
204 | assertEquals(5, p.oneNormInView(state.getForView(5, 20)), 1e-10);
|
---|
205 | assertEquals(22, p.oneNormInView(state.getForView(-1, -1)), 1e-10);
|
---|
206 | assertEquals(40, p.oneNormInView(state.getForView(30, 30)), 1e-10);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Test {@link MapViewState.MapViewViewPoint#toString()} and {@link MapViewState.MapViewEastNorthPoint#toString()}
|
---|
211 | */
|
---|
212 | @Test
|
---|
213 | public void testToString() {
|
---|
214 | assertEquals("MapViewViewPoint [x=1.0, y=2.0]",
|
---|
215 | state.getForView(1, 2).toString());
|
---|
216 | assertEquals("MapViewEastNorthPoint [eastNorth=EastNorth[e=0.0, n=0.0]]",
|
---|
217 | state.getPointFor(new EastNorth(0, 0)).toString());
|
---|
218 | }
|
---|
219 | }
|
---|