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