Changeset 12996 in josm for trunk/test/functional


Ignore:
Timestamp:
2017-10-14T20:01:24+02:00 (7 years ago)
Author:
bastiK
Message:

add threshold for MapCSSRendererTest as font rendering is slightly platform dependent

File:
1 edited

Legend:

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

    r12995 r12996  
    55import static org.junit.Assert.fail;
    66
     7import java.awt.Color;
    78import java.awt.GraphicsEnvironment;
    89import java.awt.Point;
     
    8283
    8384                /** Text for nodes */
    84                 new TestConfig("node-text", AREA_DEFAULT).usesFont("DejaVu Sans"),
     85                new TestConfig("node-text", AREA_DEFAULT).usesFont("DejaVu Sans")
     86                        .setThresholdPixels(100).setThresholdTotalColorDiff(100),
    8587
    8688                /** Tests that StyledMapRenderer#drawWay respects width */
     
    103105
    104106                /** Tests area label drawing/placement */
    105                 new TestConfig("area-text", AREA_DEFAULT),
     107                new TestConfig("area-text", AREA_DEFAULT).setThresholdPixels(50).setThresholdTotalColorDiff(50),
    106108
    107109                /** Tests area icon drawing/placement */
     
    117119
    118120                /** Tests text along a way */
    119                 new TestConfig("way-text", AREA_DEFAULT),
     121                new TestConfig("way-text", AREA_DEFAULT).setThresholdPixels(20).setThresholdTotalColorDiff(40),
    120122
    121123                /** Another test for node shapes */
     
    128130                new TestConfig("way-dashes2"),
    129131                /** Tests node text placement */
    130                 new TestConfig("node-text2"),
     132                new TestConfig("node-text2").setThresholdPixels(30).setThresholdTotalColorDiff(50),
    131133                /** Tests relation link selector */
    132134                new TestConfig("relation-linkselector"),
     
    135137
    136138                /** Tests evaluation of expressions */
    137                 new TestConfig("eval").setImageWidth(600)
     139                new TestConfig("eval").setImageWidth(600).setThresholdPixels(100).setThresholdTotalColorDiff(100)
    138140
    139141                ).map(e -> new Object[] {e, e.testDirectory})
     
    204206        StringBuilder differences = new StringBuilder();
    205207        ArrayList<Point> differencePoints = new ArrayList<>();
     208        int colorDiffSum = 0;
    206209
    207210        for (int y = 0; y < reference.getHeight(); y++) {
     
    211214                if (!colorsAreSame(expected, result)) {
    212215                    differencePoints.add(new Point(x, y));
     216                    int colorDiff = colorDiff(new Color(expected, true), new Color(result, true));
    213217                    if (differences.length() < 500) {
    214218                        differences.append("\nDifference at ")
     
    219223                        .append(Integer.toHexString(expected))
    220224                        .append(" but got ")
    221                         .append(Integer.toHexString(result));
     225                        .append(Integer.toHexString(result))
     226                        .append(" (color diff is ")
     227                        .append(colorDiff)
     228                        .append(")");
    222229                    }
     230                    colorDiffSum += colorDiff;
    223231                }
    224232            }
    225233        }
    226234
    227         if (differencePoints.size() > 0) {
     235        if (differencePoints.size() > testConfig.thresholdPixels || colorDiffSum > testConfig.thresholdTotalColorDiff) {
    228236            // You can use this to debug:
    229237            ImageIO.write(image, "png", new File(testConfig.getTestDirectory() + "/test-output.png"));
     
    236244            ImageIO.write(diffImage, "png", new File(testConfig.getTestDirectory() + "/test-differences.png"));
    237245
    238             fail(MessageFormat.format("Images for test {0} differ at {1} points: {2}",
    239                     testConfig.testDirectory, differencePoints.size(), differences.toString()));
     246            if (differencePoints.size() > testConfig.thresholdPixels) {
     247                fail(MessageFormat.format("Images for test {0} differ at {1} points, threshold is {2}: {3}",
     248                        testConfig.testDirectory, differencePoints.size(), testConfig.thresholdPixels, differences.toString()));
     249            } else {
     250                fail(MessageFormat.format("Images for test {0} differ too much in color, value is {1}, permitted threshold is {2}: {3}",
     251                        testConfig.testDirectory, colorDiffSum, testConfig.thresholdTotalColorDiff, differences.toString()));
     252            }
    240253        }
    241254    }
     
    246259            n.setDisabledState(false);
    247260        }
     261    }
     262
     263    private int colorDiff(Color c1, Color c2) {
     264        return Math.abs(c1.getAlpha() - c2.getAlpha()) + Math.abs(c1.getRed() - c2.getRed())
     265                + Math.abs(c1.getGreen() - c2.getGreen()) + Math.abs(c1.getBlue() - c2.getBlue());
    248266    }
    249267
     
    269287        private DataSet ds;
    270288        private int imageWidth = IMAGE_SIZE;
     289        private int thresholdPixels;
     290        private int thresholdTotalColorDiff;
    271291
    272292        TestConfig(String testDirectory, Bounds testArea) {
     
    284304        }
    285305
     306        /**
     307         * Set the number of pixels that can differ.
     308         *
     309         * Needed due to somewhat platform dependent font rendering.
     310         * @param thresholdPixels the number of pixels that can differ
     311         * @return this object, for convenience
     312         */
     313        public TestConfig setThresholdPixels(int thresholdPixels) {
     314            this.thresholdPixels = thresholdPixels;
     315            return this;
     316        }
     317
     318        /**
     319         * Set the threshold for total color difference.
     320         * Every difference in any color component (and alpha) will be added up and must not exceed this threshold.
     321         * Needed due to somewhat platform dependent font rendering.
     322         * @param thresholdTotalColorDiff he threshold for total color difference
     323         * @return this object, for convenience
     324         */
     325        public TestConfig setThresholdTotalColorDiff(int thresholdTotalColorDiff) {
     326            this.thresholdTotalColorDiff = thresholdTotalColorDiff;
     327            return this;
     328        }
     329
    286330        public TestConfig usesFont(String string) {
    287331            this.fonts.add(string);
Note: See TracChangeset for help on using the changeset viewer.