1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.imagery;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
9 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
10 |
|
---|
11 | import java.util.Collections;
|
---|
12 |
|
---|
13 | import org.apache.commons.jcs3.access.behavior.ICacheAccess;
|
---|
14 | import org.awaitility.Awaitility;
|
---|
15 | import org.awaitility.Durations;
|
---|
16 | import org.junit.jupiter.api.BeforeEach;
|
---|
17 | import org.junit.jupiter.api.Test;
|
---|
18 | import org.junit.jupiter.params.ParameterizedTest;
|
---|
19 | import org.junit.jupiter.params.provider.ValueSource;
|
---|
20 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
|
---|
21 | import org.openstreetmap.josm.TestUtils;
|
---|
22 | import org.openstreetmap.josm.actions.ExpertToggleAction;
|
---|
23 | import org.openstreetmap.josm.data.Bounds;
|
---|
24 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
25 | import org.openstreetmap.josm.data.imagery.TileJobOptions;
|
---|
26 | import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MVTFile;
|
---|
27 | import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MVTTile;
|
---|
28 | import org.openstreetmap.josm.data.imagery.vectortile.mapbox.MapboxVectorCachedTileLoader;
|
---|
29 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
30 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
31 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
32 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
33 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
34 | import org.openstreetmap.josm.testutils.FakeGraphics;
|
---|
35 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
36 | import org.openstreetmap.josm.testutils.annotations.HTTP;
|
---|
37 | import 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
|
---|
46 | class 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 | }
|
---|