Changeset 16982 in josm


Ignore:
Timestamp:
2020-08-30T21:05:07+02:00 (4 years ago)
Author:
simon04
Message:

see #19706, see #19725 - ImageProviderTest: add tests comparing the result with reference images

Location:
trunk/test
Files:
13 added
1 edited
1 moved

Legend:

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

    r16913 r16982  
    1111import java.io.File;
    1212import java.io.IOException;
     13import java.io.UncheckedIOException;
    1314import java.nio.file.Files;
    1415import java.nio.file.Paths;
     
    2021import java.util.List;
    2122import java.util.Locale;
     23import java.util.function.Consumer;
    2224import java.util.stream.Collectors;
    2325import java.util.stream.Stream;
     
    217219        BufferedImage image = rh.render();
    218220
     221        assertImageEquals(testConfig.testDirectory,
     222                testConfig.getReference(), image,
     223                testConfig.thresholdPixels, testConfig.thresholdTotalColorDiff, diffImage -> {
     224                    try {
     225                        // You can use this to debug:
     226                        ImageIO.write(image, "png", new File(testConfig.getTestDirectory() + "/test-output.png"));
     227                        ImageIO.write(diffImage, "png", new File(testConfig.getTestDirectory() + "/test-differences.png"));
     228                    } catch (IOException ex) {
     229                        throw new UncheckedIOException(ex);
     230                    }
     231                });
     232    }
     233
     234    /**
     235     * Compares the reference image file with the actual images given as {@link BufferedImage}.
     236     * @param testIdentifier a test identifier for error messages
     237     * @param referenceImageFile the reference image file to be read using {@link ImageIO#read(File)}
     238     * @param image the actual image
     239     * @param thresholdPixels maximum number of differing pixels
     240     * @param thresholdTotalColorDiff maximum sum of color value differences
     241     * @param diffImageConsumer a consumer for a rendered image highlighting the differing pixels, may be null
     242     * @throws IOException in case of I/O error
     243     */
     244    public static void assertImageEquals(
     245            String testIdentifier, File referenceImageFile, BufferedImage image,
     246            int thresholdPixels, int thresholdTotalColorDiff, Consumer<BufferedImage> diffImageConsumer) throws IOException {
     247
     248        // TODO move to separate class ImageTestUtils
    219249        if (UPDATE_ALL) {
    220             ImageIO.write(image, "png", new File(testConfig.getTestDirectory() + "/reference.png"));
     250            ImageIO.write(image, "png", referenceImageFile);
    221251            return;
    222252        }
    223 
    224         BufferedImage reference = testConfig.getReference();
    225 
    226         // now compute differences:
     253        final BufferedImage reference = ImageIO.read(referenceImageFile);
    227254        assertEquals(image.getWidth(), reference.getWidth());
    228255        assertEquals(image.getHeight(), reference.getHeight());
     
    236263                int expected = reference.getRGB(x, y);
    237264                int result = image.getRGB(x, y);
    238                 if (!colorsAreSame(expected, result)) {
     265                int expectedAlpha = expected >> 24;
     266                boolean colorsAreSame = expectedAlpha == 0 ? result >> 24 == 0 : expected == result;
     267                if (!colorsAreSame) {
    239268                    Color expectedColor = new Color(expected, true);
    240269                    Color resultColor = new Color(result, true);
     
    274303        }
    275304
    276         if (differencePoints.size() > testConfig.thresholdPixels || colorDiffSum > testConfig.thresholdTotalColorDiff) {
    277             // You can use this to debug:
    278             ImageIO.write(image, "png", new File(testConfig.getTestDirectory() + "/test-output.png"));
    279 
     305        if (differencePoints.size() > thresholdPixels || colorDiffSum > thresholdTotalColorDiff) {
    280306            // Add a nice image that highlights the differences:
    281307            BufferedImage diffImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    283309                diffImage.setRGB(p.x, p.y, 0xffff0000);
    284310            }
    285             ImageIO.write(diffImage, "png", new File(testConfig.getTestDirectory() + "/test-differences.png"));
    286 
    287             if (differencePoints.size() > testConfig.thresholdPixels) {
     311            if (diffImageConsumer != null) {
     312                diffImageConsumer.accept(diffImage);
     313            }
     314
     315            if (differencePoints.size() > thresholdPixels) {
    288316                fail(MessageFormat.format("Images for test {0} differ at {1} points, threshold is {2}: {3}",
    289                         testConfig.testDirectory, differencePoints.size(), testConfig.thresholdPixels, differences.toString()));
     317                        testIdentifier, differencePoints.size(), thresholdPixels, differences.toString()));
    290318            } else {
    291319                fail(MessageFormat.format("Images for test {0} differ too much in color, value is {1}, permitted threshold is {2}: {3}",
    292                         testConfig.testDirectory, colorDiffSum, testConfig.thresholdTotalColorDiff, differences.toString()));
     320                        testIdentifier, colorDiffSum, thresholdTotalColorDiff, differences.toString()));
    293321            }
    294322        }
     
    299327        if (n.isKeyTrue("disabled")) {
    300328            n.setDisabledState(false);
    301         }
    302     }
    303 
    304     /**
    305      * Check if two colors differ
    306      * @param expected The expected color
    307      * @param actual The actual color
    308      * @return <code>true</code> if they differ.
    309      */
    310     private boolean colorsAreSame(int expected, int actual) {
    311         int expectedAlpha = expected >> 24;
    312         if (expectedAlpha == 0) {
    313             return actual >> 24 == 0;
    314         } else {
    315             return expected == actual;
    316329        }
    317330    }
     
    369382        }
    370383
    371         public BufferedImage getReference() throws IOException {
    372             return ImageIO.read(new File(getTestDirectory() + "/reference.png"));
     384        public File getReference() {
     385            return new File(getTestDirectory() + "/reference.png");
    373386        }
    374387
  • trunk/test/functional/org/openstreetmap/josm/tools/ImageProviderTest.java

    r16981 r16982  
    88import static org.junit.Assert.assertTrue;
    99import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
     10import static org.openstreetmap.josm.gui.mappaint.MapCSSRendererTest.assertImageEquals;
    1011
    1112import java.awt.Color;
     
    180181
    181182    /**
     183     * Unit test of {@link ImageResource#getImageIcon(java.awt.Dimension)}
     184     * @throws IOException if an I/O error occurs
     185     */
     186    @Test
     187    public void testImageIcon() throws IOException {
     188        ImageResource resource = new ImageProvider("presets/misc/housenumber_small").getResource();
     189        testImage(12, 9, "housenumber_small-AUTO-null", resource.getImageIcon());
     190        testImage(12, 9, "housenumber_small-AUTO-default", resource.getImageIcon(ImageResource.DEFAULT_DIMENSION));
     191        testImage(8, 8, "housenumber_small-AUTO-08x08", resource.getImageIcon(new Dimension(8, 8)));
     192        testImage(16, 16, "housenumber_small-AUTO-16x16", resource.getImageIcon(new Dimension(16, 16)));
     193        testImage(24, 24, "housenumber_small-AUTO-24x24", resource.getImageIcon(new Dimension(24, 24)));
     194        testImage(36, 27, "housenumber_small-AUTO-36x27", resource.getImageIcon(new Dimension(36, 27)));
     195    }
     196
     197    /**
     198     * Unit test of {@link ImageResource#getImageIconBounded(java.awt.Dimension)}
     199     * @throws IOException if an I/O error occurs
     200     */
     201    @Test
     202    public void testImageIconBounded() throws IOException {
     203        ImageResource resource = new ImageProvider("presets/misc/housenumber_small").getResource();
     204        testImage(8, 6, "housenumber_small-BOUNDED-08x08", resource.getImageIconBounded(new Dimension(8, 8)));
     205        testImage(12, 9, "housenumber_small-BOUNDED-16x16", resource.getImageIconBounded(new Dimension(16, 16)));
     206        testImage(12, 9, "housenumber_small-BOUNDED-24x24", resource.getImageIconBounded(new Dimension(24, 24)));
     207    }
     208
     209    /**
     210     * Unit test of {@link ImageResource#getPaddedIcon(java.awt.Dimension)}
     211     * @throws IOException if an I/O error occurs
     212     */
     213    @Test
     214    public void testImageIconPadded() throws IOException {
     215        ImageResource resource = new ImageProvider("presets/misc/housenumber_small").getResource();
     216        testImage(8, 8, "housenumber_small-PADDED-08x08", resource.getPaddedIcon(new Dimension(8, 8)));
     217        testImage(16, 16, "housenumber_small-PADDED-16x16", resource.getPaddedIcon(new Dimension(16, 16)));
     218        testImage(24, 24, "housenumber_small-PADDED-24x24", resource.getPaddedIcon(new Dimension(24, 24)));
     219    }
     220
     221    private static void testImage(int width, int height, String reference, ImageIcon icon) throws IOException {
     222        final BufferedImage image = (BufferedImage) icon.getImage();
     223        final File referenceFile = new File(
     224                TestUtils.getTestDataRoot() + "/" + ImageProviderTest.class.getSimpleName() + "/" + reference + ".png");
     225        assertEquals("width", width, image.getWidth(null));
     226        assertEquals("height", height, image.getHeight(null));
     227        assertImageEquals(reference, referenceFile, image, 0, 0, null);
     228    }
     229
     230    /**
    182231     * Test getting a bounded icon given some UI scaling configured.
    183232     */
Note: See TracChangeset for help on using the changeset viewer.