1cc7a2393SLiu Zhe /**************************************************************
2cc7a2393SLiu Zhe  *
3cc7a2393SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4cc7a2393SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5cc7a2393SLiu Zhe  * distributed with this work for additional information
6cc7a2393SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7cc7a2393SLiu Zhe  * to you under the Apache License, Version 2.0 (the
8cc7a2393SLiu Zhe  * "License"); you may not use this file except in compliance
9cc7a2393SLiu Zhe  * with the License.  You may obtain a copy of the License at
10cc7a2393SLiu Zhe  *
11cc7a2393SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12cc7a2393SLiu Zhe  *
13cc7a2393SLiu Zhe  * Unless required by applicable law or agreed to in writing,
14cc7a2393SLiu Zhe  * software distributed under the License is distributed on an
15cc7a2393SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16cc7a2393SLiu Zhe  * KIND, either express or implied.  See the License for the
17cc7a2393SLiu Zhe  * specific language governing permissions and limitations
18cc7a2393SLiu Zhe  * under the License.
19cc7a2393SLiu Zhe  *
20cc7a2393SLiu Zhe  *************************************************************/
21eba4d44aSLiu Zhe package fvt.uno.sw.field;
22cc7a2393SLiu Zhe 
23cc7a2393SLiu Zhe import static org.junit.Assert.assertTrue;
24cc7a2393SLiu Zhe 
25cc7a2393SLiu Zhe import org.junit.After;
26cc7a2393SLiu Zhe import org.junit.AfterClass;
27cc7a2393SLiu Zhe import org.junit.Before;
28cc7a2393SLiu Zhe import org.junit.BeforeClass;
29cc7a2393SLiu Zhe import org.junit.Test;
30cc7a2393SLiu Zhe import org.openoffice.test.common.Testspace;
31cc7a2393SLiu Zhe import org.openoffice.test.uno.UnoApp;
32cc7a2393SLiu Zhe 
33cc7a2393SLiu Zhe import testlib.uno.SWUtil;
34cc7a2393SLiu Zhe 
35cc7a2393SLiu Zhe import com.sun.star.beans.XPropertySet;
36cc7a2393SLiu Zhe import com.sun.star.container.XEnumeration;
37cc7a2393SLiu Zhe import com.sun.star.container.XEnumerationAccess;
38cc7a2393SLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
39cc7a2393SLiu Zhe import com.sun.star.text.XTextDocument;
40cc7a2393SLiu Zhe import com.sun.star.text.XTextField;
41cc7a2393SLiu Zhe import com.sun.star.text.XTextFieldsSupplier;
42cc7a2393SLiu Zhe import com.sun.star.uno.UnoRuntime;
43cc7a2393SLiu Zhe public class SubjectField {
44cc7a2393SLiu Zhe 
45cc7a2393SLiu Zhe 	private static final UnoApp app = new UnoApp();
46cc7a2393SLiu Zhe 	private static XTextDocument odtDocument = null;
47cc7a2393SLiu Zhe 	private static XTextDocument docDocument = null;
48ba41a487SLiu Zhe 	private  static String odtSample = "uno/sw/field/SubjectField.odt";
49ba41a487SLiu Zhe 	private static String docSample = "uno/sw/field/Test_Sample.doc";
50cc7a2393SLiu Zhe 
51ba41a487SLiu Zhe 	private  static String odtSaveAsDocSample = "uno/sw/field/odtSaveAsDOC.doc";
52ba41a487SLiu Zhe 	private static String docSaveAsODTSample = "uno/sw/field/docSaveAsODT.odt";
53cc7a2393SLiu Zhe 
54cc7a2393SLiu Zhe 	@Before
setUpDocument()55cc7a2393SLiu Zhe 	public void setUpDocument() throws Exception {
56cc7a2393SLiu Zhe 
57cc7a2393SLiu Zhe 	}
58cc7a2393SLiu Zhe 
59cc7a2393SLiu Zhe 	@After
tearDownDocument()60cc7a2393SLiu Zhe 	public void tearDownDocument() {
61cc7a2393SLiu Zhe 
62cc7a2393SLiu Zhe 
63cc7a2393SLiu Zhe 	}
64cc7a2393SLiu Zhe 
65cc7a2393SLiu Zhe 	@BeforeClass
setUpConnection()66cc7a2393SLiu Zhe 	public static void setUpConnection() throws Exception {
67cc7a2393SLiu Zhe 		app.start();
68cc7a2393SLiu Zhe 	}
69cc7a2393SLiu Zhe 
70cc7a2393SLiu Zhe 	@AfterClass
tearDownConnection()71cc7a2393SLiu Zhe 	public static void tearDownConnection() throws InterruptedException,
72cc7a2393SLiu Zhe 			Exception {
73cc7a2393SLiu Zhe 		app.close();
74cc7a2393SLiu Zhe 	}
75cc7a2393SLiu Zhe 
76cc7a2393SLiu Zhe 	/**
77cc7a2393SLiu Zhe 	 *
78cc7a2393SLiu Zhe 	 * Test Subject Field Can created and Saved in odt file
79cc7a2393SLiu Zhe 	 * 1.launch a odt document
80cc7a2393SLiu Zhe 	 * 2.Create a page count field at end of this page
81cc7a2393SLiu Zhe 	 * 3.Save and Reopen this document
82cc7a2393SLiu Zhe 	 * 4.Save it as doc format and reload
83cc7a2393SLiu Zhe 	 * @throws Throwable
84cc7a2393SLiu Zhe 	 */
85cc7a2393SLiu Zhe 	@Test
testSubjectFieldODT()86cc7a2393SLiu Zhe 	public void testSubjectFieldODT() throws Throwable {
87cc7a2393SLiu Zhe 		odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app);
88cc7a2393SLiu Zhe 
89cc7a2393SLiu Zhe 
90cc7a2393SLiu Zhe 		String subjectStr = "Test Subject Field";
91cc7a2393SLiu Zhe 		createSubjectFiled(odtDocument);
92cc7a2393SLiu Zhe 
93*d271d0d8SJohn Bampton 		assertTrue("Verify Subject field is created use exist subject", isContainSubjectField(odtDocument, "UNO API Subject"));
94*d271d0d8SJohn Bampton 		assertTrue("Verify Subject field is created use exist subject, can get text from document",
95cc7a2393SLiu Zhe 				isContainSubjectText(odtDocument, "UNO API Subject"));
96cc7a2393SLiu Zhe 		//Set subject
97cc7a2393SLiu Zhe 		SWUtil.setDocumentProperty(odtDocument, "Subject", subjectStr);
98*d271d0d8SJohn Bampton 		//Verify after set subject
99cc7a2393SLiu Zhe 		assertTrue("Verify Subject field is created, can get text from document",
100cc7a2393SLiu Zhe 				isContainSubjectText(odtDocument, subjectStr));
101cc7a2393SLiu Zhe 
102cc7a2393SLiu Zhe 		odtDocument = SWUtil.saveAndReload(odtDocument, app);
103cc7a2393SLiu Zhe 		//verify after save and reload
104cc7a2393SLiu Zhe 		assertTrue("Verify Subject field is created, after save, still exist.", isContainSubjectField(odtDocument, subjectStr));
105cc7a2393SLiu Zhe 		assertTrue("Verify Subject field is created, can get text from document after saved.",
106cc7a2393SLiu Zhe 				isContainSubjectText(odtDocument, subjectStr));
107cc7a2393SLiu Zhe 		SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample));
108cc7a2393SLiu Zhe 		app.closeDocument(odtDocument);
109cc7a2393SLiu Zhe 		docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app);
110cc7a2393SLiu Zhe 		assertTrue("Verify Subject field is created, after saved to doc format, field still exist.", isContainSubjectField(docDocument, subjectStr));
111cc7a2393SLiu Zhe 		assertTrue("Verify Subject field is created, after saved to doc format, can get text from document",
112cc7a2393SLiu Zhe 				isContainSubjectText(docDocument, subjectStr));
113cc7a2393SLiu Zhe 		app.closeDocument(docDocument);
114cc7a2393SLiu Zhe 	}
115cc7a2393SLiu Zhe 
116cc7a2393SLiu Zhe 	/**
117cc7a2393SLiu Zhe 	 *  Test Subject Field Can created and Saved in Doc file
118cc7a2393SLiu Zhe 	 * 1.launch a doc document
119cc7a2393SLiu Zhe 	 * 2.Create a Subject field at end of this page
120cc7a2393SLiu Zhe 	 * 3.Save and Reopen this document, check Subject field
121cc7a2393SLiu Zhe 	 * 3.Save as odt format and reload
122cc7a2393SLiu Zhe 	 * @throws Throwable
123cc7a2393SLiu Zhe 	 */
1243ecf08d2SLi Feng Wang //	@Test
1253ecf08d2SLi Feng Wang //	public void testPageCountFieldDOC() throws Throwable {
1263ecf08d2SLi Feng Wang //		docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app);
1273ecf08d2SLi Feng Wang //		String subjectStr = "Test Subject Field";
1283ecf08d2SLi Feng Wang //		SWUtil.setDocumentProperty(docDocument, "Subject", subjectStr);
1293ecf08d2SLi Feng Wang //		createSubjectFiled(docDocument);
1303ecf08d2SLi Feng Wang //
1313ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created.", isContainSubjectField(docDocument, subjectStr));
1323ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, can get text from document",
1333ecf08d2SLi Feng Wang //				isContainSubjectText(docDocument, subjectStr));
1343ecf08d2SLi Feng Wang //
1353ecf08d2SLi Feng Wang //		docDocument = SWUtil.saveAndReload(docDocument, app);
1363ecf08d2SLi Feng Wang //		//verify after save and reload
1373ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, after save, still exist.", isContainSubjectField(docDocument, subjectStr));
1383ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, can get text from document after saved.",
1393ecf08d2SLi Feng Wang //				isContainSubjectText(docDocument, subjectStr));
1403ecf08d2SLi Feng Wang //		SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample));
1413ecf08d2SLi Feng Wang //		app.closeDocument(docDocument);
1423ecf08d2SLi Feng Wang //		odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app);
1433ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, after saved to doc format, field still exist.", isContainSubjectField(odtDocument, subjectStr));
1443ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, after saved to doc format, can get text from document",
1453ecf08d2SLi Feng Wang //				isContainSubjectText(odtDocument, subjectStr));
1463ecf08d2SLi Feng Wang //		app.closeDocument(odtDocument);
1473ecf08d2SLi Feng Wang //	}
148cc7a2393SLiu Zhe 
149cc7a2393SLiu Zhe 
150cc7a2393SLiu Zhe 
151cc7a2393SLiu Zhe 	/**
152cc7a2393SLiu Zhe 	 * Create a subject field at start of this document
153cc7a2393SLiu Zhe 	 * @param document
154cc7a2393SLiu Zhe 	 * @throws Exception
155cc7a2393SLiu Zhe 	 */
createSubjectFiled(XTextDocument document)156cc7a2393SLiu Zhe 	private void createSubjectFiled(XTextDocument document) throws Exception {
157cc7a2393SLiu Zhe 
158cc7a2393SLiu Zhe 		XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
159cc7a2393SLiu Zhe 		XTextField  subjectField = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.docinfo.Subject"));
160cc7a2393SLiu Zhe 
161cc7a2393SLiu Zhe 
162cc7a2393SLiu Zhe 		SWUtil.moveCuror2Start(document);
163cc7a2393SLiu Zhe 		document.getText().insertTextContent(document.getText().getStart(), subjectField, true);
164cc7a2393SLiu Zhe 
165cc7a2393SLiu Zhe 	}
166cc7a2393SLiu Zhe 
167cc7a2393SLiu Zhe 
168cc7a2393SLiu Zhe 	/**
169cc7a2393SLiu Zhe 	 * Check is contain subject content at start of this document
170cc7a2393SLiu Zhe 	 *
171cc7a2393SLiu Zhe 	 * @param document
172cc7a2393SLiu Zhe 	 * @param content
173cc7a2393SLiu Zhe 	 * @return
174cc7a2393SLiu Zhe 	 */
isContainSubjectText(XTextDocument document, String content)175cc7a2393SLiu Zhe 	private boolean isContainSubjectText(XTextDocument document, String content) {
176cc7a2393SLiu Zhe 		String documentString = document.getText().getString().trim();
177cc7a2393SLiu Zhe 		return documentString.indexOf(content) == 0;
178cc7a2393SLiu Zhe 	}
179cc7a2393SLiu Zhe 
180cc7a2393SLiu Zhe 	/**
181cc7a2393SLiu Zhe 	 * Check is contain subject field
182cc7a2393SLiu Zhe 	 * @param document
183cc7a2393SLiu Zhe 	 * @throws Exception
184cc7a2393SLiu Zhe 	 */
isContainSubjectField(XTextDocument document, String content)185cc7a2393SLiu Zhe 	private boolean isContainSubjectField(XTextDocument document, String content) throws Exception {
186cebb507aSLiu Zhe 		XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
187cc7a2393SLiu Zhe 		XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields();
188cc7a2393SLiu Zhe 		XEnumeration enumeration = xEnumeratedFields.createEnumeration();
189cc7a2393SLiu Zhe 		while (enumeration.hasMoreElements()) {
190cc7a2393SLiu Zhe 			  	Object field =  enumeration.nextElement();
191cc7a2393SLiu Zhe 				XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
192cc7a2393SLiu Zhe 				String strContent = (String) props.getPropertyValue("Content");
193cc7a2393SLiu Zhe 			    return content.equals(strContent);
194cc7a2393SLiu Zhe 
195cc7a2393SLiu Zhe 		}
196cc7a2393SLiu Zhe 		return false;
197cc7a2393SLiu Zhe 
198cc7a2393SLiu Zhe 	}
199cc7a2393SLiu Zhe 
200cc7a2393SLiu Zhe }
201