xref: /AOO41X/test/testuno/source/fvt/uno/sw/DocumentTest.java (revision 07d7dbdcb8e526dfaf85923a181c45494526d79d)
1*07d7dbdcSHerbert Dürr /**************************************************************
2*07d7dbdcSHerbert Dürr  *
3*07d7dbdcSHerbert Dürr  * Licensed to the Apache Software Foundation (ASF) under one
4*07d7dbdcSHerbert Dürr  * or more contributor license agreements.  See the NOTICE file
5*07d7dbdcSHerbert Dürr  * distributed with this work for additional information
6*07d7dbdcSHerbert Dürr  * regarding copyright ownership.  The ASF licenses this file
7*07d7dbdcSHerbert Dürr  * to you under the Apache License, Version 2.0 (the
8*07d7dbdcSHerbert Dürr  * "License"); you may not use this file except in compliance
9*07d7dbdcSHerbert Dürr  * with the License.  You may obtain a copy of the License at
10*07d7dbdcSHerbert Dürr  *
11*07d7dbdcSHerbert Dürr  *   http://www.apache.org/licenses/LICENSE-2.0
12*07d7dbdcSHerbert Dürr  *
13*07d7dbdcSHerbert Dürr  * Unless required by applicable law or agreed to in writing,
14*07d7dbdcSHerbert Dürr  * software distributed under the License is distributed on an
15*07d7dbdcSHerbert Dürr  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*07d7dbdcSHerbert Dürr  * KIND, either express or implied.  See the License for the
17*07d7dbdcSHerbert Dürr  * specific language governing permissions and limitations
18*07d7dbdcSHerbert Dürr  * under the License.
19*07d7dbdcSHerbert Dürr  *
20*07d7dbdcSHerbert Dürr  *************************************************************/
21*07d7dbdcSHerbert Dürr 
22eba4d44aSLiu Zhe package fvt.uno.sw;
23eba4d44aSLiu Zhe 
24eba4d44aSLiu Zhe import static org.openoffice.test.common.Testspace.*;
25eba4d44aSLiu Zhe 
26eba4d44aSLiu Zhe import java.io.File;
27eba4d44aSLiu Zhe 
28eba4d44aSLiu Zhe import org.junit.After;
29eba4d44aSLiu Zhe import org.junit.Before;
30eba4d44aSLiu Zhe import org.junit.Test;
31eba4d44aSLiu Zhe import org.junit.Assert;
32eba4d44aSLiu Zhe import org.openoffice.test.common.FileUtil;
33eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
34eba4d44aSLiu Zhe 
35eba4d44aSLiu Zhe import com.sun.star.text.XTextDocument;
36eba4d44aSLiu Zhe import com.sun.star.text.XTextCursor;
37eba4d44aSLiu Zhe import com.sun.star.text.XText;
38eba4d44aSLiu Zhe import com.sun.star.beans.XPropertySet;
39eba4d44aSLiu Zhe import com.sun.star.beans.PropertyValue;
40eba4d44aSLiu Zhe import com.sun.star.frame.*;
41eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime;
42eba4d44aSLiu Zhe import com.sun.star.util.XCloseable;
43eba4d44aSLiu Zhe import com.sun.star.lang.XComponent;
44eba4d44aSLiu Zhe import com.sun.star.container.XEnumerationAccess;
45eba4d44aSLiu Zhe import com.sun.star.container.XEnumeration;
46eba4d44aSLiu Zhe 
47eba4d44aSLiu Zhe 
48eba4d44aSLiu Zhe public class DocumentTest {
49eba4d44aSLiu Zhe 	UnoApp unoApp = new UnoApp();
50eba4d44aSLiu Zhe 	XTextDocument textDocument = null;
51eba4d44aSLiu Zhe 	File temp = null;
52eba4d44aSLiu Zhe 	String workingFilePath = "";
53eba4d44aSLiu Zhe 	String workingTemplatePath = "";
54eba4d44aSLiu Zhe 
55eba4d44aSLiu Zhe 	/**
56eba4d44aSLiu Zhe 	 * @throws java.lang.Exception
57eba4d44aSLiu Zhe 	 */
58eba4d44aSLiu Zhe 	@Before
59eba4d44aSLiu Zhe 	public void setUp() throws Exception {
60eba4d44aSLiu Zhe 		unoApp.start();
61eba4d44aSLiu Zhe 
62eba4d44aSLiu Zhe 		FileUtil.deleteFile(getPath("temp"));
63eba4d44aSLiu Zhe 		temp = new File(getPath("temp"));
64eba4d44aSLiu Zhe 		temp.mkdirs();
65eba4d44aSLiu Zhe 
66eba4d44aSLiu Zhe 		//copy sample file to temp folder
67eba4d44aSLiu Zhe 		workingFilePath = prepareData("uno/sw/DocumentTest.odt");
68eba4d44aSLiu Zhe 		workingTemplatePath = prepareData("uno/sw/DocumentTest.ott");
69eba4d44aSLiu Zhe 	}
70eba4d44aSLiu Zhe 
71eba4d44aSLiu Zhe 	@After
72eba4d44aSLiu Zhe 	public void tearDown() throws Exception {
73eba4d44aSLiu Zhe 		unoApp.close();
74eba4d44aSLiu Zhe 	}
75eba4d44aSLiu Zhe 
76eba4d44aSLiu Zhe 	private XComponent newDocumentFromTemplate(String templatePath) throws Exception
77eba4d44aSLiu Zhe 	{
78eba4d44aSLiu Zhe 		XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
79eba4d44aSLiu Zhe 		PropertyValue[] pros = new PropertyValue[1];
80eba4d44aSLiu Zhe 		pros[0] = new PropertyValue();
81eba4d44aSLiu Zhe 		pros[0].Name = "AsTemplate";
82eba4d44aSLiu Zhe 		pros[0].Value = new Boolean(true);
83eba4d44aSLiu Zhe 
84eba4d44aSLiu Zhe 		XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(workingTemplatePath), "_blank", 0,pros);
85eba4d44aSLiu Zhe 		return component;
86eba4d44aSLiu Zhe 	}
87eba4d44aSLiu Zhe 
88eba4d44aSLiu Zhe 
89eba4d44aSLiu Zhe 	/**
90eba4d44aSLiu Zhe 	 * test close document
91eba4d44aSLiu Zhe 	 * @throws Exception
92eba4d44aSLiu Zhe 	 */
93eba4d44aSLiu Zhe 	@Test
94eba4d44aSLiu Zhe 	public void testCloseDocument() throws Exception
95eba4d44aSLiu Zhe 	{
96eba4d44aSLiu Zhe 		XComponent component = unoApp.newDocument("swriter");
97eba4d44aSLiu Zhe 		unoApp.closeDocument(component);
98eba4d44aSLiu Zhe 		XModel xModel = unoApp.getDesktop().getCurrentFrame().getController().getModel();
99eba4d44aSLiu Zhe 		Assert.assertTrue("Document has been closed.",xModel==null);
100eba4d44aSLiu Zhe 	}
101eba4d44aSLiu Zhe 
102eba4d44aSLiu Zhe 	/**
103eba4d44aSLiu Zhe 	 * test new document
104eba4d44aSLiu Zhe 	 * @throws Exception
105eba4d44aSLiu Zhe 	 */
106eba4d44aSLiu Zhe 	@Test
107eba4d44aSLiu Zhe 	public void testNewDocument() throws Exception
108eba4d44aSLiu Zhe 	{
109eba4d44aSLiu Zhe 		XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
110eba4d44aSLiu Zhe 		XComponent component = componentLoader.loadComponentFromURL("private:factory/" + "swriter", "_blank", 0, new PropertyValue[0]);
111eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
112eba4d44aSLiu Zhe 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
113eba4d44aSLiu Zhe 		String title = xTitle.getTitle();
114eba4d44aSLiu Zhe 		Assert.assertEquals("New Document title start with \"Untitled\"",true, title.startsWith("Untitled"));
115eba4d44aSLiu Zhe 		unoApp.closeDocument(textDocument);
116eba4d44aSLiu Zhe 	}
117eba4d44aSLiu Zhe 
118eba4d44aSLiu Zhe 	/**
119eba4d44aSLiu Zhe 	 * test new document from template
120eba4d44aSLiu Zhe 	 * @throws Exception
121eba4d44aSLiu Zhe 	 */
122eba4d44aSLiu Zhe 	@Test
123eba4d44aSLiu Zhe 	public void testNewDocumentFromTemplate() throws Exception
124eba4d44aSLiu Zhe 	{
125eba4d44aSLiu Zhe 		XComponent component = this.newDocumentFromTemplate(workingTemplatePath);
126eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
127eba4d44aSLiu Zhe 		XText xText = textDocument.getText();
128eba4d44aSLiu Zhe 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
129eba4d44aSLiu Zhe 		xText = textDocument.getText();
130eba4d44aSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
131eba4d44aSLiu Zhe 		xTextCursor.gotoEnd(true);
132eba4d44aSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
133eba4d44aSLiu Zhe 		String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
134eba4d44aSLiu Zhe 
135eba4d44aSLiu Zhe         Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle);
136eba4d44aSLiu Zhe 
137eba4d44aSLiu Zhe         Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
138eba4d44aSLiu Zhe 
139eba4d44aSLiu Zhe         unoApp.closeDocument(textDocument);
140eba4d44aSLiu Zhe 	}
141eba4d44aSLiu Zhe 
142eba4d44aSLiu Zhe 	/**
143eba4d44aSLiu Zhe 	 * test save document as odt
144eba4d44aSLiu Zhe 	 * @throws Exception
145eba4d44aSLiu Zhe 	 */
146eba4d44aSLiu Zhe 	@Test
147eba4d44aSLiu Zhe 	public void testSaveDocument() throws Exception
148eba4d44aSLiu Zhe 	{
149eba4d44aSLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
150eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
151eba4d44aSLiu Zhe 		XText xText = textDocument.getText();
152eba4d44aSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
153eba4d44aSLiu Zhe 		xTextCursor.gotoEnd(true);
154eba4d44aSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
155eba4d44aSLiu Zhe 
156eba4d44aSLiu Zhe 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
157eba4d44aSLiu Zhe 
158eba4d44aSLiu Zhe 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
159eba4d44aSLiu Zhe         xStorable.store();
160eba4d44aSLiu Zhe         unoApp.closeDocument(textDocument);
161eba4d44aSLiu Zhe 
162eba4d44aSLiu Zhe         component = unoApp.loadDocument(workingFilePath);
163eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
164eba4d44aSLiu Zhe 		xText = textDocument.getText();
165eba4d44aSLiu Zhe 		xTextCursor = xText.createTextCursor();
166eba4d44aSLiu Zhe 		xTextCursor.gotoEnd(true);
167eba4d44aSLiu Zhe 		xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
168eba4d44aSLiu Zhe 
169eba4d44aSLiu Zhe         Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName"));
170eba4d44aSLiu Zhe         unoApp.closeDocument(textDocument);
171eba4d44aSLiu Zhe 	}
172eba4d44aSLiu Zhe 
173eba4d44aSLiu Zhe 	/**
174eba4d44aSLiu Zhe 	 * test save document as doc
175eba4d44aSLiu Zhe 	 * @throws Exception
176eba4d44aSLiu Zhe 	 */
177eba4d44aSLiu Zhe 	@Test
178eba4d44aSLiu Zhe 	public void testSaveAsDocument() throws Exception
179eba4d44aSLiu Zhe 	{
180eba4d44aSLiu Zhe 		File saveAsFile = new File(workingFilePath + ".doc");
181eba4d44aSLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
182eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
183eba4d44aSLiu Zhe 		XText xText = textDocument.getText();
184eba4d44aSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
185eba4d44aSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
186eba4d44aSLiu Zhe 
187eba4d44aSLiu Zhe 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
188eba4d44aSLiu Zhe 		xText.insertString(xTextCursor, "test Save odt as doc.", false);
189eba4d44aSLiu Zhe 
190eba4d44aSLiu Zhe 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
191eba4d44aSLiu Zhe         PropertyValue[] storeProps = new PropertyValue[2];
192eba4d44aSLiu Zhe 
193eba4d44aSLiu Zhe         storeProps[0] = new PropertyValue();
194eba4d44aSLiu Zhe         storeProps[0].Name = "Overwrite";
195eba4d44aSLiu Zhe         storeProps[0].Value = new Boolean(true);
196eba4d44aSLiu Zhe 
197eba4d44aSLiu Zhe         storeProps[1] = new PropertyValue();
198eba4d44aSLiu Zhe         storeProps[1].Name = "FilterName";
199eba4d44aSLiu Zhe         storeProps[1].Value = "MS Word 97";
200eba4d44aSLiu Zhe 
201eba4d44aSLiu Zhe         xStorable.storeAsURL(FileUtil.getUrl(saveAsFile), storeProps);
202eba4d44aSLiu Zhe         Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists());
203eba4d44aSLiu Zhe         unoApp.closeDocument(textDocument);
204eba4d44aSLiu Zhe 	}
205eba4d44aSLiu Zhe 
206eba4d44aSLiu Zhe 	/**
207eba4d44aSLiu Zhe 	 * test export document as pdf
208eba4d44aSLiu Zhe 	 * @throws Exception
209eba4d44aSLiu Zhe 	 */
210eba4d44aSLiu Zhe 	@Test
211eba4d44aSLiu Zhe 	public void testExportAsPDF() throws Exception
212eba4d44aSLiu Zhe 	{
213eba4d44aSLiu Zhe 		File saveAsFile = new File(workingFilePath + ".pdf");
214eba4d44aSLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
215eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
216eba4d44aSLiu Zhe 
217eba4d44aSLiu Zhe 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
218eba4d44aSLiu Zhe         PropertyValue[] storeProps = new PropertyValue[3];
219eba4d44aSLiu Zhe 
220eba4d44aSLiu Zhe         storeProps[0] = new PropertyValue();
221eba4d44aSLiu Zhe         storeProps[0].Name = "Overwrite";
222eba4d44aSLiu Zhe         storeProps[0].Value = new Boolean(true);
223eba4d44aSLiu Zhe 
224eba4d44aSLiu Zhe         storeProps[1] = new PropertyValue();
225eba4d44aSLiu Zhe         storeProps[1].Name = "FilterName";
226eba4d44aSLiu Zhe         storeProps[1].Value = "writer_pdf_Export";
227eba4d44aSLiu Zhe 
228eba4d44aSLiu Zhe         storeProps[2] = new PropertyValue();
229eba4d44aSLiu Zhe         storeProps[2].Name = "CompressionMode";
230eba4d44aSLiu Zhe         storeProps[2].Value = "1";
231eba4d44aSLiu Zhe 
232eba4d44aSLiu Zhe         xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
233eba4d44aSLiu Zhe 
234eba4d44aSLiu Zhe         Assert.assertTrue("Export document as PDF.", saveAsFile.exists());
235eba4d44aSLiu Zhe 
236eba4d44aSLiu Zhe         unoApp.closeDocument(textDocument);
237eba4d44aSLiu Zhe 	}
238eba4d44aSLiu Zhe 
239eba4d44aSLiu Zhe 	/**
240eba4d44aSLiu Zhe 	 * test save document as template
241eba4d44aSLiu Zhe 	 * @throws Exception
242eba4d44aSLiu Zhe 	 */
243eba4d44aSLiu Zhe 	@Test
244eba4d44aSLiu Zhe 	public void testSaveAsTemplate() throws Exception
245eba4d44aSLiu Zhe 	{
246eba4d44aSLiu Zhe 		File saveAsFile = new File(workingFilePath + ".ott");
247eba4d44aSLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
248eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
249eba4d44aSLiu Zhe 		XText xText = textDocument.getText();
250eba4d44aSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
251eba4d44aSLiu Zhe 		xTextCursor.gotoEnd(true);
252eba4d44aSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
253eba4d44aSLiu Zhe 
254eba4d44aSLiu Zhe 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
255eba4d44aSLiu Zhe 
256eba4d44aSLiu Zhe 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
257eba4d44aSLiu Zhe         xStorable.store();
258eba4d44aSLiu Zhe 
259eba4d44aSLiu Zhe 		PropertyValue[] storeProps = new PropertyValue[3];
260eba4d44aSLiu Zhe 		storeProps[0] = new PropertyValue();
261eba4d44aSLiu Zhe 		storeProps[0].Name="TemplateName";
262eba4d44aSLiu Zhe 		storeProps[0].Value="MyNewCreatedTemplate";
263eba4d44aSLiu Zhe 
264eba4d44aSLiu Zhe 		storeProps[1] = new PropertyValue();
265eba4d44aSLiu Zhe 		storeProps[1].Name="TemplateRegionName";
266eba4d44aSLiu Zhe 		storeProps[1].Value="My Templates";
267eba4d44aSLiu Zhe 
268eba4d44aSLiu Zhe 		storeProps[2] = new PropertyValue();
269eba4d44aSLiu Zhe 		storeProps[2].Name="AsTemplate";
270eba4d44aSLiu Zhe 		storeProps[2].Value=new Boolean(true);
271eba4d44aSLiu Zhe 
272eba4d44aSLiu Zhe 		xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
273eba4d44aSLiu Zhe 		unoApp.closeDocument(textDocument);
274eba4d44aSLiu Zhe 
275eba4d44aSLiu Zhe 		component = this.newDocumentFromTemplate(saveAsFile.getAbsolutePath());
276eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
277eba4d44aSLiu Zhe 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
278eba4d44aSLiu Zhe 		xText = textDocument.getText();
279eba4d44aSLiu Zhe 		xTextCursor = xText.createTextCursor();
280eba4d44aSLiu Zhe 		xTextCursor.gotoEnd(true);
281eba4d44aSLiu Zhe 		xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
282eba4d44aSLiu Zhe 		String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
283eba4d44aSLiu Zhe 		Assert.assertEquals("Save document as template, heading style is remained. ", "Heading 1", paraStyle);
284eba4d44aSLiu Zhe         Assert.assertEquals("Save document as template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
285eba4d44aSLiu Zhe         unoApp.closeDocument(textDocument);
286eba4d44aSLiu Zhe 	}
287eba4d44aSLiu Zhe 
288eba4d44aSLiu Zhe }
289