1d1ba30feSLiu Zhe /************************************************************** 2d1ba30feSLiu Zhe * 3d1ba30feSLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 4d1ba30feSLiu Zhe * or more contributor license agreements. See the NOTICE file 5d1ba30feSLiu Zhe * distributed with this work for additional information 6d1ba30feSLiu Zhe * regarding copyright ownership. The ASF licenses this file 7d1ba30feSLiu Zhe * to you under the Apache License, Version 2.0 (the 8d1ba30feSLiu Zhe * "License"); you may not use this file except in compliance 9d1ba30feSLiu Zhe * with the License. You may obtain a copy of the License at 10d1ba30feSLiu Zhe * 11d1ba30feSLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 12d1ba30feSLiu Zhe * 13d1ba30feSLiu Zhe * Unless required by applicable law or agreed to in writing, 14d1ba30feSLiu Zhe * software distributed under the License is distributed on an 15d1ba30feSLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16d1ba30feSLiu Zhe * KIND, either express or implied. See the License for the 17d1ba30feSLiu Zhe * specific language governing permissions and limitations 18d1ba30feSLiu Zhe * under the License. 19d1ba30feSLiu Zhe * 20d1ba30feSLiu Zhe *************************************************************/ 21*eba4d44aSLiu Zhe package fvt.uno.sw.field; 22d1ba30feSLiu Zhe 23d1ba30feSLiu Zhe import static org.junit.Assert.*; 24d1ba30feSLiu Zhe 25d1ba30feSLiu Zhe import org.junit.After; 26d1ba30feSLiu Zhe import org.junit.AfterClass; 27d1ba30feSLiu Zhe import org.junit.Before; 28d1ba30feSLiu Zhe import org.junit.BeforeClass; 29d1ba30feSLiu Zhe import org.junit.Test; 30d1ba30feSLiu Zhe 31d1ba30feSLiu Zhe import org.openoffice.test.common.Testspace; 32d1ba30feSLiu Zhe import org.openoffice.test.uno.UnoApp; 33d1ba30feSLiu Zhe 34d1ba30feSLiu Zhe import testlib.uno.SWUtil; 35d1ba30feSLiu Zhe 36d1ba30feSLiu Zhe import com.sun.star.beans.XPropertySet; 37d1ba30feSLiu Zhe import com.sun.star.container.XEnumeration; 38d1ba30feSLiu Zhe import com.sun.star.container.XEnumerationAccess; 39d1ba30feSLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 40d1ba30feSLiu Zhe import com.sun.star.style.NumberingType; 41d1ba30feSLiu Zhe import com.sun.star.text.XTextDocument; 42d1ba30feSLiu Zhe import com.sun.star.text.XTextField; 43d1ba30feSLiu Zhe import com.sun.star.text.XTextFieldsSupplier; 44d1ba30feSLiu Zhe import com.sun.star.uno.UnoRuntime; 45d1ba30feSLiu Zhe 46d1ba30feSLiu Zhe public class PageCountField { 47d1ba30feSLiu Zhe 48d1ba30feSLiu Zhe private static final UnoApp app = new UnoApp(); 49d1ba30feSLiu Zhe private static XTextDocument odtDocument = null; 50d1ba30feSLiu Zhe private static XTextDocument docDocument = null; 51ba41a487SLiu Zhe private static String odtSample = "uno/sw/field/PageCountField.odt"; 52ba41a487SLiu Zhe private static String docSample = "uno/sw/field/PageCountField.doc"; 53d1ba30feSLiu Zhe 54ba41a487SLiu Zhe private static String odtSaveAsDocSample = "uno/sw/field/PageCountFieldNewSave.doc"; 55ba41a487SLiu Zhe private static String docSaveAsODTSample = "uno/sw/field/PageCountFieldNewSave.odt"; 56d1ba30feSLiu Zhe 57d1ba30feSLiu Zhe @Before setUpDocument()58d1ba30feSLiu Zhe public void setUpDocument() throws Exception { 59d1ba30feSLiu Zhe 60d1ba30feSLiu Zhe } 61d1ba30feSLiu Zhe 62d1ba30feSLiu Zhe @After tearDownDocument()63d1ba30feSLiu Zhe public void tearDownDocument() { 64d1ba30feSLiu Zhe 65d1ba30feSLiu Zhe 66d1ba30feSLiu Zhe } 67d1ba30feSLiu Zhe 68d1ba30feSLiu Zhe @BeforeClass setUpConnection()69d1ba30feSLiu Zhe public static void setUpConnection() throws Exception { 70d1ba30feSLiu Zhe app.start(); 71d1ba30feSLiu Zhe } 72d1ba30feSLiu Zhe 73d1ba30feSLiu Zhe @AfterClass tearDownConnection()74d1ba30feSLiu Zhe public static void tearDownConnection() throws InterruptedException, 75d1ba30feSLiu Zhe Exception { 76d1ba30feSLiu Zhe app.close(); 77d1ba30feSLiu Zhe } 78d1ba30feSLiu Zhe 79d1ba30feSLiu Zhe 80d1ba30feSLiu Zhe /** 81d1ba30feSLiu Zhe * 82d1ba30feSLiu Zhe * Test Page count Field Can created and Saved in odt file 83d1ba30feSLiu Zhe * 1.launch a odt document 84d1ba30feSLiu Zhe * 2.Create a page count field at end of this page 85d1ba30feSLiu Zhe * 3.Save and Reopen this document 86d1ba30feSLiu Zhe * 4.Save it as doc format and reload 87d1ba30feSLiu Zhe * @throws Throwable 88d1ba30feSLiu Zhe */ 89d1ba30feSLiu Zhe @Test testPageCountFieldODT()90d1ba30feSLiu Zhe public void testPageCountFieldODT() throws Throwable { 91d1ba30feSLiu Zhe odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app); 92d1ba30feSLiu Zhe createPageCountField(odtDocument); 93d1ba30feSLiu Zhe int PageCount = getPageCount(odtDocument); 94d1ba30feSLiu Zhe assertEquals("Verify page count created in exist odt sample file.", 3, PageCount); 95d1ba30feSLiu Zhe odtDocument = SWUtil.saveAndReload(odtDocument, app); 96d1ba30feSLiu Zhe assertTrue("Test page count field still exist after odt sample file saved", isContainPageCountField(odtDocument)); 97d1ba30feSLiu Zhe PageCount = getPageCount(odtDocument); 98d1ba30feSLiu Zhe assertEquals("Verify page count value still exist after saved.", 3, PageCount); 99d1ba30feSLiu Zhe SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample)); 100d1ba30feSLiu Zhe app.closeDocument(odtDocument); 101d1ba30feSLiu Zhe docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app); 102d1ba30feSLiu Zhe 103d1ba30feSLiu Zhe assertTrue("Test page count field still exist after odt sample file save as doc format", isContainPageCountField(docDocument)); 104d1ba30feSLiu Zhe PageCount = getPageCount(docDocument); 105d1ba30feSLiu Zhe assertEquals("Verify page count value still exist after saved as doc format.", 3, PageCount); 106d1ba30feSLiu Zhe app.closeDocument(docDocument); 107d1ba30feSLiu Zhe } 108d1ba30feSLiu Zhe 109d1ba30feSLiu Zhe /** 110d1ba30feSLiu Zhe * Test Page count Field Can created and Saved in Doc file 111d1ba30feSLiu Zhe * 1.launch a doc document 112d1ba30feSLiu Zhe * 2.Create a page count field at end of this page 113d1ba30feSLiu Zhe * 3.Save and Reopen this document, check page count field 114d1ba30feSLiu Zhe * 3.Save as odt format and reload 115d1ba30feSLiu Zhe * @throws Throwable 116d1ba30feSLiu Zhe */ 117d1ba30feSLiu Zhe @Test testPageCountFieldDOC()118d1ba30feSLiu Zhe public void testPageCountFieldDOC() throws Throwable { 119d1ba30feSLiu Zhe docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app); 120d1ba30feSLiu Zhe createPageCountField(docDocument); 121d1ba30feSLiu Zhe int PageCount = getPageCount(docDocument); 122d1ba30feSLiu Zhe assertEquals("Verify page count created in exist doc sample file.", 4, PageCount); 123d1ba30feSLiu Zhe docDocument = SWUtil.saveAndReload(docDocument, app); 124d1ba30feSLiu Zhe assertTrue("Test page count field still exist after doc sample file saved", isContainPageCountField(docDocument)); 125d1ba30feSLiu Zhe PageCount = getPageCount(docDocument); 126d1ba30feSLiu Zhe assertEquals("Verify page count value still exist after saved.", 4, PageCount); 127d1ba30feSLiu Zhe SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample)); 128d1ba30feSLiu Zhe app.closeDocument(docDocument); 129d1ba30feSLiu Zhe odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app); 130d1ba30feSLiu Zhe 131d1ba30feSLiu Zhe assertTrue("Test page count field still exist after doc sample file save as odt format", isContainPageCountField(odtDocument)); 132d1ba30feSLiu Zhe PageCount = getPageCount(odtDocument); 133d1ba30feSLiu Zhe assertEquals("Verify page count value still exist after saved as doc format.", 4, PageCount); 134d1ba30feSLiu Zhe app.closeDocument(odtDocument); 135d1ba30feSLiu Zhe } 136d1ba30feSLiu Zhe 137d1ba30feSLiu Zhe 138d1ba30feSLiu Zhe 139d1ba30feSLiu Zhe /** 140d1ba30feSLiu Zhe * Create a page count field at start of this document 141d1ba30feSLiu Zhe * @param document 142d1ba30feSLiu Zhe * @throws Exception 143d1ba30feSLiu Zhe */ createPageCountField(XTextDocument document)144d1ba30feSLiu Zhe private void createPageCountField(XTextDocument document) throws Exception { 145d1ba30feSLiu Zhe XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 146d1ba30feSLiu Zhe XTextField PageCountField = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.PageCount")); 147d1ba30feSLiu Zhe 148d1ba30feSLiu Zhe XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, PageCountField); 149d1ba30feSLiu Zhe props.setPropertyValue("NumberingType", NumberingType.ARABIC);//Set page count display as Arabic 150d1ba30feSLiu Zhe 151d1ba30feSLiu Zhe SWUtil.moveCuror2Start(document); 152d1ba30feSLiu Zhe document.getText().insertTextContent(document.getText().getStart(), PageCountField, true); 153d1ba30feSLiu Zhe 154d1ba30feSLiu Zhe 155d1ba30feSLiu Zhe } 156d1ba30feSLiu Zhe /** 157d1ba30feSLiu Zhe * Get the page count by getText 158d1ba30feSLiu Zhe * This page count is at end of this document 159d1ba30feSLiu Zhe * @param document 160d1ba30feSLiu Zhe * @return 161d1ba30feSLiu Zhe */ getPageCount(XTextDocument document)162d1ba30feSLiu Zhe private int getPageCount(XTextDocument document) { 163d1ba30feSLiu Zhe String documentString = document.getText().getString().trim(); 164d1ba30feSLiu Zhe String strNum = String.valueOf(documentString.charAt(0)); 165d1ba30feSLiu Zhe int count = Integer.valueOf(strNum); 166d1ba30feSLiu Zhe return count; 167d1ba30feSLiu Zhe } 168d1ba30feSLiu Zhe 169d1ba30feSLiu Zhe 170d1ba30feSLiu Zhe /** 171d1ba30feSLiu Zhe * Check is contain page count field 172d1ba30feSLiu Zhe * @param document 173d1ba30feSLiu Zhe * @throws Exception 174d1ba30feSLiu Zhe */ isContainPageCountField(XTextDocument document)175d1ba30feSLiu Zhe private boolean isContainPageCountField(XTextDocument document) throws Exception { 176cebb507aSLiu Zhe XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document); 177d1ba30feSLiu Zhe XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields(); 178d1ba30feSLiu Zhe 179d1ba30feSLiu Zhe XEnumeration enumeration = xEnumeratedFields.createEnumeration(); 180d1ba30feSLiu Zhe while (enumeration.hasMoreElements()) { 181d1ba30feSLiu Zhe Object field = enumeration.nextElement(); 182d1ba30feSLiu Zhe 183d1ba30feSLiu Zhe XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field); 184d1ba30feSLiu Zhe short countType = (Short) props.getPropertyValue("NumberingType"); 185d1ba30feSLiu Zhe return countType == NumberingType.ARABIC; 186d1ba30feSLiu Zhe 187d1ba30feSLiu Zhe } 188d1ba30feSLiu Zhe return false; 189d1ba30feSLiu Zhe 190d1ba30feSLiu Zhe } 191d1ba30feSLiu Zhe 192d1ba30feSLiu Zhe } 193