Changeset 19221 in josm


Ignore:
Timestamp:
2024-09-12T12:37:19+02:00 (5 days ago)
Author:
taylor.smock
Message:

Fix failing build due to putting unit test for renderer in unit instead of functional

This was done by fixing a TODO comment ("move to separate class ImageTestUtils")
in the MapCSSRendererTest.

Location:
trunk/test
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/functional/org/openstreetmap/josm/gui/mappaint/MapCSSRendererTest.java

    r19220 r19221  
    22package org.openstreetmap.josm.gui.mappaint;
    33
    4 import static org.junit.jupiter.api.Assertions.assertEquals;
    5 import static org.junit.jupiter.api.Assertions.fail;
    64import static org.junit.jupiter.api.Assumptions.assumeTrue;
    7 
    8 import java.awt.Color;
     5import static org.openstreetmap.josm.testutils.ImageTestUtils.assertImageEquals;
     6
    97import java.awt.GraphicsEnvironment;
    10 import java.awt.Point;
    118import java.awt.image.BufferedImage;
    129import java.io.File;
     
    1512import java.nio.file.Files;
    1613import java.nio.file.Paths;
    17 import java.text.MessageFormat;
    1814import java.util.ArrayList;
    1915import java.util.Arrays;
     
    2117import java.util.Collections;
    2218import java.util.List;
    23 import java.util.function.Consumer;
    2419import java.util.stream.Collectors;
    2520import java.util.stream.Stream;
     
    4035import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
    4136import org.openstreetmap.josm.testutils.annotations.Projection;
    42 import org.openstreetmap.josm.tools.ColorHelper;
    4337import org.openstreetmap.josm.tools.Utils;
    4438
     
    5953    private static final int IMAGE_SIZE = 256;
    6054
    61     // development flag - set to true in order to update all reference images
    62     private static final boolean UPDATE_ALL = false;
    63 
    6455    /**
    6556     * The different configurations of this test.
     
    202193    }
    203194
    204     /**
    205      * Compares the reference image file with the actual images given as {@link BufferedImage}.
    206      * @param testIdentifier a test identifier for error messages
    207      * @param referenceImageFile the reference image file to be read using {@link ImageIO#read(File)}
    208      * @param image the actual image
    209      * @param thresholdPixels maximum number of differing pixels
    210      * @param thresholdTotalColorDiff maximum sum of color value differences
    211      * @param diffImageConsumer a consumer for a rendered image highlighting the differing pixels, may be null
    212      * @throws IOException in case of I/O error
    213      */
    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 ImageTestUtils
    219         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 messages
    230      * @param reference the reference image
    231      * @param image the actual image
    232      * @param thresholdPixels maximum number of differing pixels
    233      * @param thresholdTotalColorDiff maximum sum of color value differences
    234      * @param diffImageConsumer a consumer for a rendered image highlighting the differing pixels, may be null
    235      */
    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 on
    259                     if (alphaDiff <= 20) {
    260                         alphaDiff = 0;
    261                     }
    262                     // Ignore small color differences for the same reasons, but also completely for almost-transparent pixels
    263                     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 
    309195    private void loadPrimitiveStyle(OsmPrimitive n) {
    310196        n.setHighlighted(n.isKeyTrue("highlight"));
  • trunk/test/functional/org/openstreetmap/josm/tools/ImageProviderTest.java

    r19055 r19221  
    66import static org.junit.jupiter.api.Assertions.assertFalse;
    77import static org.junit.jupiter.api.Assertions.assertNotNull;
    8 import static org.openstreetmap.josm.gui.mappaint.MapCSSRendererTest.assertImageEquals;
     8import static org.openstreetmap.josm.testutils.ImageTestUtils.assertImageEquals;
    99
    1010import java.awt.Dimension;
  • trunk/test/unit/org/openstreetmap/josm/data/osm/visitor/paint/StyledTiledMapRendererTest.java

    r19220 r19221  
    22package org.openstreetmap.josm.data.osm.visitor.paint;
    33
    4 import static org.openstreetmap.josm.gui.mappaint.MapCSSRendererTest.assertImageEquals;
     4import static org.openstreetmap.josm.testutils.ImageTestUtils.assertImageEquals;
     5import static org.openstreetmap.josm.testutils.ImageTestUtils.writeDebugImages;
    56
    67import java.awt.Graphics2D;
    78import 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;
    129import java.nio.file.Paths;
    1310import java.util.Arrays;
     
    2017import java.util.stream.IntStream;
    2118import java.util.stream.Stream;
    22 
    23 import javax.imageio.ImageIO;
    2419
    2520import org.apache.commons.jcs3.access.CacheAccess;
     
    7368    }
    7469
    75     @ParameterizedTest(name = "{0} - {2}")
     70    @ParameterizedTest(name = "{2} - {0}")
    7671    @MethodSource
    7772    void testRender(String testIdentifier, final Supplier<DataSet> dataSetSupplier, final TileZXY tile)
     
    118113        }).collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().image()));
    119114        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            );
    134119        } finally {
    135120            cache.clear();
  • trunk/test/unit/org/openstreetmap/josm/testutils/annotations/Plugins.java

    r19209 r19221  
    3434    class PluginExtension implements AfterEachCallback {
    3535
     36        @SuppressWarnings("unchecked")
    3637        @Override
    3738        public void afterEach(ExtensionContext context) throws Exception {
Note: See TracChangeset for help on using the changeset viewer.