| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.gui; |
| | 3 | |
| | 4 | import static org.junit.Assert.assertEquals; |
| | 5 | import static org.openstreetmap.josm.TestUtils.assertEastNorthEquals; |
| | 6 | import static org.openstreetmap.josm.TestUtils.assertLatLonEquals; |
| | 7 | import static org.openstreetmap.josm.TestUtils.assertPointEquals; |
| | 8 | |
| | 9 | import java.awt.Rectangle; |
| | 10 | import java.awt.geom.Point2D; |
| | 11 | |
| | 12 | import org.junit.Before; |
| | 13 | import org.junit.BeforeClass; |
| | 14 | import org.junit.Test; |
| | 15 | import org.openstreetmap.josm.JOSMFixture; |
| | 16 | import org.openstreetmap.josm.Main; |
| | 17 | import org.openstreetmap.josm.data.Bounds; |
| | 18 | import org.openstreetmap.josm.data.ProjectionBounds; |
| | 19 | import org.openstreetmap.josm.data.coor.EastNorth; |
| | 20 | import org.openstreetmap.josm.data.coor.LatLon; |
| | 21 | import org.openstreetmap.josm.gui.util.GuiHelper; |
| | 22 | |
| | 23 | /** |
| | 24 | * Some tests for the {@link NavigatableComponent} class. |
| | 25 | * @author Michael Zangl |
| | 26 | * |
| | 27 | */ |
| | 28 | public 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 | assertPointEquals(new Point2D.Double(), component.getPoint2D((EastNorth) null)); |
| | 71 | Point2D shouldBeCenter = component.getPoint2D(component.getCenter()); |
| | 72 | assertPointEquals(new Point2D.Double(WIDTH / 2, HEIGHT / 2), shouldBeCenter); |
| | 73 | |
| | 74 | EastNorth testPoint = component.getCenter().add(300 * component.getScale(), 200 * component.getScale()); |
| | 75 | Point2D testPointConverted = component.getPoint2D(testPoint); |
| | 76 | assertPointEquals(new Point2D.Double(WIDTH / 2 + 300, HEIGHT / 2 - 200), testPointConverted); |
| | 77 | } |
| | 78 | |
| | 79 | /** |
| | 80 | * TODO: Implement this test. |
| | 81 | */ |
| | 82 | @Test |
| | 83 | public void testPoint2DLatLon() { |
| | 84 | assertPointEquals(new Point2D.Double(), component.getPoint2D((LatLon) null)); |
| | 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 | assertPointEquals(new Point2D.Double(WIDTH / 2, HEIGHT / 2), shouldBeCenter); |
| | 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 | assertEastNorthEquals(center, component.getCenter()); |
| | 110 | component.zoomToFactor(2); |
| | 111 | assertEquals(initialScale, component.getScale(), 0.00000001); |
| | 112 | assertEastNorthEquals(center, component.getCenter()); |
| | 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 | assertEastNorthEquals(newCenter, component.getCenter()); |
| | 119 | component.zoomToFactor(newCenter, 2); |
| | 120 | assertEquals(initialScale, component.getScale(), 0.00000001); |
| | 121 | assertEastNorthEquals(newCenter, component.getCenter()); |
| | 122 | } |
| | 123 | |
| | 124 | /** |
| | 125 | * Tests {@link NavigatableComponent#getEastNorth(int, int)} |
| | 126 | */ |
| | 127 | @Test |
| | 128 | public void testGetEastNorth() { |
| | 129 | EastNorth center = component.getCenter(); |
| | 130 | assertEastNorthEquals(center, component.getEastNorth(WIDTH / 2, HEIGHT / 2)); |
| | 131 | |
| | 132 | EastNorth testPoint = component.getCenter().add(WIDTH * component.getScale(), HEIGHT * component.getScale()); |
| | 133 | assertEastNorthEquals(testPoint, component.getEastNorth(3 * WIDTH / 2, -HEIGHT / 2)); |
| | 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 | assertEastNorthEquals(testPoint1, component.getEastNorth(0, 0)); |
| | 150 | component.zoomToFactor(0, 0, 2); |
| | 151 | assertEquals(initialScale, component.getScale(), 0.00000001); |
| | 152 | assertEastNorthEquals(testPoint1, component.getEastNorth(0, 0)); |
| | 153 | |
| | 154 | component.zoomToFactor(200, 150, 0.5); |
| | 155 | assertEquals(initialScale / 2, component.getScale(), 0.00000001); |
| | 156 | assertEastNorthEquals(testPoint2, component.getEastNorth(200, 150)); |
| | 157 | component.zoomToFactor(200, 150, 2); |
| | 158 | assertEquals(initialScale, component.getScale(), 0.00000001); |
| | 159 | assertEastNorthEquals(testPoint2, component.getEastNorth(200, 150)); |
| | 160 | |
| | 161 | } |
| | 162 | |
| | 163 | /** |
| | 164 | * Tests {@link NavigatableComponent#getProjectionBounds()} |
| | 165 | */ |
| | 166 | @Test |
| | 167 | public void testGetProjectionBounds() { |
| | 168 | ProjectionBounds bounds = component.getProjectionBounds(); |
| | 169 | assertEastNorthEquals(component.getCenter(), bounds.getCenter()); |
| | 170 | |
| | 171 | assertEastNorthEquals(component.getEastNorth(0, HEIGHT), bounds.getMin()); |
| | 172 | assertEastNorthEquals(component.getEastNorth(WIDTH, 0), bounds.getMax()); |
| | 173 | } |
| | 174 | |
| | 175 | /** |
| | 176 | * Tests {@link NavigatableComponent#getRealBounds()} |
| | 177 | */ |
| | 178 | @Test |
| | 179 | public void testGetRealBounds() { |
| | 180 | Bounds bounds = component.getRealBounds(); |
| | 181 | assertLatLonEquals(component.getLatLon(WIDTH / 2, HEIGHT / 2), bounds.getCenter()); |
| | 182 | |
| | 183 | assertLatLonEquals(component.getLatLon(0, HEIGHT), bounds.getMin()); |
| | 184 | assertLatLonEquals(component.getLatLon(WIDTH, 0), bounds.getMax()); |
| | 185 | } |
| | 186 | |
| | 187 | } |