1970a06e3SLiu Zhe /************************************************************** 2970a06e3SLiu Zhe * 3970a06e3SLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 4970a06e3SLiu Zhe * or more contributor license agreements. See the NOTICE file 5970a06e3SLiu Zhe * distributed with this work for additional information 6970a06e3SLiu Zhe * regarding copyright ownership. The ASF licenses this file 7970a06e3SLiu Zhe * to you under the Apache License, Version 2.0 (the 8970a06e3SLiu Zhe * "License"); you may not use this file except in compliance 9970a06e3SLiu Zhe * with the License. You may obtain a copy of the License at 10970a06e3SLiu Zhe * 11970a06e3SLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 12970a06e3SLiu Zhe * 13970a06e3SLiu Zhe * Unless required by applicable law or agreed to in writing, 14970a06e3SLiu Zhe * software distributed under the License is distributed on an 15970a06e3SLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16970a06e3SLiu Zhe * KIND, either express or implied. See the License for the 17970a06e3SLiu Zhe * specific language governing permissions and limitations 18970a06e3SLiu Zhe * under the License. 19970a06e3SLiu Zhe * 20970a06e3SLiu Zhe *************************************************************/ 21*eba4d44aSLiu Zhe package fvt.uno.sw.field; 22970a06e3SLiu Zhe import static org.junit.Assert.assertEquals; 23759fae2dSLiu Zhe import static org.junit.Assert.assertTrue; 24970a06e3SLiu Zhe 25970a06e3SLiu Zhe import org.junit.After; 26970a06e3SLiu Zhe import org.junit.AfterClass; 27970a06e3SLiu Zhe import org.junit.Before; 28970a06e3SLiu Zhe import org.junit.BeforeClass; 29970a06e3SLiu Zhe import org.junit.Test; 30970a06e3SLiu Zhe import org.openoffice.test.common.Testspace; 31970a06e3SLiu Zhe import org.openoffice.test.uno.UnoApp; 32970a06e3SLiu Zhe 33970a06e3SLiu Zhe import testlib.uno.SWUtil; 34970a06e3SLiu Zhe 35970a06e3SLiu Zhe import com.sun.star.beans.XPropertySet; 36970a06e3SLiu Zhe import com.sun.star.container.XEnumeration; 37970a06e3SLiu Zhe import com.sun.star.container.XEnumerationAccess; 38970a06e3SLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 39759fae2dSLiu Zhe import com.sun.star.style.NumberingType; 40970a06e3SLiu Zhe import com.sun.star.text.XTextDocument; 41970a06e3SLiu Zhe import com.sun.star.text.XTextField; 42970a06e3SLiu Zhe import com.sun.star.text.XTextFieldsSupplier; 43970a06e3SLiu Zhe import com.sun.star.uno.UnoRuntime; 44970a06e3SLiu Zhe 45970a06e3SLiu Zhe 46970a06e3SLiu Zhe public class PageNumberField { 47970a06e3SLiu Zhe 48970a06e3SLiu Zhe private static final UnoApp app = new UnoApp(); 49970a06e3SLiu Zhe private static XTextDocument odtDocument = null; 50970a06e3SLiu Zhe private static XTextDocument docDocument = null; 51ba41a487SLiu Zhe private static String odtSample = "uno/sw/field/PageNumberFieldTest.odt"; 52ba41a487SLiu Zhe private static String docSample = "uno/sw/field/PageNumberFieldTest.doc"; 53970a06e3SLiu Zhe 54ba41a487SLiu Zhe private static String odtSaveAsDocSample = "uno/sw/field/PageNumberFieldTest_1.doc"; 55ba41a487SLiu Zhe private static String docSaveAsODTSample = "uno/sw/field/PageNumberFieldTest_1.odt"; 56970a06e3SLiu Zhe 57970a06e3SLiu Zhe @Before 58970a06e3SLiu Zhe public void setUpDocument() throws Exception { 59759fae2dSLiu Zhe 60970a06e3SLiu Zhe } 61970a06e3SLiu Zhe 62970a06e3SLiu Zhe @After 63970a06e3SLiu Zhe public void tearDownDocument() { 64970a06e3SLiu Zhe 65970a06e3SLiu Zhe 66970a06e3SLiu Zhe } 67970a06e3SLiu Zhe @BeforeClass 68970a06e3SLiu Zhe public static void setUpConnection() throws Exception { 69759fae2dSLiu Zhe app.start(); 70970a06e3SLiu Zhe } 71970a06e3SLiu Zhe 72970a06e3SLiu Zhe 73970a06e3SLiu Zhe @AfterClass 74970a06e3SLiu Zhe public static void tearDownConnection() throws InterruptedException, 75970a06e3SLiu Zhe Exception { 76970a06e3SLiu Zhe app.close(); 77970a06e3SLiu Zhe } 78970a06e3SLiu Zhe /** 79970a06e3SLiu Zhe * There is a bug : Bug 120625 80970a06e3SLiu Zhe * Test Page Number Field Can created and Saved in odt file 81970a06e3SLiu Zhe * 1.launch a odt document 82970a06e3SLiu Zhe * 2.Create a page number field at end of this page 83970a06e3SLiu Zhe * 3.Save and Reopen this document 84759fae2dSLiu Zhe * 4.Save it as doc format and reload 85970a06e3SLiu Zhe * @throws Throwable 86970a06e3SLiu Zhe */ 87970a06e3SLiu Zhe @Test 88d1ba30feSLiu Zhe 89970a06e3SLiu Zhe public void testPageNumberFieldODT() throws Throwable { 90759fae2dSLiu Zhe odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app); 91759fae2dSLiu Zhe createPageNumberFiled(odtDocument); 92759fae2dSLiu Zhe int pageNumber = getPageNumber(odtDocument); 93759fae2dSLiu Zhe assertEquals("Verify page number created in exist odt sample file.", 3, pageNumber); 94759fae2dSLiu Zhe odtDocument = SWUtil.saveAndReload(odtDocument, app); 95759fae2dSLiu Zhe assertTrue("Test page number field still exist after odt sample file saved", isContainPageNumberField(odtDocument)); 96759fae2dSLiu Zhe pageNumber = getPageNumber(odtDocument); 97759fae2dSLiu Zhe assertEquals("Verify page number value still exist after saved.", 3, pageNumber); 98759fae2dSLiu Zhe SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample)); 99970a06e3SLiu Zhe app.closeDocument(odtDocument); 100759fae2dSLiu Zhe docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app); 101759fae2dSLiu Zhe 102759fae2dSLiu Zhe assertTrue("Test page number field still exist after odt sample file save as doc format", isContainPageNumberField(docDocument)); 103759fae2dSLiu Zhe pageNumber = getPageNumber(docDocument); 104759fae2dSLiu Zhe assertEquals("Verify page number value still exist after saved as doc format.", 3, pageNumber); 105759fae2dSLiu Zhe app.closeDocument(docDocument); 106970a06e3SLiu Zhe } 107970a06e3SLiu Zhe 108970a06e3SLiu Zhe /** 109970a06e3SLiu Zhe * Bug 120625 110970a06e3SLiu Zhe * Test Page Number Field Can created and Saved in Doc file 111970a06e3SLiu Zhe * 1.launch a doc document 112970a06e3SLiu Zhe * 2.Create a page number field at end of this page 113970a06e3SLiu Zhe * 3.Save and Reopen this document, check page number field 114759fae2dSLiu Zhe * 3.Save as odt format and reload 115970a06e3SLiu Zhe * @throws Throwable 116970a06e3SLiu Zhe */ 117970a06e3SLiu Zhe @Test 118970a06e3SLiu Zhe public void testPageNumberFieldDOC() throws Throwable { 119759fae2dSLiu Zhe docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app); 120759fae2dSLiu Zhe createPageNumberFiled(docDocument); 121759fae2dSLiu Zhe int pageNumber = getPageNumber(docDocument); 122759fae2dSLiu Zhe assertEquals("Verify page number created in exist doc sample file.", 2, pageNumber); 123759fae2dSLiu Zhe docDocument = SWUtil.saveAndReload(docDocument, app); 124759fae2dSLiu Zhe assertTrue("Test page number field still exist after doc sample file saved", isContainPageNumberField(docDocument)); 125759fae2dSLiu Zhe pageNumber = getPageNumber(docDocument); 126759fae2dSLiu Zhe assertEquals("Verify page number value still exist after saved.", 2, pageNumber); 127759fae2dSLiu Zhe SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample)); 128970a06e3SLiu Zhe app.closeDocument(docDocument); 129970a06e3SLiu Zhe odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app); 130759fae2dSLiu Zhe 131759fae2dSLiu Zhe assertTrue("Test page number field still exist after doc sample file save as odt format", isContainPageNumberField(odtDocument)); 132759fae2dSLiu Zhe pageNumber = getPageNumber(odtDocument); 133759fae2dSLiu Zhe assertEquals("Verify page number value still exist after saved as doc format.", 2, pageNumber); 134970a06e3SLiu Zhe app.closeDocument(odtDocument); 135970a06e3SLiu Zhe } 136970a06e3SLiu Zhe 137759fae2dSLiu Zhe 138759fae2dSLiu Zhe 139970a06e3SLiu Zhe /** 140759fae2dSLiu Zhe * Create a page number field at end of this document 141759fae2dSLiu Zhe * @param document 142759fae2dSLiu Zhe * @throws Exception 143970a06e3SLiu Zhe */ 144759fae2dSLiu Zhe private void createPageNumberFiled(XTextDocument document) throws Exception { 145970a06e3SLiu Zhe XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 146970a06e3SLiu Zhe XTextField pageNumberFiled = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.PageNumber")); 147970a06e3SLiu Zhe 148970a06e3SLiu Zhe XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, pageNumberFiled); 149759fae2dSLiu Zhe props.setPropertyValue("NumberingType", NumberingType.ARABIC);//Set page number display as Arabic 150970a06e3SLiu Zhe 151970a06e3SLiu Zhe SWUtil.moveCuror2End(document); 152970a06e3SLiu Zhe document.getText().insertTextContent(document.getText().getEnd(), pageNumberFiled, true); 153759fae2dSLiu Zhe 154759fae2dSLiu Zhe 155759fae2dSLiu Zhe } 156759fae2dSLiu Zhe /** 157759fae2dSLiu Zhe * Get the page number by getText 158759fae2dSLiu Zhe * This page number is at end of this document 159759fae2dSLiu Zhe * @param document 160759fae2dSLiu Zhe * @return 161759fae2dSLiu Zhe */ 162759fae2dSLiu Zhe private int getPageNumber(XTextDocument document) { 163759fae2dSLiu Zhe try { 164759fae2dSLiu Zhe Thread.sleep(1000); //sleep before get page number field, there is a bug:120625 165759fae2dSLiu Zhe } catch (InterruptedException e) { 166759fae2dSLiu Zhe e.printStackTrace(); 167759fae2dSLiu Zhe } 168970a06e3SLiu Zhe String documentString = document.getText().getString().trim(); 169970a06e3SLiu Zhe int length = documentString.length(); 170970a06e3SLiu Zhe String strNum = String.valueOf(documentString.charAt(length -1)); 171970a06e3SLiu Zhe int number = Integer.valueOf(strNum); 172759fae2dSLiu Zhe return number; 173970a06e3SLiu Zhe } 174759fae2dSLiu Zhe 175970a06e3SLiu Zhe 176759fae2dSLiu Zhe /** 177759fae2dSLiu Zhe * Check is contain page number field 178759fae2dSLiu Zhe * @param document 179759fae2dSLiu Zhe * @throws Exception 180759fae2dSLiu Zhe */ 181759fae2dSLiu Zhe private boolean isContainPageNumberField(XTextDocument document) throws Exception { 182cebb507aSLiu Zhe XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document); 183970a06e3SLiu Zhe XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields(); 184970a06e3SLiu Zhe XEnumeration enumeration = xEnumeratedFields.createEnumeration(); 185970a06e3SLiu Zhe while (enumeration.hasMoreElements()) { 186970a06e3SLiu Zhe Object field = enumeration.nextElement(); 187970a06e3SLiu Zhe XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field); 188970a06e3SLiu Zhe short numberType = (Short) props.getPropertyValue("NumberingType"); 189759fae2dSLiu Zhe return numberType == 4; 190970a06e3SLiu Zhe 191970a06e3SLiu Zhe } 192759fae2dSLiu Zhe return false; 193970a06e3SLiu Zhe 194970a06e3SLiu Zhe } 195970a06e3SLiu Zhe } 196