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