1 16 17 package org.apache.batik.gvt; 18 19 import java.io.PrintWriter ; 20 import java.io.StringWriter ; 21 22 import org.w3c.dom.Element ; 23 import org.w3c.dom.svg.SVGTextContentElement; 24 25 import org.apache.batik.swing.JSVGCanvas; 26 import org.apache.batik.swing.JSVGCanvasHandler; 27 import org.apache.batik.test.DefaultTestReport; 28 import org.apache.batik.test.TestReport; 29 import org.apache.batik.test.svg.JSVGRenderingAccuracyTest; 30 31 32 38 public class TextSelectionTest extends JSVGRenderingAccuracyTest { 39 40 43 public static final String REFERENCE_DIR 44 = "test-references/org/apache/batik/gvt/"; 45 46 public static final String VARIATION_DIR 47 = "variation/"; 48 49 public static final String CANDIDATE_DIR 50 = "candidate/"; 51 52 53 58 public static final String ERROR_READING_SVG 59 = "TextSelectionTest.error.reading.svg"; 60 61 65 public static final String ERROR_BAD_ID 66 = "TextSelectionTest.error.bad.id"; 67 68 73 public static final String ERROR_ID_NOT_TEXT 74 = "TextSelectionTest.error.id.not.text"; 75 76 83 public static final String ERROR_GETTING_SELECTION 84 = "TextSelectionTest.error.getting.selection"; 85 86 91 public static final String ERROR_CANNOT_READ_REF_URL 92 = "TextSelectionTest.error.cannot.read.ref.url"; 93 94 98 public static final String ERROR_WRONG_RESULT 99 = "TextSelectionTest.error.wrong.result"; 100 101 105 public static final String ERROR_NO_REFERENCE 106 = "TextSelectionTest.error.no.reference"; 107 108 109 public static final String ENTRY_KEY_ERROR_DESCRIPTION 110 = "TextSelectionTest.entry.key.error.description"; 111 112 protected String textID = null; 113 protected int start; 114 protected int end; 115 116 public void setId(String id) { this.id = id; } 117 118 126 public TextSelectionTest(String file, String textID, 127 Integer start, Integer end) { 128 this.textID = textID; 129 this.start = start.intValue(); 130 this.end = end.intValue(); 131 super.setFile(file); 132 } 133 134 protected String buildRefImgURL(String svgDir, String svgFile){ 135 return getRefImagePrefix() + svgDir + getRefImageSuffix() + 136 svgFile + "-" +textID+ "-" + start + "-" + end +PNG_EXTENSION; 137 } 138 139 public String buildVariationURL(String svgDir, String svgFile){ 140 return getVariationPrefix() + svgDir + getVariationSuffix() + 141 svgFile + "-" +textID+ "-" + start + "-" + end +PNG_EXTENSION; 142 143 } 144 145 public String buildSaveVariationFile(String svgDir, String svgFile){ 146 return getSaveVariationPrefix() + svgDir + getSaveVariationSuffix() + 147 svgFile + "-" +textID+ "-" + start + "-" + end +PNG_EXTENSION; 148 } 149 150 public String buildCandidateReferenceFile(String svgDir, String svgFile){ 151 return getCandidateReferencePrefix() + svgDir + getCandidateReferenceSuffix() + 152 svgFile + "-" +textID+ "-" + start + "-" + end +PNG_EXTENSION; 153 } 154 157 public String getName() { 158 return super.getName() + "#" +textID+ "(" + start + "," + end + ")"; 159 } 160 161 public JSVGCanvasHandler createCanvasHandler() { 162 return new JSVGCanvasHandler(this, this) { 163 public JSVGCanvas createCanvas() { 164 JSVGCanvas ret = new JSVGCanvas(); 165 ret.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC); 166 return ret; 167 } 168 }; 169 } 170 171 public void canvasRendered(JSVGCanvas canvas) { 172 DefaultTestReport report = new DefaultTestReport(this); 173 try { 174 Element e = canvas.getSVGDocument().getElementById(textID); 175 if (e == null) { 176 report.setErrorCode(ERROR_BAD_ID); 177 report.setDescription(new TestReport.Entry[] { 178 new TestReport.Entry 179 (Messages.formatMessage 180 (ENTRY_KEY_ERROR_DESCRIPTION, null), 181 Messages.formatMessage 182 (ERROR_BAD_ID, new String []{textID})) 183 }); 184 report.setPassed(false); 185 failReport = report; 186 return; 187 } 188 if (!(e instanceof SVGTextContentElement)) { 189 report.setErrorCode(ERROR_ID_NOT_TEXT); 190 report.setDescription(new TestReport.Entry[] { 191 new TestReport.Entry 192 (Messages.formatMessage 193 (ENTRY_KEY_ERROR_DESCRIPTION, null), 194 Messages.formatMessage 195 (ERROR_ID_NOT_TEXT, new String []{id, e.toString()})) 196 }); 197 report.setPassed(false); 198 failReport = report; 199 return; 200 } 201 SVGTextContentElement tce = (SVGTextContentElement)e; 202 tce.selectSubString(start, end); 203 } catch(Exception e) { 204 StringWriter trace = new StringWriter (); 205 e.printStackTrace(new PrintWriter (trace)); 206 report.setErrorCode(ERROR_GETTING_SELECTION); 207 report.setDescription(new TestReport.Entry[] { 208 new TestReport.Entry 209 (Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 210 Messages.formatMessage 211 (ERROR_GETTING_SELECTION, 212 new String []{id, ""+start, ""+end, trace.toString()})) 213 }); 214 report.setPassed(false); 215 failReport = report; 216 } 217 finally { 218 scriptDone(); 219 } 220 } 221 } 222 223 | Popular Tags |