xref: /AOO41X/test/testuno/source/fvt/uno/sw/crossreference/CheckCrossReferences.java (revision eba4d44a33e5be0b2528d5a9a6f0dcbf65adaa0d)
1*eba4d44aSLiu Zhe /**************************************************************
2*eba4d44aSLiu Zhe  *
3*eba4d44aSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*eba4d44aSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*eba4d44aSLiu Zhe  * distributed with this work for additional information
6*eba4d44aSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*eba4d44aSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*eba4d44aSLiu Zhe  * "License"); you may not use this file except in compliance
9*eba4d44aSLiu Zhe  * with the License.  You may obtain a copy of the License at
10*eba4d44aSLiu Zhe  *
11*eba4d44aSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*eba4d44aSLiu Zhe  *
13*eba4d44aSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*eba4d44aSLiu Zhe  * software distributed under the License is distributed on an
15*eba4d44aSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*eba4d44aSLiu Zhe  * KIND, either express or implied.  See the License for the
17*eba4d44aSLiu Zhe  * specific language governing permissions and limitations
18*eba4d44aSLiu Zhe  * under the License.
19*eba4d44aSLiu Zhe  *
20*eba4d44aSLiu Zhe  *************************************************************/
21*eba4d44aSLiu Zhe 
22*eba4d44aSLiu Zhe package fvt.uno.sw.crossreference;
23*eba4d44aSLiu Zhe 
24*eba4d44aSLiu Zhe import static org.junit.Assert.assertEquals;
25*eba4d44aSLiu Zhe import static org.junit.Assert.assertNotNull;
26*eba4d44aSLiu Zhe 
27*eba4d44aSLiu Zhe import org.junit.After;
28*eba4d44aSLiu Zhe import org.junit.AfterClass;
29*eba4d44aSLiu Zhe import org.junit.Before;
30*eba4d44aSLiu Zhe import org.junit.Test;
31*eba4d44aSLiu Zhe import org.openoffice.test.common.Testspace;
32*eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
33*eba4d44aSLiu Zhe 
34*eba4d44aSLiu Zhe import com.sun.star.beans.XPropertySet;
35*eba4d44aSLiu Zhe import com.sun.star.container.XEnumeration;
36*eba4d44aSLiu Zhe import com.sun.star.container.XEnumerationAccess;
37*eba4d44aSLiu Zhe import com.sun.star.container.XNamed;
38*eba4d44aSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
39*eba4d44aSLiu Zhe import com.sun.star.text.ReferenceFieldPart;
40*eba4d44aSLiu Zhe import com.sun.star.text.ReferenceFieldSource;
41*eba4d44aSLiu Zhe import com.sun.star.text.XTextContent;
42*eba4d44aSLiu Zhe import com.sun.star.text.XTextDocument;
43*eba4d44aSLiu Zhe import com.sun.star.text.XTextField;
44*eba4d44aSLiu Zhe import com.sun.star.text.XTextFieldsSupplier;
45*eba4d44aSLiu Zhe import com.sun.star.text.XTextRange;
46*eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime;
47*eba4d44aSLiu Zhe import com.sun.star.util.XRefreshable;
48*eba4d44aSLiu Zhe 
49*eba4d44aSLiu Zhe /**
50*eba4d44aSLiu Zhe  *
51*eba4d44aSLiu Zhe  */
52*eba4d44aSLiu Zhe public class CheckCrossReferences {
53*eba4d44aSLiu Zhe 
54*eba4d44aSLiu Zhe 	private XEnumeration xParaEnum;
55*eba4d44aSLiu Zhe 	private XEnumeration xPortionEnum;
56*eba4d44aSLiu Zhe 	private com.sun.star.util.XRefreshable xFldsRefresh;
57*eba4d44aSLiu Zhe 	private final static UnoApp app = new UnoApp();
58*eba4d44aSLiu Zhe 
59*eba4d44aSLiu Zhe 	private XTextDocument document = null;
60*eba4d44aSLiu Zhe 
61*eba4d44aSLiu Zhe 	@Before
setUpDocument()62*eba4d44aSLiu Zhe 	public void setUpDocument() throws Exception {
63*eba4d44aSLiu Zhe 		app.start();
64*eba4d44aSLiu Zhe 		document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.prepareData("uno/sw/CheckCrossReferences.odt")));
65*eba4d44aSLiu Zhe 	}
66*eba4d44aSLiu Zhe 
67*eba4d44aSLiu Zhe 	@After
tearDownDocument()68*eba4d44aSLiu Zhe 	public void tearDownDocument() {
69*eba4d44aSLiu Zhe 		app.closeDocument(document);
70*eba4d44aSLiu Zhe 	}
71*eba4d44aSLiu Zhe 
72*eba4d44aSLiu Zhe 	@AfterClass
tearDownConnection()73*eba4d44aSLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
74*eba4d44aSLiu Zhe 		app.close();
75*eba4d44aSLiu Zhe 	}
76*eba4d44aSLiu Zhe 
getNextField()77*eba4d44aSLiu Zhe 	public XTextField getNextField() throws Exception {
78*eba4d44aSLiu Zhe 		if (xPortionEnum != null) {
79*eba4d44aSLiu Zhe 			while (xPortionEnum.hasMoreElements()) {
80*eba4d44aSLiu Zhe 				XPropertySet xPortionProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xPortionEnum.nextElement());
81*eba4d44aSLiu Zhe 				final String sPortionType = xPortionProps.getPropertyValue("TextPortionType").toString();
82*eba4d44aSLiu Zhe 				if (sPortionType.equals("TextField"))
83*eba4d44aSLiu Zhe 					return (XTextField) UnoRuntime.queryInterface(XTextField.class, xPortionProps.getPropertyValue("TextField"));
84*eba4d44aSLiu Zhe 			}
85*eba4d44aSLiu Zhe 		}
86*eba4d44aSLiu Zhe 
87*eba4d44aSLiu Zhe 		while (xParaEnum.hasMoreElements()) {
88*eba4d44aSLiu Zhe 			XEnumerationAccess aPara = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, xParaEnum.nextElement());
89*eba4d44aSLiu Zhe 			xPortionEnum = aPara.createEnumeration();
90*eba4d44aSLiu Zhe 			while (xPortionEnum.hasMoreElements()) {
91*eba4d44aSLiu Zhe 				XPropertySet xPortionProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xPortionEnum.nextElement());
92*eba4d44aSLiu Zhe 				final String sPortionType = xPortionProps.getPropertyValue("TextPortionType").toString();
93*eba4d44aSLiu Zhe 				if (sPortionType.equals("TextField"))
94*eba4d44aSLiu Zhe 					return (XTextField) UnoRuntime.queryInterface(XTextField.class, xPortionProps.getPropertyValue("TextField"));
95*eba4d44aSLiu Zhe 			}
96*eba4d44aSLiu Zhe 		}
97*eba4d44aSLiu Zhe 
98*eba4d44aSLiu Zhe 		return null;
99*eba4d44aSLiu Zhe 	}
100*eba4d44aSLiu Zhe 
getFieldProps(XTextField xField)101*eba4d44aSLiu Zhe 	public XPropertySet getFieldProps(XTextField xField) {
102*eba4d44aSLiu Zhe 		XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xField);
103*eba4d44aSLiu Zhe 		return xProps;
104*eba4d44aSLiu Zhe 	}
105*eba4d44aSLiu Zhe 
checkField(XTextField xField, XPropertySet xProps, short nFormat, String aExpectedFldResult)106*eba4d44aSLiu Zhe 	public void checkField(XTextField xField, XPropertySet xProps, short nFormat, String aExpectedFldResult) throws Exception {
107*eba4d44aSLiu Zhe 		// set requested format
108*eba4d44aSLiu Zhe 		xProps.setPropertyValue("ReferenceFieldPart", new Short(nFormat));
109*eba4d44aSLiu Zhe 		// refresh fields in order to get new format applied
110*eba4d44aSLiu Zhe 		xFldsRefresh.refresh();
111*eba4d44aSLiu Zhe 		String aFldResult = xField.getPresentation(false);
112*eba4d44aSLiu Zhe 		assertEquals("set reference field format doesn't result in correct field result", aExpectedFldResult, aFldResult);
113*eba4d44aSLiu Zhe 	}
114*eba4d44aSLiu Zhe 
115*eba4d44aSLiu Zhe 	@Test
checkCrossReferences()116*eba4d44aSLiu Zhe 	public void checkCrossReferences() throws Exception {
117*eba4d44aSLiu Zhe 		// setup paragraph enumeration
118*eba4d44aSLiu Zhe 		xParaEnum = ((XEnumerationAccess)UnoRuntime.queryInterface(XEnumerationAccess.class, document.getText())).createEnumeration();
119*eba4d44aSLiu Zhe 
120*eba4d44aSLiu Zhe 		// get field refresher
121*eba4d44aSLiu Zhe 		XTextFieldsSupplier xFieldSupp = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
122*eba4d44aSLiu Zhe 		xFldsRefresh = (XRefreshable) UnoRuntime.queryInterface(XRefreshable.class, xFieldSupp.getTextFields());
123*eba4d44aSLiu Zhe 
124*eba4d44aSLiu Zhe 		// strings for checking
125*eba4d44aSLiu Zhe 		final String FldResult1 = "*i*";
126*eba4d44aSLiu Zhe 		final String FldResult2 = "+b+*i*";
127*eba4d44aSLiu Zhe 		final String FldResult3 = "-1-+b+*i*";
128*eba4d44aSLiu Zhe 		final String FldResult4 = "1.";
129*eba4d44aSLiu Zhe 		final String FldResult5 = " 1.";
130*eba4d44aSLiu Zhe 		final String FldResult6 = "A. 1.";
131*eba4d44aSLiu Zhe 
132*eba4d44aSLiu Zhe 		// variables for current field
133*eba4d44aSLiu Zhe 		XTextField xField = null;
134*eba4d44aSLiu Zhe 		XPropertySet xProps = null;
135*eba4d44aSLiu Zhe 
136*eba4d44aSLiu Zhe 		xField = getNextField();
137*eba4d44aSLiu Zhe 		xProps = getFieldProps(xField);
138*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER, FldResult2);
139*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1);
140*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3);
141*eba4d44aSLiu Zhe 
142*eba4d44aSLiu Zhe 		xField = getNextField();
143*eba4d44aSLiu Zhe 		xProps = getFieldProps(xField);
144*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER, FldResult1);
145*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1);
146*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3);
147*eba4d44aSLiu Zhe 
148*eba4d44aSLiu Zhe 		xField = getNextField();
149*eba4d44aSLiu Zhe 		xProps = getFieldProps(xField);
150*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER, FldResult3);
151*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult1);
152*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult3);
153*eba4d44aSLiu Zhe 
154*eba4d44aSLiu Zhe 		xField = getNextField();
155*eba4d44aSLiu Zhe 		xProps = getFieldProps(xField);
156*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER, FldResult5);
157*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4);
158*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6);
159*eba4d44aSLiu Zhe 
160*eba4d44aSLiu Zhe 		xField = getNextField();
161*eba4d44aSLiu Zhe 		xProps = getFieldProps(xField);
162*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER, FldResult4);
163*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4);
164*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6);
165*eba4d44aSLiu Zhe 
166*eba4d44aSLiu Zhe 		xField = getNextField();
167*eba4d44aSLiu Zhe 		xProps = getFieldProps(xField);
168*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER, FldResult6);
169*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_NO_CONTEXT, FldResult4);
170*eba4d44aSLiu Zhe 		checkField(xField, xProps, ReferenceFieldPart.NUMBER_FULL_CONTEXT, FldResult6);
171*eba4d44aSLiu Zhe 
172*eba4d44aSLiu Zhe 		// insert a certain cross-reference bookmark and a reference field to this bookmark
173*eba4d44aSLiu Zhe 
174*eba4d44aSLiu Zhe 		// restart paragraph enumeration
175*eba4d44aSLiu Zhe 		xParaEnum = ((XEnumerationAccess)UnoRuntime.queryInterface(XEnumerationAccess.class, document.getText())).createEnumeration();
176*eba4d44aSLiu Zhe 
177*eba4d44aSLiu Zhe 		// iterate on the paragraphs to find certain paragraph to insert the bookmark
178*eba4d44aSLiu Zhe 		XTextRange xParaTextRange = null;
179*eba4d44aSLiu Zhe 		while (xParaEnum.hasMoreElements()) {
180*eba4d44aSLiu Zhe 			xParaTextRange = (XTextRange) UnoRuntime.queryInterface(XTextRange.class, xParaEnum.nextElement());
181*eba4d44aSLiu Zhe 			if (xParaTextRange.getString().equals("J")) {
182*eba4d44aSLiu Zhe 				break;
183*eba4d44aSLiu Zhe 			} else {
184*eba4d44aSLiu Zhe 				xParaTextRange = null;
185*eba4d44aSLiu Zhe 			}
186*eba4d44aSLiu Zhe 		}
187*eba4d44aSLiu Zhe 		assertNotNull("Cannot find paragraph to insert cross-reference bookmark.", xParaTextRange);
188*eba4d44aSLiu Zhe 
189*eba4d44aSLiu Zhe 		// insert bookmark
190*eba4d44aSLiu Zhe 		XMultiServiceFactory xFac = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
191*eba4d44aSLiu Zhe 		final String cBookmarkName = "__RefNumPara__47114711";
192*eba4d44aSLiu Zhe 		XTextContent xBookmark = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xFac.createInstance("com.sun.star.text.Bookmark"));
193*eba4d44aSLiu Zhe 
194*eba4d44aSLiu Zhe 		XNamed xName = (XNamed) UnoRuntime.queryInterface(XNamed.class, xBookmark);
195*eba4d44aSLiu Zhe 		xName.setName(cBookmarkName);
196*eba4d44aSLiu Zhe 		xBookmark.attach(xParaTextRange.getStart());
197*eba4d44aSLiu Zhe 
198*eba4d44aSLiu Zhe 		// insert reference field, which references the inserted bookmark
199*eba4d44aSLiu Zhe 		XTextContent xNewField = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xFac.createInstance("com.sun.star.text.textfield.GetReference"));
200*eba4d44aSLiu Zhe 
201*eba4d44aSLiu Zhe 		XPropertySet xFieldProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xNewField);
202*eba4d44aSLiu Zhe 		xFieldProps.setPropertyValue("ReferenceFieldPart", new Short(ReferenceFieldPart.TEXT));
203*eba4d44aSLiu Zhe 		xFieldProps.setPropertyValue("ReferenceFieldSource", new Short(ReferenceFieldSource.BOOKMARK));
204*eba4d44aSLiu Zhe 		xFieldProps.setPropertyValue("SourceName", cBookmarkName);
205*eba4d44aSLiu Zhe 		XTextRange xFieldTextRange = (XTextRange) UnoRuntime.queryInterface(XTextRange.class, xParaEnum.nextElement());
206*eba4d44aSLiu Zhe 		xNewField.attach(xFieldTextRange.getEnd());
207*eba4d44aSLiu Zhe 		xFldsRefresh.refresh();
208*eba4d44aSLiu Zhe 
209*eba4d44aSLiu Zhe 		// check inserted reference field
210*eba4d44aSLiu Zhe 		xField = (XTextField) UnoRuntime.queryInterface(XTextField.class, xNewField);
211*eba4d44aSLiu Zhe 		assertEquals("inserted reference field doesn't has correct field result", "J", xField.getPresentation(false));
212*eba4d44aSLiu Zhe 		xParaTextRange.getStart().setString("Hallo new bookmark: ");
213*eba4d44aSLiu Zhe 		xFldsRefresh.refresh();
214*eba4d44aSLiu Zhe 		assertEquals("inserted reference field doesn't has correct field result", "Hallo new bookmark: J", xField.getPresentation(false));
215*eba4d44aSLiu Zhe 
216*eba4d44aSLiu Zhe 	}
217*eba4d44aSLiu Zhe 
218*eba4d44aSLiu Zhe }
219