Ignore:
Timestamp:
2018-12-11T00:24:50+01:00 (5 years ago)
Author:
Don-vip
Message:

see #16073 - detect correct min_zoom value

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java

    r14550 r14551  
    22package org.openstreetmap.josm.gui.preferences.imagery;
    33
     4import static org.junit.Assert.assertFalse;
    45import static org.junit.Assert.assertTrue;
    56
     
    6364public class ImageryPreferenceTestIT {
    6465
     66    private static final LatLon GREENWICH = new LatLon(51.47810, -0.00170);
     67    private static final int DEFAULT_ZOOM = 12;
     68
    6569    /**
    6670     * Setup rule
     
    133137    }
    134138
    135     private void checkTileUrl(ImageryInfo info, AbstractTileSource tileSource, ICoordinate center, int zoom)
     139    private boolean checkTileUrl(ImageryInfo info, AbstractTileSource tileSource, ICoordinate center, int zoom)
    136140            throws IOException {
    137141        TileXY xy = tileSource.latLonToTileXY(center, zoom);
     
    142146                    try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
    143147                        if (ImageIO.read(bais) == null) {
    144                             addImageError(info, url, data, "did not return an image");
     148                            addImageError(info, url, data, zoom, "did not return an image");
    145149                        }
    146150                    } catch (IOException e) {
    147                         addImageError(info, url, data, e.toString());
     151                        addImageError(info, url, data, zoom, e.toString());
    148152                        Logging.trace(e);
    149153                    }
    150154                });
    151                 return;
     155                // Determines if this is a success (no error message with current zoom marker)
     156                return errors.getOrDefault(info.getCountryCode(), Collections.emptyMap())
     157                              .getOrDefault(info, Collections.emptyList())
     158                              .stream().noneMatch(e -> e.contains(zoomMarker(zoom)));
    152159            } catch (IOException e) {
    153160                // Try up to three times max to allow Bing source to initialize itself
     
    164171            }
    165172        }
    166     }
    167 
    168     private void addImageError(ImageryInfo info, String url, byte[] data, String defaultMessage) {
     173        return false;
     174    }
     175
     176    private static String zoomMarker(int zoom) {
     177        return " -> zoom " + zoom + " -> ";
     178    }
     179
     180    private void addImageError(ImageryInfo info, String url, byte[] data, int zoom, String defaultMessage) {
    169181        // Check if we have received an error message
    170182        String error = helper.detectErrorMessage(new String(data, StandardCharsets.UTF_8));
    171         addError(info, url + " -> " + (error != null ? error.split("\\n")[0] : defaultMessage));
     183        addError(info, url + zoomMarker(zoom) + (error != null ? error.split("\\n")[0] : defaultMessage));
    172184    }
    173185
     
    241253            ImageryBounds bounds = info.getBounds();
    242254            // Some imagery sources do not define tiles at (0,0). So pickup Greenwich Royal Observatory for global sources
    243             ICoordinate center = CoordinateConversion.llToCoor(bounds != null ? getCenter(bounds) : new LatLon(51.47810, -0.00170));
     255            ICoordinate center = CoordinateConversion.llToCoor(bounds != null ? getCenter(bounds) : GREENWICH);
    244256            AbstractTileSource tileSource = getTileSource(info);
    245             checkTileUrl(info, tileSource, center, info.getMinZoom());
     257            // test min zoom and try to detect the correct value in case of error
     258            int maxZoom = info.getMaxZoom() > 0 ? Math.min(DEFAULT_ZOOM, info.getMaxZoom()) : DEFAULT_ZOOM;
     259            for (int zoom = info.getMinZoom(); zoom < maxZoom; zoom++) {
     260                if (checkTileUrl(info, tileSource, center, zoom)) {
     261                    break;
     262                }
     263            }
    246264            // checking max zoom for real is complex, see https://josm.openstreetmap.de/ticket/16073#comment:27
    247265            if (info.getMaxZoom() > 0 && info.getImageryType() != ImageryType.SCANEX) {
    248                 checkTileUrl(info, tileSource, center, Utils.clamp(12, info.getMinZoom() + 1, info.getMaxZoom()));
     266                checkTileUrl(info, tileSource, center, Utils.clamp(DEFAULT_ZOOM, info.getMinZoom() + 1, info.getMaxZoom()));
    249267            }
    250268        } catch (IOException | RuntimeException | WMSGetCapabilitiesException | WMTSGetCapabilitiesException e) {
     
    314332        assertTrue(errors.toString().replaceAll("\\}, ", "\n\\}, ").replaceAll(", ImageryInfo\\{", "\n      ,ImageryInfo\\{"),
    315333                errors.isEmpty());
     334        assertFalse(workingURLs.isEmpty());
    316335    }
    317336}
Note: See TracChangeset for help on using the changeset viewer.