1 18 package org.apache.batik.test.svg; 19 20 import java.awt.Graphics2D ; 21 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.StringWriter ; 25 import java.io.PrintWriter ; 26 import java.net.URL ; 27 28 import java.util.List ; 29 import java.util.Iterator ; 30 31 import org.apache.batik.test.DefaultTestReport; 32 import org.apache.batik.test.TestReport; 33 34 import org.apache.batik.swing.JSVGCanvasHandler; 35 36 import org.apache.batik.swing.JSVGCanvas; 37 import org.apache.batik.swing.gvt.Overlay; 38 39 import java.awt.image.BufferedImage ; 40 41 49 public class JSVGRenderingAccuracyTest extends SamplesRenderingTest 50 implements JSVGCanvasHandler.Delegate { 51 52 56 public static final String ERROR_SAVE_FAILED = 57 "JSVGRenderingAccuracyTest.message.error.save.failed"; 58 59 public static String fmt(String key, Object []args) { 60 return Messages.formatMessage(key, args); 61 } 62 63 66 public JSVGRenderingAccuracyTest(){ 67 } 68 69 protected URL srcURL; 70 protected FileOutputStream fos; 71 protected TestReport failReport = null; 72 protected boolean done; 73 protected JSVGCanvasHandler handler = null; 74 75 public JSVGCanvasHandler createCanvasHandler() { 76 return new JSVGCanvasHandler(this, this); 77 } 78 79 public TestReport encode(URL srcURL, FileOutputStream fos) { 80 this.srcURL = srcURL; 81 this.fos = fos; 82 83 handler = createCanvasHandler(); 84 done = false; 85 handler.runCanvas(srcURL.toString()); 86 87 handler = null; 88 89 if (failReport != null) return failReport; 90 91 DefaultTestReport report = new DefaultTestReport(this); 92 report.setPassed(true); 93 return report; 94 } 95 96 public void scriptDone() { 97 synchronized (this) { 98 done = true; 99 handler.scriptDone(); 100 } 101 } 102 103 104 public boolean canvasInit(JSVGCanvas canvas) { 105 canvas.setURI(srcURL.toString()); 106 return true; 107 } 108 109 public void canvasLoaded(JSVGCanvas canvas) { 110 } 111 112 public void canvasRendered(JSVGCanvas canvas) { 113 } 114 115 public boolean canvasUpdated(JSVGCanvas canvas) { 116 synchronized (this) { 117 return done; 118 } 119 } 120 121 public void canvasDone(JSVGCanvas canvas) { 122 synchronized (this) { 123 done = true; 124 if (failReport != null) 125 return; 126 127 try { 128 BufferedImage theImage = copyImage(canvas.getOffScreen()); 130 131 List overlays = canvas.getOverlays(); 133 134 Graphics2D g = theImage.createGraphics(); 136 Iterator it = overlays.iterator(); 137 while (it.hasNext()) { 138 ((Overlay)it.next()).paint(g); 139 } 140 141 saveImage(theImage, fos); 142 } catch (IOException ioe) { 143 StringWriter trace = new StringWriter (); 144 ioe.printStackTrace(new PrintWriter (trace)); 145 DefaultTestReport report = new DefaultTestReport(this); 146 report.setErrorCode(ERROR_SAVE_FAILED); 147 report.setDescription(new TestReport.Entry[] { 148 new TestReport.Entry 149 (fmt(ENTRY_KEY_ERROR_DESCRIPTION, null), 150 fmt(ERROR_SAVE_FAILED, 151 new Object []{ srcURL.toString(), 152 trace.toString()})) 153 }); 154 report.setPassed(false); 155 failReport = report; 156 } 157 } 158 } 159 160 public void failure(TestReport report) { 161 synchronized (this) { 162 done = true; 163 failReport = report; 164 } 165 } 166 167 public static BufferedImage copyImage(BufferedImage bi) { 168 BufferedImage ret; 170 ret = new BufferedImage (bi.getWidth(), bi.getHeight(), bi.getType()); 171 bi.copyData(ret.getRaster()); 172 return ret; 173 } 174 } 175 176 | Popular Tags |