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