1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.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.jupiter.api.BeforeAll;
|
---|
12 | import org.junit.jupiter.api.BeforeEach;
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.openstreetmap.josm.JOSMFixture;
|
---|
15 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
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 | 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 | @BeforeAll
|
---|
35 | public static void setUpBeforeClass() {
|
---|
36 | JOSMFixture.createUnitTestFixture().init();
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Create the default state.
|
---|
41 | */
|
---|
42 | @BeforeEach
|
---|
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(3, center.getEastNorth().east(), 0.01, "east");
|
---|
59 | assertEquals(4, center.getEastNorth().north(), 0.01, "north");
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Test {@link MapViewState#getCenter()} returns map view center.
|
---|
64 | */
|
---|
65 | @Test
|
---|
66 | 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, center.getInViewX(), 0.01, "x");
|
---|
72 | assertEquals(y, center.getInViewY(), 0.01, "y");
|
---|
73 | assertEquals(x, center.getInView().getX(), 0.01, "x");
|
---|
74 | assertEquals(y, center.getInView().getY(), 0.01, "y");
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Test {@link MapViewState#getForView(double, double)}
|
---|
79 | */
|
---|
80 | @Test
|
---|
81 | 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 | 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 | 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 = ProjectionRegistry.getProjection().getWorldBoundsLatLon().getCenter();
|
---|
114 | EastNorth shouldEastNorth = ProjectionRegistry.getProjection().latlon2eastNorth(shouldLatLon);
|
---|
115 | assertEquals(shouldEastNorth.east(), eastnorth.east(), 0.01, "east");
|
---|
116 | assertEquals(shouldEastNorth.north(), eastnorth.north(), 0.01, "north");
|
---|
117 | MapViewPoint reversed = state.getPointFor(shouldEastNorth);
|
---|
118 | assertHasViewCoords(WIDTH / 2d, HEIGHT / 2d, reversed);
|
---|
119 |
|
---|
120 | LatLon latlon = p.getLatLon();
|
---|
121 | assertEquals(shouldLatLon.lat(), latlon.lat(), 0.01, "lat");
|
---|
122 | assertEquals(shouldLatLon.lon(), latlon.lon(), 0.01, "lon");
|
---|
123 |
|
---|
124 | MapViewPoint p2 = state.getPointFor(new EastNorth(2, 3));
|
---|
125 | assertEquals(2, p2.getEastNorth().east(), 0.01, "east");
|
---|
126 | assertEquals(3, p2.getEastNorth().north(), 0.01, "north");
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Test {@link MapViewState#getAffineTransform()}
|
---|
131 | */
|
---|
132 | @Test
|
---|
133 | 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(should.getInViewX(), result.getX(), 0.01, "x");
|
---|
141 | assertEquals(should.getInViewY(), result.getY(), 0.01, "y");
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Test {@link MapViewState#OUTSIDE_BOTTOM} and similar constants.
|
---|
147 | */
|
---|
148 | @Test
|
---|
149 | 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 | 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 | 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 | 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 | }
|
---|