xref: /AOO41X/test/testuno/source/fvt/uno/sw/field/PageNumberField.java (revision eba4d44a33e5be0b2528d5a9a6f0dcbf65adaa0d)
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 import static org.junit.Assert.assertEquals;
23*eba4d44aSLiu Zhe import static org.junit.Assert.assertTrue;
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 import org.openoffice.test.common.Testspace;
31*eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
32*eba4d44aSLiu Zhe 
33*eba4d44aSLiu Zhe import testlib.uno.SWUtil;
34*eba4d44aSLiu Zhe 
35*eba4d44aSLiu Zhe import com.sun.star.beans.XPropertySet;
36*eba4d44aSLiu Zhe import com.sun.star.container.XEnumeration;
37*eba4d44aSLiu Zhe import com.sun.star.container.XEnumerationAccess;
38*eba4d44aSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
39*eba4d44aSLiu Zhe import com.sun.star.style.NumberingType;
40*eba4d44aSLiu Zhe import com.sun.star.text.XTextDocument;
41*eba4d44aSLiu Zhe import com.sun.star.text.XTextField;
42*eba4d44aSLiu Zhe import com.sun.star.text.XTextFieldsSupplier;
43*eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime;
44*eba4d44aSLiu Zhe 
45*eba4d44aSLiu Zhe 
46*eba4d44aSLiu Zhe public class PageNumberField {
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/PageNumberFieldTest.odt";
52*eba4d44aSLiu Zhe 	private static String docSample = "uno/sw/field/PageNumberFieldTest.doc";
53*eba4d44aSLiu Zhe 
54*eba4d44aSLiu Zhe 	private  static String odtSaveAsDocSample = "uno/sw/field/PageNumberFieldTest_1.doc";
55*eba4d44aSLiu Zhe 	private static String docSaveAsODTSample = "uno/sw/field/PageNumberFieldTest_1.odt";
56*eba4d44aSLiu Zhe 
57*eba4d44aSLiu Zhe 	@Before
58*eba4d44aSLiu Zhe 	public void setUpDocument() throws Exception {
59*eba4d44aSLiu Zhe 
60*eba4d44aSLiu Zhe 	}
61*eba4d44aSLiu Zhe 
62*eba4d44aSLiu Zhe 	@After
63*eba4d44aSLiu Zhe 	public void tearDownDocument() {
64*eba4d44aSLiu Zhe 
65*eba4d44aSLiu Zhe 
66*eba4d44aSLiu Zhe 	}
67*eba4d44aSLiu Zhe 	@BeforeClass
68*eba4d44aSLiu Zhe 	public static void setUpConnection() throws Exception {
69*eba4d44aSLiu Zhe 		app.start();
70*eba4d44aSLiu Zhe 	}
71*eba4d44aSLiu Zhe 
72*eba4d44aSLiu Zhe 
73*eba4d44aSLiu Zhe 	@AfterClass
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 	 * There is a bug : Bug 120625
80*eba4d44aSLiu Zhe 	 * Test Page Number Field Can created and Saved in odt file
81*eba4d44aSLiu Zhe 	 * 1.launch a odt document
82*eba4d44aSLiu Zhe 	 * 2.Create a page number field at end of this page
83*eba4d44aSLiu Zhe 	 * 3.Save and Reopen this document
84*eba4d44aSLiu Zhe 	 * 4.Save it as doc format and reload
85*eba4d44aSLiu Zhe 	 * @throws Throwable
86*eba4d44aSLiu Zhe 	 */
87*eba4d44aSLiu Zhe 	@Test
88*eba4d44aSLiu Zhe 
89*eba4d44aSLiu Zhe 	public void testPageNumberFieldODT() throws Throwable {
90*eba4d44aSLiu Zhe 		odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app);
91*eba4d44aSLiu Zhe 		createPageNumberFiled(odtDocument);
92*eba4d44aSLiu Zhe 		int pageNumber = getPageNumber(odtDocument);
93*eba4d44aSLiu Zhe 		assertEquals("Verify page number created in exist odt sample file.", 3, pageNumber);
94*eba4d44aSLiu Zhe 		odtDocument = SWUtil.saveAndReload(odtDocument, app);
95*eba4d44aSLiu Zhe 		assertTrue("Test page number field still exist after odt sample file saved", isContainPageNumberField(odtDocument));
96*eba4d44aSLiu Zhe 		pageNumber = getPageNumber(odtDocument);
97*eba4d44aSLiu Zhe 		assertEquals("Verify page number value still exist after saved.", 3, pageNumber);
98*eba4d44aSLiu Zhe 		SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample));
99*eba4d44aSLiu Zhe 		app.closeDocument(odtDocument);
100*eba4d44aSLiu Zhe 		docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app);
101*eba4d44aSLiu Zhe 
102*eba4d44aSLiu Zhe 		assertTrue("Test page number field still exist after odt sample file save as doc format", isContainPageNumberField(docDocument));
103*eba4d44aSLiu Zhe 		pageNumber = getPageNumber(docDocument);
104*eba4d44aSLiu Zhe 		assertEquals("Verify page number value still exist after saved as doc format.", 3, pageNumber);
105*eba4d44aSLiu Zhe 		app.closeDocument(docDocument);
106*eba4d44aSLiu Zhe 	}
107*eba4d44aSLiu Zhe 
108*eba4d44aSLiu Zhe 	/**
109*eba4d44aSLiu Zhe 	 * Bug 120625
110*eba4d44aSLiu Zhe 	 * Test Page Number Field Can created and Saved in Doc file
111*eba4d44aSLiu Zhe 	 * 1.launch a doc document
112*eba4d44aSLiu Zhe 	 * 2.Create a page number field at end of this page
113*eba4d44aSLiu Zhe 	 * 3.Save and Reopen this document, check page number field
114*eba4d44aSLiu Zhe 	 * 3.Save as odt format and reload
115*eba4d44aSLiu Zhe 	 * @throws Throwable
116*eba4d44aSLiu Zhe 	 */
117*eba4d44aSLiu Zhe 	@Test
118*eba4d44aSLiu Zhe 	public void testPageNumberFieldDOC() throws Throwable {
119*eba4d44aSLiu Zhe 		docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app);
120*eba4d44aSLiu Zhe 		createPageNumberFiled(docDocument);
121*eba4d44aSLiu Zhe 		int pageNumber = getPageNumber(docDocument);
122*eba4d44aSLiu Zhe 		assertEquals("Verify page number created in exist doc sample file.", 2, pageNumber);
123*eba4d44aSLiu Zhe 		docDocument = SWUtil.saveAndReload(docDocument, app);
124*eba4d44aSLiu Zhe 		assertTrue("Test page number field still exist after doc sample file saved", isContainPageNumberField(docDocument));
125*eba4d44aSLiu Zhe 		pageNumber = getPageNumber(docDocument);
126*eba4d44aSLiu Zhe 		assertEquals("Verify page number value still exist after saved.", 2, pageNumber);
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 number field still exist after doc sample file save as odt format", isContainPageNumberField(odtDocument));
132*eba4d44aSLiu Zhe 		pageNumber = getPageNumber(odtDocument);
133*eba4d44aSLiu Zhe 		assertEquals("Verify page number value still exist after saved as doc format.", 2, pageNumber);
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 number field at end of this document
141*eba4d44aSLiu Zhe 	 * @param document
142*eba4d44aSLiu Zhe 	 * @throws Exception
143*eba4d44aSLiu Zhe 	 */
144*eba4d44aSLiu Zhe 	private void createPageNumberFiled(XTextDocument document) throws Exception {
145*eba4d44aSLiu Zhe 		XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
146*eba4d44aSLiu Zhe 		XTextField  pageNumberFiled = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.PageNumber"));
147*eba4d44aSLiu Zhe 
148*eba4d44aSLiu Zhe 		XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, pageNumberFiled);
149*eba4d44aSLiu Zhe 		props.setPropertyValue("NumberingType", NumberingType.ARABIC);//Set page number display as Arabic
150*eba4d44aSLiu Zhe 
151*eba4d44aSLiu Zhe 		SWUtil.moveCuror2End(document);
152*eba4d44aSLiu Zhe 		document.getText().insertTextContent(document.getText().getEnd(), pageNumberFiled, true);
153*eba4d44aSLiu Zhe 
154*eba4d44aSLiu Zhe 
155*eba4d44aSLiu Zhe 	}
156*eba4d44aSLiu Zhe 	/**
157*eba4d44aSLiu Zhe 	 * Get the page number by getText
158*eba4d44aSLiu Zhe 	 * This page number is at end of this document
159*eba4d44aSLiu Zhe 	 * @param document
160*eba4d44aSLiu Zhe 	 * @return
161*eba4d44aSLiu Zhe 	 */
162*eba4d44aSLiu Zhe 	private int getPageNumber(XTextDocument document) {
163*eba4d44aSLiu Zhe 		try {
164*eba4d44aSLiu Zhe 			Thread.sleep(1000); //sleep before get page number field, there is a bug:120625
165*eba4d44aSLiu Zhe 		} catch (InterruptedException e) {
166*eba4d44aSLiu Zhe 			e.printStackTrace();
167*eba4d44aSLiu Zhe 		}
168*eba4d44aSLiu Zhe 		String documentString = document.getText().getString().trim();
169*eba4d44aSLiu Zhe 		int length = documentString.length();
170*eba4d44aSLiu Zhe 		String strNum = String.valueOf(documentString.charAt(length -1));
171*eba4d44aSLiu Zhe 		int number = Integer.valueOf(strNum);
172*eba4d44aSLiu Zhe 		return number;
173*eba4d44aSLiu Zhe 	}
174*eba4d44aSLiu Zhe 
175*eba4d44aSLiu Zhe 
176*eba4d44aSLiu Zhe 	/**
177*eba4d44aSLiu Zhe 	 * Check is contain page number field
178*eba4d44aSLiu Zhe 	 * @param document
179*eba4d44aSLiu Zhe 	 * @throws Exception
180*eba4d44aSLiu Zhe 	 */
181*eba4d44aSLiu Zhe 	private boolean isContainPageNumberField(XTextDocument document) throws Exception {
182*eba4d44aSLiu Zhe 		XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
183*eba4d44aSLiu Zhe 		XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields();
184*eba4d44aSLiu Zhe 		XEnumeration enumeration = xEnumeratedFields.createEnumeration();
185*eba4d44aSLiu Zhe 		while (enumeration.hasMoreElements()) {
186*eba4d44aSLiu Zhe 			  	Object field =  enumeration.nextElement();
187*eba4d44aSLiu Zhe 				XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
188*eba4d44aSLiu Zhe 				short numberType = (Short) props.getPropertyValue("NumberingType");
189*eba4d44aSLiu Zhe 			    return numberType == 4;
190*eba4d44aSLiu Zhe 
191*eba4d44aSLiu Zhe 		}
192*eba4d44aSLiu Zhe 		return false;
193*eba4d44aSLiu Zhe 
194*eba4d44aSLiu Zhe 	}
195*eba4d44aSLiu Zhe }
196