Changeset 19221 in josm
- Timestamp:
- 2024-09-12T12:37:19+02:00 (4 months ago)
- Location:
- trunk/test
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/functional/org/openstreetmap/josm/gui/mappaint/MapCSSRendererTest.java
r19220 r19221 2 2 package org.openstreetmap.josm.gui.mappaint; 3 3 4 import static org.junit.jupiter.api.Assertions.assertEquals;5 import static org.junit.jupiter.api.Assertions.fail;6 4 import static org.junit.jupiter.api.Assumptions.assumeTrue; 7 8 import java.awt.Color; 5 import static org.openstreetmap.josm.testutils.ImageTestUtils.assertImageEquals; 6 9 7 import java.awt.GraphicsEnvironment; 10 import java.awt.Point;11 8 import java.awt.image.BufferedImage; 12 9 import java.io.File; … … 15 12 import java.nio.file.Files; 16 13 import java.nio.file.Paths; 17 import java.text.MessageFormat;18 14 import java.util.ArrayList; 19 15 import java.util.Arrays; … … 21 17 import java.util.Collections; 22 18 import java.util.List; 23 import java.util.function.Consumer;24 19 import java.util.stream.Collectors; 25 20 import java.util.stream.Stream; … … 40 35 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 41 36 import org.openstreetmap.josm.testutils.annotations.Projection; 42 import org.openstreetmap.josm.tools.ColorHelper;43 37 import org.openstreetmap.josm.tools.Utils; 44 38 … … 59 53 private static final int IMAGE_SIZE = 256; 60 54 61 // development flag - set to true in order to update all reference images62 private static final boolean UPDATE_ALL = false;63 64 55 /** 65 56 * The different configurations of this test. … … 202 193 } 203 194 204 /**205 * Compares the reference image file with the actual images given as {@link BufferedImage}.206 * @param testIdentifier a test identifier for error messages207 * @param referenceImageFile the reference image file to be read using {@link ImageIO#read(File)}208 * @param image the actual image209 * @param thresholdPixels maximum number of differing pixels210 * @param thresholdTotalColorDiff maximum sum of color value differences211 * @param diffImageConsumer a consumer for a rendered image highlighting the differing pixels, may be null212 * @throws IOException in case of I/O error213 */214 public static void assertImageEquals(215 String testIdentifier, File referenceImageFile, BufferedImage image,216 int thresholdPixels, int thresholdTotalColorDiff, Consumer<BufferedImage> diffImageConsumer) throws IOException {217 218 // TODO move to separate class ImageTestUtils219 if (UPDATE_ALL) {220 ImageIO.write(image, "png", referenceImageFile);221 return;222 }223 final BufferedImage reference = ImageIO.read(referenceImageFile);224 assertImageEquals(testIdentifier, reference, image, thresholdPixels, thresholdTotalColorDiff, diffImageConsumer);225 }226 227 /**228 * Compares the reference image file with the actual images given as {@link BufferedImage}.229 * @param testIdentifier a test identifier for error messages230 * @param reference the reference image231 * @param image the actual image232 * @param thresholdPixels maximum number of differing pixels233 * @param thresholdTotalColorDiff maximum sum of color value differences234 * @param diffImageConsumer a consumer for a rendered image highlighting the differing pixels, may be null235 */236 public static void assertImageEquals(String testIdentifier, BufferedImage reference, BufferedImage image,237 int thresholdPixels, int thresholdTotalColorDiff, Consumer<BufferedImage> diffImageConsumer) {238 assertEquals(reference.getWidth(), image.getWidth());239 assertEquals(reference.getHeight(), image.getHeight());240 241 StringBuilder differences = new StringBuilder();242 ArrayList<Point> differencePoints = new ArrayList<>();243 int colorDiffSum = 0;244 245 for (int y = 0; y < reference.getHeight(); y++) {246 for (int x = 0; x < reference.getWidth(); x++) {247 int expected = reference.getRGB(x, y);248 int result = image.getRGB(x, y);249 int expectedAlpha = expected >> 24;250 boolean colorsAreSame = expectedAlpha == 0 ? result >> 24 == 0 : expected == result;251 if (!colorsAreSame) {252 Color expectedColor = new Color(expected, true);253 Color resultColor = new Color(result, true);254 int colorDiff = Math.abs(expectedColor.getRed() - resultColor.getRed())255 + Math.abs(expectedColor.getGreen() - resultColor.getGreen())256 + Math.abs(expectedColor.getBlue() - resultColor.getBlue());257 int alphaDiff = Math.abs(expectedColor.getAlpha() - resultColor.getAlpha());258 // Ignore small alpha differences due to Java versions, rendering libraries and so on259 if (alphaDiff <= 20) {260 alphaDiff = 0;261 }262 // Ignore small color differences for the same reasons, but also completely for almost-transparent pixels263 if (colorDiff <= 15 || resultColor.getAlpha() <= 20) {264 colorDiff = 0;265 }266 if (colorDiff + alphaDiff > 0) {267 differencePoints.add(new Point(x, y));268 if (differences.length() < 2000) {269 differences.append("\nDifference at ")270 .append(x)271 .append(",")272 .append(y)273 .append(": Expected ")274 .append(ColorHelper.color2html(expectedColor))275 .append(" but got ")276 .append(ColorHelper.color2html(resultColor))277 .append(" (color diff is ")278 .append(colorDiff)279 .append(", alpha diff is ")280 .append(alphaDiff)281 .append(")");282 }283 }284 colorDiffSum += colorDiff + alphaDiff;285 }286 }287 }288 289 if (differencePoints.size() > thresholdPixels || colorDiffSum > thresholdTotalColorDiff) {290 // Add a nice image that highlights the differences:291 BufferedImage diffImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);292 for (Point p : differencePoints) {293 diffImage.setRGB(p.x, p.y, 0xffff0000);294 }295 if (diffImageConsumer != null) {296 diffImageConsumer.accept(diffImage);297 }298 299 if (differencePoints.size() > thresholdPixels) {300 fail(MessageFormat.format("Images for test {0} differ at {1} points, threshold is {2}: {3}",301 testIdentifier, differencePoints.size(), thresholdPixels, differences.toString()));302 } else {303 fail(MessageFormat.format("Images for test {0} differ too much in color, value is {1}, permitted threshold is {2}: {3}",304 testIdentifier, colorDiffSum, thresholdTotalColorDiff, differences.toString()));305 }306 }307 }308 309 195 private void loadPrimitiveStyle(OsmPrimitive n) { 310 196 n.setHighlighted(n.isKeyTrue("highlight")); -
trunk/test/functional/org/openstreetmap/josm/tools/ImageProviderTest.java
r19055 r19221 6 6 import static org.junit.jupiter.api.Assertions.assertFalse; 7 7 import static org.junit.jupiter.api.Assertions.assertNotNull; 8 import static org.openstreetmap.josm. gui.mappaint.MapCSSRendererTest.assertImageEquals;8 import static org.openstreetmap.josm.testutils.ImageTestUtils.assertImageEquals; 9 9 10 10 import java.awt.Dimension; -
trunk/test/unit/org/openstreetmap/josm/data/osm/visitor/paint/StyledTiledMapRendererTest.java
r19220 r19221 2 2 package org.openstreetmap.josm.data.osm.visitor.paint; 3 3 4 import static org.openstreetmap.josm.gui.mappaint.MapCSSRendererTest.assertImageEquals; 4 import static org.openstreetmap.josm.testutils.ImageTestUtils.assertImageEquals; 5 import static org.openstreetmap.josm.testutils.ImageTestUtils.writeDebugImages; 5 6 6 7 import java.awt.Graphics2D; 7 8 import java.awt.image.BufferedImage; 8 import java.io.File;9 import java.io.IOException;10 import java.io.UncheckedIOException;11 import java.nio.file.Files;12 9 import java.nio.file.Paths; 13 10 import java.util.Arrays; … … 20 17 import java.util.stream.IntStream; 21 18 import java.util.stream.Stream; 22 23 import javax.imageio.ImageIO;24 19 25 20 import org.apache.commons.jcs3.access.CacheAccess; … … 73 68 } 74 69 75 @ParameterizedTest(name = "{ 0} - {2}")70 @ParameterizedTest(name = "{2} - {0}") 76 71 @MethodSource 77 72 void testRender(String testIdentifier, final Supplier<DataSet> dataSetSupplier, final TileZXY tile) … … 118 113 }).collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().image())); 119 114 try { 120 assertImageEquals(testIdentifier, oldRenderStyle, newRenderStyle, 0, 0, diff -> { 121 try { 122 if (!Files.isDirectory(Paths.get(TestUtils.getTestDataRoot(), "output"))) { 123 Files.createDirectories(Paths.get(TestUtils.getTestDataRoot(), "output")); 124 } 125 final String basename = TestUtils.getTestDataRoot() + "output/" + 126 testIdentifier + ' ' + tile.zoom() + '-' + tile.x() + '-' + tile.y(); 127 ImageIO.write(diff, "png", new File(basename + "-diff.png")); 128 ImageIO.write(newRenderStyle, "png", new File(basename + "-new.png")); 129 ImageIO.write(oldRenderStyle, "png", new File(basename + "-old.png")); 130 } catch (IOException e) { 131 throw new UncheckedIOException(e); 132 } 133 }); 115 assertImageEquals(testIdentifier, oldRenderStyle, newRenderStyle, 0, 0, diff -> 116 writeDebugImages(Paths.get(TestUtils.getTestDataRoot(), "output"), 117 testIdentifier + ' ' + tile.zoom() + '-' + tile.x() + '-' + tile.y(), diff, oldRenderStyle, newRenderStyle) 118 ); 134 119 } finally { 135 120 cache.clear(); -
trunk/test/unit/org/openstreetmap/josm/testutils/annotations/Plugins.java
r19209 r19221 34 34 class PluginExtension implements AfterEachCallback { 35 35 36 @SuppressWarnings("unchecked") 36 37 @Override 37 38 public void afterEach(ExtensionContext context) throws Exception {
Note:
See TracChangeset
for help on using the changeset viewer.