merge the formfield patch from ooo-build
[ooovba.git] / sw / qa / complex / writer / CheckCrossReferences.java
blob7b469c0a4afc1d64354fad5b2647630eeaea9846
1 /*
2 * CheckCrossReferences.java
4 * Created on November 1, 2007, 1:49 PM
6 * To change this template, choose Tools | Template Manager
7 * and open the template in the editor.
8 */
10 package complex.writer;
12 import complexlib.ComplexTestCase;
13 import com.sun.star.lang.XMultiServiceFactory;
14 import com.sun.star.uno.UnoRuntime;
16 /**
18 * @author od138299
20 public class CheckCrossReferences extends ComplexTestCase {
22 private com.sun.star.text.XTextDocument xDoc;
23 private com.sun.star.container.XEnumeration xParaEnum;
24 private com.sun.star.container.XEnumeration xPortionEnum;
25 private com.sun.star.util.XRefreshable xFldsRefresh;
27 public String[] getTestMethodNames() {
28 return new String[]{"checkCrossReferences"};
31 public com.sun.star.text.XTextField getNextField() {
33 com.sun.star.text.XTextField xField = null;
34 if ( xPortionEnum != null ) {
35 try {
36 while ( xPortionEnum.hasMoreElements() ) {
37 com.sun.star.beans.XPropertySet xPortionProps =
38 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
39 com.sun.star.beans.XPropertySet.class , xPortionEnum.nextElement());
40 final String sPortionType =
41 xPortionProps.getPropertyValue( "TextPortionType" ).toString();
42 if ( sPortionType.equals( "TextField") ) {
43 xField = (com.sun.star.text.XTextField)UnoRuntime.queryInterface(
44 com.sun.star.text.XTextField.class,
45 xPortionProps.getPropertyValue( "TextField" ) );
46 if ( xField == null ) {
47 System.out.println("Cannot retrieve next field.");
48 failed("Cannot retrieve next field.");
49 return null;
51 return xField;
54 } catch (com.sun.star.container.NoSuchElementException e) {
55 System.out.println("Cannot retrieve next field.");
56 e.printStackTrace();
57 failed(e.getMessage());
58 return null;
59 } catch (com.sun.star.beans.UnknownPropertyException e) {
60 System.out.println("Cannot retrieve next field.");
61 e.printStackTrace();
62 failed(e.getMessage());
63 return null;
64 } catch (com.sun.star.lang.WrappedTargetException e) {
65 System.out.println("Cannot retrieve next field.");
66 e.printStackTrace();
67 failed(e.getMessage());
68 return null;
72 while ( xParaEnum.hasMoreElements() ) {
73 try {
74 com.sun.star.container.XEnumerationAccess aPara =
75 (com.sun.star.container.XEnumerationAccess)UnoRuntime.queryInterface(
76 com.sun.star.container.XEnumerationAccess.class, xParaEnum.nextElement());
77 xPortionEnum = aPara.createEnumeration();
78 while ( xPortionEnum.hasMoreElements() ) {
79 com.sun.star.beans.XPropertySet xPortionProps =
80 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
81 com.sun.star.beans.XPropertySet.class , xPortionEnum.nextElement());
82 final String sPortionType =
83 xPortionProps.getPropertyValue( "TextPortionType" ).toString();
84 if ( sPortionType.equals( "TextField") ) {
85 xField = (com.sun.star.text.XTextField)UnoRuntime.queryInterface(
86 com.sun.star.text.XTextField.class,
87 xPortionProps.getPropertyValue( "TextField" ) );
88 if ( xField == null ) {
89 System.out.println("Cannot retrieve next field.");
90 failed("Cannot retrieve next field.");
91 return null;
93 return xField;
96 } catch (com.sun.star.container.NoSuchElementException e) {
97 System.out.println("Cannot retrieve next field.");
98 e.printStackTrace();
99 failed(e.getMessage());
100 return null;
101 } catch (com.sun.star.beans.UnknownPropertyException e) {
102 System.out.println("Cannot retrieve next field.");
103 e.printStackTrace();
104 failed(e.getMessage());
105 return null;
106 } catch (com.sun.star.lang.WrappedTargetException e) {
107 System.out.println("Cannot retrieve next field.");
108 e.printStackTrace();
109 failed(e.getMessage());
110 return null;
114 if ( xField == null ) {
115 System.out.println("Cannot retrieve next field.");
116 failed("Cannot retrieve next field.");
117 return null;
120 return xField;
123 public com.sun.star.beans.XPropertySet getFieldProps(
124 com.sun.star.text.XTextField xField ) {
125 com.sun.star.beans.XPropertySet xProps =
126 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
127 com.sun.star.beans.XPropertySet.class, xField );
129 if ( xProps == null ) {
130 System.out.println("Cannot retrieve field properties.");
131 failed("Cannot retrieve field properties.");
132 return null;
135 return xProps;
138 public void checkField( com.sun.star.text.XTextField xField,
139 com.sun.star.beans.XPropertySet xProps,
140 short nFormat,
141 String aExpectedFldResult ) {
142 // set requested format
143 try {
144 xProps.setPropertyValue("ReferenceFieldPart", new Short(nFormat));
145 } catch (com.sun.star.lang.IllegalArgumentException e) {
146 System.out.println("Cannot set ReferenceFieldPart property at field.");
147 e.printStackTrace();
148 failed(e.getMessage());
149 return;
150 } catch (com.sun.star.beans.PropertyVetoException e) {
151 System.out.println("Cannot set ReferenceFieldPart property at field.");
152 e.printStackTrace();
153 failed(e.getMessage());
154 return;
155 } catch (com.sun.star.lang.WrappedTargetException e) {
156 System.out.println("Cannot set ReferenceFieldPart property at field.");
157 e.printStackTrace();
158 failed(e.getMessage());
159 return;
160 } catch (com.sun.star.beans.UnknownPropertyException e) {
161 System.out.println("Cannot set ReferenceFieldPart property at field.");
162 e.printStackTrace();
163 failed(e.getMessage());
164 return;
166 // refresh fields in order to get new format applied
167 xFldsRefresh.refresh();
169 String aFldResult = xField.getPresentation( false );
170 assure( "set reference field format doesn't result in correct field result",
171 aFldResult.equals(aExpectedFldResult), true );
174 public void checkCrossReferences() throws com.sun.star.uno.Exception {
175 // load test document
176 try {
177 XMultiServiceFactory xMSF = (XMultiServiceFactory)param.getMSF();
178 xDoc = util.WriterTools.loadTextDoc( xMSF, util.utils.getFullTestURL("CheckCrossReferences.odt"));
179 } catch(com.sun.star.uno.RuntimeException e) {
180 System.out.println("Cannot load test document.");
181 e.printStackTrace();
182 failed(e.getMessage());
183 return;
186 // setup paragraph enumeration
188 com.sun.star.container.XEnumerationAccess xParaEnumAccess =
189 (com.sun.star.container.XEnumerationAccess)UnoRuntime.queryInterface(
190 com.sun.star.container.XEnumerationAccess.class, xDoc.getText());
191 xParaEnum = xParaEnumAccess.createEnumeration();
194 // get field refresher
196 com.sun.star.text.XTextFieldsSupplier xFieldSupp =
197 (com.sun.star.text.XTextFieldsSupplier)UnoRuntime.queryInterface(
198 com.sun.star.text.XTextFieldsSupplier.class, xDoc);
199 xFldsRefresh = (com.sun.star.util.XRefreshable)UnoRuntime.queryInterface(
200 com.sun.star.util.XRefreshable.class, xFieldSupp.getTextFields());
203 // check first reference field
205 // strings for checking
206 final String FldResult1 = "*i*";
207 final String FldResult2 = "+b+*i*";
208 final String FldResult3 = "-1-+b+*i*";
209 final String FldResult4 = "1.";
210 final String FldResult5 = " 1.";
211 final String FldResult6 = "A. 1.";
213 // variables for current field
214 com.sun.star.text.XTextField xField = null;
215 com.sun.star.beans.XPropertySet xProps = null;
217 log.println( "Checking field reference formats NUMBER, NUMBER_NO_CONTEXT and NUMBER_FULL_CONTEXT for existing fields" );
218 xField = getNextField();
219 xProps = getFieldProps( xField );
220 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult2 );
221 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1 );
222 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3 );
224 xField = getNextField();
225 xProps = getFieldProps( xField );
226 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult1 );
227 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1 );
228 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3 );
230 xField = getNextField();
231 xProps = getFieldProps( xField );
232 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult3 );
233 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1 );
234 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3 );
236 xField = getNextField();
237 xProps = getFieldProps( xField );
238 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult5 );
239 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4 );
240 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6 );
242 xField = getNextField();
243 xProps = getFieldProps( xField );
244 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult4 );
245 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4 );
246 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6 );
248 xField = getNextField();
249 xProps = getFieldProps( xField );
250 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER, FldResult6 );
251 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4 );
252 checkField( xField, xProps, com.sun.star.text.ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6 );
255 // insert a certain cross-reference bookmark and a reference field to this bookmark
257 log.println("Checking insert of cross-reference bookmark and corresponding reference field");
258 // restart paragraph enumeration
259 com.sun.star.container.XEnumerationAccess xParaEnumAccess =
260 (com.sun.star.container.XEnumerationAccess)UnoRuntime.queryInterface(
261 com.sun.star.container.XEnumerationAccess.class, xDoc.getText());
262 xParaEnum = xParaEnumAccess.createEnumeration();
264 // iterate on the paragraphs to find certain paragraph to insert the bookmark
265 com.sun.star.text.XTextRange xParaTextRange = null;
266 while ( xParaEnum.hasMoreElements() ) {
267 try {
268 xParaTextRange = (com.sun.star.text.XTextRange)UnoRuntime.queryInterface(
269 com.sun.star.text.XTextRange.class, xParaEnum.nextElement());
270 if ( xParaTextRange.getString().equals( "J" ) ) {
271 break;
273 else {
274 xParaTextRange = null;
276 } catch (com.sun.star.container.NoSuchElementException e) {
277 System.out.println("Cannot find paragraph to insert cross-reference bookmark.");
278 e.printStackTrace();
279 failed(e.getMessage());
280 return;
281 } catch (com.sun.star.lang.WrappedTargetException e) {
282 System.out.println("Cannot find paragraph to insert cross-reference bookmark.");
283 e.printStackTrace();
284 failed(e.getMessage());
285 return;
288 if ( xParaTextRange == null ) {
289 System.out.println("Cannot find paragraph to insert cross-reference bookmark.");
290 failed("Cannot find paragraph to insert cross-reference bookmark.");
291 return;
294 // insert bookmark
295 XMultiServiceFactory xFac = (XMultiServiceFactory)UnoRuntime.queryInterface(
296 XMultiServiceFactory.class, xDoc);
297 final String cBookmarkName = "__RefNumPara__47114711";
298 com.sun.star.text.XTextContent xBookmark = null;
299 try {
300 xBookmark = (com.sun.star.text.XTextContent)UnoRuntime.queryInterface(
301 com.sun.star.text.XTextContent.class,
302 xFac.createInstance( "com.sun.star.text.Bookmark" ) );
303 } catch (com.sun.star.lang.IllegalArgumentException e) {
304 System.out.println("Cannot create bookmark.");
305 e.printStackTrace();
306 failed(e.getMessage());
307 return;
308 } catch (com.sun.star.uno.Exception e) {
309 System.out.println("Cannot create bookmark.");
310 e.printStackTrace();
311 failed(e.getMessage());
312 return;
314 if ( xBookmark != null ) {
315 com.sun.star.container.XNamed xName =
316 (com.sun.star.container.XNamed)UnoRuntime.queryInterface(
317 com.sun.star.container.XNamed.class, xBookmark );
318 xName.setName( cBookmarkName );
319 xBookmark.attach(xParaTextRange.getStart());
322 // insert reference field, which references the inserted bookmark
323 com.sun.star.text.XTextContent xNewField = null;
324 try {
325 xNewField = (com.sun.star.text.XTextContent)UnoRuntime.queryInterface(
326 com.sun.star.text.XTextContent.class,
327 xFac.createInstance( "com.sun.star.text.TextField.GetReference" ) );
328 } catch (com.sun.star.lang.IllegalArgumentException e) {
329 System.out.println("Cannot create new field.");
330 e.printStackTrace();
331 failed(e.getMessage());
332 return;
333 } catch (com.sun.star.uno.Exception e) {
334 System.out.println("Cannot create new field.");
335 e.printStackTrace();
336 failed(e.getMessage());
337 return;
339 if ( xNewField != null ) {
340 com.sun.star.beans.XPropertySet xFieldProps =
341 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
342 com.sun.star.beans.XPropertySet.class, xNewField );
343 xFieldProps.setPropertyValue( "ReferenceFieldPart", new Short(com.sun.star.text.ReferenceFieldPart.TEXT) );
344 xFieldProps.setPropertyValue( "ReferenceFieldSource", new Short(com.sun.star.text.ReferenceFieldSource.BOOKMARK) );
345 xFieldProps.setPropertyValue( "SourceName", cBookmarkName );
346 com.sun.star.text.XTextRange xFieldTextRange =
347 (com.sun.star.text.XTextRange)UnoRuntime.queryInterface(
348 com.sun.star.text.XTextRange.class, xParaEnum.nextElement());
349 xNewField.attach(xFieldTextRange.getEnd());
350 xFldsRefresh.refresh();
353 // check inserted reference field
354 com.sun.star.text.XTextField xField =
355 (com.sun.star.text.XTextField)UnoRuntime.queryInterface(
356 com.sun.star.text.XTextField.class, xNewField );
357 assure( "inserted reference field doesn't has correct field result",
358 xField.getPresentation( false ).equals("J"), true );
360 xParaTextRange.getStart().setString( "Hallo new bookmark: " );
361 xFldsRefresh.refresh();
362 assure( "inserted reference field doesn't has correct field result. Instead it's: "+xField.getPresentation( false ),
363 xField.getPresentation( false ).equals("Hallo new bookmark: J"), true );
366 // closing test document
367 util.DesktopTools.closeDoc( xDoc );