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

Last change on this file since 8759 was 8759, checked in by simon04, 9 years ago

fix #11849 - Add NavigationComponentTest (patch by michael2402, modified)

File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.hamcrest.CoreMatchers.is;
5import static org.junit.Assert.assertEquals;
6import static org.junit.Assert.assertThat;
7
8import java.awt.Rectangle;
9import java.awt.geom.Point2D;
10
11import org.CustomMatchers;
12import org.junit.Before;
13import org.junit.BeforeClass;
14import org.junit.Test;
15import org.openstreetmap.josm.JOSMFixture;
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.Bounds;
18import org.openstreetmap.josm.data.ProjectionBounds;
19import org.openstreetmap.josm.data.coor.EastNorth;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.gui.util.GuiHelper;
22
23/**
24 * Some tests for the {@link NavigatableComponent} class.
25 * @author Michael Zangl
26 *
27 */
28public class NavigatableComponentTest {
29
30 private static final int HEIGHT = 200;
31 private static final int WIDTH = 300;
32 private NavigatableComponent component;
33
34 /**
35 * Setup test.
36 */
37 @BeforeClass
38 public static void setUp() {
39 JOSMFixture.createUnitTestFixture().init();
40 }
41
42 /**
43 * Create a new, fresh {@link NavigatableComponent}
44 */
45 @Before
46 public void setup() {
47 component = new NavigatableComponent();
48 component.setBounds(new Rectangle(WIDTH, HEIGHT));
49 // wait for the event to be propagated.
50 GuiHelper.runInEDTAndWait(new Runnable() {
51 @Override
52 public void run() {
53 }
54 });
55 }
56
57 /**
58 * Test if the default scale was set correctly.
59 */
60 @Test
61 public void testDefaultScale() {
62 assertEquals(Main.getProjection().getDefaultZoomInPPD(), component.getScale(), 0.00001);
63 }
64
65 /**
66 * Tests {@link NavigatableComponent#getPoint2D(EastNorth)}
67 */
68 @Test
69 public void testPoint2DEastNorth() {
70 assertThat(component.getPoint2D((EastNorth) null), CustomMatchers.is(new Point2D.Double()));
71 Point2D shouldBeCenter = component.getPoint2D(component.getCenter());
72 assertThat(shouldBeCenter, CustomMatchers.is(new Point2D.Double(WIDTH / 2, HEIGHT / 2)));
73
74 EastNorth testPoint = component.getCenter().add(300 * component.getScale(), 200 * component.getScale());
75 Point2D testPointConverted = component.getPoint2D(testPoint);
76 assertThat(testPointConverted, CustomMatchers.is(new Point2D.Double(WIDTH / 2 + 300, HEIGHT / 2 - 200)));
77 }
78
79 /**
80 * TODO: Implement this test.
81 */
82 @Test
83 public void testPoint2DLatLon() {
84 assertThat(component.getPoint2D((LatLon) null), CustomMatchers.is(new Point2D.Double()));
85 // TODO: Really test this.
86 }
87
88 /**
89 * Tests {@link NavigatableComponent#zoomTo(LatLon)}
90 */
91 @Test
92 public void testZoomToLatLon() {
93 component.zoomTo(new LatLon(10, 10));
94 Point2D shouldBeCenter = component.getPoint2D(new LatLon(10, 10));
95 assertThat(shouldBeCenter, CustomMatchers.is(new Point2D.Double(WIDTH / 2, HEIGHT / 2)));
96 }
97
98 /**
99 * Tests {@link NavigatableComponent#zoomToFactor(double)} and {@link NavigatableComponent#zoomToFactor(EastNorth, double)}
100 */
101 @Test
102 public void testZoomToFactor() {
103 EastNorth center = component.getCenter();
104 double initialScale = component.getScale();
105
106 // zoomToFactor(double)
107 component.zoomToFactor(0.5);
108 assertEquals(initialScale / 2, component.getScale(), 0.00000001);
109 assertThat(component.getCenter(), is(center));
110 component.zoomToFactor(2);
111 assertEquals(initialScale, component.getScale(), 0.00000001);
112 assertThat(component.getCenter(), is(center));
113
114 // zoomToFactor(EastNorth, double)
115 EastNorth newCenter = new EastNorth(10, 20);
116 component.zoomToFactor(newCenter, 0.5);
117 assertEquals(initialScale / 2, component.getScale(), 0.00000001);
118 assertThat(component.getCenter(), is(newCenter));
119 component.zoomToFactor(newCenter, 2);
120 assertEquals(initialScale, component.getScale(), 0.00000001);
121 assertThat(component.getCenter(), is(newCenter));
122 }
123
124 /**
125 * Tests {@link NavigatableComponent#getEastNorth(int, int)}
126 */
127 @Test
128 public void testGetEastNorth() {
129 EastNorth center = component.getCenter();
130 assertThat(component.getEastNorth(WIDTH / 2, HEIGHT / 2), CustomMatchers.is(center));
131
132 EastNorth testPoint = component.getCenter().add(WIDTH * component.getScale(), HEIGHT * component.getScale());
133 assertThat(component.getEastNorth(3 * WIDTH / 2, -HEIGHT / 2), CustomMatchers.is(testPoint));
134 }
135
136 /**
137 * Tests {@link NavigatableComponent#zoomToFactor(double, double, double)}
138 */
139 @Test
140 public void testZoomToFactorCenter() {
141 // zoomToFactor(double, double, double)
142 // assumes getEastNorth works as expected
143 EastNorth testPoint1 = component.getEastNorth(0, 0);
144 EastNorth testPoint2 = component.getEastNorth(200, 150);
145 double initialScale = component.getScale();
146
147 component.zoomToFactor(0, 0, 0.5);
148 assertEquals(initialScale / 2, component.getScale(), 0.00000001);
149 assertThat(component.getEastNorth(0, 0), CustomMatchers.is(testPoint1));
150 component.zoomToFactor(0, 0, 2);
151 assertEquals(initialScale, component.getScale(), 0.00000001);
152 assertThat(component.getEastNorth(0, 0), CustomMatchers.is(testPoint1));
153
154 component.zoomToFactor(200, 150, 0.5);
155 assertEquals(initialScale / 2, component.getScale(), 0.00000001);
156 assertThat(component.getEastNorth(200, 150), CustomMatchers.is(testPoint2));
157 component.zoomToFactor(200, 150, 2);
158 assertEquals(initialScale, component.getScale(), 0.00000001);
159 assertThat(component.getEastNorth(200, 150), CustomMatchers.is(testPoint2));
160
161 }
162
163 /**
164 * Tests {@link NavigatableComponent#getProjectionBounds()}
165 */
166 @Test
167 public void testGetProjectionBounds() {
168 ProjectionBounds bounds = component.getProjectionBounds();
169 assertThat(bounds.getCenter(), CustomMatchers.is(component.getCenter()));
170
171 assertThat(bounds.getMin(), CustomMatchers.is(component.getEastNorth(0, HEIGHT)));
172 assertThat(bounds.getMax(), CustomMatchers.is(component.getEastNorth(WIDTH, 0)));
173 }
174
175 /**
176 * Tests {@link NavigatableComponent#getRealBounds()}
177 */
178 @Test
179 public void testGetRealBounds() {
180 Bounds bounds = component.getRealBounds();
181 assertThat(bounds.getCenter(), CustomMatchers.is(component.getLatLon(WIDTH / 2, HEIGHT / 2)));
182
183 assertThat(bounds.getMin(), CustomMatchers.is(component.getLatLon(0, HEIGHT)));
184 assertThat(bounds.getMax(), CustomMatchers.is(component.getLatLon(WIDTH, 0)));
185 }
186
187}
Note: See TracBrowser for help on using the repository browser.