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

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

fix #12959 - Move state changes to MapViewState class (patch by michael2402) - gscore-core + checkstyle fixes

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