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 fvt.uno.sw.field;
22 
23 import static org.junit.Assert.*;
24 
25 import org.junit.After;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 
31 import org.openoffice.test.common.Testspace;
32 import org.openoffice.test.uno.UnoApp;
33 
34 import testlib.uno.SWUtil;
35 
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.container.XEnumeration;
38 import com.sun.star.container.XEnumerationAccess;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.text.XTextDocument;
41 import com.sun.star.text.XTextField;
42 import com.sun.star.text.XTextFieldsSupplier;
43 import com.sun.star.uno.UnoRuntime;
44 public class TitleField {
45 
46 	private static final UnoApp app = new UnoApp();
47 	private static XTextDocument odtDocument = null;
48 	private static XTextDocument docDocument = null;
49 	private  static String odtSample = "uno/sw/field/Test_Sample.odt";
50 	private static String docSample = "uno/sw/field/Test_Sample.doc";
51 
52 	private  static String odtSaveAsDocSample = "uno/sw/field/odtSaveAsDoc.doc";
53 	private static String docSaveAsODTSample = "uno/sw/field/docSaveAsODT.odt";
54 
55 	@Before
setUpDocument()56 	public void setUpDocument() throws Exception {
57 
58 	}
59 
60 	@After
tearDownDocument()61 	public void tearDownDocument() {
62 
63 
64 	}
65 
66 	@BeforeClass
setUpConnection()67 	public static void setUpConnection() throws Exception {
68 		app.start();
69 	}
70 
71 	@AfterClass
tearDownConnection()72 	public static void tearDownConnection() throws InterruptedException,
73 			Exception {
74 		app.close();
75 	}
76 
77 	/**
78 	 *
79 	 * Test Title Field Can created and Saved in odt file
80 	 * 1.launch a odt document
81 	 * 2.Create a title field at end of this page
82 	 * 3.Save and Reopen this document
83 	 * 4.Save it as doc format and reload
84 	 * @throws Throwable
85 	 */
86 	@Test
testTitleFieldODT()87 	public void testTitleFieldODT() throws Throwable {
88 		odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app);
89 
90 
91 		String titleStr = "Title Test";
92 		createTitleFiled(odtDocument);
93 
94 		assertTrue("Verify Title field is created use exsit Title", isContainTitleField(odtDocument, titleStr));
95 		assertTrue("Verify Title field is created use exsit Title, can get text from document",
96 				isContainTitleText(odtDocument, titleStr));
97 		//Set Title
98 		SWUtil.setDocumentProperty(odtDocument, "Title", titleStr);
99 		//Verfiy after set Title
100 		assertTrue("Verify Title field is created, can get text from document",
101 				isContainTitleText(odtDocument, titleStr));
102 
103 		odtDocument = SWUtil.saveAndReload(odtDocument, app);
104 		//verify after save and reload
105 		assertTrue("Verify Title field is created, after save, still exist.", isContainTitleField(odtDocument, titleStr));
106 		assertTrue("Verify Title field is created, can get text from document after saved.",
107 				isContainTitleText(odtDocument, titleStr));
108 		SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample));
109 		app.closeDocument(odtDocument);
110 		docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app);
111 		assertTrue("Verify Title field is created, after saved to doc format, field still exist.", isContainTitleField(docDocument, titleStr));
112 		assertTrue("Verify Title field is created, after saved to doc format, can get text from document",
113 				isContainTitleText(docDocument, titleStr));
114 		app.closeDocument(docDocument);
115 	}
116 
117 	/**
118 	 *  Test Title Field Can created and Saved in Doc file
119 	 * 1.launch a doc document
120 	 * 2.Create a Title field at end of this page
121 	 * 3.Save and Reopen this document, check Title field
122 	 * 3.Save as odt format and reload
123 	 * @throws Throwable
124 	 */
125 //	@Test
126 //	public void testPageCountFieldDOC() throws Throwable {
127 //		docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app);
128 //		String TitleStr = "Title Test";
129 //		SWUtil.setDocumentProperty(docDocument, "Title", TitleStr);
130 //		createTitleFiled(docDocument);
131 //
132 //		assertTrue("Verify Title field is created.", isContainTitleField(docDocument, TitleStr));
133 //		assertTrue("Verify Title field is created, can get text from document",
134 //				isContainTitleText(docDocument, TitleStr));
135 //
136 //		docDocument = SWUtil.saveAndReload(docDocument, app);
137 //		//verify after save and reload
138 //		assertTrue("Verify Title field is created, after save, still exist.", isContainTitleField(docDocument, TitleStr));
139 //		assertTrue("Verify Title field is created, can get text from document after saved.",
140 //				isContainTitleText(docDocument, TitleStr));
141 //		SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample));
142 //		app.closeDocument(docDocument);
143 //		odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app);
144 //		assertTrue("Verify Title field is created, after saved to doc format, field still exist.", isContainTitleField(odtDocument, TitleStr));
145 //		assertTrue("Verify Title field is created, after saved to doc format, can get text from document",
146 //				isContainTitleText(odtDocument, TitleStr));
147 //		app.closeDocument(odtDocument);
148 //	}
149 
150 
151 
152 	/**
153 	 * Create a Title field at start of this document
154 	 * @param document
155 	 * @throws Exception
156 	 */
createTitleFiled(XTextDocument document)157 	private void createTitleFiled(XTextDocument document) throws Exception {
158 
159 		XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
160 		XTextField  TitleField = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.docinfo.Title"));
161 
162 
163 		SWUtil.moveCuror2Start(document);
164 		document.getText().insertTextContent(document.getText().getStart(), TitleField, true);
165 
166 	}
167 
168 
169 	/**
170 	 * Check is contain Title content at start of this document
171 	 *
172 	 * @param document
173 	 * @param content
174 	 * @return
175 	 */
isContainTitleText(XTextDocument document, String content)176 	private boolean isContainTitleText(XTextDocument document, String content) {
177 		String documentString = document.getText().getString().trim();
178 		return documentString.indexOf(content) == 0;
179 	}
180 
181 	/**
182 	 * Check is contain Title field
183 	 * @param document
184 	 * @throws Exception
185 	 */
isContainTitleField(XTextDocument document, String content)186 	private boolean isContainTitleField(XTextDocument document, String content) throws Exception {
187 		XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
188 		XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields();
189 		XEnumeration enumeration = xEnumeratedFields.createEnumeration();
190 		while (enumeration.hasMoreElements()) {
191 			  	Object field =  enumeration.nextElement();
192 				XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
193 				String strContent = (String) props.getPropertyValue("CurrentPresentation");
194 			    return content.equals(strContent);
195 
196 		}
197 		return false;
198 
199 	}
200 }
201