source: josm/trunk/test/unit/org/openstreetmap/josm/gui/layer/imagery/MVTLayerTest.java@ 18870

Last change on this file since 18870 was 18870, checked in by taylor.smock, 11 months ago

See #16567: Update to JUnit 5

This converts most tests to use @Annotations. There are also some performance
improvements as it relates to tests.

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.imagery;
3
4import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
6import static org.junit.jupiter.api.Assertions.assertEquals;
7import static org.junit.jupiter.api.Assertions.assertFalse;
8import static org.junit.jupiter.api.Assertions.assertNotNull;
9import static org.junit.jupiter.api.Assertions.assertTrue;
10
11import java.util.Collections;
12
13import org.apache.commons.jcs3.access.behavior.ICacheAccess;
14import org.awaitility.Awaitility;
15import org.awaitility.Durations;
16import org.junit.jupiter.api.BeforeEach;
17import org.junit.jupiter.api.Test;
18import org.junit.jupiter.params.ParameterizedTest;
19import org.junit.jupiter.params.provider.ValueSource;
20import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
21import org.openstreetmap.josm.TestUtils;
22import org.openstreetmap.josm.actions.ExpertToggleAction;
23import org.openstreetmap.josm.data.Bounds;
24import org.openstreetmap.josm.data.imagery.ImageryInfo;
25import org.openstreetmap.josm.data.imagery.TileJobOptions;
26import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MVTFile;
27import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MVTTile;
28import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MapboxVectorCachedTileLoader;
29import org.openstreetmap.josm.data.osm.BBox;
30import org.openstreetmap.josm.data.projection.Projection;
31import org.openstreetmap.josm.data.projection.ProjectionRegistry;
32import org.openstreetmap.josm.data.projection.Projections;
33import org.openstreetmap.josm.gui.MainApplication;
34import org.openstreetmap.josm.testutils.FakeGraphics;
35import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
36import org.openstreetmap.josm.testutils.annotations.HTTP;
37import org.openstreetmap.josm.testutils.annotations.Main;
38
39/**
40 * Test class for {@link MVTLayer}
41 */
42@BasicPreferences
43@HTTP
44@Main
45@org.openstreetmap.josm.testutils.annotations.Projection
46class MVTLayerTest {
47 MVTLayer testLayer;
48
49 @BeforeEach
50 void setUp() {
51 final ImageryInfo imageryInfo = new ImageryInfo("MvtLayerTest", "file:" + TestUtils.getTestDataRoot() + "pbf/mapillary/{z}/{x}/{y}.mvt");
52 imageryInfo.setImageryType(ImageryInfo.ImageryType.MVT);
53 this.testLayer = new MVTLayer(imageryInfo);
54 }
55
56 @Test
57 void testGetTileLoaderClass() {
58 assertEquals(MapboxVectorCachedTileLoader.class, this.testLayer.getTileLoaderClass());
59 }
60
61 @Test
62 void testGetCacheName() {
63 assertEquals("MVT", this.testLayer.getCacheName());
64 }
65
66 @Test
67 void testGetCache() {
68 assertNotNull(MVTLayer.getCache());
69 }
70
71 @Test
72 void testGetNativeProjections() {
73 assertArrayEquals(Collections.singleton(MVTFile.DEFAULT_PROJECTION).toArray(), this.testLayer.getNativeProjections().toArray());
74 }
75
76 /**
77 * This is a non-regression test for JOSM #21260
78 * @param projectionCode The projection code to use
79 * @throws ReflectiveOperationException If the required method was unable to be called
80 */
81 @ParameterizedTest
82 @ValueSource(strings = {"EPSG:3857" /* WGS 84 */, "EPSG:4326" /* Mercator (default) */, "EPSG:32612" /* UTM 12 N */})
83 void testEnsureDifferentProjectionsAreFetched(final String projectionCode) throws ReflectiveOperationException {
84 final Projection originalProjection = ProjectionRegistry.getProjection();
85 try {
86 ProjectionRegistry.setProjection(Projections.getProjectionByCode(projectionCode));
87 // Needed to initialize mapView
88 MainApplication.getLayerManager().addLayer(this.testLayer);
89 final BBox tileBBox = new MVTTile(this.testLayer.getTileSource(), 3248, 6258, 14).getBBox();
90 MainApplication.getMap().mapView.zoomTo(new Bounds(tileBBox.getMinLat(), tileBBox.getMinLon(),
91 tileBBox.getMaxLat(), tileBBox.getMaxLon()));
92 final FakeGraphics graphics2D = new FakeGraphics();
93 graphics2D.setClip(0, 0, 100, 100);
94 this.testLayer.setZoomLevel(14);
95 this.testLayer.getDisplaySettings().setAutoZoom(false);
96 MainApplication.getMap().mapView.paintLayer(this.testLayer, graphics2D);
97 Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> !this.testLayer.getData().allPrimitives().isEmpty());
98 assertFalse(this.testLayer.getData().allPrimitives().isEmpty());
99 } finally {
100 ProjectionRegistry.setProjection(originalProjection);
101 }
102 }
103
104 @Test
105 void testGetTileSource() {
106 assertEquals(this.testLayer.getInfo().getUrl(), this.testLayer.getTileSource().getBaseUrl());
107 }
108
109 @Test
110 void testCreateTile() {
111 assertNotNull(this.testLayer.createTile(this.testLayer.getTileSource(), 3251, 6258, 14));
112 }
113
114 @ParameterizedTest
115 @ValueSource(booleans = {true, false})
116 void testGetMenuEntries(final boolean isExpert) {
117 ExpertToggleAction.getInstance().setExpert(isExpert);
118 // For now, just ensure that nothing throws on implementation
119 MainApplication.getLayerManager().addLayer(this.testLayer);
120 assertNotNull(assertDoesNotThrow(() -> this.testLayer.getMenuEntries()));
121 }
122
123 @Test
124 void testGetData() {
125 assertNotNull(this.testLayer.getData());
126 }
127
128 @Test
129 void testFinishedLoading() throws ReflectiveOperationException {
130 final MVTTile mvtTile = (MVTTile) this.testLayer.createTile(this.testLayer.getTileSource(), 3248, 6258, 14);
131 final FinishedLoading finishedLoading = new FinishedLoading();
132 mvtTile.addTileLoaderFinisher(finishedLoading);
133 assertTrue(this.testLayer.getData().allPrimitives().isEmpty());
134 this.testLayer.getTileLoaderClass().getConstructor(TileLoaderListener.class, ICacheAccess.class, TileJobOptions.class)
135 .newInstance(this.testLayer, MVTLayer.getCache(), new TileJobOptions(50, 50, Collections.emptyMap(), 1))
136 .createTileLoaderJob(mvtTile).submit();
137 Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> finishedLoading.finished);
138 assertFalse(this.testLayer.getData().allPrimitives().isEmpty());
139 }
140
141 /**
142 * For some reason, lambdas get garbage collected by WeakReference's. This avoids that.
143 */
144 private static final class FinishedLoading implements MVTTile.TileListener {
145 boolean finished;
146 @Override
147 public void finishedLoading(MVTTile tile) {
148 this.finished = true;
149 }
150 }
151}
Note: See TracBrowser for help on using the repository browser.