xref: /AOO41X/test/testuno/source/fvt/uno/sw/DocumentTest.java (revision e4b54efc5301035b455ddd610b25941e76d4f4e3)
107d7dbdcSHerbert Dürr /**************************************************************
207d7dbdcSHerbert Dürr  *
307d7dbdcSHerbert Dürr  * Licensed to the Apache Software Foundation (ASF) under one
407d7dbdcSHerbert Dürr  * or more contributor license agreements.  See the NOTICE file
507d7dbdcSHerbert Dürr  * distributed with this work for additional information
607d7dbdcSHerbert Dürr  * regarding copyright ownership.  The ASF licenses this file
707d7dbdcSHerbert Dürr  * to you under the Apache License, Version 2.0 (the
807d7dbdcSHerbert Dürr  * "License"); you may not use this file except in compliance
907d7dbdcSHerbert Dürr  * with the License.  You may obtain a copy of the License at
1007d7dbdcSHerbert Dürr  *
1107d7dbdcSHerbert Dürr  *   http://www.apache.org/licenses/LICENSE-2.0
1207d7dbdcSHerbert Dürr  *
1307d7dbdcSHerbert Dürr  * Unless required by applicable law or agreed to in writing,
1407d7dbdcSHerbert Dürr  * software distributed under the License is distributed on an
1507d7dbdcSHerbert Dürr  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1607d7dbdcSHerbert Dürr  * KIND, either express or implied.  See the License for the
1707d7dbdcSHerbert Dürr  * specific language governing permissions and limitations
1807d7dbdcSHerbert Dürr  * under the License.
1907d7dbdcSHerbert Dürr  *
2007d7dbdcSHerbert Dürr  *************************************************************/
2107d7dbdcSHerbert 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 
351ff9903bSLi Feng Wang import testlib.uno.SWUtil;
361ff9903bSLi Feng Wang 
37eba4d44aSLiu Zhe import com.sun.star.text.XTextDocument;
38eba4d44aSLiu Zhe import com.sun.star.text.XTextCursor;
39eba4d44aSLiu Zhe import com.sun.star.text.XText;
40eba4d44aSLiu Zhe import com.sun.star.beans.XPropertySet;
41eba4d44aSLiu Zhe import com.sun.star.beans.PropertyValue;
42eba4d44aSLiu Zhe import com.sun.star.frame.*;
43eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime;
44eba4d44aSLiu Zhe import com.sun.star.lang.XComponent;
45eba4d44aSLiu Zhe 
46eba4d44aSLiu Zhe 
47eba4d44aSLiu Zhe public class DocumentTest {
48eba4d44aSLiu Zhe 	UnoApp unoApp = new UnoApp();
49eba4d44aSLiu Zhe 	XTextDocument textDocument = null;
50eba4d44aSLiu Zhe 	File temp = null;
51eba4d44aSLiu Zhe 	String workingFilePath = "";
52eba4d44aSLiu Zhe 	String workingTemplatePath = "";
53eba4d44aSLiu Zhe 
54eba4d44aSLiu Zhe 	@Before
55eba4d44aSLiu Zhe 	public void setUp() throws Exception {
56eba4d44aSLiu Zhe 		unoApp.start();
57eba4d44aSLiu Zhe 
58eba4d44aSLiu Zhe 		FileUtil.deleteFile(getPath("temp"));
59eba4d44aSLiu Zhe 		temp = new File(getPath("temp"));
60eba4d44aSLiu Zhe 		temp.mkdirs();
61eba4d44aSLiu Zhe 
62eba4d44aSLiu Zhe 		//copy sample file to temp folder
63eba4d44aSLiu Zhe 		workingFilePath = prepareData("uno/sw/DocumentTest.odt");
64eba4d44aSLiu Zhe 		workingTemplatePath = prepareData("uno/sw/DocumentTest.ott");
65eba4d44aSLiu Zhe 	}
66eba4d44aSLiu Zhe 
67eba4d44aSLiu Zhe 	@After
68eba4d44aSLiu Zhe 	public void tearDown() throws Exception {
69eba4d44aSLiu Zhe 		unoApp.close();
70eba4d44aSLiu Zhe 	}
71eba4d44aSLiu Zhe 	/**
721ff9903bSLi Feng Wang 	 * test new document and close document
73eba4d44aSLiu Zhe 	 * @throws Exception
74eba4d44aSLiu Zhe 	 */
75eba4d44aSLiu Zhe 	@Test
761ff9903bSLi Feng Wang 	public void testNewAndCloseDocument() throws Exception
77eba4d44aSLiu Zhe 	{
78eba4d44aSLiu Zhe 		XComponent component = unoApp.newDocument("swriter");
79eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
80eba4d44aSLiu Zhe 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
81eba4d44aSLiu Zhe 		String title = xTitle.getTitle();
82eba4d44aSLiu Zhe 		Assert.assertEquals("New Document title start with \"Untitled\"",true, title.startsWith("Untitled"));
831ff9903bSLi Feng Wang 		unoApp.closeDocument(component);
84*e4b54efcSHerbert Dürr 		XDesktop xDesktop = unoApp.getDesktop();
85*e4b54efcSHerbert Dürr 		XFrame xFrame     = (xDesktop == null) ? null : xDesktop.getCurrentFrame();
86*e4b54efcSHerbert Dürr 		XController xCtrl = (xFrame == null)   ? null : xFrame.getController();
87*e4b54efcSHerbert Dürr 		XModel xModel     = (xCtrl == null)    ? null : xCtrl.getModel();
881ff9903bSLi Feng Wang 		Assert.assertTrue("Document has been closed.",xModel==null);
89eba4d44aSLiu Zhe 	}
90eba4d44aSLiu Zhe 	/**
91eba4d44aSLiu Zhe 	 * test new document from template
92eba4d44aSLiu Zhe 	 * @throws Exception
93eba4d44aSLiu Zhe 	 */
94eba4d44aSLiu Zhe 	@Test
95eba4d44aSLiu Zhe 	public void testNewDocumentFromTemplate() throws Exception
96eba4d44aSLiu Zhe 	{
971ff9903bSLi Feng Wang 		XComponent component = SWUtil.newDocumentFromTemplate(workingTemplatePath,unoApp);
98eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
99eba4d44aSLiu Zhe 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
1001ff9903bSLi Feng Wang 		XText xText = textDocument.getText();
101eba4d44aSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
102eba4d44aSLiu Zhe 		xTextCursor.gotoEnd(true);
103eba4d44aSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
104eba4d44aSLiu Zhe 		String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
105eba4d44aSLiu Zhe         Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle);
106eba4d44aSLiu Zhe         Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
107eba4d44aSLiu Zhe 	}
108eba4d44aSLiu Zhe 
109eba4d44aSLiu Zhe 	/**
110eba4d44aSLiu Zhe 	 * test save document as odt
111eba4d44aSLiu Zhe 	 * @throws Exception
112eba4d44aSLiu Zhe 	 */
113eba4d44aSLiu Zhe 	@Test
114eba4d44aSLiu Zhe 	public void testSaveDocument() throws Exception
115eba4d44aSLiu Zhe 	{
116eba4d44aSLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
117eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
118eba4d44aSLiu Zhe 		XText xText = textDocument.getText();
119eba4d44aSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
120eba4d44aSLiu Zhe 		xTextCursor.gotoEnd(true);
121eba4d44aSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
122eba4d44aSLiu Zhe 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
1231ff9903bSLi Feng Wang 		SWUtil.save(textDocument);
124eba4d44aSLiu Zhe         unoApp.closeDocument(textDocument);
125eba4d44aSLiu Zhe         component = unoApp.loadDocument(workingFilePath);
126eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
127eba4d44aSLiu Zhe 		xText = textDocument.getText();
128eba4d44aSLiu Zhe 		xTextCursor = xText.createTextCursor();
129eba4d44aSLiu Zhe 		xTextCursor.gotoEnd(true);
130eba4d44aSLiu Zhe 		xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
131eba4d44aSLiu Zhe         Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName"));
132eba4d44aSLiu Zhe 	}
133eba4d44aSLiu Zhe 
134eba4d44aSLiu Zhe 	/**
135eba4d44aSLiu Zhe 	 * test save document as doc
136eba4d44aSLiu Zhe 	 * @throws Exception
137eba4d44aSLiu Zhe 	 */
138eba4d44aSLiu Zhe 	@Test
139eba4d44aSLiu Zhe 	public void testSaveAsDocument() throws Exception
140eba4d44aSLiu Zhe 	{
141eba4d44aSLiu Zhe 		File saveAsFile = new File(workingFilePath + ".doc");
142eba4d44aSLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
143eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
144eba4d44aSLiu Zhe 		XText xText = textDocument.getText();
145eba4d44aSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
146eba4d44aSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
147eba4d44aSLiu Zhe 
148eba4d44aSLiu Zhe 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
149eba4d44aSLiu Zhe 		xText.insertString(xTextCursor, "test Save odt as doc.", false);
1501ff9903bSLi Feng Wang 		SWUtil.saveAsDoc(textDocument, FileUtil.getUrl(saveAsFile));
151eba4d44aSLiu Zhe         Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists());
152eba4d44aSLiu Zhe 	}
153eba4d44aSLiu Zhe 
154eba4d44aSLiu Zhe 	/**
155eba4d44aSLiu Zhe 	 * test export document as pdf
156eba4d44aSLiu Zhe 	 * @throws Exception
157eba4d44aSLiu Zhe 	 */
158eba4d44aSLiu Zhe 	@Test
159eba4d44aSLiu Zhe 	public void testExportAsPDF() throws Exception
160eba4d44aSLiu Zhe 	{
161eba4d44aSLiu Zhe 		File saveAsFile = new File(workingFilePath + ".pdf");
162eba4d44aSLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
163eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
164eba4d44aSLiu Zhe 
165eba4d44aSLiu Zhe 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
166eba4d44aSLiu Zhe         PropertyValue[] storeProps = new PropertyValue[3];
167eba4d44aSLiu Zhe 
168eba4d44aSLiu Zhe         storeProps[0] = new PropertyValue();
169eba4d44aSLiu Zhe         storeProps[0].Name = "Overwrite";
170eba4d44aSLiu Zhe         storeProps[0].Value = new Boolean(true);
171eba4d44aSLiu Zhe 
172eba4d44aSLiu Zhe         storeProps[1] = new PropertyValue();
173eba4d44aSLiu Zhe         storeProps[1].Name = "FilterName";
174eba4d44aSLiu Zhe         storeProps[1].Value = "writer_pdf_Export";
175eba4d44aSLiu Zhe 
176eba4d44aSLiu Zhe         storeProps[2] = new PropertyValue();
177eba4d44aSLiu Zhe         storeProps[2].Name = "CompressionMode";
178eba4d44aSLiu Zhe         storeProps[2].Value = "1";
179eba4d44aSLiu Zhe 
180eba4d44aSLiu Zhe         xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
181eba4d44aSLiu Zhe 
182eba4d44aSLiu Zhe         Assert.assertTrue("Export document as PDF.", saveAsFile.exists());
183eba4d44aSLiu Zhe 	}
184eba4d44aSLiu Zhe 
185eba4d44aSLiu Zhe 	/**
186eba4d44aSLiu Zhe 	 * test save document as template
187eba4d44aSLiu Zhe 	 * @throws Exception
188eba4d44aSLiu Zhe 	 */
189eba4d44aSLiu Zhe 	@Test
190eba4d44aSLiu Zhe 	public void testSaveAsTemplate() throws Exception
191eba4d44aSLiu Zhe 	{
192eba4d44aSLiu Zhe 		File saveAsFile = new File(workingFilePath + ".ott");
193eba4d44aSLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
194eba4d44aSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
195eba4d44aSLiu Zhe 		XText xText = textDocument.getText();
196eba4d44aSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
197eba4d44aSLiu Zhe 		xTextCursor.gotoEnd(true);
198eba4d44aSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
199eba4d44aSLiu Zhe 
200eba4d44aSLiu Zhe 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
201eba4d44aSLiu Zhe 
202eba4d44aSLiu Zhe 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
203eba4d44aSLiu Zhe         xStorable.store();
204eba4d44aSLiu Zhe 
205eba4d44aSLiu Zhe 		PropertyValue[] storeProps = new PropertyValue[3];
206eba4d44aSLiu Zhe 		storeProps[0] = new PropertyValue();
207eba4d44aSLiu Zhe 		storeProps[0].Name="TemplateName";
208eba4d44aSLiu Zhe 		storeProps[0].Value="MyNewCreatedTemplate";
209eba4d44aSLiu Zhe 
210eba4d44aSLiu Zhe 		storeProps[1] = new PropertyValue();
211eba4d44aSLiu Zhe 		storeProps[1].Name="TemplateRegionName";
212eba4d44aSLiu Zhe 		storeProps[1].Value="My Templates";
213eba4d44aSLiu Zhe 
214eba4d44aSLiu Zhe 		storeProps[2] = new PropertyValue();
215eba4d44aSLiu Zhe 		storeProps[2].Name="AsTemplate";
216eba4d44aSLiu Zhe 		storeProps[2].Value=new Boolean(true);
217eba4d44aSLiu Zhe 
218eba4d44aSLiu Zhe 		xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
219eba4d44aSLiu Zhe 		unoApp.closeDocument(textDocument);
2201ff9903bSLi Feng Wang 		Assert.assertTrue("Export document as ott.", saveAsFile.exists());
221eba4d44aSLiu Zhe 	}
222eba4d44aSLiu Zhe 
223eba4d44aSLiu Zhe }
224