Changeset 16982 in josm
- Timestamp:
- 2020-08-30T21:05:07+02:00 (4 years ago)
- 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 11 11 import java.io.File; 12 12 import java.io.IOException; 13 import java.io.UncheckedIOException; 13 14 import java.nio.file.Files; 14 15 import java.nio.file.Paths; … … 20 21 import java.util.List; 21 22 import java.util.Locale; 23 import java.util.function.Consumer; 22 24 import java.util.stream.Collectors; 23 25 import java.util.stream.Stream; … … 217 219 BufferedImage image = rh.render(); 218 220 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 219 249 if (UPDATE_ALL) { 220 ImageIO.write(image, "png", new File(testConfig.getTestDirectory() + "/reference.png"));250 ImageIO.write(image, "png", referenceImageFile); 221 251 return; 222 252 } 223 224 BufferedImage reference = testConfig.getReference(); 225 226 // now compute differences: 253 final BufferedImage reference = ImageIO.read(referenceImageFile); 227 254 assertEquals(image.getWidth(), reference.getWidth()); 228 255 assertEquals(image.getHeight(), reference.getHeight()); … … 236 263 int expected = reference.getRGB(x, y); 237 264 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) { 239 268 Color expectedColor = new Color(expected, true); 240 269 Color resultColor = new Color(result, true); … … 274 303 } 275 304 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) { 280 306 // Add a nice image that highlights the differences: 281 307 BufferedImage diffImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); … … 283 309 diffImage.setRGB(p.x, p.y, 0xffff0000); 284 310 } 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) { 288 316 fail(MessageFormat.format("Images for test {0} differ at {1} points, threshold is {2}: {3}", 289 test Config.testDirectory, differencePoints.size(), testConfig.thresholdPixels, differences.toString()));317 testIdentifier, differencePoints.size(), thresholdPixels, differences.toString())); 290 318 } else { 291 319 fail(MessageFormat.format("Images for test {0} differ too much in color, value is {1}, permitted threshold is {2}: {3}", 292 test Config.testDirectory, colorDiffSum, testConfig.thresholdTotalColorDiff, differences.toString()));320 testIdentifier, colorDiffSum, thresholdTotalColorDiff, differences.toString())); 293 321 } 294 322 } … … 299 327 if (n.isKeyTrue("disabled")) { 300 328 n.setDisabledState(false); 301 }302 }303 304 /**305 * Check if two colors differ306 * @param expected The expected color307 * @param actual The actual color308 * @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;316 329 } 317 330 } … … 369 382 } 370 383 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"); 373 386 } 374 387 -
trunk/test/functional/org/openstreetmap/josm/tools/ImageProviderTest.java
r16981 r16982 8 8 import static org.junit.Assert.assertTrue; 9 9 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 10 import static org.openstreetmap.josm.gui.mappaint.MapCSSRendererTest.assertImageEquals; 10 11 11 12 import java.awt.Color; … … 180 181 181 182 /** 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 /** 182 231 * Test getting a bounded icon given some UI scaling configured. 183 232 */
Note:
See TracChangeset
for help on using the changeset viewer.