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