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 testcase.uno.sw.field;
22 import static org.junit.Assert.assertEquals;
23 
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Ignore;
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.text.XTextDocument;
40 import com.sun.star.text.XTextField;
41 import com.sun.star.text.XTextFieldsSupplier;
42 import com.sun.star.uno.UnoRuntime;
43 
44 
45 public class PageNumberField {
46 
47 	private static final UnoApp app = new UnoApp();
48 	private static XTextDocument odtDocument = null;
49 	private static XTextDocument docDocument = null;
50 	private  static String odtSample = "testcase/uno/sw/field/PageNumberFieldTest.odt";
51 	private static String odtSampleWorking = "";
52 	private static String docSample = "testcase/uno/sw/field/PageNumberFieldTest.doc";
53 	private static String docSampleWorking = "";
54 
55 	private  static String odtSaveAsDocSample = "testcase/uno/sw/field/PageNumberFieldTest_1.doc";
56 	private static String docSaveAsODTSample = "testcase/uno/sw/field/PageNumberFieldTest_1.odt";
57 
58 	@Before
59 	public void setUpDocument() throws Exception {
60 		app.start();
61 	}
62 
63 	@After
64 	public void tearDownDocument() {
65 
66 
67 	}
68 
69 	@BeforeClass
70 	public static void setUpConnection() throws Exception {
71 
72 	}
73 
74 
75 	@AfterClass
76 	public static void tearDownConnection() throws InterruptedException,
77 			Exception {
78 		app.close();
79 	}
80 
81 	/**
82 	 * There is a bug : Bug 120625
83 	 * Test Page Number Field Can created and Saved in odt file
84 	 * 1.launch a odt document
85 	 * 2.Create a page number field at end of this page
86 	 * 3.Save and Reopen this document
87 	 * @throws Throwable
88 	 */
89 	@Test
90 	@Ignore("Bug 120625")
91 	public void testPageNumberFieldODT() throws Throwable {
92 		odtSampleWorking = Testspace.prepareData(odtSample);
93 		odtDocument = SWUtil.openDocument(odtSampleWorking, app);
94 		createPageNumberFiled(odtDocument, 3, "odt");
95 		app.closeDocument(odtDocument);
96 	}
97 
98 	/**
99 	 * Bug 120625
100 	 * Test Page Number Field Can created and Saved in Doc file
101 	 * 1.launch a doc document
102 	 * 2.Create a page number field at end of this page
103 	 * 3.Save and Reopen this document, check page number field
104 	 * @throws Throwable
105 	 */
106 	@Test
107 	@Ignore("Bug 120625")
108 	public void testPageNumberFieldDOC() throws Throwable {
109 		docSampleWorking = Testspace.prepareData(docSample);
110 		docDocument = SWUtil.openDocument(docSampleWorking, app);
111 		createPageNumberFiled(docDocument, 2, "odt");
112 		app.closeDocument(docDocument);
113 	}
114 
115 	/**
116 	 * Test Page Number Field in odt file save to doc format works well
117 	 * 1.Launch the new saved file
118 	 * 2.Check page number filed.
119 	 * @throws Throwable
120 	 */
121 	@Test
122 	public void testSavedDoc2ODTPageNumberField() throws Throwable {
123 		odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app);
124 		getPageNumberField(odtDocument, 2);
125 		app.closeDocument(odtDocument);
126 	}
127 
128 	/**
129 	 * Test Page Number Field in doc file save to odt format works well
130 	 * 1.Launch the new saved file
131 	 * 2.Check page number filed.
132 	 * @throws Throwable
133 	 */
134 	@Test
135 	public void testSavedODT2DOCPageNumberField() throws Throwable {
136 		docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app);
137 		getPageNumberField(docDocument, 2);
138 		app.closeDocument(docDocument);
139 	}
140 
141 	private void createPageNumberFiled(XTextDocument document, int expectNumber, String saveAsFormat) throws Exception {
142 		XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
143 		XTextField  pageNumberFiled = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.PageNumber"));
144 
145 		XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, pageNumberFiled);
146 		props.setPropertyValue("NumberingType", 4);//Set page number display as Arabic
147 
148 
149 		SWUtil.moveCuror2End(document);
150 		document.getText().insertTextContent(document.getText().getEnd(), pageNumberFiled, true);
151 
152 		String documentString = document.getText().getString().trim();
153 		int length = documentString.length();
154 		String strNum = String.valueOf(documentString.charAt(length -1));
155 		int number = Integer.valueOf(strNum);
156 		assertEquals("Test Page Number field can insert correctly", expectNumber, number);
157 		SWUtil.save(document);
158 		if("odt".equals(saveAsFormat)) {
159 			SWUtil.saveAsDoc(document, Testspace.getUrl(odtSaveAsDocSample));
160 
161 
162 		} else if ("doc".equals(saveAsFormat)) {
163 			SWUtil.saveAsDoc(document, Testspace.getUrl(docSaveAsODTSample));
164 
165 		}
166 		app.closeDocument(document);
167 
168 		//Verify after save.
169 		document = SWUtil.openDocument(odtSampleWorking, app);
170 
171 		getPageNumberField(document, expectNumber);
172 
173 
174 	}
175 
176 	private void getPageNumberField(XTextDocument document, int expectNumber) throws Exception {
177 		XTextFieldsSupplier fieldsSupplier = UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
178 		XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields();
179 		XEnumeration enumeration = xEnumeratedFields.createEnumeration();
180 		while (enumeration.hasMoreElements()) {
181 			  	Object field =  enumeration.nextElement();
182 				XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
183 				short numberType = (Short) props.getPropertyValue("NumberingType");
184 				assertEquals("Verify page number field type is Arabic", 4, numberType);
185 
186 		}
187 
188 		String documentString = document.getText().getString().trim();
189 		int length = documentString.length();
190 		String strNum = String.valueOf(documentString.charAt(length -1));
191 		int number = Integer.valueOf(strNum);
192 		assertEquals("Test Page Number field can insert and saved correctly", expectNumber, number);
193 
194 
195 	}
196 }
197