1 18 package org.apache.batik.test.svg; 19 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.FileWriter ; 23 import java.io.StringReader ; 24 import java.net.URL ; 25 26 import org.apache.batik.test.AbstractTest; 27 import org.apache.batik.test.DefaultTestSuite; 28 import org.apache.batik.test.Test; 29 import org.apache.batik.test.TestReport; 30 import org.apache.batik.test.TestReportValidator; 31 import org.apache.batik.transcoder.TranscoderInput; 32 import org.apache.batik.transcoder.TranscoderOutput; 33 import org.apache.batik.transcoder.image.ImageTranscoder; 34 import org.apache.batik.transcoder.image.PNGTranscoder; 35 36 44 public class SVGRenderingAccuracyTestValidator extends DefaultTestSuite { 45 48 private static final String validSVG 49 = "<svg width=\"450\" height=\"500\" viewBox=\"0 0 450 500\"> \n" + 50 " <rect x=\"25\" y=\"25\" width=\"400\" height=\"450\" fill=\"blue\" /> \n" + 51 "</svg>\n"; 52 53 56 private static final String validSVGVariation 57 = "<svg width=\"450\" height=\"500\" viewBox=\"0 0 450 500\"> \n" + 58 " <rect x=\"25\" y=\"25\" width=\"400\" height=\"450\" fill=\"#0000cc\" /> \n" + 59 "</svg>\n"; 60 61 64 private static final String validSmallSVG 65 = "<svg width=\"45\" height=\"50\" viewBox=\"0 0 45 50\"> \n" + 66 " <rect x=\"2.5\" y=\"2.5\" width=\"40\" height=\"45\" fill=\"blue\" /> \n" + 67 "</svg>\n"; 68 69 72 private static final String validRedSVG 73 = "<svg width=\"450\" height=\"500\" viewBox=\"0 0 450 500\"> \n" + 74 " <rect x=\"25\" y=\"25\" width=\"400\" height=\"450\" fill=\"red\" /> \n" + 75 "</svg>\n"; 76 77 82 private static final String invalidSVG 83 = "<svg width=\"450 height=\"500\" viewBox=\"0 0 450 500\"> \n" + 84 " <rect x=\"25\" y=\"25\" width=\"400\" height=\"450\" fill=\"blue\" /> \n" + 85 "</svg>\n"; 86 87 105 public SVGRenderingAccuracyTestValidator(){ 106 addTest(new InvalidSVGURL()); 107 addTest(new InvalidSVGContent()); 108 addTest(new InvalidReferenceImageURL()); 109 addTest(new InexistingReferenceImage()); 110 addTest(new DifferentSizes()); 111 addTest(new SameSizeDifferentContent()); 112 addTest(new AccurateRendering()); 113 addTest(new AccurateRenderingWithVariation()); 114 addTest(new DefaultConfigTest()); 115 } 116 117 120 public static URL createSVGSourceURL(String svgContent) throws Exception { 121 File tmpFile = File.createTempFile(SVGRenderingAccuracyTest.TEMP_FILE_PREFIX, 122 null); 123 FileWriter writer = new FileWriter (tmpFile); 124 writer.write(svgContent); 125 writer.close(); 126 return tmpFile.toURL(); 127 } 128 129 132 public static URL createValidReferenceImage(String svgContent) throws Exception { 133 TranscoderInput validSrc = new TranscoderInput(new StringReader (svgContent)); 134 135 File tmpFile = File.createTempFile(SVGRenderingAccuracyTest.TEMP_FILE_PREFIX, 136 SVGRenderingAccuracyTest.TEMP_FILE_SUFFIX); 137 138 TranscoderOutput validDst 139 = new TranscoderOutput(new FileOutputStream (tmpFile)); 140 141 ImageTranscoder transcoder 142 = new PNGTranscoder(); 143 144 transcoder.transcode(validSrc, validDst); 145 146 tmpFile.deleteOnExit(); 147 148 return tmpFile.toURL(); 149 } 150 151 152 156 static class DefaultConfigTest extends AbstractTest { 157 String svgURL = "samples/anne.svg"; 158 String expectedRefImgURL = "test-references/samples/anne.png"; 159 String expectedVariationURL = "test-references/samples/accepted-variation/anne.png"; 160 String expectedCandidateURL = "test-references/samples/candidate-variation/anne.png"; 161 162 String ERROR_EXCEPTION_WHILE_BUILDING_TEST 163 = "error.exception.while.building.test"; 164 165 String ERROR_UNEXPECTED_REFERENCE_IMAGE_URL 166 = "error.unexpected.reference.image.url"; 167 168 String ERROR_UNEXPECTED_VARIATION_URL 169 = "error.unexpected.variation.url"; 170 171 String ERROR_UNEXPECTED_CANDIDATE_URL 172 = "error.unexpected.candidate.url"; 173 174 String ENTRY_KEY_EXPECTED_VALUE 175 = "entry.key.expected.value"; 176 177 String ENTRY_KEY_FOUND_VALUE 178 = "entry.key.found.value"; 179 180 public DefaultConfigTest(){ 181 super(); 182 setId("defaultTest"); 183 } 184 185 public TestReport runImpl() throws Exception { 186 SVGRenderingAccuracyTest t 187 = new SamplesRenderingTest(); 188 189 t.setId(svgURL); 190 191 if(!t.refImgURL.toString().endsWith(expectedRefImgURL)){ 192 TestReport r = reportError(ERROR_UNEXPECTED_REFERENCE_IMAGE_URL); 193 r.addDescriptionEntry(ENTRY_KEY_EXPECTED_VALUE, expectedRefImgURL); 194 r.addDescriptionEntry(ENTRY_KEY_FOUND_VALUE, "" + t.refImgURL); 195 return r; 196 } 197 198 if(!t.variationURL.toString().endsWith(expectedVariationURL)){ 199 TestReport r = reportError(ERROR_UNEXPECTED_VARIATION_URL); 200 r.addDescriptionEntry(ENTRY_KEY_EXPECTED_VALUE, expectedVariationURL); 201 r.addDescriptionEntry(ENTRY_KEY_FOUND_VALUE, "" + t.variationURL); 202 return r; 203 } 204 205 if(!t.saveVariation.toURL().toString().endsWith(expectedCandidateURL)){ 206 TestReport r = reportError(ERROR_UNEXPECTED_CANDIDATE_URL); 207 r.addDescriptionEntry(ENTRY_KEY_EXPECTED_VALUE, expectedCandidateURL); 208 r.addDescriptionEntry(ENTRY_KEY_FOUND_VALUE, "" + t.saveVariation.toURL().toString()); 209 return r; 210 } 211 212 return reportSuccess(); 213 } 214 } 215 216 217 218 223 static class InvalidSVGURL extends TestReportValidator { 224 public TestReport runImpl() throws Exception { 225 URL invalidSVGURL = new URL ("http", 227 "dummyHost", 228 "dummyFile.svg"); 229 230 URL refImgURL = createValidReferenceImage(validSVG); 232 233 Test t = new SVGRenderingAccuracyTest(invalidSVGURL.toString(), 234 refImgURL.toString()); 235 236 setConfig(t, 237 false, 238 SVGRenderingAccuracyTest.ERROR_CANNOT_TRANSCODE_SVG); 239 240 return super.runImpl(); 241 } 242 } 243 244 249 static class InvalidSVGContent extends TestReportValidator { 250 public TestReport runImpl() throws Exception { 251 URL validSVGURL = createSVGSourceURL(invalidSVG); 253 254 URL refImgURL = createValidReferenceImage(validSVG); 256 257 Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(), 258 refImgURL.toString()); 259 setConfig(t, 260 false, 261 SVGRenderingAccuracyTest.ERROR_CANNOT_TRANSCODE_SVG); 262 263 return super.runImpl(); 264 } 265 } 266 267 272 static class InvalidReferenceImageURL extends TestReportValidator { 273 public TestReport runImpl() throws Exception { 274 URL validSVGURL = createSVGSourceURL(validSVG); 276 277 URL invalidReferenceImageURL = null; 279 280 invalidReferenceImageURL = new URL ("http", 281 "dummyHost", 282 "dummyFile.png"); 283 Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(), 284 invalidReferenceImageURL.toString()); 285 286 setConfig(t, 287 false, 288 SVGRenderingAccuracyTest.ERROR_CANNOT_OPEN_REFERENCE_IMAGE); 289 290 return super.runImpl(); 291 } 292 } 293 294 300 static class InexistingReferenceImage extends TestReportValidator { 301 public TestReport runImpl() throws Exception { 302 URL validSVGURL = createSVGSourceURL(validSVG); 304 305 File tmpFile = File.createTempFile(SVGRenderingAccuracyTest.TEMP_FILE_PREFIX, 309 null); 310 URL refImgURL = tmpFile.toURL(); 311 tmpFile.delete(); 312 313 Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(), 314 refImgURL.toString()); 315 316 setConfig(t, 317 false, 318 SVGRenderingAccuracyTest.ERROR_CANNOT_OPEN_REFERENCE_IMAGE); 319 320 return super.runImpl(); 321 } 322 } 323 324 static class DifferentSizes extends TestReportValidator { 325 public TestReport runImpl() throws Exception { 326 URL validSVGURL = createSVGSourceURL(validSVG); 330 331 URL validRefImageURL = createValidReferenceImage(validSmallSVG); 335 336 Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(), 340 validRefImageURL.toString()); 341 342 setConfig(t, 343 false, 344 SVGRenderingAccuracyTest.ERROR_SVG_RENDERING_NOT_ACCURATE); 345 346 return super.runImpl(); 347 } 348 } 349 350 static class SameSizeDifferentContent extends TestReportValidator { 351 public TestReport runImpl() throws Exception { 352 URL validSVGURL = createSVGSourceURL(validSVG); 354 355 URL validRefImageURL = createValidReferenceImage(validRedSVG); 357 358 359 Test t = new SVGRenderingAccuracyTest(validSVGURL.toString(), 360 validRefImageURL.toString()); 361 362 setConfig(t, 363 false, 364 SVGRenderingAccuracyTest.ERROR_SVG_RENDERING_NOT_ACCURATE); 365 366 return super.runImpl(); 367 } 368 } 369 370 static class AccurateRendering extends TestReportValidator { 371 public TestReport runImpl() throws Exception { 372 URL validSVGURL = createSVGSourceURL(validSVG); 374 375 URL validRefImageURL = createValidReferenceImage(validSVG); 377 378 setConfig(new SVGRenderingAccuracyTest(validSVGURL.toString(), 379 validRefImageURL.toString()), 380 true, 381 null); 382 383 return super.runImpl(); 384 } 385 } 386 387 390 static class AccurateRenderingWithVariation extends TestReportValidator { 391 public TestReport runImpl() throws Exception { 392 URL validSVGURL = createSVGSourceURL(validSVG); 394 395 URL validRefImageURL = createValidReferenceImage(validSVGVariation); 397 398 SVGRenderingAccuracyTest t 399 = new SVGRenderingAccuracyTest(validSVGURL.toString(), 400 validRefImageURL.toString()); 401 402 File tmpVariationFile = File.createTempFile(SVGRenderingAccuracyTest.TEMP_FILE_PREFIX, null); 403 404 t.setSaveVariation(tmpVariationFile); 406 407 setConfig(t, 408 false, 409 SVGRenderingAccuracyTest.ERROR_SVG_RENDERING_NOT_ACCURATE); 410 411 super.runImpl(); 412 413 t.setVariationURL(tmpVariationFile.toURL().toString()); 414 t.setSaveVariation(null); 415 416 setConfig(t, 417 true, 418 null); 419 420 return super.runImpl(); 421 } 422 } 423 424 } 425 | Popular Tags |