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 
93cc7a2393SLiu Zhe 		assertTrue("Verify Subject field is created use exsit subject", isContainSubjectField(odtDocument, "UNO API Subject"));
94cc7a2393SLiu Zhe 		assertTrue("Verify Subject field is created use exsit subject, can get text from document",
95cc7a2393SLiu Zhe 				isContainSubjectText(odtDocument, "UNO API Subject"));
96cc7a2393SLiu Zhe 		//Set subject
97cc7a2393SLiu Zhe 		SWUtil.setDocumentProperty(odtDocument, "Subject", subjectStr);
98cc7a2393SLiu Zhe 		//Verfiy 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 	 */
124*3ecf08d2SLi Feng Wang //	@Test
125*3ecf08d2SLi Feng Wang //	public void testPageCountFieldDOC() throws Throwable {
126*3ecf08d2SLi Feng Wang //		docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app);
127*3ecf08d2SLi Feng Wang //		String subjectStr = "Test Subject Field";
128*3ecf08d2SLi Feng Wang //		SWUtil.setDocumentProperty(docDocument, "Subject", subjectStr);
129*3ecf08d2SLi Feng Wang //		createSubjectFiled(docDocument);
130*3ecf08d2SLi Feng Wang //
131*3ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created.", isContainSubjectField(docDocument, subjectStr));
132*3ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, can get text from document",
133*3ecf08d2SLi Feng Wang //				isContainSubjectText(docDocument, subjectStr));
134*3ecf08d2SLi Feng Wang //
135*3ecf08d2SLi Feng Wang //		docDocument = SWUtil.saveAndReload(docDocument, app);
136*3ecf08d2SLi Feng Wang //		//verify after save and reload
137*3ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, after save, still exist.", isContainSubjectField(docDocument, subjectStr));
138*3ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, can get text from document after saved.",
139*3ecf08d2SLi Feng Wang //				isContainSubjectText(docDocument, subjectStr));
140*3ecf08d2SLi Feng Wang //		SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample));
141*3ecf08d2SLi Feng Wang //		app.closeDocument(docDocument);
142*3ecf08d2SLi Feng Wang //		odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app);
143*3ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, after saved to doc format, field still exist.", isContainSubjectField(odtDocument, subjectStr));
144*3ecf08d2SLi Feng Wang //		assertTrue("Verify Subject field is created, after saved to doc format, can get text from document",
145*3ecf08d2SLi Feng Wang //				isContainSubjectText(odtDocument, subjectStr));
146*3ecf08d2SLi Feng Wang //		app.closeDocument(odtDocument);
147*3ecf08d2SLi 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