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

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

sonar minor fixes

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