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