source: josm/trunk/test/unit/org/openstreetmap/josm/gui/NavigatableComponentTest.java@ 11867

Last change on this file since 11867 was 11867, checked in by bastiK, 7 years ago

see #7427 - fix some unit tests

  • 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;
5import static org.junit.Assert.assertThat;
6
7import java.awt.Point;
8import java.awt.Rectangle;
9import java.awt.geom.Point2D;
10import java.util.Objects;
11
12import javax.swing.JPanel;
13
14import org.CustomMatchers;
15import org.hamcrest.CustomTypeSafeMatcher;
16import org.hamcrest.Matcher;
17import org.junit.Before;
18import org.junit.Rule;
19import org.junit.Test;
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.Bounds;
22import org.openstreetmap.josm.data.ProjectionBounds;
23import org.openstreetmap.josm.data.coor.EastNorth;
24import org.openstreetmap.josm.data.coor.LatLon;
25import org.openstreetmap.josm.gui.util.GuiHelper;
26import org.openstreetmap.josm.testutils.JOSMTestRules;
27
28import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29
30/**
31 * Some tests for the {@link NavigatableComponent} class.
32 * @author Michael Zangl
33 *
34 */
35public class NavigatableComponentTest {
36
37 private static final class NavigatableComponentMock extends NavigatableComponent {
38 @Override
39 public Point getLocationOnScreen() {
40 return new Point(30, 40);
41 }
42
43 @Override
44 protected boolean isVisibleOnScreen() {
45 return true;
46 }
47 }
48
49 private static final int HEIGHT = 200;
50 private static final int WIDTH = 300;
51 private NavigatableComponent component;
52
53 /**
54 * We need the projection for coordinate conversions.
55 */
56 @Rule
57 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
58 public JOSMTestRules test = new JOSMTestRules().preferences().platform().projection();
59
60 /**
61 * Create a new, fresh {@link NavigatableComponent}
62 */
63 @Before
64 public void setUp() {
65 component = new NavigatableComponentMock();
66 component.setBounds(new Rectangle(WIDTH, HEIGHT));
67 // wait for the event to be propagated.
68 GuiHelper.runInEDTAndWait(new Runnable() {
69 @Override
70 public void run() {
71 }
72 });
73 component.setVisible(true);
74 JPanel parent = new JPanel();
75 parent.add(component);
76 component.updateLocationState();
77 }
78
79 /**
80 * Test if the default scale was set correctly.
81 */
82 @Test
83 public void testDefaultScale() {
84 assertEquals(Main.getProjection().getDefaultZoomInPPD(), component.getScale(), 0.00001);
85 }
86
87 /**
88 * Tests {@link NavigatableComponent#getPoint2D(EastNorth)}
89 */
90 @Test
91 public void testPoint2DEastNorth() {
92 assertThat(component.getPoint2D((EastNorth) null), CustomMatchers.is(new Point2D.Double()));
93 Point2D shouldBeCenter = component.getPoint2D(component.getCenter());
94 assertThat(shouldBeCenter, CustomMatchers.is(new Point2D.Double(WIDTH / 2, HEIGHT / 2)));
95
96 EastNorth testPoint = component.getCenter().add(300 * component.getScale(), 200 * component.getScale());
97 Point2D testPointConverted = component.getPoint2D(testPoint);
98 assertThat(testPointConverted, CustomMatchers.is(new Point2D.Double(WIDTH / 2 + 300, HEIGHT / 2 - 200)));
99 }
100
101 /**
102 * TODO: Implement this test.
103 */
104 @Test
105 public void testPoint2DLatLon() {
106 assertThat(component.getPoint2D((LatLon) null), CustomMatchers.is(new Point2D.Double()));
107 // TODO: Really test this.
108 }
109
110 /**
111 * Tests {@link NavigatableComponent#zoomTo(LatLon)}
112 */
113 @Test
114 public void testZoomToLatLon() {
115 component.zoomTo(new LatLon(10, 10));
116 Point2D shouldBeCenter = component.getPoint2D(new LatLon(10, 10));
117 // 0.5 pixel tolerance, see isAfterZoom
118 assertEquals(shouldBeCenter.getX(), WIDTH / 2., 0.5);
119 assertEquals(shouldBeCenter.getY(), HEIGHT / 2., 0.5);
120 }
121
122 /**
123 * Tests {@link NavigatableComponent#zoomToFactor(double)} and {@link NavigatableComponent#zoomToFactor(EastNorth, double)}
124 */
125 @Test
126 public void testZoomToFactor() {
127 EastNorth center = component.getCenter();
128 double initialScale = component.getScale();
129
130 // zoomToFactor(double)
131 component.zoomToFactor(0.5);
132 assertEquals(initialScale / 2, component.getScale(), 0.00000001);
133 assertThat(component.getCenter(), isAfterZoom(center, component.getScale()));
134 component.zoomToFactor(2);
135 assertEquals(initialScale, component.getScale(), 0.00000001);
136 assertThat(component.getCenter(), isAfterZoom(center, component.getScale()));
137
138 // zoomToFactor(EastNorth, double)
139 EastNorth newCenter = new EastNorth(10, 20);
140 component.zoomToFactor(newCenter, 0.5);
141 assertEquals(initialScale / 2, component.getScale(), 0.00000001);
142 assertThat(component.getCenter(), isAfterZoom(newCenter, component.getScale()));
143 component.zoomToFactor(newCenter, 2);
144 assertEquals(initialScale, component.getScale(), 0.00000001);
145 assertThat(component.getCenter(), isAfterZoom(newCenter, component.getScale()));
146 }
147
148 /**
149 * Tests {@link NavigatableComponent#getEastNorth(int, int)}
150 */
151 @Test
152 public void testGetEastNorth() {
153 EastNorth center = component.getCenter();
154 assertThat(component.getEastNorth(WIDTH / 2, HEIGHT / 2), CustomMatchers.is(center));
155
156 EastNorth testPoint = component.getCenter().add(WIDTH * component.getScale(), HEIGHT * component.getScale());
157 assertThat(component.getEastNorth(3 * WIDTH / 2, -HEIGHT / 2), CustomMatchers.is(testPoint));
158 }
159
160 /**
161 * Tests {@link NavigatableComponent#zoomToFactor(double, double, double)}
162 */
163 @Test
164 public void testZoomToFactorCenter() {
165 // zoomToFactor(double, double, double)
166 // assumes getEastNorth works as expected
167 EastNorth testPoint1 = component.getEastNorth(0, 0);
168 EastNorth testPoint2 = component.getEastNorth(200, 150);
169 double initialScale = component.getScale();
170
171 component.zoomToFactor(0, 0, 0.5);
172 assertEquals(initialScale / 2, component.getScale(), 0.00000001);
173 assertThat(component.getEastNorth(0, 0), isAfterZoom(testPoint1, component.getScale()));
174 component.zoomToFactor(0, 0, 2);
175 assertEquals(initialScale, component.getScale(), 0.00000001);
176 assertThat(component.getEastNorth(0, 0), isAfterZoom(testPoint1, component.getScale()));
177
178 component.zoomToFactor(200, 150, 0.5);
179 assertEquals(initialScale / 2, component.getScale(), 0.00000001);
180 assertThat(component.getEastNorth(200, 150), isAfterZoom(testPoint2, component.getScale()));
181 component.zoomToFactor(200, 150, 2);
182 assertEquals(initialScale, component.getScale(), 0.00000001);
183 assertThat(component.getEastNorth(200, 150), isAfterZoom(testPoint2, component.getScale()));
184
185 }
186
187 /**
188 * Tests {@link NavigatableComponent#getProjectionBounds()}
189 */
190 @Test
191 public void testGetProjectionBounds() {
192 ProjectionBounds bounds = component.getProjectionBounds();
193 assertThat(bounds.getCenter(), CustomMatchers.is(component.getCenter()));
194
195 assertThat(bounds.getMin(), CustomMatchers.is(component.getEastNorth(0, HEIGHT)));
196 assertThat(bounds.getMax(), CustomMatchers.is(component.getEastNorth(WIDTH, 0)));
197 }
198
199 /**
200 * Tests {@link NavigatableComponent#getRealBounds()}
201 */
202 @Test
203 public void testGetRealBounds() {
204 Bounds bounds = component.getRealBounds();
205 assertThat(bounds.getCenter(), CustomMatchers.is(component.getLatLon(WIDTH / 2, HEIGHT / 2)));
206
207 assertThat(bounds.getMin(), CustomMatchers.is(component.getLatLon(0, HEIGHT)));
208 assertThat(bounds.getMax(), CustomMatchers.is(component.getLatLon(WIDTH, 0)));
209 }
210
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
231}
Note: See TracBrowser for help on using the repository browser.