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