KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > TextSelectionTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.batik.gvt;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 import org.w3c.dom.Element JavaDoc;
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 /**
33  * This test validates that the text selection API's work properly.
34  *
35  * @author <a HREF="mailto:deweese@apache.org">Thomas DeWeese</a>
36  * @version $Id: TextSelectionTest.java,v 1.6 2005/04/01 02:28:16 deweese Exp $
37  */

38 public class TextSelectionTest extends JSVGRenderingAccuracyTest {
39
40     /**
41      * Directory for reference files
42      */

43     public static final String JavaDoc REFERENCE_DIR
44         = "test-references/org/apache/batik/gvt/";
45
46     public static final String JavaDoc VARIATION_DIR
47         = "variation/";
48
49     public static final String JavaDoc CANDIDATE_DIR
50         = "candidate/";
51
52
53     /**
54      * Error when unable to load requested SVG file
55      * {0} = file
56      * {1} = exception
57      */

58     public static final String JavaDoc ERROR_READING_SVG
59         = "TextSelectionTest.error.reading.svg";
60
61     /**
62      * Error id doesn't reference an element
63      * {0} = id
64      */

65     public static final String JavaDoc ERROR_BAD_ID
66         = "TextSelectionTest.error.bad.id";
67
68     /**
69      * Error id doesn't reference a text element
70      * {0} = id
71      * {1} = element referenced
72      */

73     public static final String JavaDoc ERROR_ID_NOT_TEXT
74         = "TextSelectionTest.error.id.not.text";
75
76     /**
77      * Error couldn't get selection highlight specified.
78      * {0} = id
79      * {1} = start index
80      * {2} = end index
81      * {3} = exception
82      */

83     public static final String JavaDoc ERROR_GETTING_SELECTION
84         = "TextSelectionTest.error.getting.selection";
85
86     /**
87      * Error when unable to read/open ref URL
88      * {0} = URL
89      * {1} = exception stack trace.
90      */

91     public static final String JavaDoc ERROR_CANNOT_READ_REF_URL
92         = "TextSelectionTest.error.cannot.read.ref.url";
93
94     /**
95      * Result didn't match reference result.
96      * {0} = first byte of mismatch
97      */

98     public static final String JavaDoc ERROR_WRONG_RESULT
99         = "TextSelectionTest.error.wrong.result";
100
101     /**
102      * No Reference or Variation file to compaire with.
103      * {0} = reference url
104      */

105     public static final String JavaDoc ERROR_NO_REFERENCE
106         = "TextSelectionTest.error.no.reference";
107
108
109     public static final String JavaDoc ENTRY_KEY_ERROR_DESCRIPTION
110         = "TextSelectionTest.entry.key.error.description";
111
112     protected String JavaDoc textID = null;
113     protected int start;
114     protected int end;
115
116     public void setId(String JavaDoc id) { this.id = id; }
117
118     /**
119      * Constructor. ref is ignored if action == ROUND.
120      * @param svg The svg file to load
121      * @param id The element to select text from (must be a <text> element)
122      * @param start The first character to select
123      * @param end The last character to select
124      * @param ref The reference file.
125      */

126     public TextSelectionTest(String JavaDoc file, String JavaDoc textID,
127                              Integer JavaDoc start, Integer JavaDoc end) {
128         this.textID = textID;
129         this.start = start.intValue();
130         this.end = end.intValue();
131         super.setFile(file);
132     }
133
134     protected String JavaDoc buildRefImgURL(String JavaDoc svgDir, String JavaDoc svgFile){
135         return getRefImagePrefix() + svgDir + getRefImageSuffix() +
136             svgFile + "-" +textID+ "-" + start + "-" + end +PNG_EXTENSION;
137     }
138
139     public String JavaDoc buildVariationURL(String JavaDoc svgDir, String JavaDoc svgFile){
140         return getVariationPrefix() + svgDir + getVariationSuffix() +
141             svgFile + "-" +textID+ "-" + start + "-" + end +PNG_EXTENSION;
142
143     }
144
145     public String JavaDoc buildSaveVariationFile(String JavaDoc svgDir, String JavaDoc svgFile){
146         return getSaveVariationPrefix() + svgDir + getSaveVariationSuffix() +
147             svgFile + "-" +textID+ "-" + start + "-" + end +PNG_EXTENSION;
148     }
149
150     public String JavaDoc buildCandidateReferenceFile(String JavaDoc svgDir, String JavaDoc svgFile){
151         return getCandidateReferencePrefix() + svgDir + getCandidateReferenceSuffix() +
152             svgFile + "-" +textID+ "-" + start + "-" + end +PNG_EXTENSION;
153     }
154     /**
155      * Returns this Test's name
156      */

157     public String JavaDoc 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 JavaDoc 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 JavaDoc[]{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 JavaDoc[]{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 JavaDoc e) {
204             StringWriter JavaDoc trace = new StringWriter JavaDoc();
205             e.printStackTrace(new PrintWriter JavaDoc(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 JavaDoc[]{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