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