source: josm/trunk/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java

Last change on this file was 19428, checked in by taylor.smock, 9 months ago

Fix some test issues (from order) and clean up some try blocks

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
5import static com.github.tomakehurst.wiremock.client.WireMock.get;
6import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
7import static org.junit.jupiter.api.Assertions.assertEquals;
8import static org.junit.jupiter.api.Assertions.assertThrows;
9import static org.junit.jupiter.api.Assertions.assertTrue;
10
11import java.util.Arrays;
12import java.util.List;
13
14import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
15import org.junit.jupiter.api.Disabled;
16import org.junit.jupiter.api.Test;
17import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
18import org.openstreetmap.josm.data.Bounds;
19import org.openstreetmap.josm.data.imagery.ImageryInfo;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.projection.ProjectionRegistry;
22import org.openstreetmap.josm.gui.MainApplication;
23import org.openstreetmap.josm.gui.layer.OsmDataLayer;
24import org.openstreetmap.josm.gui.layer.TMSLayer;
25import org.openstreetmap.josm.gui.layer.WMSLayer;
26import org.openstreetmap.josm.gui.util.GuiHelper;
27import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
28import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
29import org.openstreetmap.josm.testutils.annotations.Main;
30import org.openstreetmap.josm.testutils.annotations.OsmApi;
31import org.openstreetmap.josm.testutils.annotations.Projection;
32
33/**
34 * Unit tests for class {@link AddImageryLayerAction}.
35 */
36@BasicPreferences
37@BasicWiremock
38@OsmApi(OsmApi.APIType.FAKE)
39@Projection
40final class AddImageryLayerActionTest {
41 /**
42 * Unit test of {@link AddImageryLayerAction#updateEnabledState}.
43 */
44 @Test
45 void testEnabledState() {
46 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo")).isEnabled());
47 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).isEnabled());
48 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_bing", "http://bar", "bing", null, null)).isEnabled());
49 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_scanex", "http://bar", "scanex", null, null)).isEnabled());
50 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_wms_endpoint", "http://bar", "wms_endpoint", null, null)).isEnabled());
51 }
52
53 /**
54 * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for TMS.
55 */
56 @Test
57 void testActionPerformedEnabledTms() {
58 assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
59 new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).actionPerformed(null);
60 List<TMSLayer> tmsLayers = MainApplication.getLayerManager().getLayersOfType(TMSLayer.class);
61 assertEquals(1, tmsLayers.size());
62 MainApplication.getLayerManager().removeLayer(tmsLayers.get(0));
63 }
64
65 /**
66 * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for WMS.
67 */
68 @Test
69 void testActionPerformedEnabledWms(WireMockRuntimeInfo wireMockRuntimeInfo) {
70 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1"))
71 .willReturn(aResponse()
72 .withStatus(200)
73 .withHeader("Content-Type", "text/xml")
74 .withBodyFile("imagery/wms-capabilities.xml")));
75 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities"))
76 .willReturn(aResponse()
77 .withStatus(404)));
78 wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0"))
79 .willReturn(aResponse()
80 .withStatus(404)));
81
82 try {
83 FeatureAdapter.registerApiKeyAdapter(id -> "random_key");
84 final ImageryInfo imageryInfo = new ImageryInfo("localhost", wireMockRuntimeInfo.getHttpBaseUrl() + "/wms?apikey={apikey}",
85 "wms_endpoint", null, null);
86 imageryInfo.setId("testActionPerformedEnabledWms");
87 new AddImageryLayerAction(imageryInfo).actionPerformed(null);
88 List<WMSLayer> wmsLayers = MainApplication.getLayerManager().getLayersOfType(WMSLayer.class);
89 assertEquals(1, wmsLayers.size());
90
91 MainApplication.getLayerManager().removeLayer(wmsLayers.get(0));
92 } finally {
93 FeatureAdapter.registerApiKeyAdapter(new FeatureAdapter.DefaultApiKeyAdapter());
94 }
95 }
96
97 /**
98 * Unit test of {@link AddImageryLayerAction#actionPerformed} - disabled case.
99 */
100 @Test
101 void testActionPerformedDisabled() {
102 assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
103 final AddImageryLayerAction action = new AddImageryLayerAction(new ImageryInfo("foo"));
104 IllegalArgumentException expected = assertThrows(IllegalArgumentException.class, () -> action.actionPerformed(null));
105 assertEquals("Parameter 'info.url' must not be null", expected.getMessage());
106 assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
107 }
108
109
110 /**
111 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/24097">#24097</a>: Zoom to imagery layer
112 * This tests two things:
113 * <ul>
114 * <li>Imagery layer zoom to action works properly</li>
115 * <li>Imagery layer bounds is not zoomed to on layer add</li>
116 * </ul>
117 */
118 @Disabled("See #24097 comment 3")
119 @Main
120 @Test
121 void testNonRegression24097() {
122 // First, add a new data layer
123 MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(),
124 "AddImageryLayerActionTest#testNonRegression24097", null));
125 // Now zoom to a random area
126 MainApplication.getMap().mapView.zoomTo(new Bounds(39.0665807, -108.5212326, 39.0793079, -108.4986591));
127 // Initialize the zoom actions
128 MainApplication.getMenu().initialize();
129 final Bounds startingBounds = MainApplication.getMap().mapView.getRealBounds();
130 ImageryInfo testInfo = new ImageryInfo("Test", "https://127.0.0.1/{zoom}/{x}/{y}.png", "tms", null, null, "Test");
131 testInfo.setBounds(new ImageryInfo.ImageryBounds("-0.001,-0.001,0.001,0.001", ","));
132 new AddImageryLayerAction(testInfo).actionPerformed(null);
133 GuiHelper.runInEDTAndWait(() -> { /* Sync GUI thread */ });
134 // There is a bit of zooming done during the load of the imagery
135 assertTrue(startingBounds.toBBox().bboxIsFunctionallyEqual(MainApplication.getMap().mapView.getRealBounds().toBBox(), 0.001),
136 "Adding an imagery layer should not zoom to the imagery layer bounds");
137 assertEquals(1, MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).size());
138 final TMSLayer tmsLayer = MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).get(0);
139 final AutoScaleAction autoScaleAction = Arrays.stream(tmsLayer.getMenuEntries()).filter(AutoScaleAction.class::isInstance)
140 .map(AutoScaleAction.class::cast).findFirst().orElseThrow();
141 autoScaleAction.actionPerformed(null);
142 // We can't check the bbox here, since the mapView doesn't have any actual width/height.
143 // So we just check the center.
144 assertTrue(new Bounds(-0.001, -0.001, 0.001, 0.001)
145 .contains(ProjectionRegistry.getProjection().eastNorth2latlon(
146 MainApplication.getMap().mapView.getCenter())),
147 "The action should have zoomed to the bbox for the imagery layer");
148 }
149}
Note: See TracBrowser for help on using the repository browser.