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

Last change on this file since 10956 was 10405, checked in by Don-vip, 8 years ago

fix #12959 - Do not make unit tests rely on JFrame for headless mode (patch by michael2402) - gsoc-core

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