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 package fvt.uno.sw.field; 22 23 import static org.junit.Assert.*; 24 25 import org.junit.After; 26 import org.junit.AfterClass; 27 import org.junit.Before; 28 import org.junit.BeforeClass; 29 import org.junit.Test; 30 31 import org.openoffice.test.common.Testspace; 32 import org.openoffice.test.uno.UnoApp; 33 34 import testlib.uno.SWUtil; 35 36 import com.sun.star.beans.XPropertySet; 37 import com.sun.star.container.XEnumeration; 38 import com.sun.star.container.XEnumerationAccess; 39 import com.sun.star.lang.XMultiServiceFactory; 40 import com.sun.star.style.NumberingType; 41 import com.sun.star.text.XTextDocument; 42 import com.sun.star.text.XTextField; 43 import com.sun.star.text.XTextFieldsSupplier; 44 import com.sun.star.uno.UnoRuntime; 45 46 public class PageCountField { 47 48 private static final UnoApp app = new UnoApp(); 49 private static XTextDocument odtDocument = null; 50 private static XTextDocument docDocument = null; 51 private static String odtSample = "uno/sw/field/PageCountField.odt"; 52 private static String docSample = "uno/sw/field/PageCountField.doc"; 53 54 private static String odtSaveAsDocSample = "uno/sw/field/PageCountFieldNewSave.doc"; 55 private static String docSaveAsODTSample = "uno/sw/field/PageCountFieldNewSave.odt"; 56 57 @Before setUpDocument()58 public void setUpDocument() throws Exception { 59 60 } 61 62 @After tearDownDocument()63 public void tearDownDocument() { 64 65 66 } 67 68 @BeforeClass setUpConnection()69 public static void setUpConnection() throws Exception { 70 app.start(); 71 } 72 73 @AfterClass tearDownConnection()74 public static void tearDownConnection() throws InterruptedException, 75 Exception { 76 app.close(); 77 } 78 79 80 /** 81 * 82 * Test Page count Field Can created and Saved in odt file 83 * 1.launch a odt document 84 * 2.Create a page count field at end of this page 85 * 3.Save and Reopen this document 86 * 4.Save it as doc format and reload 87 * @throws Throwable 88 */ 89 @Test testPageCountFieldODT()90 public void testPageCountFieldODT() throws Throwable { 91 odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app); 92 createPageCountField(odtDocument); 93 int PageCount = getPageCount(odtDocument); 94 assertEquals("Verify page count created in exist odt sample file.", 3, PageCount); 95 odtDocument = SWUtil.saveAndReload(odtDocument, app); 96 assertTrue("Test page count field still exist after odt sample file saved", isContainPageCountField(odtDocument)); 97 PageCount = getPageCount(odtDocument); 98 assertEquals("Verify page count value still exist after saved.", 3, PageCount); 99 SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample)); 100 app.closeDocument(odtDocument); 101 docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app); 102 103 assertTrue("Test page count field still exist after odt sample file save as doc format", isContainPageCountField(docDocument)); 104 PageCount = getPageCount(docDocument); 105 assertEquals("Verify page count value still exist after saved as doc format.", 3, PageCount); 106 app.closeDocument(docDocument); 107 } 108 109 /** 110 * Test Page count Field Can created and Saved in Doc file 111 * 1.launch a doc document 112 * 2.Create a page count field at end of this page 113 * 3.Save and Reopen this document, check page count field 114 * 3.Save as odt format and reload 115 * @throws Throwable 116 */ 117 @Test testPageCountFieldDOC()118 public void testPageCountFieldDOC() throws Throwable { 119 docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app); 120 createPageCountField(docDocument); 121 int PageCount = getPageCount(docDocument); 122 assertEquals("Verify page count created in exist doc sample file.", 4, PageCount); 123 docDocument = SWUtil.saveAndReload(docDocument, app); 124 assertTrue("Test page count field still exist after doc sample file saved", isContainPageCountField(docDocument)); 125 PageCount = getPageCount(docDocument); 126 assertEquals("Verify page count value still exist after saved.", 4, PageCount); 127 SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample)); 128 app.closeDocument(docDocument); 129 odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app); 130 131 assertTrue("Test page count field still exist after doc sample file save as odt format", isContainPageCountField(odtDocument)); 132 PageCount = getPageCount(odtDocument); 133 assertEquals("Verify page count value still exist after saved as doc format.", 4, PageCount); 134 app.closeDocument(odtDocument); 135 } 136 137 138 139 /** 140 * Create a page count field at start of this document 141 * @param document 142 * @throws Exception 143 */ createPageCountField(XTextDocument document)144 private void createPageCountField(XTextDocument document) throws Exception { 145 XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 146 XTextField PageCountField = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.PageCount")); 147 148 XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, PageCountField); 149 props.setPropertyValue("NumberingType", NumberingType.ARABIC);//Set page count display as Arabic 150 151 SWUtil.moveCuror2Start(document); 152 document.getText().insertTextContent(document.getText().getStart(), PageCountField, true); 153 154 155 } 156 /** 157 * Get the page count by getText 158 * This page count is at end of this document 159 * @param document 160 * @return 161 */ getPageCount(XTextDocument document)162 private int getPageCount(XTextDocument document) { 163 String documentString = document.getText().getString().trim(); 164 String strNum = String.valueOf(documentString.charAt(0)); 165 int count = Integer.valueOf(strNum); 166 return count; 167 } 168 169 170 /** 171 * Check is contain page count field 172 * @param document 173 * @throws Exception 174 */ isContainPageCountField(XTextDocument document)175 private boolean isContainPageCountField(XTextDocument document) throws Exception { 176 XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document); 177 XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields(); 178 179 XEnumeration enumeration = xEnumeratedFields.createEnumeration(); 180 while (enumeration.hasMoreElements()) { 181 Object field = enumeration.nextElement(); 182 183 XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field); 184 short countType = (Short) props.getPropertyValue("NumberingType"); 185 return countType == NumberingType.ARABIC; 186 187 } 188 return false; 189 190 } 191 192 } 193